From 92d6a8dd81370b908334419ae93ce9500f81be7b Mon Sep 17 00:00:00 2001 From: su530201 <olivier.kaufmann@umons.ac.be> Date: Tue, 10 May 2022 08:48:01 +0200 Subject: [PATCH] Fixes continuous measurement using control --- logging_setup.py | 10 ++++++++-- mqtt_controller.py | 2 +- mqtt_interface.py | 2 +- mqtt_logger.py | 1 + ohmpi.py | 9 +++++---- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/logging_setup.py b/logging_setup.py index 0b9a6baf..8ca05399 100644 --- a/logging_setup.py +++ b/logging_setup.py @@ -51,7 +51,10 @@ def setup_loggers(mqtt=True): if logging_to_console: exec_logger.addHandler(logging.StreamHandler()) if mqtt: - mqtt_msg_handler = MQTTHandler(MQTT_LOGGING_CONFIG['hostname'], MQTT_LOGGING_CONFIG['exec_topic']) + mqtt_settings = MQTT_LOGGING_CONFIG.copy() + [mqtt_settings.pop(i) for i in ['client_id', 'exec_topic', 'data_topic', 'soh_topic']] + mqtt_settings.update({'topic':MQTT_LOGGING_CONFIG['exec_topic']}) + mqtt_msg_handler = MQTTHandler(**mqtt_settings) mqtt_msg_handler.setLevel(logging_level) mqtt_msg_handler.setFormatter(exec_formatter) exec_logger.addHandler(mqtt_msg_handler) @@ -75,7 +78,10 @@ def setup_loggers(mqtt=True): if logging_to_console: data_logger.addHandler(logging.StreamHandler()) if mqtt: - mqtt_data_handler = MQTTHandler(MQTT_LOGGING_CONFIG['hostname'], MQTT_LOGGING_CONFIG['data_topic']) + mqtt_settings = MQTT_LOGGING_CONFIG.copy() + [mqtt_settings.pop(i) for i in ['client_id', 'exec_topic', 'data_topic', 'soh_topic']] + mqtt_settings.update({'topic': MQTT_LOGGING_CONFIG['data_topic']}) + mqtt_data_handler = MQTTHandler(**mqtt_settings) mqtt_data_handler.setLevel(logging_level) mqtt_data_handler.setFormatter(data_formatter) data_logger.addHandler(mqtt_data_handler) diff --git a/mqtt_controller.py b/mqtt_controller.py index aadb0f4a..13b6a086 100644 --- a/mqtt_controller.py +++ b/mqtt_controller.py @@ -6,7 +6,7 @@ from config import MQTT_CONTROL_CONFIG, OHMPI_CONFIG import time import uuid -client = mqtt.Client(f'ohmpi_{OHMPI_CONFIG["id"]}_controller') # create new instance +client = mqtt.Client(f'ohmpi_{OHMPI_CONFIG["id"]}_controller', clean_session=False) # create new instance print('connecting to broker') client.connect(MQTT_CONTROL_CONFIG['hostname']) client.loop_start() diff --git a/mqtt_interface.py b/mqtt_interface.py index a6b52585..916a964c 100644 --- a/mqtt_interface.py +++ b/mqtt_interface.py @@ -19,7 +19,7 @@ def on_message(client, userdata, message): print(f'Received reply {message.payload.decode("utf-8")}: {reply}') -mqtt_client = mqtt.Client(f'ohmpi_{OHMPI_CONFIG["id"]}_listener') # create new instance +mqtt_client = mqtt.Client(f'ohmpi_{OHMPI_CONFIG["id"]}_listener', clean_session=False) # create new instance print('connecting to broker') mqtt_client.connect(MQTT_CONTROL_CONFIG['hostname']) print('Subscribing to topic', MQTT_CONTROL_CONFIG['ctrl_topic']) diff --git a/mqtt_logger.py b/mqtt_logger.py index 10cbbc9d..8bde814d 100644 --- a/mqtt_logger.py +++ b/mqtt_logger.py @@ -44,6 +44,7 @@ class MQTTHandler(logging.Handler): self.tls = tls self.protocol = protocol self.transport = transport + print(f'init logger QoS={self.qos}') def emit(self, record): """ diff --git a/ohmpi.py b/ohmpi.py index 81dd8eb9..bddcfae9 100644 --- a/ohmpi.py +++ b/ohmpi.py @@ -645,6 +645,7 @@ class OhmPi(object): while True: message = socket.recv() print(f'Received command: {message}') + e = None try: cmd_id = None decoded_message = json.loads(message.decode('utf-8')) @@ -658,6 +659,7 @@ class OhmPi(object): self._update_acquisition_settings(args) elif cmd == 'start': self.measure(cmd_id) + self.stop() status = True elif cmd == 'stop': self.stop() @@ -680,19 +682,18 @@ class OhmPi(object): status = True except Exception as e: self.exec_logger.warning(f'Unable to run rs-check: {e}') + else: + self.exec_logger.warning(f'Unkown command {cmd} - cmd_id: {cmd_id}') except Exception as e: self.exec_logger.warning(f'Unable to decode command {message}: {e}') status = False finally: reply = {'cmd_id': cmd_id, 'status': status} - if not status: - reply.update({'Exception': e}) reply = json.dumps(reply) + print(reply) self.exec_logger.debug(reply) reply = bytes(reply, 'utf-8') socket.send(reply) - - print(cmd_id) # Do some 'work' time.sleep(.1) -- GitLab