diff --git a/.gitignore b/.gitignore index 226f9a636df782cb42f9c4c861198cd4e114209b..3cfef627ae3cb83ce32aa73b6c30585b1ce44987 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /venv/ /.idea/ +/__pycache__/ diff --git a/main.py b/main.py index 6838b75928ac7675798adc917f4066c67040c00b..d170105c6b9d15b200a59c5f99501e3c10b574e5 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 c807eaf686e3ccf37bcd155b1b3b6e315b23a51e..ffbbbc9f3152046b3421c3e6d86ffac52dd6c31e 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:]