Commit 92d6a8dd authored by Olivier Kaufmann's avatar Olivier Kaufmann
Browse files

Fixes continuous measurement using control

Showing with 16 additions and 8 deletions
+16 -8
...@@ -51,7 +51,10 @@ def setup_loggers(mqtt=True): ...@@ -51,7 +51,10 @@ def setup_loggers(mqtt=True):
if logging_to_console: if logging_to_console:
exec_logger.addHandler(logging.StreamHandler()) exec_logger.addHandler(logging.StreamHandler())
if mqtt: 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.setLevel(logging_level)
mqtt_msg_handler.setFormatter(exec_formatter) mqtt_msg_handler.setFormatter(exec_formatter)
exec_logger.addHandler(mqtt_msg_handler) exec_logger.addHandler(mqtt_msg_handler)
...@@ -75,7 +78,10 @@ def setup_loggers(mqtt=True): ...@@ -75,7 +78,10 @@ def setup_loggers(mqtt=True):
if logging_to_console: if logging_to_console:
data_logger.addHandler(logging.StreamHandler()) data_logger.addHandler(logging.StreamHandler())
if mqtt: 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.setLevel(logging_level)
mqtt_data_handler.setFormatter(data_formatter) mqtt_data_handler.setFormatter(data_formatter)
data_logger.addHandler(mqtt_data_handler) data_logger.addHandler(mqtt_data_handler)
......
...@@ -6,7 +6,7 @@ from config import MQTT_CONTROL_CONFIG, OHMPI_CONFIG ...@@ -6,7 +6,7 @@ from config import MQTT_CONTROL_CONFIG, OHMPI_CONFIG
import time import time
import uuid 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') print('connecting to broker')
client.connect(MQTT_CONTROL_CONFIG['hostname']) client.connect(MQTT_CONTROL_CONFIG['hostname'])
client.loop_start() client.loop_start()
......
...@@ -19,7 +19,7 @@ def on_message(client, userdata, message): ...@@ -19,7 +19,7 @@ def on_message(client, userdata, message):
print(f'Received reply {message.payload.decode("utf-8")}: {reply}') 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') print('connecting to broker')
mqtt_client.connect(MQTT_CONTROL_CONFIG['hostname']) mqtt_client.connect(MQTT_CONTROL_CONFIG['hostname'])
print('Subscribing to topic', MQTT_CONTROL_CONFIG['ctrl_topic']) print('Subscribing to topic', MQTT_CONTROL_CONFIG['ctrl_topic'])
......
...@@ -44,6 +44,7 @@ class MQTTHandler(logging.Handler): ...@@ -44,6 +44,7 @@ class MQTTHandler(logging.Handler):
self.tls = tls self.tls = tls
self.protocol = protocol self.protocol = protocol
self.transport = transport self.transport = transport
print(f'init logger QoS={self.qos}')
def emit(self, record): def emit(self, record):
""" """
......
...@@ -645,6 +645,7 @@ class OhmPi(object): ...@@ -645,6 +645,7 @@ class OhmPi(object):
while True: while True:
message = socket.recv() message = socket.recv()
print(f'Received command: {message}') print(f'Received command: {message}')
e = None
try: try:
cmd_id = None cmd_id = None
decoded_message = json.loads(message.decode('utf-8')) decoded_message = json.loads(message.decode('utf-8'))
...@@ -658,6 +659,7 @@ class OhmPi(object): ...@@ -658,6 +659,7 @@ class OhmPi(object):
self._update_acquisition_settings(args) self._update_acquisition_settings(args)
elif cmd == 'start': elif cmd == 'start':
self.measure(cmd_id) self.measure(cmd_id)
self.stop()
status = True status = True
elif cmd == 'stop': elif cmd == 'stop':
self.stop() self.stop()
...@@ -680,19 +682,18 @@ class OhmPi(object): ...@@ -680,19 +682,18 @@ class OhmPi(object):
status = True status = True
except Exception as e: except Exception as e:
self.exec_logger.warning(f'Unable to run rs-check: {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: except Exception as e:
self.exec_logger.warning(f'Unable to decode command {message}: {e}') self.exec_logger.warning(f'Unable to decode command {message}: {e}')
status = False status = False
finally: finally:
reply = {'cmd_id': cmd_id, 'status': status} reply = {'cmd_id': cmd_id, 'status': status}
if not status:
reply.update({'Exception': e})
reply = json.dumps(reply) reply = json.dumps(reply)
print(reply)
self.exec_logger.debug(reply) self.exec_logger.debug(reply)
reply = bytes(reply, 'utf-8') reply = bytes(reply, 'utf-8')
socket.send(reply) socket.send(reply)
print(cmd_id)
# Do some 'work' # Do some 'work'
time.sleep(.1) time.sleep(.1)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment