From 0a5820685620ba2a95abeae7229b5927dac07cd8 Mon Sep 17 00:00:00 2001 From: rschoene <rene.schoene@tu-dresden.de> Date: Mon, 3 May 2021 18:35:18 +0200 Subject: [PATCH] Add manual mqtt message sending. --- .gitignore | 1 + main.py | 26 +++++++++++++++++++++++++- utils.py | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 226f9a6..3cfef62 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /venv/ /.idea/ +/__pycache__/ diff --git a/main.py b/main.py index 6838b75..d170105 100644 --- a/main.py +++ b/main.py @@ -76,6 +76,19 @@ app.layout = html.Div([ rows=50, style={'width': '100%', 'height': '200px', 'font-family': 'Consolas, monospace'} ), + dcc.Markdown("---"), + html.Div([ + # html.Div([ + html.P("Topic"), + dcc.Input(id='manual-mqtt-topic'), + html.P("Message"), + dcc.Input(id='manual-mqtt-message'), + # dcc.Textarea(id='manual-mqtt-message', style={'width': '50%', 'font-family': 'Consolas, monospace'}), + html.Button('Send', id='manual-mqtt-send') + # ], className="six columns"), + ], className='row'), + + # -- Invisible elements -- dcc.Interval( id='every-1-second', interval=1000, # in milliseconds @@ -132,6 +145,17 @@ def button_clicked_to_add_to_mqtt_log(*_): return dash.no_update +@app.callback( + Output('manual-mqtt-topic', 'value'), + Input('manual-mqtt-send', 'n_clicks'), + State('manual-mqtt-topic', 'value'), + State('manual-mqtt-message', 'value'), +) +def send_manual(n_clicks, topic, message): + if n_clicks: + mqttc.publish(topic=topic, payload=message) + + def on_mqtt_connect(_client, _userdata, _flags, _rc, _properties=None): print('Connected at ' + datetime.datetime.now().isoformat()) ready_event.set() @@ -141,7 +165,7 @@ def on_mqtt_message(_client, _userdata, message): max_mqtt_topic_length = max_topic.process_topic(message.topic) message_queue.put_nowait(utils.format_log_msg(message.topic, max_mqtt_topic_length, - message.payload.decode("utf-8"))) + message.payload.decode("utf-8")).replace("\n", " ~ ")) def publish_test_message(): diff --git a/utils.py b/utils.py index c807eaf..ffbbbc9 100644 --- a/utils.py +++ b/utils.py @@ -28,6 +28,7 @@ def format_log_msg(topic: str, max_mqtt_topic_length: int, message: str, timesta def parse_log_msg(entry: str): + print("Parsing >", entry, "<") at_index = entry.index('@') closing_bracket_index = entry.index(']') return entry[1:at_index].strip(), entry[at_index + 1:closing_bracket_index].strip(), entry[closing_bracket_index+2:] -- GitLab