Browse Source

下载消息推送

zhangyh 9 months ago
parent
commit
410ffaad7e
3 changed files with 27 additions and 1 deletions
  1. 12 0
      python/service/init_load_source.py
  2. 11 1
      python/sockets/socket_server.py
  3. 4 0
      python/utils/common.py

+ 12 - 0
python/service/init_load_source.py

@@ -5,6 +5,7 @@ from settings import HLM_HOST
 import aiohttp
 import os
 from utils.utils_func import get_md5, get_modified_time, compare_two_times, check_path
+from utils.common import message_queue
 
 class init_load_source:
 
@@ -25,10 +26,21 @@ class init_load_source:
                         pass
                 else:
                     print(f"文件不存在 开始下载:{file_path}")
+                    await self.send_message(f"开始下载:{file_path}")
                     await self.async_download_file(value["url"], file_path)
         else:
             print("获取更新文件内容失败")
 
+    async def send_message(self, msg):
+
+        message = {
+            'code': 0,
+            'msg_code': 'down_source',
+            'message': msg
+        }
+        await message_queue.put(json.dumps(message))
+
+
     async def get_update_file(self, type="client_camera", plugins_name="plugins_A"):
         """异步获取指定类型的插件文件更新信息
 

+ 11 - 1
python/sockets/socket_server.py

@@ -12,6 +12,7 @@ conn_manager = ConnectionManager()
 active_connections = set()
 device_ctrl = DeviceControl(websocket_manager=conn_manager)
 blue_tooth = BlueToothMode(websocket_manager=conn_manager)
+from utils.common import message_queue
 @app.websocket("/ws")
 async def websocket_endpoint(websocket: WebSocket):
     await conn_manager.connect(websocket)
@@ -33,7 +34,16 @@ async def websocket_endpoint(websocket: WebSocket):
             await checkMcuConnection(device_ctrl)
         async def connectBlueTooth():
             await blue_tooth.main_func()
-        await asyncio.gather(handler_messages(), checkConnMcu(), connectBlueTooth())
+        async def send_message():
+            while True:
+                try:
+                    message = await message_queue.get()
+                    await websocket.send_text(message)
+                except Exception as e:
+                    print(e)
+                    break
+
+        await asyncio.gather(handler_messages(), checkConnMcu(), connectBlueTooth(), send_message())
     except WebSocketDisconnect:
         # socket_manager.close()
         print("Client disconnected")

+ 4 - 0
python/utils/common.py

@@ -0,0 +1,4 @@
+
+import asyncio
+
+message_queue = asyncio.Queue()