|
@@ -16,6 +16,7 @@ import threading
|
|
|
import ctypes
|
|
import ctypes
|
|
|
import inspect
|
|
import inspect
|
|
|
import re
|
|
import re
|
|
|
|
|
+import base64
|
|
|
|
|
|
|
|
from message_base import MessageBase
|
|
from message_base import MessageBase
|
|
|
from websocket_server import WebServer
|
|
from websocket_server import WebServer
|
|
@@ -60,6 +61,7 @@ g_config = {}
|
|
|
g_ble_mtu = 20
|
|
g_ble_mtu = 20
|
|
|
g_ble_client = None
|
|
g_ble_client = None
|
|
|
g_download_cfg = {"Key":"", "Total":0, "Number":0, "DataCrc16":0}
|
|
g_download_cfg = {"Key":"", "Total":0, "Number":0, "DataCrc16":0}
|
|
|
|
|
+g_recv_count = 0
|
|
|
|
|
|
|
|
mb = None
|
|
mb = None
|
|
|
ws = None
|
|
ws = None
|
|
@@ -271,11 +273,13 @@ def batch_split_key_value(input_str):
|
|
|
def recv_handler(data: bytearray, reve=False):
|
|
def recv_handler(data: bytearray, reve=False):
|
|
|
mq.add("ble_recv", data)
|
|
mq.add("ble_recv", data)
|
|
|
|
|
|
|
|
|
|
+# 记录数据
|
|
|
def notification_handler(characteristic: BleakGATTCharacteristic, data: bytearray):
|
|
def notification_handler(characteristic: BleakGATTCharacteristic, data: bytearray):
|
|
|
global mq
|
|
global mq
|
|
|
- # recv_handler(data)
|
|
|
|
|
mq.add("ble_recv", data)
|
|
mq.add("ble_recv", data)
|
|
|
- mq.add("ws_send", data)
|
|
|
|
|
|
|
+ if ws:
|
|
|
|
|
+ mq.add("ws_send", data)
|
|
|
|
|
+
|
|
|
|
|
|
|
|
async def ble_send(client, data):
|
|
async def ble_send(client, data):
|
|
|
global recv_start_time
|
|
global recv_start_time
|
|
@@ -290,13 +294,12 @@ async def ble_send(client, data):
|
|
|
send_start_time = time.time()
|
|
send_start_time = time.time()
|
|
|
try:
|
|
try:
|
|
|
while send_count<all_count:
|
|
while send_count<all_count:
|
|
|
- cur_len = frame_len
|
|
|
|
|
- if all_count-send_count<frame_len:
|
|
|
|
|
- cur_len = all_count-send_count
|
|
|
|
|
-
|
|
|
|
|
|
|
+ cur_len = frame_len if all_count-send_count>=frame_len else all_count-send_count
|
|
|
s = data[send_count:send_count+cur_len]
|
|
s = data[send_count:send_count+cur_len]
|
|
|
await client.write_gatt_char(g_config["def_cfg"]["write_char"], s)
|
|
await client.write_gatt_char(g_config["def_cfg"]["write_char"], s)
|
|
|
|
|
+ await asyncio.sleep(0.050)
|
|
|
send_count += cur_len
|
|
send_count += cur_len
|
|
|
|
|
+ logger.info('发送包:{}/{}'.format(send_count, all_count))
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
|
|
logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
|
|
|
|
|
|
|
@@ -304,7 +307,7 @@ async def ble_send(client, data):
|
|
|
send_end_time = time.time()
|
|
send_end_time = time.time()
|
|
|
recv_start_time = time.time()
|
|
recv_start_time = time.time()
|
|
|
logger.info('发送耗时:%.3fs', send_end_time - send_start_time)
|
|
logger.info('发送耗时:%.3fs', send_end_time - send_start_time)
|
|
|
- await asyncio.sleep(g_config["def_cfg"]["ble_send_wait"]) #每休眠1秒发送一次
|
|
|
|
|
|
|
+ # await asyncio.sleep(g_config["def_cfg"]["ble_send_wait"]) #每休眠1秒发送一次
|
|
|
|
|
|
|
|
|
|
|
|
|
import sys, select
|
|
import sys, select
|
|
@@ -317,7 +320,8 @@ def print_data_list():
|
|
|
global g_config
|
|
global g_config
|
|
|
try:
|
|
try:
|
|
|
for i in range(len(g_config["cmd_list"])):
|
|
for i in range(len(g_config["cmd_list"])):
|
|
|
- print(i, g_config["cmd_list"][i][2])
|
|
|
|
|
|
|
+ # print(i, g_config["cmd_list"][i][2])
|
|
|
|
|
+ print(i, g_config["cmd_list"][i]["note"])
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
|
|
logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
|
|
|
|
|
|
|
@@ -331,34 +335,34 @@ def send_form_data_list(client, i, data=None):
|
|
|
return None
|
|
return None
|
|
|
json_data = g_config["cmd_list"][i]
|
|
json_data = g_config["cmd_list"][i]
|
|
|
|
|
|
|
|
- if len(json_data) < 3:
|
|
|
|
|
- return
|
|
|
|
|
|
|
+ # if len(json_data) < 3:
|
|
|
|
|
+ # return
|
|
|
|
|
|
|
|
- byte_sequence = b''
|
|
|
|
|
- if len(json_data[0]) > 4:
|
|
|
|
|
- if json_data[0][:4] == "hex:":
|
|
|
|
|
- byte_sequence += bytes.fromhex(json_data[0][4:])
|
|
|
|
|
- elif json_data[0][:4] == "str:":
|
|
|
|
|
- byte_sequence += bytes(json_data[0][4:], 'utf-8')
|
|
|
|
|
- elif json_data[0][:4] == "bin:":
|
|
|
|
|
- byte_value = int(json_data[0][4:], 2) # 将二进制字符串转换为整数
|
|
|
|
|
- byte_array = bytes([byte_value]) # 将整数转换为单字节的字节串
|
|
|
|
|
- byte_sequence += byte_array
|
|
|
|
|
-
|
|
|
|
|
- if len(json_data[1]) > 4:
|
|
|
|
|
- if json_data[1][:4] == "hex:":
|
|
|
|
|
- byte_sequence += bytes.fromhex(json_data[1][4:])
|
|
|
|
|
- elif json_data[1][:4] == "str:":
|
|
|
|
|
- byte_sequence += bytes(json_data[1][4:], 'utf-8')
|
|
|
|
|
- elif json_data[1][:4] == "bin:":
|
|
|
|
|
- byte_value = int(json_data[1][4:], 2) # 将二进制字符串转换为整数
|
|
|
|
|
- byte_array = bytes([byte_value]) # 将整数转换为单字节的字节串
|
|
|
|
|
- byte_sequence += byte_array
|
|
|
|
|
-
|
|
|
|
|
|
|
+ # byte_sequence = b''
|
|
|
|
|
+ # if len(json_data[0]) > 4:
|
|
|
|
|
+ # if json_data[0][:4] == "hex:":
|
|
|
|
|
+ # byte_sequence += bytes.fromhex(json_data[0][4:])
|
|
|
|
|
+ # elif json_data[0][:4] == "str:":
|
|
|
|
|
+ # byte_sequence += bytes(json_data[0][4:], 'utf-8')
|
|
|
|
|
+ # elif json_data[0][:4] == "bin:":
|
|
|
|
|
+ # byte_value = int(json_data[0][4:], 2) # 将二进制字符串转换为整数
|
|
|
|
|
+ # byte_array = bytes([byte_value]) # 将整数转换为单字节的字节串
|
|
|
|
|
+ # byte_sequence += byte_array
|
|
|
|
|
+
|
|
|
|
|
+ # if len(json_data[1]) > 4:
|
|
|
|
|
+ # if json_data[1][:4] == "hex:":
|
|
|
|
|
+ # byte_sequence += bytes.fromhex(json_data[1][4:])
|
|
|
|
|
+ # elif json_data[1][:4] == "str:":
|
|
|
|
|
+ # byte_sequence += bytes(json_data[1][4:], 'utf-8')
|
|
|
|
|
+ # elif json_data[1][:4] == "bin:":
|
|
|
|
|
+ # byte_value = int(json_data[1][4:], 2) # 将二进制字符串转换为整数
|
|
|
|
|
+ # byte_array = bytes([byte_value]) # 将整数转换为单字节的字节串
|
|
|
|
|
+ # byte_sequence += byte_array
|
|
|
|
|
+ byte_sequence = json_data
|
|
|
logger.info("发送前:{}".format(byte_sequence))
|
|
logger.info("发送前:{}".format(byte_sequence))
|
|
|
- # data_str = json.dumps(json_data)
|
|
|
|
|
|
|
+ data_str = json.dumps(json_data)
|
|
|
# 字符串编码为字节序列
|
|
# 字符串编码为字节序列
|
|
|
- # byte_sequence = str.encode(data_str)
|
|
|
|
|
|
|
+ byte_sequence = str.encode(data_str)
|
|
|
# 字节序列转换为bytearray类型
|
|
# 字节序列转换为bytearray类型
|
|
|
byte_array = bytearray(byte_sequence)
|
|
byte_array = bytearray(byte_sequence)
|
|
|
byte_array = ev_packing(byte_array)
|
|
byte_array = ev_packing(byte_array)
|
|
@@ -406,7 +410,7 @@ def input_call(client):
|
|
|
userinput = None
|
|
userinput = None
|
|
|
if g_download_cfg["Number"] == 0: #非批量获取配置的状态下
|
|
if g_download_cfg["Number"] == 0: #非批量获取配置的状态下
|
|
|
try:
|
|
try:
|
|
|
- userinput = inputimeout(prompt='请命令序号:', timeout=60)
|
|
|
|
|
|
|
+ userinput = inputimeout(prompt='请命令序号:', timeout=g_config["def_cfg"]["input_interval"])
|
|
|
except TimeoutOccurred:
|
|
except TimeoutOccurred:
|
|
|
userinput = None
|
|
userinput = None
|
|
|
if userinput:
|
|
if userinput:
|
|
@@ -421,29 +425,6 @@ def input_call(client):
|
|
|
logger.info("json内容如下:")
|
|
logger.info("json内容如下:")
|
|
|
print_data_list()
|
|
print_data_list()
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-@calculate_time
|
|
|
|
|
-def auto_getcfg_call(client):
|
|
|
|
|
- global g_config
|
|
|
|
|
- global g_download_cfg
|
|
|
|
|
- # 自动发送任务
|
|
|
|
|
- try:
|
|
|
|
|
- if g_download_cfg["Number"]>0:
|
|
|
|
|
- new_one = []
|
|
|
|
|
- for i, v in enumerate(g_config["cmd_list"]):
|
|
|
|
|
- if v[2] == "GetConfigurationNumber":
|
|
|
|
|
- new_one = copy.deepcopy(v) #深拷贝
|
|
|
|
|
- break
|
|
|
|
|
- if new_one:
|
|
|
|
|
- Number = g_download_cfg["Number"]
|
|
|
|
|
- Total = g_download_cfg["Total"]
|
|
|
|
|
- new_one[3]["Number"] = Number
|
|
|
|
|
- send_form_data_list(client, 0, new_one)
|
|
|
|
|
- g_download_cfg["Number"] = Number+1 if Number < Total else 0
|
|
|
|
|
-
|
|
|
|
|
- except Exception as e:
|
|
|
|
|
- logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
|
|
|
|
|
-
|
|
|
|
|
async def ble_main():
|
|
async def ble_main():
|
|
|
global g_run
|
|
global g_run
|
|
|
global g_ble_client
|
|
global g_ble_client
|
|
@@ -476,18 +457,25 @@ async def ble_main():
|
|
|
logger.info("尝试连接设备({})...".format(g_config["def_cfg"]["ble_mac"]))
|
|
logger.info("尝试连接设备({})...".format(g_config["def_cfg"]["ble_mac"]))
|
|
|
|
|
|
|
|
async with BleakClient(device,disconnected_callback=disconnected_callback) as client:
|
|
async with BleakClient(device,disconnected_callback=disconnected_callback) as client:
|
|
|
- g_ble_client = client
|
|
|
|
|
- logger.info("已连接(mtu=%d)", client.mtu_size)
|
|
|
|
|
- g_ble_mtu = client.mtu_size-3
|
|
|
|
|
- try:
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ g_ble_client = client
|
|
|
|
|
+ logger.info("已连接(mtu=%d)", client.mtu_size)
|
|
|
|
|
+ g_ble_mtu = client.mtu_size-3
|
|
|
|
|
+
|
|
|
|
|
+ # 是否连接
|
|
|
|
|
+ # if not client.is_connected:
|
|
|
|
|
+ # client.connect()
|
|
|
|
|
+ # logger.info(f"连接状态: {client.is_connected}")
|
|
|
|
|
+ # 是否配对
|
|
|
|
|
+ # paired = await client.pair(protection_level=2)
|
|
|
|
|
+ # logger.info(f"配对: {paired}")
|
|
|
|
|
+ # 开启通知的接收
|
|
|
await client.start_notify(g_config["def_cfg"]["notif_char"], notification_handler)
|
|
await client.start_notify(g_config["def_cfg"]["notif_char"], notification_handler)
|
|
|
- except Exception as e:
|
|
|
|
|
- logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
|
|
|
|
|
|
|
+ # 循环发送指令
|
|
|
|
|
|
|
|
- g_download_cfg["Number"] = 0
|
|
|
|
|
-
|
|
|
|
|
- while client and client.is_connected and g_run:
|
|
|
|
|
- try:
|
|
|
|
|
|
|
+ g_download_cfg["Number"] = 0
|
|
|
|
|
+
|
|
|
|
|
+ while client.is_connected and g_run:
|
|
|
ble_data = mq.get("ble_send")
|
|
ble_data = mq.get("ble_send")
|
|
|
if ble_data:
|
|
if ble_data:
|
|
|
await ble_send(client, ble_data)
|
|
await ble_send(client, ble_data)
|
|
@@ -495,9 +483,20 @@ async def ble_main():
|
|
|
# 开始根据设备即功能处理消息
|
|
# 开始根据设备即功能处理消息
|
|
|
ws_data = mq.get("ws_recv")
|
|
ws_data = mq.get("ws_recv")
|
|
|
if ws_data:
|
|
if ws_data:
|
|
|
- await ble_send(client, ws_data)
|
|
|
|
|
- except Exception as e:
|
|
|
|
|
- logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
|
|
|
|
|
|
|
+ await ble_send(client, ws_data)
|
|
|
|
|
+ logger.info('断开连接')
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
|
|
|
|
|
+ finally:
|
|
|
|
|
+ # try:
|
|
|
|
|
+ # if client.is_connected:
|
|
|
|
|
+ # # 结束监听
|
|
|
|
|
+ # await client.stop_notify(g_config["def_cfg"]["notif_char"])
|
|
|
|
|
+ # # 断开与蓝牙设备的连接
|
|
|
|
|
+ # await client.disconnect()
|
|
|
|
|
+ # except Exception as e:
|
|
|
|
|
+ # logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
|
|
|
|
|
+ logger.info("结束")
|
|
|
|
|
|
|
|
logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
|
|
logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
|
|
|
|
|
|
|
@@ -538,100 +537,128 @@ def asyncio_handler():
|
|
|
loop.run_until_complete(asyncio.wait([ble_main()]))
|
|
loop.run_until_complete(asyncio.wait([ble_main()]))
|
|
|
|
|
|
|
|
|
|
|
|
|
-def ble_recv_handler():
|
|
|
|
|
- global g_run
|
|
|
|
|
|
|
+def ble_handler(dec_str):
|
|
|
|
|
+ try:
|
|
|
|
|
+ out = json.loads(str(dec_str))
|
|
|
|
|
+ if "key" in out and "value" in out and out["key"] == "fwreq":
|
|
|
|
|
+ file = str(out["value"]["file"])
|
|
|
|
|
+ addr = int(out["value"]["addr"])
|
|
|
|
|
+ size = int(out["value"]["size"])
|
|
|
|
|
+ filesize = os.stat(file).st_size
|
|
|
|
|
+ with open(file, "rb") as f:
|
|
|
|
|
+ f.seek(addr) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
|
|
|
|
|
+ datas = f.read(size)
|
|
|
|
|
+ f.close()
|
|
|
|
|
+
|
|
|
|
|
+ # 发送
|
|
|
|
|
+ fwdata = {
|
|
|
|
|
+ "type": "set",
|
|
|
|
|
+ "data": [
|
|
|
|
|
+ {
|
|
|
|
|
+ "key":"fwdata",
|
|
|
|
|
+ "value":{"cur":addr, "total":filesize, "data": base64.b64encode(datas).decode("utf-8")}
|
|
|
|
|
+ }
|
|
|
|
|
+ ],
|
|
|
|
|
+ "note":"set_fwdata"
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ byte_array = json.dumps(fwdata).encode("utf-8")
|
|
|
|
|
+ byte_array = ev_packing(byte_array)
|
|
|
|
|
+ send_data = bytes(byte_array)
|
|
|
|
|
+ mq.add("ble_send", send_data)
|
|
|
|
|
+ logger.info("add event:fwdata" )
|
|
|
|
|
+
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ logger.error("Err in {}[{}]:\n {}".format(os.path.basename(e.__traceback__.tb_frame.f_globals["__file__"]), e.__traceback__.tb_lineno, e))
|
|
|
|
|
+
|
|
|
|
|
+def ble_unpack(data):
|
|
|
global recv_start_time
|
|
global recv_start_time
|
|
|
global g_download_cfg
|
|
global g_download_cfg
|
|
|
- while g_run:
|
|
|
|
|
- data = mq.get("ble_recv")
|
|
|
|
|
- if not data:
|
|
|
|
|
- time.sleep(0.01)
|
|
|
|
|
- continue
|
|
|
|
|
|
|
+ global g_recv_count
|
|
|
|
|
+ global recv_data
|
|
|
|
|
+ HEAD_SEND = str(g_config["def_cfg"]["head_send"])
|
|
|
|
|
+ HEAD_RECV = str(g_config["def_cfg"]["head_recv"])
|
|
|
|
|
|
|
|
- if g_config["def_cfg"]["recv_detail_print"]:
|
|
|
|
|
- logger.info("包接收:{}".format(bytes(data)))
|
|
|
|
|
|
|
+ # 如果当前包头是'EV>',直接清除之前的数据
|
|
|
|
|
+ if str(bytes(data)).startswith(HEAD_RECV):
|
|
|
|
|
+ recv_data.clear()
|
|
|
|
|
|
|
|
- for d in data:
|
|
|
|
|
- recv_data.append(d)
|
|
|
|
|
|
|
+ for d in data:
|
|
|
|
|
+ recv_data.append(d)
|
|
|
|
|
|
|
|
- if len(recv_data)>1500:
|
|
|
|
|
- recv_data.clear()
|
|
|
|
|
- return
|
|
|
|
|
|
|
+ if g_config["def_cfg"]["recv_detail_print"]:
|
|
|
|
|
+ logger.info("包接收({}),总长度({}):{}".format(len(data), len(recv_data),bytes(data)))
|
|
|
|
|
|
|
|
- if len(recv_data)<PACK_LEN():
|
|
|
|
|
- return
|
|
|
|
|
|
|
+ if len(recv_data)>g_config["def_cfg"]["ble_pack_size"]:
|
|
|
|
|
+ recv_data.clear()
|
|
|
|
|
+ return
|
|
|
|
|
|
|
|
- recv_data_str = str(bytes(recv_data))
|
|
|
|
|
- HEAD_SEND = str(g_config["def_cfg"]["head_send"])
|
|
|
|
|
- HEAD_RECV = str(g_config["def_cfg"]["head_recv"])
|
|
|
|
|
- reve=True
|
|
|
|
|
- find_head = HEAD_SEND if not reve else HEAD_RECV
|
|
|
|
|
- index = -1
|
|
|
|
|
- if find_head != "":
|
|
|
|
|
- index = recv_data_str.find(find_head)#'EV>'
|
|
|
|
|
- if index < 2:
|
|
|
|
|
- return
|
|
|
|
|
- index -= 2
|
|
|
|
|
- data_len = (recv_data[index+4]&0xff) | (recv_data[index+3]&0xff)<<8
|
|
|
|
|
- if index+PACK_LEN()+data_len > len(recv_data):
|
|
|
|
|
|
|
+ if len(recv_data)<PACK_LEN():
|
|
|
|
|
+ return
|
|
|
|
|
+
|
|
|
|
|
+ g_recv_count = g_config["def_cfg"]["heart_beat_interval"]
|
|
|
|
|
+
|
|
|
|
|
+ recv_data_str = str(bytes(recv_data))
|
|
|
|
|
+ find_head = HEAD_RECV
|
|
|
|
|
+ index = -1
|
|
|
|
|
+ if find_head != "":
|
|
|
|
|
+ index = recv_data_str.find(find_head)#'EV>'
|
|
|
|
|
+ if index < 2:
|
|
|
|
|
+ return
|
|
|
|
|
+ index -= 2
|
|
|
|
|
+ data_len = (recv_data[index+4]&0xff) | (recv_data[index+3]&0xff)<<8
|
|
|
|
|
+ if len(recv_data) < data_len+index+PACK_LEN():
|
|
|
|
|
+ logger.info("数据不够%d < %d(index(%d),head(%d),len(%d):0x%02x%02x)", len(recv_data), index+PACK_LEN()+data_len, index,PACK_LEN(),data_len, recv_data[index+3], recv_data[index+4])
|
|
|
|
|
+ return
|
|
|
|
|
+
|
|
|
|
|
+ if g_config["def_cfg"]["recv_soc_data_print"]:
|
|
|
|
|
+ logger.info("接收:{}".format(bytes(recv_data).hex()))
|
|
|
|
|
+
|
|
|
|
|
+ if recv_start_time:
|
|
|
|
|
+ recv_end_time = time.time()
|
|
|
|
|
+ logger.info('接收耗时:%.3fs', recv_end_time - recv_start_time)
|
|
|
|
|
+ recv_start_time = 0
|
|
|
|
|
+
|
|
|
|
|
+ soc_data = recv_data[index+PACK_LEN():index+PACK_LEN()+data_len]
|
|
|
|
|
+ if g_config["def_cfg"]["checknum_type"]!="":
|
|
|
|
|
+ H = len(g_config["def_cfg"]["head_send"])
|
|
|
|
|
+ L = 2
|
|
|
|
|
+ get_crc = (recv_data[index+(H+L+1)]&0xff)| (recv_data[index+(H+L)]&0xff)<<8
|
|
|
|
|
+ check_num = checknum_16(soc_data)
|
|
|
|
|
+ if get_crc != check_num:
|
|
|
|
|
+ logger.info("校验失败(%d):0x%04X 0x%04X", len(recv_data), get_crc, check_num)
|
|
|
|
|
+ recv_data.clear()
|
|
|
return
|
|
return
|
|
|
-
|
|
|
|
|
- if g_config["def_cfg"]["recv_soc_data_print"]:
|
|
|
|
|
- logger.info("接收:{}".format(bytes(recv_data).hex()))
|
|
|
|
|
-
|
|
|
|
|
- if recv_start_time:
|
|
|
|
|
- recv_end_time = time.time()
|
|
|
|
|
- logger.info('接收耗时:%.3fs', recv_end_time - recv_start_time)
|
|
|
|
|
- recv_start_time = 0
|
|
|
|
|
-
|
|
|
|
|
- soc_data = recv_data[index+PACK_LEN():index+PACK_LEN()+data_len]
|
|
|
|
|
- if g_config["def_cfg"]["checknum_type"]!="":
|
|
|
|
|
- H = len(g_config["def_cfg"]["head_send"])
|
|
|
|
|
- L = 2
|
|
|
|
|
- get_crc = (recv_data[index+(H+L+1)]&0xff)| (recv_data[index+(H+L)]&0xff)<<8
|
|
|
|
|
- check_num = checknum_16(soc_data)
|
|
|
|
|
- if get_crc != check_num:
|
|
|
|
|
- logger.info("校验失败(%d):0x%04X 0x%04X", len(recv_data), get_crc, check_num)
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- logger.info("校验成功")
|
|
|
|
|
-
|
|
|
|
|
- if g_config["def_cfg"]["aes_cbc_enbable"]:
|
|
|
|
|
- find_type= "recv" if not reve else 'send'
|
|
|
|
|
- key = get_aes_key(find_type) #"recv"
|
|
|
|
|
- dec_data = AES_Decrypt(bytes(key), bytes(key), bytes(soc_data))
|
|
|
|
|
- logger.info("解密后:{}".format(bytes(dec_data)))
|
|
|
|
|
- else:
|
|
|
|
|
- dec_data = soc_data
|
|
|
|
|
|
|
|
|
|
- dec_str = bytes(dec_data).decode('utf-8')
|
|
|
|
|
- logger.info("解析:{}".format(dec_str) )
|
|
|
|
|
-
|
|
|
|
|
- # 特殊处理:升级
|
|
|
|
|
- out = batch_split_key_value(dec_str)
|
|
|
|
|
- if "fwreq" in out.keys():
|
|
|
|
|
- values = out["fwreq"]
|
|
|
|
|
- if len(values) == 3:
|
|
|
|
|
- file = str(values[0])
|
|
|
|
|
- addr = int(values[1])
|
|
|
|
|
- size = int(values[2])
|
|
|
|
|
- filesize = os.stat(file).st_size
|
|
|
|
|
- with open(file, "rb") as f:
|
|
|
|
|
- f.seek(addr) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
|
|
|
|
|
- datas = f.read(size)
|
|
|
|
|
- f.close()
|
|
|
|
|
-
|
|
|
|
|
- # 发送
|
|
|
|
|
- byte_array = bytearray("set:fwdata={}/{},".format(addr, filesize), encoding="utf8")+bytearray(datas)
|
|
|
|
|
- byte_array = ev_packing(byte_array)
|
|
|
|
|
- send_data = bytes(byte_array)
|
|
|
|
|
- mq.add("ble_send", send_data)
|
|
|
|
|
- logger.info("add event:fwdata" )
|
|
|
|
|
-
|
|
|
|
|
- # 清理
|
|
|
|
|
- recv_data.clear()
|
|
|
|
|
|
|
+ logger.info("校验成功")
|
|
|
|
|
|
|
|
|
|
+ if g_config["def_cfg"]["aes_cbc_enbable"]:
|
|
|
|
|
+ key = get_aes_key("recv") #"recv" 'send'
|
|
|
|
|
+ dec_data = AES_Decrypt(bytes(key), bytes(key), bytes(soc_data))
|
|
|
|
|
+ logger.info("解密后:{}".format(bytes(dec_data)))
|
|
|
|
|
+ else:
|
|
|
|
|
+ dec_data = soc_data
|
|
|
|
|
|
|
|
|
|
+ dec_str = bytes(dec_data).decode('gbk')
|
|
|
|
|
+ logger.info("解析:{}".format(dec_str) )
|
|
|
|
|
+
|
|
|
|
|
+ # 特殊处理:升级
|
|
|
|
|
+ # out = batch_split_key_value(dec_str)
|
|
|
|
|
+ ble_handler(dec_str)
|
|
|
|
|
+
|
|
|
|
|
+ # 清理
|
|
|
|
|
+ recv_data.clear()
|
|
|
|
|
+
|
|
|
|
|
+def ble_recv_handler():
|
|
|
|
|
+ global g_run
|
|
|
|
|
+
|
|
|
|
|
+ while g_run:
|
|
|
|
|
+ data = mq.get("ble_recv")
|
|
|
|
|
+ if data:
|
|
|
|
|
+ ble_unpack(data)
|
|
|
|
|
+
|
|
|
|
|
+ time.sleep(0.1)
|
|
|
|
|
+
|
|
|
logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
|
|
logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
|
|
|
|
|
|
|
|
def key_handler():
|
|
def key_handler():
|
|
@@ -649,21 +676,26 @@ def key_handler():
|
|
|
|
|
|
|
|
def main_handler():
|
|
def main_handler():
|
|
|
global g_run
|
|
global g_run
|
|
|
|
|
+ global g_recv_count
|
|
|
read_config_time = 0
|
|
read_config_time = 0
|
|
|
- heart_beart_time = 0
|
|
|
|
|
|
|
+ sec_count = 0
|
|
|
|
|
|
|
|
while g_run:
|
|
while g_run:
|
|
|
- if time.time()-read_config_time >= 3:
|
|
|
|
|
|
|
+ if time.time()-sec_count >= 1:
|
|
|
|
|
+ sec_count = time.time()
|
|
|
|
|
+ if g_recv_count > 0:
|
|
|
|
|
+ g_recv_count -= 1
|
|
|
|
|
+
|
|
|
|
|
+ if time.time()-read_config_time >= 5:
|
|
|
read_config_time = time.time()
|
|
read_config_time = time.time()
|
|
|
read_config_call()
|
|
read_config_call()
|
|
|
|
|
|
|
|
- if g_config["def_cfg"]["auto_send_GetConfigurationNumber"] and g_download_cfg["Number"]>0:
|
|
|
|
|
- auto_getcfg_call(None)
|
|
|
|
|
-
|
|
|
|
|
- elif g_config["def_cfg"]["heart_beat_interval"] >0 and time.time()-heart_beart_time>=g_config["def_cfg"]["heart_beat_interval"]:
|
|
|
|
|
- heart_beart_time = time.time()
|
|
|
|
|
- heart_beat_call(None)
|
|
|
|
|
|
|
+ if g_ble_client and g_ble_client.is_connected:
|
|
|
|
|
+ if g_recv_count == 0:
|
|
|
|
|
+ g_recv_count = g_config["def_cfg"]["heart_beat_interval"]
|
|
|
|
|
+ heart_beat_call(None)
|
|
|
|
|
|
|
|
|
|
+ time.sleep(0.1)
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
|
|
logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
|
|
@@ -686,7 +718,6 @@ if __name__ == "__main__":
|
|
|
# test_recv()
|
|
# test_recv()
|
|
|
import websockets
|
|
import websockets
|
|
|
mq = MessageBase()
|
|
mq = MessageBase()
|
|
|
- ws = WebServer("0.0.0.0", 11100, mq)
|
|
|
|
|
|
|
|
|
|
thread_list.append(threading.Thread(target=main_handler, args=()))
|
|
thread_list.append(threading.Thread(target=main_handler, args=()))
|
|
|
thread_list.append(threading.Thread(target=key_handler, args=()))
|
|
thread_list.append(threading.Thread(target=key_handler, args=()))
|
|
@@ -694,9 +725,9 @@ if __name__ == "__main__":
|
|
|
for thread in thread_list:
|
|
for thread in thread_list:
|
|
|
thread.start()
|
|
thread.start()
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- ws_serve = websockets.serve(ws.echo, ws.host, ws.port)
|
|
|
|
|
- asyncio.get_event_loop().run_until_complete(ws_serve)
|
|
|
|
|
|
|
+ # ws = WebServer("0.0.0.0", 11100, mq)
|
|
|
|
|
+ # ws_serve = websockets.serve(ws.echo, ws.host, ws.port)
|
|
|
|
|
+ # asyncio.get_event_loop().run_until_complete(ws_serve)
|
|
|
task = asyncio.get_event_loop().create_task(ble_main())
|
|
task = asyncio.get_event_loop().create_task(ble_main())
|
|
|
asyncio.get_event_loop().run_until_complete(task)
|
|
asyncio.get_event_loop().run_until_complete(task)
|
|
|
|
|
|