Browse Source

解决车辆状态不能实时显示问题

Casper 4 months ago
parent
commit
bfc1f81775
2 changed files with 49 additions and 2 deletions
  1. 19 2
      sri-server-bg01/api/v6/code2000.py
  2. 30 0
      sri-server-bg03/lib/Connection_e1.py

+ 19 - 2
sri-server-bg01/api/v6/code2000.py

@@ -54,6 +54,11 @@ async def code2002(**sources):
     elif not sources.get('size'):
         return dict(code=2, details=f"something is wrong.")
 
+    # --- get VehicleStatus ---
+    unique_dict = {'name': 'VehicleStatus'}
+    item = Global.mdb.get_one('GlobalVariable', unique_dict)
+    status_dict = item.get('args', {})
+
     # --- fill d1 ---
     d1 = list()
     page = sources.get('page', 1)
@@ -72,9 +77,17 @@ async def code2002(**sources):
             'rtk_address': item.get('rtk_address'),
             'cpe_address': item.get('cpe_address'),
             'release_at': item.get('release_at'),
-            'state': item.get('state', 1),
+            'state': item.get('state', 1),  # 1:离线 2:在线(空闲) 3: 现场驾驶中 4: 远程驾驶中
             'permit_state': item.get('permit_state', 0),
         }
+
+        # --- update state ---
+        vehicle_id = int(f"{item.get('host_address').replace('.', '')}")
+        if not status_dict.get(vehicle_id):
+            data['state'] = 1
+        else:
+            data['state'] = status_dict.get(vehicle_id)
+
         d1.append(data)
     return dict(code=0, data=d1[(page - 1) * size: page * size], total=len(d1), page=page, size=size)
 
@@ -128,6 +141,7 @@ async def code2003(**sources):
     Global.mdb.update_one_by_id('VehicleInfo', uuid, update_dict)
     return dict(code=0, data=uuid)
 
+
 async def code2004(**sources):
     """
     禁止指定车辆远程操作
@@ -160,6 +174,7 @@ async def code2004(**sources):
     item = Global.mdb.update_one_by_id('VehicleInfo', uuid, update_dict, need_back=True)
     return dict(code=0, data=item)
 
+
 async def code2005(**sources):
     """
     允许指定车辆远程操作
@@ -192,6 +207,7 @@ async def code2005(**sources):
     item = Global.mdb.update_one_by_id('VehicleInfo', uuid, update_dict, need_back=True)
     return dict(code=0, data=item)
 
+
 async def code2006(**sources):
     """
     删除指定车辆
@@ -210,6 +226,7 @@ async def code2006(**sources):
     Global.mdb.remove_one_by_id('VehicleInfo', uuid)
     return dict(code=0, data=uuid)
 
+
 async def code2007(**sources):
     """
     获取指定作业车辆详情
@@ -237,4 +254,4 @@ async def code2007(**sources):
     """
     item.pop('_id')
     item['uuid'] = uuid
-    return dict(code=0, data=item)
+    return dict(code=0, data=item)

+ 30 - 0
sri-server-bg03/lib/Connection_e1.py

@@ -49,6 +49,16 @@ class SRIConnection(asyncio.Protocol):
         self.update_at = methods.now_ts()
         clients[self.connection_id] = self
 
+        # --- get VehicleStatus ---
+        unique_dict = {'name': 'VehicleStatus'}
+        item = Global.mdb.get_one('GlobalVariable', unique_dict)
+        data = item.get('args', {})
+
+        # --- set VehicleStatus ---
+        data[self.connection_id] = 2
+        update_dict = {'args': data}
+        Global.mdb.update_one('GlobalVariable', unique_dict, update_dict)
+
         # 获取底层 socket
         # sock = self.client.get_extra_info('socket')
 
@@ -64,6 +74,16 @@ class SRIConnection(asyncio.Protocol):
         """
         methods.debug_log(f"{self.connection_id}|SRIConnection085", f"连接已关闭")
 
+        # --- get VehicleStatus ---
+        unique_dict = {'name': 'VehicleStatus'}
+        item = Global.mdb.get_one('GlobalVariable', unique_dict)
+        data = item.get('args', {})
+
+        # --- set VehicleStatus ---
+        data[self.connection_id] = 1
+        update_dict = {'args': data}
+        Global.mdb.update_one('GlobalVariable', unique_dict, update_dict)
+
         # --- 处理车端掉线,通知所有舱端
         if self.client_type == 'vehicle':
             """
@@ -479,6 +499,16 @@ class SRIConnection(asyncio.Protocol):
                 methods.debug_log(f"{self.connection_id}|SRIConnection428", f"#index: {o1.index}")
                 item.client.write(re_send_data)
 
+        # --- get VehicleStatus ---
+        unique_dict = {'name': 'VehicleStatus'}
+        item = Global.mdb.get_one('GlobalVariable', unique_dict)
+        data = item.get('args', {})
+
+        # --- set VehicleStatus ---
+        data[self.connection_id] = 4
+        update_dict = {'args': data}
+        Global.mdb.update_one('GlobalVariable', unique_dict, update_dict)
+
     def message2006(self, body_data):
 
         # --- 解析消息体