From eb810f7c61e9c61aae1e06668701a69f4e07e5f6 Mon Sep 17 00:00:00 2001
From: rschoene <rene.schoene@tu-dresden.de>
Date: Thu, 13 May 2021 15:07:35 +0200
Subject: [PATCH] more log formatting

---
 main.py  | 34 ++++++++++++++--------------------
 utils.py | 29 ++++++++++++++++++++++++++---
 2 files changed, 40 insertions(+), 23 deletions(-)

diff --git a/main.py b/main.py
index 0a4b69a..67414d6 100644
--- a/main.py
+++ b/main.py
@@ -59,26 +59,15 @@ complex_commands = {
         ('place-b/reachability/arm2', 'place-b-reachability-2-json', cgv_connector_pb2.Reachability()),
 }
 
-
-def format_scene(scene: cgv_connector_pb2.Scene):
-    result = ""
-    for obj in scene.objects:
-        if obj.type == cgv_connector_pb2.Object.Type.BOX:
-            pos = obj.pos
-            result += f"\n<obj {obj.id:15} at ({pos.x:6.2} {pos.y:6.2} {pos.z:6.2})>"
-    return result
-
 conversion_topics = {
-    'place-a/scene/update': (cgv_connector_pb2.Scene(), format_scene),
-    'place-b/scene/update': (cgv_connector_pb2.Scene(), format_scene),
+    'place-a/scene/update': (cgv_connector_pb2.Scene(), utils.format_scene),
+    'place-b/scene/update': (cgv_connector_pb2.Scene(), utils.format_scene),
+    'place-b/command': (cgv_connector_pb2.MergedSelection(), utils.format_command),
+    'place-b/reachability/arm1': (cgv_connector_pb2.Reachability(), utils.format_reachability),
+    'place-b/reachability/arm2': (cgv_connector_pb2.Reachability(), utils.format_reachability),
 }
 
 bytes_topics = [
-    'place-a/scene/update',
-    'place-b/scene/update',
-    'place-b/reachability/arm1',
-    'place-b/reachability/arm2',
-    'place-b/command',
 ]
 
 button_style_normal = {"marginRight": "15px"}
@@ -292,10 +281,15 @@ def append_to_mqtt_log(_n_intervals, clear_n_clicks, value, should_scroll):
     :param should_scroll: checkbox value whether to scroll to the end after update
     :return: new content of mqtt log
     """
-    global last_clear_n_clicks
-    if clear_n_clicks and clear_n_clicks > last_clear_n_clicks:
-        value = ""
-        last_clear_n_clicks = clear_n_clicks
+    ctx = dash.callback_context
+    if not ctx.triggered:
+        return dash.no_update
+
+    trigger_id = ctx.triggered[0]['prop_id'].split('.')[0]
+    if trigger_id == 'clear-mqtt-log':
+        return "", ""
+
+    # assume trigger_id == 'every-1-second'
     local_messages = []
     while not message_queue.empty():
         local_messages.append(message_queue.get_nowait())
diff --git a/utils.py b/utils.py
index a01c4f5..49b4fe1 100644
--- a/utils.py
+++ b/utils.py
@@ -1,5 +1,7 @@
 import threading
 from datetime import datetime
+from google.protobuf import json_format
+import cgv_connector_pb2
 
 
 class MaxTopicLength:
@@ -10,7 +12,7 @@ class MaxTopicLength:
     def process_topic(self, topic):
         if len(topic) > self.max_mqtt_topic_length:
             self.max_mqtt_topic_length = len(topic)
-            print(f'new long topic length: {self.max_mqtt_topic_length}')
+            print(f"new long topic length: {self.max_mqtt_topic_length}")
             self.mqtt_log_reformat_event.set()
         return self.max_mqtt_topic_length
 
@@ -30,5 +32,26 @@ 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:]
+    bracket_index = entry.index(']')
+    return entry[1:at_index].strip(), entry[at_index + 1:bracket_index].strip(), entry[bracket_index + 2:]
+
+
+def format_scene(scene: cgv_connector_pb2.Scene):
+    result = ""
+    for obj in scene.objects:
+        if obj.type == cgv_connector_pb2.Object.Type.BOX:
+            pos = obj.pos
+            result += f"\n<obj {obj.id:15} at ({pos.x:6.2} {pos.y:6.2} {pos.z:6.2})>"
+    return result
+
+
+def format_command(command: cgv_connector_pb2.MergedSelection):
+    return f"<cmd by {command.idRobot} of {command.idPick} to {command.idPlace}"
+
+
+def _get_reach_objects(r):
+    return [objReach.idObject + ' ' + ('✔' if objReach.reachable else '✖') for objReach in r.objects]
+
+
+def format_reachability(r: cgv_connector_pb2.Reachability):
+    return f"<reach of {r.idRobot}: [{', '.join(_get_reach_objects(r))}]>"
-- 
GitLab