Georg commited on
Commit
90be8a0
·
1 Parent(s): 3305880

Add trainer connection status broadcasting in mujoco_server.py

Browse files

- Implemented a new function to notify UI clients about the trainer's connection status, enhancing real-time feedback.
- Updated the trainer websocket handler to call the new broadcasting function upon client connection and disconnection.
- Modified the UI message handling to update the trainer connection status dynamically, improving user experience during teleoperation.

Files changed (1) hide show
  1. mujoco_server.py +22 -0
mujoco_server.py CHANGED
@@ -565,6 +565,21 @@ def broadcast_state():
565
  # Remove dead clients
566
  ws_clients.difference_update(dead_clients)
567
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
568
  def broadcast_to_trainer(event_type: str, payload: dict):
569
  """Broadcast a structured event to connected trainer WebSocket clients."""
570
  message = json.dumps({"type": event_type, "data": payload})
@@ -1100,6 +1115,7 @@ def trainer_websocket_handler(ws):
1100
  print('Trainer client connected')
1101
  with trainer_ws_clients_lock:
1102
  trainer_ws_clients.add(ws)
 
1103
  broadcast_state()
1104
 
1105
  try:
@@ -1119,6 +1135,7 @@ def trainer_websocket_handler(ws):
1119
  trainer_ws_clients.discard(ws)
1120
  print('Trainer client disconnected')
1121
  broadcast_state()
 
1122
 
1123
 
1124
  def _handle_trainer_message(data):
@@ -2442,6 +2459,11 @@ def index():
2442
  };
2443
  updateRobotUI(data.robot, sceneValue);
2444
  }
 
 
 
 
 
2445
  } else if (msg.type === 'trainer_notification') {
2446
  showTrainerNotification(msg.data);
2447
  }
 
565
  # Remove dead clients
566
  ws_clients.difference_update(dead_clients)
567
 
568
+
569
+ def broadcast_trainer_connection_status():
570
+ """Notify UI clients about trainer connection state."""
571
+ with trainer_ws_clients_lock:
572
+ connected = len(trainer_ws_clients) > 0
573
+ message = json.dumps({"type": "trainer_status", "data": {"connected": connected}})
574
+ with ws_clients_lock:
575
+ dead_clients = set()
576
+ for client in ws_clients:
577
+ try:
578
+ client.send(message)
579
+ except:
580
+ dead_clients.add(client)
581
+ ws_clients.difference_update(dead_clients)
582
+
583
  def broadcast_to_trainer(event_type: str, payload: dict):
584
  """Broadcast a structured event to connected trainer WebSocket clients."""
585
  message = json.dumps({"type": event_type, "data": payload})
 
1115
  print('Trainer client connected')
1116
  with trainer_ws_clients_lock:
1117
  trainer_ws_clients.add(ws)
1118
+ broadcast_trainer_connection_status()
1119
  broadcast_state()
1120
 
1121
  try:
 
1135
  trainer_ws_clients.discard(ws)
1136
  print('Trainer client disconnected')
1137
  broadcast_state()
1138
+ broadcast_trainer_connection_status()
1139
 
1140
 
1141
  def _handle_trainer_message(data):
 
2459
  };
2460
  updateRobotUI(data.robot, sceneValue);
2461
  }
2462
+ } else if (msg.type === 'trainer_status') {
2463
+ const payload = msg.data || {};
2464
+ if (typeof payload.connected === 'boolean') {
2465
+ updateTrainerStatus(payload.connected);
2466
+ }
2467
  } else if (msg.type === 'trainer_notification') {
2468
  showTrainerNotification(msg.data);
2469
  }