54ac76a2 by huangyf2

new

1 parent 4696f3d6
......@@ -237,3 +237,65 @@
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 45}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":46}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 46}
接收: {"jsonrpc":"2.0","method":"discover","params":{"filters":[{"services":["00001523-1212-efde-1523-785feabcd123"]},{"services":["00b6c4bc-8170-268e-4627-e07f37ed6744"]}],"optionalServices":["00004f0e-1212-efde-1523-785feabcd123","00001524-1212-efde-1523-785feabcd123","00001527-1212-efde-1523-785feabcd123","00001528-1212-efde-1523-785feabcd123","00001560-1212-efde-1523-785feabcd123","00001563-1212-efde-1523-785feabcd123","00001565-1212-efde-1523-785feabcd123","01b6c4bc-8170-268e-4627-e07f37ed6744","01b6c4bc-8170-268e-4627-e07f37ed6744","01b6c4bc-8170-268e-4627-e07f37ed6744","01b6c4bc-8170-268e-4627-e07f37ed6744","02b6c4bc-8170-268e-4627-e07f37ed6744","01B6C4BC-8170-268E-4627-E07F37ED6744","02B6C4BC-8170-268E-4627-E07F37ED6744"]},"id":0}
下发: {"jsonrpc": "2.0", "method": "didDiscoverPeripheral", "params": {"name": "ccc", "peripheralId": "07:00:00:00:00:00", "rssi": -44}}
下发: {"jsonrpc": "2.0", "result": null, "id": 0}
接收: {"jsonrpc":"2.0","method":"connect","params":{"peripheralId":"07:00:00:00:00:00"},"id":1}
下发: {"jsonrpc": "2.0", "result": null, "id": 1}
接收: {"jsonrpc":"2.0","method":"write","params":{"serviceId":"00004f0e-1212-efde-1523-785feabcd123","characteristicId":"00001563-1212-efde-1523-785feabcd123","message":"AQIGFwEAAAAAAAA=","encoding":"base64"},"id":2}
下发: {"jsonrpc": "2.0", "result": null, "id": 2}
接收: {"jsonrpc":"2.0","method":"write","params":{"serviceId":"00004f0e-1212-efde-1523-785feabcd123","characteristicId":"00001565-1212-efde-1523-785feabcd123","message":"BgQDAAD/","encoding":"base64"},"id":3}
下发: {"jsonrpc": "2.0", "result": null, "id": 3}
接收: {"jsonrpc":"2.0","method":"startNotifications","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001527-1212-efde-1523-785feabcd123"},"id":4}
下发: {"jsonrpc": "2.0", "result": null, "id": 4}
下发: {"jsonrpc": "2.0", "method": "characteristicDidChange", "params": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001527-1212-efde-1523-785feabcd123", "message": "AgEBAQEAAAABAAAA"}}
下发: {"jsonrpc": "2.0", "method": "characteristicDidChange", "params": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001527-1212-efde-1523-785feabcd123", "message": "AwEzFQEAAAABAAAA"}}
下发: {"jsonrpc": "2.0", "method": "characteristicDidChange", "params": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001527-1212-efde-1523-785feabcd123", "message": "BAE0FAEAAAABAAAA"}}
下发: {"jsonrpc": "2.0", "method": "characteristicDidChange", "params": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001527-1212-efde-1523-785feabcd123", "message": "BQE1FgEAAAABAAAA"}}
下发: {"jsonrpc": "2.0", "method": "characteristicDidChange", "params": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001527-1212-efde-1523-785feabcd123", "message": "BgE2FwEAAAABAAAA"}}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":5}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 5}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":6}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 6}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":7}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 7}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":8}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 8}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":9}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 9}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":10}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 10}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":11}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 11}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":12}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 12}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":13}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 13}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":14}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 14}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":15}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 15}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":16}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 16}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":17}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 17}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":18}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 18}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":19}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 19}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":20}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 20}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":21}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 21}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":22}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 22}
接收: {"jsonrpc":"2.0","method":"read","params":{"serviceId":"00001523-1212-efde-1523-785feabcd123","characteristicId":"00001528-1212-efde-1523-785feabcd123"},"id":23}
下发: {"jsonrpc": "2.0", "result": {"serviceId": "00001523-1212-efde-1523-785feabcd123", "characteristicId": "00001528-1212-efde-1523-785feabcd123", "message": "AA=="}, "id": 23}
自测: {"jsonrpc": "2.0", "method": "ping", "params": {"timestamp": 1744284296}, "id": "test"}
接收: {"jsonrpc": "2.0", "method": "ping", "params": {"timestamp": 1744284296}, "id": "test"}
下发: {"jsonrpc": "2.0", "result": {"pong": true, "timestamp": 1744284296}, "id": "test"}
自测响应: {"jsonrpc": "2.0", "result": {"pong": true, "timestamp": 1744284296}, "id": "test"}
自测: {"jsonrpc": "2.0", "method": "ping", "params": {"timestamp": 1744284371}, "id": "test"}
接收: {"jsonrpc": "2.0", "method": "ping", "params": {"timestamp": 1744284371}, "id": "test"}
下发: {"jsonrpc": "2.0", "result": {"pong": true, "timestamp": 1744284371}, "id": "test"}
自测响应: {"jsonrpc": "2.0", "result": {"pong": true, "timestamp": 1744284371}, "id": "test"}
......
2025-04-09 21:55:22,617 - websockets.server - INFO - server listening on [::1]:20111
2025-04-09 21:55:22,617 - websockets.server - INFO - server listening on 127.0.0.1:20111
2025-04-09 21:55:30,670 - websockets.server - INFO - connection open
2025-04-09 21:56:04,105 - websockets.server - INFO - server listening on [::1]:20111
2025-04-09 21:56:04,105 - websockets.server - INFO - server listening on 127.0.0.1:20111
2025-04-09 21:56:21,751 - websockets.server - INFO - connection open
......@@ -7,6 +7,8 @@ import json
import base64
import threading
from collections import defaultdict
import socket
import time
import platform
......@@ -36,6 +38,38 @@ async def log_message(direction, message):
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, log_message_sync, direction, message)
def is_port_in_use(port, host='localhost'):
"""检查端口是否被占用"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind((host, port))
return False
except socket.error:
return True
async def self_test(websocket):
"""发送自检测试消息"""
test_message = json.dumps({
"jsonrpc": "2.0",
"method": "ping",
"params": {"timestamp": int(time.time())},
"id": "test"
})
await log_message("自测", test_message)
await websocket.send(test_message)
try:
# 等待响应,设置超时
response = await asyncio.wait_for(websocket.recv(), timeout=5.0)
await log_message("自测响应", response)
return True
except asyncio.TimeoutError:
await log_message("自测", "自测超时,未收到响应")
return False
except Exception as e:
await log_message("自测", f"自测异常: {str(e)}")
return False
class BLEClient:
def __init__(self):
self.target_device = None
......@@ -210,6 +244,16 @@ class BLEClient:
})
await log_message("下发", response)
await websocket.send(response)
elif method == "ping":
# 处理ping请求,返回pong响应
response = json.dumps({
"jsonrpc": "2.0",
"result": {"pong": True, "timestamp": int(time.time())},
"id": request_id
})
await log_message("下发", response)
await websocket.send(response)
except json.JSONDecodeError:
error_msg = json.dumps({
......@@ -262,14 +306,39 @@ class BLEClient:
await websocket.send(response)
return callback
async def main():
async with websockets.serve(
async def check_port_and_start_server(port=20111, host='localhost'):
"""检查端口并启动服务器"""
if is_port_in_use(port, host):
print(f"错误: 端口 {port} 已被占用,无法启动服务")
return False
print(f"端口 {port} 可用,正在启动服务...")
server = await websockets.serve(
lambda websocket, path: BLEClient().handle_client(websocket, path),
"localhost", 20111
):
print("WebSocket服务已启动: ws://localhost:20111/scratch/ble")
print("日志文件路径: ./b.log")
await asyncio.Future()
host, port
)
print(f"WebSocket服务已启动: ws://{host}:{port}/scratch/ble")
print("日志文件路径: ./b.log")
# 执行自检测试
try:
async with websockets.connect(f"ws://{host}:{port}/scratch/ble") as websocket:
print("正在执行自检测试...")
test_result = await self_test(websocket)
if test_result:
print("自检测试成功: 服务正常运行")
else:
print("自检测试失败: 服务可能存在问题")
except Exception as e:
print(f"自检测试异常: {str(e)}")
return server
async def main():
server = await check_port_and_start_server()
if server:
await asyncio.Future() # 保持服务器运行
if __name__ == "__main__":
asyncio.run(main())
\ No newline at end of file
......
......@@ -3,14 +3,14 @@
a = Analysis(
['s6.9.2.py'],
pathex=[],
binaries=[],
pathex=['C:\\Python\\Lib\\site-packages\\bleak\\backends'],
binaries=[('C:\\Windows\\System32\\BluetoothApis.dll', '.')],
datas=[('data', 'data')],
hiddenimports=['bleak.backends', 'asyncio'],
hiddenimports=['bleak.backends', 'bleak.backends.winrt', 'asyncio'],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
excludes=['unnecessary_module'],
noarchive=False,
optimize=0,
)
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!