From 92976ad5259693815196e2e08a29bb2d53cc4d3d Mon Sep 17 00:00:00 2001 From: rschoene <rene.schoene@tu-dresden.de> Date: Tue, 4 May 2021 18:57:52 +0200 Subject: [PATCH] New buttons for demo mode. --- main.py | 62 +++++++++++++++++++++++++++++++++++--------------------- utils.py | 2 +- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/main.py b/main.py index d170105..4bcbf54 100644 --- a/main.py +++ b/main.py @@ -29,11 +29,19 @@ message_queue = queue.Queue() # button-id: (topic, payload) commands = { 'send-place-a-model': ('place-a/model', '1'), + 'send-place-a-model-details': ('place-a/model', 'details'), 'send-place-a-exit': ('place-a/exit', '1'), 'send-place-b-model': ('place-b/model', '1'), + 'send-place-b-model-details': ('place-b/model', 'details'), 'send-place-b-exit': ('place-b/exit', '1'), + 'send-place-a-demo-objRed-blue': ('place-a/demo/move/objectRed1/blue', '1'), + 'send-place-a-demo-objRed-red': ('place-a/demo/move/objectRed1/red', '1'), + 'send-place-b-demo-objRed-red': ('place-b/demo/move/objectRed1/red', '1'), } +button_style_normal = {"marginRight": "15px"} +button_style_exit = {**button_style_normal, "backgroundColor": "red", "color": "white"} + app = dash.Dash(__name__, external_stylesheets=external_stylesheets) app.layout = html.Div([ html.Div([ @@ -44,7 +52,7 @@ app.layout = html.Div([ value='{}', style={'width': '100%'} ), - html.Button('Send to place-a/scene/update', id='send-place-a-update'), + html.Button('Send to place-a/scene/update', id='send-place-a-update', disabled=True), ], className="six columns"), html.Div([ dcc.Textarea( @@ -53,28 +61,34 @@ app.layout = html.Div([ value='{}', style={'width': '100%'} ), - html.Button('Send to place-b/scene/update', id='send-place-b-update'), + html.Button('Send to place-b/scene/update', id='send-place-b-update', disabled=True), ], className="six columns"), ], className='row'), dcc.Markdown("---"), - html.H3("Commands"), html.Div([ html.Div([ - html.Button('Place A: Model', id='send-place-a-model', style={"margin-right": "15px"}), - html.Button('Place A: Exit', id='send-place-a-exit', style={"margin-right": "15px"}), + html.H3("Commands Place A"), + html.Button('Model', id='send-place-a-model', style=button_style_normal), + html.Button('Model (Details)', id='send-place-a-model-details', style=button_style_normal), + html.Button('Exit', id='send-place-a-exit', style=button_style_exit), + html.Button('obj-Red -> Red', id='send-place-a-demo-objRed-red', style=button_style_normal), + html.Button('obj-Red -> Blue', id='send-place-a-demo-objRed-blue', style=button_style_normal), ], className="six columns"), html.Div([ - html.Button('Place B: Model', id='send-place-b-model', style={"margin-right": "15px"}), - html.Button('Place B: Exit', id='send-place-b-exit', style={"margin-right": "15px"}), + html.H3("Commands Place B"), + html.Button('Model', id='send-place-b-model', style=button_style_normal), + html.Button('Model (Details)', id='send-place-b-model-details', style=button_style_normal), + html.Button('Exit', id='send-place-b-exit', style=button_style_exit), + html.Button('obj-Red -> Red', id='send-place-b-demo-objRed-red', style=button_style_normal), ], className="six columns"), ], className='row'), dcc.Markdown("---"), html.H3("MQTT Log"), dcc.Textarea( id='mqtt-log', + value="", readOnly=True, - rows=50, - style={'width': '100%', 'height': '200px', 'font-family': 'Consolas, monospace'} + style={'width': '100%', 'height': '200px', 'fontFamily': 'Consolas, monospace'} ), dcc.Markdown("---"), html.Div([ @@ -113,24 +127,21 @@ def append_to_mqtt_log(_, value): else: value = "" value += '\n'.join(local_messages) - if max_topic.get_and_clear(): - lines = value.split('\n') - reformatted_lines = [] - for line in lines: - timestamp, topic, message = utils.parse_log_msg(line) - print(f'{timestamp, topic, message}') - reformatted_lines.append(utils.format_log_msg(topic, max_topic.max_mqtt_topic_length, - message, timestamp=timestamp)) - value = '\n'.join(reformatted_lines) + # if max_topic.get_and_clear(): + # lines = value.split('\n') + # reformatted_lines = [] + # for line in lines: + # timestamp, topic, message = utils.parse_log_msg(line) + # print(f'{timestamp, topic, message}') + # reformatted_lines.append(utils.format_log_msg(topic, max_topic.max_mqtt_topic_length, + # message, timestamp=timestamp)) + # value = '\n'.join(reformatted_lines) return value @app.callback( Output('hidden-div', 'children'), - Input('send-place-a-model', 'n_clicks'), - Input('send-place-a-exit', 'n_clicks'), - Input('send-place-b-model', 'n_clicks'), - Input('send-place-b-exit', 'n_clicks'), + [Input(button_id, 'n_clicks') for button_id in commands] ) def button_clicked_to_add_to_mqtt_log(*_): ctx = dash.callback_context @@ -154,6 +165,7 @@ def button_clicked_to_add_to_mqtt_log(*_): def send_manual(n_clicks, topic, message): if n_clicks: mqttc.publish(topic=topic, payload=message) + return dash.no_update def on_mqtt_connect(_client, _userdata, _flags, _rc, _properties=None): @@ -163,9 +175,13 @@ def on_mqtt_connect(_client, _userdata, _flags, _rc, _properties=None): def on_mqtt_message(_client, _userdata, message): max_mqtt_topic_length = max_topic.process_topic(message.topic) + try: + payload = message.payload.decode("utf-8") + except UnicodeDecodeError: + payload = "(unreadable bytes)" message_queue.put_nowait(utils.format_log_msg(message.topic, max_mqtt_topic_length, - message.payload.decode("utf-8")).replace("\n", " ~ ")) + payload)) #.replace("\n", " ~ ")) def publish_test_message(): diff --git a/utils.py b/utils.py index ffbbbc9..a01c4f5 100644 --- a/utils.py +++ b/utils.py @@ -4,7 +4,7 @@ from datetime import datetime class MaxTopicLength: def __init__(self): - self.max_mqtt_topic_length = 1 + self.max_mqtt_topic_length = 22 self.mqtt_log_reformat_event = threading.Event() def process_topic(self, topic): -- GitLab