ソースを参照

Get correct workpiece information from mqtt message

anfeny 7 ヶ月 前
コミット
45ed023de4
1 ファイル変更11 行追加2 行削除
  1. 11 2
      simulator/realtime_simulation.py

+ 11 - 2
simulator/realtime_simulation.py

@@ -31,10 +31,17 @@ class MQTTSimulationBridge:
         self.trace_real = []  # (time_wall, topic, payload)
         self.trace_sim = []
 
+        self.id_counter = 0  # used to generate unique IDs for workpieces in the simulation (pure sim mode)
+
         # vars to keep track of the real factory state, and notifications to the simulation
         self.is_new_wp_notified: bool = False  # if there is new input, will be set to true to prevent double notification
         self.is_dso_busy: bool = False  # whether there is a workpiece in the DSO (output tray)
 
+    def _get_wp_id(self):
+        """ Generates a unique ID for a workpiece in the simulation. """
+        self.id_counter += 1
+        return str(self.id_counter).zfill(8)  # zero-padded to 8 digits
+    
     def _log(self, origin, msg):
         """ Logs the received mqtt messages """
         t = time.time()
@@ -86,8 +93,10 @@ class MQTTSimulationBridge:
         if not self.sim:
             return
         if msg.topic.startswith("simulation/spawn"):  # TODO: change
-            wp = Workpiece(id="?", color=WorkpieceColor.RED)  # TODO: get from payload
-            self._inject_wp_in_sim(wp)  # Injects the workpiece into the simulation
+            wp = json.loads(msg.payload)["workpiece"]
+            new_wp = Workpiece(id=self._get_wp_id(), color=WorkpieceColor(wp["type"]), state=wp["state"])
+            self._inject_wp_in_sim(new_wp)  # Injects the workpiece into the simulation
+
         elif msg.topic.startswith("simulation/"):
             print(self.sim.server.getTime())
             logger.info(f"Received SIM MQTT message, topic: {msg.topic}")