diff --git a/modules/__init__.py b/modules/__init__.py index 74a6700a487fb98bc1d1e9b92fafe6c48e65ed67..69c9c37cfe8027ba443e66730ba6f1020c043160 100644 --- a/modules/__init__.py +++ b/modules/__init__.py @@ -5,8 +5,6 @@ import fcntl import socket import logging -hostname = socket.gethostname() - class Module(Thread): def __init__(self, module_loader, host = False, port = False): super(Module, self).__init__(target=self) @@ -15,23 +13,17 @@ class Module(Thread): self.running = False self.module_prefix = self.__class__.__name__.lower() self.logger = logging.getLogger( self.__class__.__name__ ) - #self.mqtt_topic_prefix = None - self.mqtt_topic_prefix = self.module_prefix + '/' + hostname + '/' - self.mqtt_topic_state = self.mqtt_topic_prefix + 'state' - + self.mqtt_topic_prefix = None + self.mqtt_client = mqtt.Client(userdata = socket.gethostname()) + self.mqtt_client.on_connect = self.on_connect + self.mqtt_client.on_message = self.on_message + self.mqtt_client.on_disconnect = self.on_disconnect if not host: host = "127.0.0.1" if not port: port = 1883 else: port = int( port ) - - self.mqtt_client = mqtt.Client(userdata = hostname) - self.mqtt_client.on_message = self.on_message - self.mqtt_client.on_connect = self.on_connect - self.mqtt_client.on_disconnect = self.on_disconnect - # Setting last_will has to be made *before* connecting - self.mqtt_client.will_set(self.mqtt_topic_state, "0", qos=2, retain=True) self.mqtt_client.connect(host, port) self.mqtt_client.loop_start() @@ -42,13 +34,8 @@ class Module(Thread): self.running = False def on_connect(self, client, userdata, flags, rc): - # Moved to __init__ to set topic name for state for last_will - # self.mqtt_topic_prefix = self.module_prefix + '/' + userdata + '/' - self.mqtt_client.publish( self.mqtt_topic_state, '1', qos=2, retain=True ) self.logger.debug("Connected to broker") - - def publish(self, topic, payload): - return self.mqtt_client.publish( self.mqtt_topic_prefix + topic, payload, qos=2 ) + self.mqtt_topic_prefix = self.module_prefix + '/' + userdata + '/' def on_disconnect(self, client, userdata, rc): # TODO(rh): check arguments @@ -72,4 +59,4 @@ class I2C: self.write_bytes( d.to_bytes(1, byteorder='big') ) def read_bytes(self, n): - return os.read(self.fh, n) + return os.read(self.fh, n) \ No newline at end of file diff --git a/modules/chambercontroller.py b/modules/chambercontroller.py index e58e858bef2531a4daa2f9d873473e790fc0db19..df49660e006036c2e88a19edb1ea0a8f2406d67c 100644 --- a/modules/chambercontroller.py +++ b/modules/chambercontroller.py @@ -382,9 +382,8 @@ class ChamberController(Module): #time.sleep(0.1) #self.actual_current = current = protocol.get_actual_current() - # TODO(rh): Test this! if current is not None: - self.mqtt_client.publish(self.mqtt_topic_prefix + "current", pack("d", current), qos=2) + self.mqtt_client.publish(self.mqtt_topic_prefix+"current", pack("d", current), qos=2) # self.publish() # logger.log("delta={:05.2f} delta_ambient={:05.2f} delta_current={:05.2f} voltage={:05.2f} v_out={:05.2f} v_actual={:05.2f} i_actual={:05.2f} pid_sum={:05.2f}".format(delta, delta_ambient, delta_current, self.voltage, v_out, self.actual_voltage, self.actual_current, self.pid.sum)) except AssertionError as e: @@ -446,7 +445,7 @@ class ChamberController(Module): logger.debug( "target: {} ambient: {} chamber: {}".format( self.target_temp, self.ambient_temp, self.chamber_temp ) ) start = time.time() # When the controller sets the the temp to none, this might throw an exception - self.control_tick_pid(protocol) #temp is set here + self.control_tick_pid(protocol) diff = time.time() - start if diff < 1.0: time.sleep(1.0 - diff) diff --git a/mosquitto_master.conf b/mosquitto_master.conf new file mode 100644 index 0000000000000000000000000000000000000000..4665093f248f09cf465a62d1f69bafe1f92a8579 --- /dev/null +++ b/mosquitto_master.conf @@ -0,0 +1,2 @@ +port 1883 +#clientid master \ No newline at end of file diff --git a/mosquitto_slave.conf b/mosquitto_slave.conf new file mode 100644 index 0000000000000000000000000000000000000000..252d7425a2c6428ff3895974238470ec1f09c189 --- /dev/null +++ b/mosquitto_slave.conf @@ -0,0 +1,17 @@ +# Different port for slaves? +port 1883 +#try_private 1 + + +# Bridge configuration +connection ctc +#local_clientid rhpc.local +remote_clientid lctc-c01 +address ctc.ibr.cs.tu-bs.de:1883 +# qos=2, this has to be set! +topic # both 2 +# Forward $SYS topic of state +notifications true +topic $SYS/broker/connection/+/state +cleansession false +try_private true