|
@@ -0,0 +1,118 @@
|
|
|
+import asyncio
|
|
|
+import queue
|
|
|
+import threading
|
|
|
+import websockets
|
|
|
+import os
|
|
|
+import sys
|
|
|
+import time
|
|
|
+import json
|
|
|
+from pprint import pprint
|
|
|
+from InquirerPy import prompt
|
|
|
+from InquirerPy.base.control import Choice
|
|
|
+from mock_messages import mock_message_json
|
|
|
+
|
|
|
+
|
|
|
+# 服务器端口
|
|
|
+port = 8765
|
|
|
+# Gavin
|
|
|
+user_code = "4991656C2F524F57A910358B8CF8838F"
|
|
|
+
|
|
|
+
|
|
|
+def get_option_by_index(index):
|
|
|
+ return {"index": index, "content": mock_message_json[index]}
|
|
|
+
|
|
|
+options = [
|
|
|
+ {'name': '❌「0」断开连接', 'value': '0'},
|
|
|
+ {'name': '❓「1」发送监护仪模拟信号', 'value': get_option_by_index(1)},
|
|
|
+]
|
|
|
+
|
|
|
+
|
|
|
+def get_questions(index):
|
|
|
+ questions = [
|
|
|
+ {
|
|
|
+ 'type': 'list',
|
|
|
+ 'name': 'message',
|
|
|
+ 'message': '请选择要发送的消息:',
|
|
|
+ 'choices': [Choice(**option) for option in options],
|
|
|
+ 'default': options[index]['value']
|
|
|
+ },
|
|
|
+ ]
|
|
|
+ return questions
|
|
|
+
|
|
|
+
|
|
|
+is_ws_connected = False
|
|
|
+# 创建消息队列
|
|
|
+msg_queue = asyncio.Queue()
|
|
|
+# 创建通知队列
|
|
|
+flag_queue = queue.Queue()
|
|
|
+
|
|
|
+
|
|
|
+async def send_messages(websocket):
|
|
|
+ # 不停遍历队列,如果有消息就发送
|
|
|
+ print('客户端已经接入,地址:', websocket.remote_address)
|
|
|
+ flag_queue.put('connected')
|
|
|
+ while True:
|
|
|
+ await asyncio.sleep(0.1)
|
|
|
+ if msg_queue.empty():
|
|
|
+ continue
|
|
|
+ else:
|
|
|
+ message = await msg_queue.get()
|
|
|
+ await websocket.send(message)
|
|
|
+
|
|
|
+
|
|
|
+async def echo(websocket, path):
|
|
|
+ await send_messages(websocket)
|
|
|
+
|
|
|
+
|
|
|
+async def ws_server():
|
|
|
+ async with websockets.serve(echo, "192.168.6.66", 8765):
|
|
|
+ print('WS 服务已经建立,地址 ws://192.168.6.66:'+str(port)+',等待客户端接入...')
|
|
|
+ await asyncio.Future() # run forever
|
|
|
+
|
|
|
+
|
|
|
+def create_ws_server():
|
|
|
+ # 启动线程
|
|
|
+ asyncio.run(ws_server())
|
|
|
+
|
|
|
+
|
|
|
+def create_message(msg_queue):
|
|
|
+ # 创建消息
|
|
|
+ json_message = ""
|
|
|
+ message_name = ""
|
|
|
+ default_index = 0
|
|
|
+ while json_message != "0":
|
|
|
+ os.system('cls') # 清空控制台
|
|
|
+ if json_message != "":
|
|
|
+ print('----------------------------------------')
|
|
|
+ print('已经发送的消息:'+options[default_index]['name']+'\n')
|
|
|
+ message_content = json.loads(json_message)
|
|
|
+ pprint(message_content)
|
|
|
+ print('\n----------------------------------------\n')
|
|
|
+ answers = prompt(get_questions(default_index))
|
|
|
+ if answers['message'] == "0":
|
|
|
+ os.system('cls') # 清空控制台
|
|
|
+ break
|
|
|
+ json_message = answers['message']['content']
|
|
|
+ default_index = answers['message']['index']
|
|
|
+ msg_queue.put_nowait(json_message) # 将消息添加到消息队列中
|
|
|
+ os._exit(0)
|
|
|
+
|
|
|
+
|
|
|
+def wait_connect_start():
|
|
|
+ # 等待连接
|
|
|
+ while True:
|
|
|
+ time.sleep(1)
|
|
|
+ if flag_queue.empty():
|
|
|
+ continue
|
|
|
+ else:
|
|
|
+ break
|
|
|
+ create_message(msg_queue)
|
|
|
+
|
|
|
+
|
|
|
+def main():
|
|
|
+ thread = threading.Thread(target=create_ws_server)
|
|
|
+ thread.start()
|
|
|
+ wait_connect_start()
|
|
|
+
|
|
|
+
|
|
|
+main()
|