Skip to content
Snippets Groups Projects
Commit 28ea71b6 authored by René Schöne's avatar René Schöne
Browse files

add filtering

parent 4d8ed9ad
Branches
Tags
No related merge requests found
...@@ -81,6 +81,7 @@ conversion_topics = { ...@@ -81,6 +81,7 @@ conversion_topics = {
'place-b/command': (cgv_connector_pb2.MergedSelection(), utils.format_command), 'place-b/command': (cgv_connector_pb2.MergedSelection(), utils.format_command),
'place-b/reachability/arm1': (cgv_connector_pb2.Reachability(), utils.format_reachability), 'place-b/reachability/arm1': (cgv_connector_pb2.Reachability(), utils.format_reachability),
'place-b/reachability/arm2': (cgv_connector_pb2.Reachability(), utils.format_reachability), 'place-b/reachability/arm2': (cgv_connector_pb2.Reachability(), utils.format_reachability),
'place-b/reachability/arm3': (cgv_connector_pb2.Reachability(), utils.format_reachability),
} }
bytes_topics = [ bytes_topics = [
...@@ -174,7 +175,20 @@ app.layout = html.Div([ ...@@ -174,7 +175,20 @@ app.layout = html.Div([
html.Button('Dummy Ready', id='send-dummy-ready', style=button_style_normal), html.Button('Dummy Ready', id='send-dummy-ready', style=button_style_normal),
], className='row'), ], className='row'),
# dcc.Markdown("---"), # dcc.Markdown("---"),
html.H3("MQTT Log"), html.H2("Filtered MQTT Log"),
dcc.Textarea(
id='filtered-mqtt-log',
value="",
readOnly=True,
style={**textarea_style_normal, 'height': '400px', 'fontFamily': 'Consolas, monospace'}
),
dcc.Checklist(
id='topics-to-filter',
options=[{'label': topic, 'value': topic} for topic in conversion_topics],
value=[topic for topic in conversion_topics],
labelStyle={'display': 'inline-block'}
),
html.H2("MQTT Log"),
dcc.Textarea( dcc.Textarea(
id='mqtt-log', id='mqtt-log',
value="", value="",
...@@ -300,20 +314,29 @@ def send_complex(*_): ...@@ -300,20 +314,29 @@ def send_complex(*_):
@app.callback( @app.callback(
Output('filtered-mqtt-log', 'value'),
Output('mqtt-log', 'value'), Output('mqtt-log', 'value'),
Output('javascriptLog', 'run'), Output('javascriptLog', 'run'),
Output('topics-to-filter', 'options'),
Input('every-1-second', 'n_intervals'), Input('every-1-second', 'n_intervals'),
Input('clear-mqtt-log', 'n_clicks'), Input('clear-mqtt-log', 'n_clicks'),
Input('topics-to-filter', 'options'),
Input('topics-to-filter', 'value'),
State('filtered-mqtt-log', 'value'),
State('mqtt-log', 'value'), State('mqtt-log', 'value'),
State('should-scroll-mqtt-log', 'value') State('should-scroll-mqtt-log', 'value')
) )
def append_to_mqtt_log(_n_intervals, clear_n_clicks, value, should_scroll): def append_to_mqtt_log(_n_intervals, clear_n_clicks, filter_options, topics_to_filter,
filtered_value, value, should_scroll):
""" """
Periodically update mqtt log Periodically update mqtt log
:param _n_intervals: Unused value of intervals :param (Input) _n_intervals: Unused value of intervals
:param clear_n_clicks: clear.n_clicks :param (Input) clear_n_clicks: clear.n_clicks
:param value: current content of mqtt log :param (Input) filter_options: displayed topics to show in filtered log
:param should_scroll: checkbox value whether to scroll to the end after update :param (Input) topics_to_filter: topics to show in filtered log
:param (State) value: current content of mqtt log
:param (State) filtered_value: current content of filtered mqtt log
:param (State) should_scroll: checkbox value whether to scroll to the end after update
:return: new content of mqtt log :return: new content of mqtt log
""" """
ctx = dash.callback_context ctx = dash.callback_context
...@@ -322,8 +345,14 @@ def append_to_mqtt_log(_n_intervals, clear_n_clicks, value, should_scroll): ...@@ -322,8 +345,14 @@ def append_to_mqtt_log(_n_intervals, clear_n_clicks, value, should_scroll):
trigger_id = ctx.triggered[0]['prop_id'].split('.')[0] trigger_id = ctx.triggered[0]['prop_id'].split('.')[0]
if trigger_id == 'clear-mqtt-log': if trigger_id == 'clear-mqtt-log':
return "", "" return "", "", "", filter_options
if trigger_id == 'topics-to-filter':
filtered_value = ""
for line in value.split('\n'):
if utils.topic_match(topics_to_filter, line):
filtered_value += line + '\n'
else:
# assume trigger_id == 'every-1-second' # assume trigger_id == 'every-1-second'
local_messages = [] local_messages = []
while not message_queue.empty(): while not message_queue.empty():
...@@ -334,22 +363,25 @@ def append_to_mqtt_log(_n_intervals, clear_n_clicks, value, should_scroll): ...@@ -334,22 +363,25 @@ def append_to_mqtt_log(_n_intervals, clear_n_clicks, value, should_scroll):
else: else:
value = "" value = ""
value += '\n'.join(local_messages) value += '\n'.join(local_messages)
# if max_topic.get_and_clear(): if not filtered_value:
# lines = value.split('\n') filtered_value = ""
# reformatted_lines = [] for msg in local_messages:
# for line in lines: timestamp, topic, message = utils.parse_log_msg(msg)
# timestamp, topic, message = utils.parse_log_msg(line) topic = topic.replace(chr(65532), '')
# print(f'{timestamp, topic, message}') topic_match = topic in topics_to_filter
# reformatted_lines.append(utils.format_log_msg(topic, max_topic.max_mqtt_topic_length, if topic_match:
# message, timestamp=timestamp)) filtered_value += msg + "\n"
# value = '\n'.join(reformatted_lines) if topic not in filter_options:
filter_options.append({'label': topic, 'value': topic})
else: else:
return dash.no_update return dash.no_update
log_cmd = ''' log_cmd = '''
var filtered_textarea = document.getElementById('filtered-mqtt-log');
filtered_textarea.scrollTop = filtered_textarea.scrollHeight;
var textarea = document.getElementById('mqtt-log'); var textarea = document.getElementById('mqtt-log');
textarea.scrollTop = textarea.scrollHeight; textarea.scrollTop = textarea.scrollHeight;
''' if should_scroll else "" ''' if should_scroll else ""
return value, log_cmd return filtered_value, value, log_cmd, filter_options
@app.callback( @app.callback(
......
...@@ -30,12 +30,19 @@ def format_log_msg(topic: str, max_mqtt_topic_length: int, message: str, timesta ...@@ -30,12 +30,19 @@ def format_log_msg(topic: str, max_mqtt_topic_length: int, message: str, timesta
def parse_log_msg(entry: str): def parse_log_msg(entry: str):
print("Parsing >", entry, "<") # print("Parsing >", entry, "<")
at_index = entry.index('@') at_index = entry.index('@')
bracket_index = entry.index(']') bracket_index = entry.index(']')
return entry[1:at_index].strip(), entry[at_index + 1:bracket_index].strip(), entry[bracket_index + 2:] return entry[1:at_index].strip(), entry[at_index + 1:bracket_index].strip(), entry[bracket_index + 2:]
def topic_match(topics_to_filter, msg):
timestamp, topic, message = parse_log_msg(msg)
# replacing strange space characters
topic = topic.replace(chr(65532), '')
return topic in topics_to_filter
def format_scene(scene: cgv_connector_pb2.Scene): def format_scene(scene: cgv_connector_pb2.Scene):
result = "" result = ""
for obj in scene.objects: for obj in scene.objects:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment