ble.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import asyncio
  4. from bleak import BleakClient, BleakScanner
  5. from bleak.backends.characteristic import BleakGATTCharacteristic
  6. import chardet
  7. import time
  8. import json
  9. from inputimeout import inputimeout, TimeoutOccurred
  10. import base64
  11. from Crypto.Cipher import AES
  12. from Crypto.Util.Padding import pad, unpad
  13. import copy
  14. import os
  15. import threading
  16. import ctypes
  17. import inspect
  18. import re
  19. from message_base import MessageBase
  20. from websocket_server import WebServer
  21. import sys
  22. import atexit
  23. import signal
  24. import traceback
  25. import logging
  26. logger = logging.getLogger(__name__)
  27. logger.setLevel(logging.DEBUG)
  28. s_h = logging.StreamHandler(sys.stderr)
  29. # formatter = logging.Formatter('%(asctime)s.%(msecs)03d-%(name)s-%(filename)s-[line:%(lineno)d]'
  30. # '-%(levelname)s-[日志信息]: %(message)s',
  31. # datefmt='%Y-%m-%d,%H:%M:%S')
  32. formatter = logging.Formatter('%(asctime)s-[%(lineno)d]'
  33. '-%(levelname)s: %(message)s',
  34. datefmt='%d %H:%M:%S')
  35. s_h.setFormatter(formatter)
  36. logger.addHandler(s_h)
  37. config_file="config.json"
  38. #设备的Characteristic UUID
  39. par_notification_characteristic="0000ffe2-0000-1000-8000-00805f9b34fb"
  40. #设备的Characteristic UUID(具备写属性Write)
  41. par_write_characteristic="0000ffe1-0000-1000-8000-00805f9b34fb"
  42. #设备的MAC地址
  43. # par_device_addr="D1:1D:6A:52:CD:F8"
  44. par_device_addr="D5:2D:D4:9E:5C:3C"
  45. # 密钥(key), 密斯偏移量(iv) CBC模式加密
  46. EV_SOFT_BLE_SEND_CODE = '#MOIF@KHab%DECR$' #//蓝牙发送密钥
  47. EV_SOFT_BLE_RECV_CODE = '$*@AB%DHJqopENC#' #//蓝牙接收密钥
  48. # EV_SOFT_BLE_SEND_CODE = 'XH23456789ABCDEF' #//蓝牙发送密钥
  49. # EV_SOFT_BLE_RECV_CODE = 'XH23456789ABCDEF' #//蓝牙接收密钥
  50. g_run = True
  51. asyncio_list = []
  52. thread_list = []
  53. g_config = {}
  54. g_ble_mtu = 20
  55. g_ble_client = None
  56. g_download_cfg = {"Key":"", "Total":0, "Number":0, "DataCrc16":0}
  57. mb = None
  58. ws = None
  59. def calculate_time(func):
  60. def wrapper(*args, **kwargs):
  61. start_time = time.time()
  62. result = func(*args, **kwargs)
  63. end_time = time.time()
  64. if end_time - start_time >= 0.5 :
  65. print("函数 %s 运行时间为 %.3f 秒" % (func.__name__, end_time - start_time))
  66. return result
  67. return wrapper
  68. @atexit.register
  69. def exit_handler():
  70. logger.info("异常退出")
  71. # 采用traceback模块查看异常,这个方法会打印出异常代码的行号
  72. exc_type, exc_value, exc_tb = sys.exc_info()
  73. logger.info(str(traceback.format_exception(exc_type, exc_value, exc_tb)))
  74. def sig_handler(signum, frame):
  75. global g_run
  76. logger.info('singal: %d' % signum)
  77. logger.info("sig退出")
  78. g_run = False
  79. # 设置事件,通知所有线程退出
  80. exit_event.set()
  81. # for asy in asyncio_list:
  82. # asy.stop()
  83. # for thread in thread_list:
  84. # thread.stop()
  85. sys.exit(0)
  86. def shuncomdacode(data):
  87. """ 识别data的编码格式 """
  88. result = chardet.detect(data)
  89. print(result['encoding'])
  90. return (result['encoding'])
  91. def AES_Encrypt(aes_iv, aes_key, plain_text):
  92. """
  93. AES encrypt
  94. :param plain_text: bytes
  95. :param aes_key: bytes
  96. :param aes_iv: bytes
  97. :return: bytes
  98. """
  99. try:
  100. pad_data = pad(plain_text, AES.block_size)
  101. return AES.new(aes_key, AES.MODE_CBC, aes_iv).encrypt(pad_data)
  102. except Exception as e:
  103. logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
  104. return bytes()
  105. def AES_Decrypt(aes_iv, aes_key, plain_text):
  106. """
  107. AES decrypt
  108. :param plain_text: bytes
  109. :param aes_key: bytes, aes_key
  110. :param aes_iv: bytes, aes_iv
  111. :return: bytes
  112. """
  113. try:
  114. dec_data = AES.new(aes_key, AES.MODE_CBC, aes_iv).decrypt(plain_text)
  115. return unpad(dec_data, AES.block_size)
  116. except Exception as e:
  117. logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
  118. return bytes()
  119. def AES_Encrypt2(vi, key, data):
  120. enctext = bytes()
  121. try:
  122. # vi = '0102030405060708'
  123. pad = lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16)
  124. data = pad(data)
  125. # 字符串补位
  126. cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
  127. encryptedbytes = cipher.encrypt(data.encode('utf8'))
  128. # 加密后得到的是bytes类型的数据
  129. encodestrs = base64.b64encode(encryptedbytes)
  130. # 使用Base64进行编码,返回byte字符串
  131. enctext = encodestrs.decode('utf8')
  132. # 对byte字符串按utf-8进行解码
  133. except Exception as e:
  134. logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
  135. return enctext
  136. def AES_Decrypt2(vi, key, data):
  137. text_decrypted = bytes()
  138. try:
  139. # vi = '0102030405060708'
  140. data = data.encode('utf8')
  141. encodebytes = base64.decodebytes(data)
  142. # 将加密数据转换位bytes类型数据
  143. cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
  144. text_decrypted = cipher.decrypt(encodebytes)
  145. unpad = lambda s: s[0:-s[-1]]
  146. text_decrypted = unpad(text_decrypted)
  147. # 去补位
  148. text_decrypted = text_decrypted.decode('utf8')
  149. except Exception as e:
  150. logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
  151. return text_decrypted
  152. def sum_ccitt_16(data):
  153. total = sum(data)
  154. total &= 0xFFFF
  155. return total
  156. def crc_ccitt_16(data):
  157. crc = 0
  158. for byte in data:
  159. crc ^= (byte << 8)
  160. for _ in range(8):
  161. if crc & 0x8000:
  162. crc = (crc << 1) ^ 0x1021
  163. else:
  164. crc <<= 1
  165. crc &= 0xFFFF
  166. return crc
  167. def checknum_16(data):
  168. type = g_config["def_cfg"]["checknum_type"]
  169. if type == "sum16":
  170. return sum_ccitt_16(data)
  171. elif type == "crc16":
  172. return crc_ccitt_16(data)
  173. return 0
  174. def get_aes_key(type):
  175. if type == "send":
  176. if g_config["def_cfg"]["aes_cbc_key_send"]:
  177. return bytes(g_config["def_cfg"]["aes_cbc_key_send"], encoding='utf-8')
  178. else:
  179. return bytes(EV_SOFT_BLE_SEND_CODE, encoding='utf-8')
  180. else:
  181. if g_config["def_cfg"]["aes_cbc_key_recv"]:
  182. return bytes(g_config["def_cfg"]["aes_cbc_key_recv"], encoding='utf-8')
  183. else:
  184. return bytes(EV_SOFT_BLE_RECV_CODE, encoding='utf-8')
  185. # 初始化一个CRC16校验码计算函数,多项式为0x8005
  186. def ev_packing(data):
  187. data = bytes(data)
  188. # 加密
  189. if g_config["def_cfg"]["aes_cbc_enbable"]:
  190. logger.info("加密前:{}".format(bytes(data)))
  191. key = get_aes_key("send")
  192. data = AES_Encrypt(bytes(key), bytes(key), bytes(data))
  193. lenght = len(data)
  194. # crc16 = crc16_func(data) ## 计算数据的CRC-16校验码
  195. # checknum = crc16(data, 0, len(data))
  196. out = []
  197. # 头
  198. if g_config["def_cfg"]["head_send"] != "":
  199. HEAD_SEND = str(g_config["def_cfg"]["head_send"]).encode()
  200. out = list(HEAD_SEND)
  201. # 长度
  202. out = out + [ (lenght>>8)&0xFF, (lenght>>0)&0xFF]
  203. # 校验码
  204. if g_config["def_cfg"]["checknum_type"] != "":
  205. checknum = checknum_16(data)
  206. out = out + [ (checknum>>8)&0xFF, (checknum>>0)&0xFF]
  207. for d in bytes(data):
  208. out.append(d)
  209. return bytes(out)
  210. #监听回调函数,此处为打印消息
  211. # 记录数据
  212. recv_data = []
  213. recv_start_time = 0
  214. GetConfiguration = []
  215. def PACK_LEN():
  216. H = len(g_config["def_cfg"]["head_send"])
  217. L = 2
  218. C = 2 if g_config["def_cfg"]["checknum_type"]!="" else 0
  219. return H+L+C
  220. def batch_split_key_value(input_str):
  221. # 使用split()方法按';'切割字符串,获取键值对列表
  222. pairs = input_str.split(';')
  223. # 初始化一个字典来存储结果
  224. result = {}
  225. # 遍历键值对列表
  226. for pair in pairs:
  227. # 去除空白字符
  228. pair = pair.strip()
  229. # 检查是否为空字符串
  230. if pair:
  231. # 使用split()方法按'='切割键值对,获取键和值字符串
  232. key, values_str = pair.split('=', 1) # 限制分割次数为1
  233. # 使用split()方法按','切割值字符串,获取值列表
  234. values = values_str.split(',')
  235. # 将键和值列表存储到字典中
  236. result[key] = values
  237. return result
  238. @calculate_time
  239. def recv_handler(data: bytearray, reve=False):
  240. mq.add("ble_recv", data)
  241. def notification_handler(characteristic: BleakGATTCharacteristic, data: bytearray):
  242. global mq
  243. # recv_handler(data)
  244. mq.add("ble_recv", data)
  245. mq.add("ws_send", data)
  246. async def ble_send(client, data):
  247. global recv_start_time
  248. global g_ble_mtu
  249. logger.info('发送:{}'.format(bytes(data).hex()))
  250. frame_len = g_ble_mtu #244 #20 #244
  251. if g_config["def_cfg"]["ble_mtu"] > 0:
  252. frame_len = g_config["def_cfg"]["ble_mtu"]
  253. all_count = len(data)
  254. send_count = 0
  255. send_start_time = time.time()
  256. try:
  257. while send_count<all_count:
  258. cur_len = frame_len
  259. if all_count-send_count<frame_len:
  260. cur_len = all_count-send_count
  261. s = data[send_count:send_count+cur_len]
  262. await client.write_gatt_char(g_config["def_cfg"]["write_char"], s)
  263. send_count += cur_len
  264. except Exception as e:
  265. logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
  266. send_end_time = time.time()
  267. recv_start_time = time.time()
  268. logger.info('发送耗时:%.3fs', send_end_time - send_start_time)
  269. await asyncio.sleep(g_config["def_cfg"]["ble_send_wait"]) #每休眠1秒发送一次
  270. import sys, select
  271. def timeoutable_input(clue="",timeout=None):
  272. print(clue,end="")
  273. i, o, e = select.select([sys.stdin], [], [], timeout)
  274. return sys.stdin.readline() if len(i)>0 else None
  275. def print_data_list():
  276. global g_config
  277. try:
  278. for i in range(len(g_config["cmd_list"])):
  279. print(i, g_config["cmd_list"][i][2])
  280. except Exception as e:
  281. logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
  282. @calculate_time
  283. def send_form_data_list(client, i, data=None):
  284. global g_config
  285. if data:
  286. json_data = data
  287. else:
  288. if i >= len(g_config["cmd_list"]):
  289. return None
  290. json_data = g_config["cmd_list"][i]
  291. if len(json_data) < 3:
  292. return
  293. byte_sequence = b''
  294. if len(json_data[0]) > 4:
  295. if json_data[0][:4] == "hex:":
  296. byte_sequence += bytes.fromhex(json_data[0][4:])
  297. elif json_data[0][:4] == "str:":
  298. byte_sequence += bytes(json_data[0][4:], 'utf-8')
  299. elif json_data[0][:4] == "bin:":
  300. byte_value = int(json_data[0][4:], 2) # 将二进制字符串转换为整数
  301. byte_array = bytes([byte_value]) # 将整数转换为单字节的字节串
  302. byte_sequence += byte_array
  303. if len(json_data[1]) > 4:
  304. if json_data[1][:4] == "hex:":
  305. byte_sequence += bytes.fromhex(json_data[1][4:])
  306. elif json_data[1][:4] == "str:":
  307. byte_sequence += bytes(json_data[1][4:], 'utf-8')
  308. elif json_data[1][:4] == "bin:":
  309. byte_value = int(json_data[1][4:], 2) # 将二进制字符串转换为整数
  310. byte_array = bytes([byte_value]) # 将整数转换为单字节的字节串
  311. byte_sequence += byte_array
  312. logger.info("发送前:{}".format(byte_sequence))
  313. # data_str = json.dumps(json_data)
  314. # 字符串编码为字节序列
  315. # byte_sequence = str.encode(data_str)
  316. # 字节序列转换为bytearray类型
  317. byte_array = bytearray(byte_sequence)
  318. byte_array = ev_packing(byte_array)
  319. send_data = bytes(byte_array)
  320. # await ble_send(client, send_data)
  321. mq.add("ble_send", send_data)
  322. async def task():
  323. global g_config
  324. await asyncio.sleep(3)
  325. while True:
  326. await asyncio.sleep(1)
  327. @calculate_time
  328. def read_config_call():
  329. global g_config
  330. # 读取更新json
  331. with open(config_file, "r", encoding="utf-8") as f:
  332. send_list_new = json.load(f)
  333. if g_config != send_list_new:
  334. g_config = send_list_new
  335. logger.info("json内容改变,内容如下:")
  336. print_data_list()
  337. @calculate_time
  338. def heart_beat_call(client):
  339. global g_config
  340. # 心跳发送:维持蓝牙通讯
  341. logger.info("执行心跳发送任务..")
  342. send_form_data_list(client, g_config["def_cfg"]["heart_beat_sel"])
  343. def extract_key_value(text):
  344. pattern = r"(\w+)=([^;]+)" # 匹配形如 key=value 的字符串
  345. matches = re.findall(pattern, text)
  346. for key, value in matches:
  347. print(f"Key: {key}, Value: {value}")
  348. return matches
  349. @calculate_time
  350. def input_call(client):
  351. global g_config
  352. # 输入
  353. userinput = None
  354. if g_download_cfg["Number"] == 0: #非批量获取配置的状态下
  355. try:
  356. userinput = inputimeout(prompt='请命令序号:', timeout=60)
  357. except TimeoutOccurred:
  358. userinput = None
  359. if userinput:
  360. # 判断类型: 数字int 字符串str 列表list 元组tuple 字典dict
  361. # isinstance(userinput, str)
  362. if not userinput.isalpha():
  363. send_form_data_list(client, int(userinput))
  364. elif str(userinput)=='l':
  365. # 读取更新json
  366. with open(config_file, "r", encoding="utf-8") as f:
  367. send_list_new = json.load(f)
  368. logger.info("json内容如下:")
  369. print_data_list()
  370. @calculate_time
  371. def auto_getcfg_call(client):
  372. global g_config
  373. global g_download_cfg
  374. # 自动发送任务
  375. try:
  376. if g_download_cfg["Number"]>0:
  377. new_one = []
  378. for i, v in enumerate(g_config["cmd_list"]):
  379. if v[2] == "GetConfigurationNumber":
  380. new_one = copy.deepcopy(v) #深拷贝
  381. break
  382. if new_one:
  383. Number = g_download_cfg["Number"]
  384. Total = g_download_cfg["Total"]
  385. new_one[3]["Number"] = Number
  386. send_form_data_list(client, 0, new_one)
  387. g_download_cfg["Number"] = Number+1 if Number < Total else 0
  388. except Exception as e:
  389. logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
  390. async def ble_main():
  391. global g_run
  392. global g_ble_client
  393. global g_config
  394. global g_ble_mtu
  395. global mq
  396. logger.info("快速显示列表, 可输入l")
  397. while g_run:
  398. logger.info("开始扫描...")
  399. #基于MAC地址查找设备
  400. device = await BleakScanner.find_device_by_address(
  401. g_config["def_cfg"]["ble_mac"], cb=dict(use_bdaddr=False) #use_bdaddr判断是否是MOC系统
  402. )
  403. if device is None:
  404. logger.error("无法找到设备({})".format(g_config["def_cfg"]["ble_mac"]))
  405. continue
  406. #事件定义
  407. disconnected_event = asyncio.Event()
  408. #断开连接事件回调
  409. def disconnected_callback(client):
  410. global g_ble_client
  411. logger.info("断开回调!")
  412. disconnected_event.set()
  413. g_ble_client = None
  414. logger.info("尝试连接设备({})...".format(g_config["def_cfg"]["ble_mac"]))
  415. async with BleakClient(device,disconnected_callback=disconnected_callback) as client:
  416. g_ble_client = client
  417. logger.info("已连接(mtu=%d)", client.mtu_size)
  418. g_ble_mtu = client.mtu_size-3
  419. try:
  420. await client.start_notify(g_config["def_cfg"]["notif_char"], notification_handler)
  421. except Exception as e:
  422. logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
  423. g_download_cfg["Number"] = 0
  424. while client and client.is_connected and g_run:
  425. try:
  426. ble_data = mq.get("ble_send")
  427. if ble_data:
  428. await ble_send(client, ble_data)
  429. # 开始根据设备即功能处理消息
  430. ws_data = mq.get("ws_recv")
  431. if ws_data:
  432. await ble_send(client, ws_data)
  433. except Exception as e:
  434. logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
  435. logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
  436. def test_aes():
  437. key = EV_SOFT_BLE_SEND_CODE
  438. data = b'[2, "123456789012", "KeepAlive", {}]' #需要加密的内容
  439. AES_Encrypt(key, key, data)
  440. enctext = AES_Encrypt(key, key, data)
  441. print(enctext)
  442. text_decrypted = AES_Decrypt(key, key, enctext)
  443. print(text_decrypted)
  444. import array,struct
  445. def test_base64():
  446. in_data = b'1234567890\0_+,.//abcdefg'
  447. print(len(in_data), in_data)
  448. base64_bytes = base64.b64encode(in_data)
  449. print(len(base64_bytes),base64_bytes)
  450. out = base64.b64decode(base64_bytes)
  451. print(len(out),out)
  452. def test_recv():
  453. with open(config_file, "r", encoding="utf-8") as f:
  454. g_config = json.load(f)
  455. print_data_list()
  456. str_a = '45 56 3C 00 40 EB AE E3 1F 7B FA 9A 61 86 22 BE 36 0D 0D 09 FE 12 C6 60 27 A8 74 2A 32 08 0D D1 54 B4 4C E1 69 10 4B FC DD 00 23 58 14 74 C1 3F 64 48 9C 88 12 41 17 59 6B 82 F7 90 E7 89 A1 71 80 5A 07 FF 4D 4D 99'
  457. recv_handler(bytes.fromhex(str_a), True)
  458. str_b = '45 56 3C 00 40 B2 E6 73 BD E7 EE B8 1E D4 DE B7 BF 64 19 80 95 24 94 D2 F3 4E 45 97 99 A3 B5 F7 29 E0 86 1A 60 07 96 77 C4 73 DC C0 4B 2B FB 7E 73 D2 A1 CE 17 26 CF 12 FF 06 DC 83 B7 5E 65 C9 18 0B 87 16 EB 75 0F'
  459. recv_handler(bytes.fromhex(str_b), True)
  460. def asyncio_handler():
  461. # tasks.append(task())
  462. # loop = asyncio.get_event_loop()
  463. # loop.run_until_complete(asyncio.wait([ble_main]))
  464. # loop.close()
  465. loop = asyncio.new_event_loop()
  466. loop.run_until_complete(asyncio.wait([ble_main()]))
  467. def ble_recv_handler():
  468. global g_run
  469. global recv_start_time
  470. global g_download_cfg
  471. while g_run:
  472. data = mq.get("ble_recv")
  473. if not data:
  474. time.sleep(0.01)
  475. continue
  476. if g_config["def_cfg"]["recv_detail_print"]:
  477. logger.info("包接收:{}".format(bytes(data)))
  478. for d in data:
  479. recv_data.append(d)
  480. if len(recv_data)>1500:
  481. recv_data.clear()
  482. return
  483. if len(recv_data)<PACK_LEN():
  484. return
  485. recv_data_str = str(bytes(recv_data))
  486. HEAD_SEND = str(g_config["def_cfg"]["head_send"])
  487. HEAD_RECV = str(g_config["def_cfg"]["head_recv"])
  488. reve=True
  489. find_head = HEAD_SEND if not reve else HEAD_RECV
  490. index = -1
  491. if find_head != "":
  492. index = recv_data_str.find(find_head)#'EV>'
  493. if index < 2:
  494. return
  495. index -= 2
  496. data_len = (recv_data[index+4]&0xff) | (recv_data[index+3]&0xff)<<8
  497. if index+PACK_LEN()+data_len > len(recv_data):
  498. return
  499. if g_config["def_cfg"]["recv_soc_data_print"]:
  500. logger.info("接收:{}".format(bytes(recv_data).hex()))
  501. if recv_start_time:
  502. recv_end_time = time.time()
  503. logger.info('接收耗时:%.3fs', recv_end_time - recv_start_time)
  504. recv_start_time = 0
  505. soc_data = recv_data[index+PACK_LEN():index+PACK_LEN()+data_len]
  506. if g_config["def_cfg"]["checknum_type"]!="":
  507. H = len(g_config["def_cfg"]["head_send"])
  508. L = 2
  509. get_crc = (recv_data[index+(H+L+1)]&0xff)| (recv_data[index+(H+L)]&0xff)<<8
  510. check_num = checknum_16(soc_data)
  511. if get_crc != check_num:
  512. logger.info("校验失败(%d):0x%04X 0x%04X", len(recv_data), get_crc, check_num)
  513. return
  514. logger.info("校验成功")
  515. if g_config["def_cfg"]["aes_cbc_enbable"]:
  516. find_type= "recv" if not reve else 'send'
  517. key = get_aes_key(find_type) #"recv"
  518. dec_data = AES_Decrypt(bytes(key), bytes(key), bytes(soc_data))
  519. logger.info("解密后:{}".format(bytes(dec_data)))
  520. else:
  521. dec_data = soc_data
  522. dec_str = bytes(dec_data).decode('utf-8')
  523. logger.info("解析:{}".format(dec_str) )
  524. # 特殊处理:升级
  525. out = batch_split_key_value(dec_str)
  526. if "fwreq" in out.keys():
  527. values = out["fwreq"]
  528. if len(values) == 3:
  529. file = str(values[0])
  530. addr = int(values[1])
  531. size = int(values[2])
  532. filesize = os.stat(file).st_size
  533. with open(file, "rb") as f:
  534. f.seek(addr) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
  535. datas = f.read(size)
  536. f.close()
  537. # 发送
  538. byte_array = bytearray("set:fwdata={}/{},".format(addr, filesize), encoding="utf8")+bytearray(datas)
  539. byte_array = ev_packing(byte_array)
  540. send_data = bytes(byte_array)
  541. mq.add("ble_send", send_data)
  542. logger.info("add event:fwdata" )
  543. # 清理
  544. recv_data.clear()
  545. logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
  546. def key_handler():
  547. global g_run
  548. input_wait_time = 0
  549. while g_run:
  550. if g_config["def_cfg"]["input_interval"]>0 and time.time()-input_wait_time>=g_config["def_cfg"]["input_interval"]:
  551. input_wait_time = time.time()
  552. input_call(None)
  553. time.sleep(0.1)
  554. logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
  555. def main_handler():
  556. global g_run
  557. read_config_time = 0
  558. heart_beart_time = 0
  559. while g_run:
  560. if time.time()-read_config_time >= 3:
  561. read_config_time = time.time()
  562. read_config_call()
  563. if g_config["def_cfg"]["auto_send_GetConfigurationNumber"] and g_download_cfg["Number"]>0:
  564. auto_getcfg_call(None)
  565. elif g_config["def_cfg"]["heart_beat_interval"] >0 and time.time()-heart_beart_time>=g_config["def_cfg"]["heart_beat_interval"]:
  566. heart_beart_time = time.time()
  567. heart_beat_call(None)
  568. logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
  569. #---------------------------------------------------------
  570. if __name__ == "__main__":
  571. signal.signal(signal.SIGTERM, sig_handler) # kill pid
  572. signal.signal(signal.SIGINT, sig_handler) # ctrl -c
  573. logger.info("读取配置...")
  574. with open(config_file, "r", encoding="utf-8") as f:
  575. g_config = json.load(f)
  576. print_data_list()
  577. exit_event = threading.Event()
  578. asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
  579. # test_aes()
  580. # test_base64()
  581. # test_recv()
  582. import websockets
  583. mq = MessageBase()
  584. ws = WebServer("0.0.0.0", 11100, mq)
  585. thread_list.append(threading.Thread(target=main_handler, args=()))
  586. thread_list.append(threading.Thread(target=key_handler, args=()))
  587. thread_list.append(threading.Thread(target=ble_recv_handler, args=()))
  588. for thread in thread_list:
  589. thread.start()
  590. ws_serve = websockets.serve(ws.echo, ws.host, ws.port)
  591. asyncio.get_event_loop().run_until_complete(ws_serve)
  592. task = asyncio.get_event_loop().create_task(ble_main())
  593. asyncio.get_event_loop().run_until_complete(task)
  594. # 等待所有线程完成
  595. for thread in thread_list:
  596. thread.join()
  597. sys.exit(0)