we.py
14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# -*- coding: utf-8 -*--
import asyncio
import websockets
from bleak import BleakScanner, BleakClient
import json
import base64
import threading
from tray_icon import start_tray_icon
import os
import sys
def resource_path(relative_path):
try:
base_path = sys._MEIPASS # 打包后运行时的临时路径
except AttributeError:
base_path = os.path.dirname(os.path.abspath(__file__)) # 开发环境下的路径
# 使用 os.path.join 拼接路径,并通过 os.path.abspath 转换为绝对路径
return os.path.abspath(os.path.join(base_path, relative_path))
def resource_path(relative_path):
"""获取资源文件的绝对路径,适用于打包后的程序"""
try:
base_path = sys._MEIPASS # 打包后运行时的临时路径
except AttributeError:
base_path = os.path.abspath(".") # 开发环境下的路径
return os.path.join(base_path, relative_path)
# 获取图标路径
icon_path = resource_path(resource_path("data/app.ico"))
# 调用系统托盘图标函数
start_tray_icon(icon_path)
# 添加线程锁以确保日志写入的原子性
write_lock = threading.Lock()
def log_message_sync(direction, message):
"""同步日志记录函数"""
log_entry = f"{direction}: {message}\n"
print(log_entry, end='') # 控制台仍然输出
with write_lock:
with open('a.log', 'a', encoding='utf-8') as f:
f.write(log_entry)
async def log_message(direction, message):
"""异步封装日志记录"""
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, log_message_sync, direction, message)
class BLEClient:
def __init__(self):
self.target_device = None
self.client = None
self.services = []
self.optional_services = []
self.websocket = None
def on_disconnect(self, client):
print("BLE连接断开,关闭WebSocket")
if self.websocket and not self.websocket.closed:
asyncio.create_task(self.close_websocket())
async def close_websocket(self):
await self.websocket.close()
self.websocket = None
def detection_callback(self, device, advertisement_data):
if any(service_uuid in advertisement_data.service_uuids for service_uuid in self.services):
self.target_device = (device, advertisement_data)
if not self.target_device:
print("未找到匹配设备")
return
else:
device, adv_data = self.target_device
print("\n找到目标设备:")
print(f"设备名称: {device.name}")
print(f"设备地址: {device.address}")
print(f"信号强度: {device.rssi} dBm")
print("\n广播信息:")
print(f"服务UUID列表: {adv_data.service_uuids}")
print(f"制造商数据: {adv_data.manufacturer_data}")
print(f"服务数据: {adv_data.service_data}")
print(f"本地名称: {adv_data.local_name}")
return self.target_device
async def handle_client(self, websocket, path):
self.websocket = websocket
if path != "/scratch/ble":
await websocket.close(code=1003, reason="Path not allowed")
return
try:
async for message in websocket:
try:
# 记录接收到的消息
await log_message("接收", message)
request = json.loads(message)
if request["jsonrpc"] != "2.0":
response = json.dumps({
"jsonrpc": "2.0",
"result": {"message": "Invalid Request"},
"id": request.get("id", None)
})
await log_message("下发", response)
await websocket.send(response)
continue
method = request.get("method")
params = request.get("params", {})
request_id = request.get("id")
if method == "discover":
self.services = params.get("filters", [{}])[0].get("services", [])
self.optional_services = params.get("optionalServices", [])
scanner = BleakScanner()
scanner.register_detection_callback(self.detection_callback)
# 添加重试机制
max_retries = 3
found = False
for attempt in range(max_retries):
self.target_device = None # 重置目标设备
await scanner.start()
await asyncio.sleep(5)
await scanner.stop()
if self.target_device:
found = True
break
if attempt < max_retries - 1: # 最后一次不等待
print(f"未找到设备,第{attempt+1}次重试...")
await asyncio.sleep(3)
if not found:
# response = json.dumps({
# "jsonrpc": "2.0",
# "result": {"message": "Device not found"},
# "id": request_id
# })
# await log_message("下发", response)
# await websocket.send(response)
continue
device, adv_data = self.target_device
discover_response = json.dumps({
"jsonrpc": "2.0",
"method": "didDiscoverPeripheral",
"params": {
"name": device.name,
"peripheralId": device.address,
"rssi": device.rssi
}
})
await log_message("下发", discover_response)
await websocket.send(discover_response)
result_response = json.dumps({
"jsonrpc": "2.0",
"result": None,
"id": request_id
})
await log_message("下发", result_response)
await websocket.send(result_response)
elif method == "connect":
peripheral_id = params.get("peripheralId")
if not peripheral_id:
response = json.dumps({
"jsonrpc": "2.0",
"result": {"message": "Invalid params"},
"id": request_id
})
await log_message("下发", response)
await websocket.send(response)
continue
self.client = BleakClient(peripheral_id)
self.client.set_disconnected_callback(self.on_disconnect)
await self.client.connect()
if self.client.is_connected:
print(f"已连接至设备: {peripheral_id}")
response = json.dumps({
"jsonrpc": "2.0",
"result": None,
"id": request_id
})
await log_message("下发", response)
await websocket.send(response)
else:
response = json.dumps({
"jsonrpc": "2.0",
"result": {"message": "Failed to connect"},
"id": request_id
})
await log_message("下发", response)
await websocket.send(response)
elif method == "write":
service_id = params.get("serviceId")
characteristic_id = params.get("characteristicId")
message = params.get("message")
encoding = params.get("encoding", "utf-8")
if not all([service_id, characteristic_id, message]):
response = json.dumps({
"jsonrpc": "2.0",
"result": {"message": "Invalid params"},
"id": request_id
})
await log_message("下发", response)
await websocket.send(response)
continue
if encoding == "base64":
message_bytes = base64.b64decode(message)
else:
message_bytes = message.encode(encoding)
await self.client.write_gatt_char(characteristic_id, message_bytes)
response = json.dumps({
"jsonrpc": "2.0",
"result": None,
"id": request_id
})
await log_message("下发", response)
await websocket.send(response)
elif method == "read":
service_id = params.get("serviceId")
characteristic_id = params.get("characteristicId")
if not all([service_id, characteristic_id]):
response = json.dumps({
"jsonrpc": "2.0",
"result": {"message": "Invalid params"},
"id": request_id
})
await log_message("下发", response)
await websocket.send(response)
continue
data = await self.client.read_gatt_char(characteristic_id)
response = json.dumps({
"jsonrpc": "2.0",
"result": {
"serviceId": service_id,
"characteristicId": characteristic_id,
"message": base64.b64encode(data).decode("utf-8")
},
"id": request_id
})
await log_message("下发", response)
await websocket.send(response)
elif method == "startNotifications":
service_id = params.get("serviceId")
characteristic_id = params.get("characteristicId")
if not all([service_id, characteristic_id]):
response = json.dumps({
"jsonrpc": "2.0",
"result": {"message": "Invalid params"},
"id": request_id
})
await log_message("下发", response)
await websocket.send(response)
continue
await self.client.start_notify(
characteristic_id,
self.notification_handler(websocket, service_id, characteristic_id)
)
response = json.dumps({
"jsonrpc": "2.0",
"result": None,
"id": request_id
})
await log_message("下发", response)
await websocket.send(response)
else:
response = json.dumps({
"jsonrpc": "2.0",
"result": {"message": "Method not found"},
"id": request_id
})
await log_message("下发", response)
await websocket.send(response)
except json.JSONDecodeError:
response = json.dumps({
"jsonrpc": "2.0",
"result": {"message": "Parse error"},
"id": None
})
await log_message("下发", response)
await websocket.send(response)
except Exception as e:
response = json.dumps({
"jsonrpc": "2.0",
"result": {"message": str(e)},
"id": request_id
})
await log_message("下发", response)
await websocket.send(response)
except websockets.exceptions.ConnectionClosedOK:
print("WebSocket客户端正常断开")
except websockets.exceptions.ConnectionClosedError as e:
print(f"WebSocket客户端异常断开: {e.code} - {e.reason}")
finally:
if self.client and self.client.is_connected:
await self.client.disconnect()
print("BLE设备已主动断开")
self.client = None
self.target_device = None
def notification_handler(self, websocket, service_id, characteristic_id):
async def callback(sender, data):
response = json.dumps({
"jsonrpc": "2.0",
"method": "characteristicDidChange",
"params": {
"serviceId": service_id,
"characteristicId": characteristic_id,
"message": base64.b64encode(data).decode("utf-8")
}
})
await log_message("下发", response)
await websocket.send(response)
return callback
async def main():
async with 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() # 永久运行
if __name__ == "__main__":
asyncio.run(main())