|
@@ -245,12 +245,25 @@ def PACK_LEN():
|
|
|
return H+L+C
|
|
return H+L+C
|
|
|
|
|
|
|
|
def batch_split_key_value(input_str):
|
|
def batch_split_key_value(input_str):
|
|
|
- # 正则表达式匹配key=value格式,支持多个键值对
|
|
|
|
|
- pattern = re.compile(r'(\w+)=([\w.]+)')
|
|
|
|
|
|
|
+ # 使用split()方法按';'切割字符串,获取键值对列表
|
|
|
|
|
+ pairs = input_str.split(';')
|
|
|
|
|
+
|
|
|
|
|
+ # 初始化一个字典来存储结果
|
|
|
result = {}
|
|
result = {}
|
|
|
- for match in pattern.finditer(input_str):
|
|
|
|
|
- key, value = match.groups()
|
|
|
|
|
- result[key] = value
|
|
|
|
|
|
|
+
|
|
|
|
|
+ # 遍历键值对列表
|
|
|
|
|
+ for pair in pairs:
|
|
|
|
|
+ # 去除空白字符
|
|
|
|
|
+ pair = pair.strip()
|
|
|
|
|
+ # 检查是否为空字符串
|
|
|
|
|
+ if pair:
|
|
|
|
|
+ # 使用split()方法按'='切割键值对,获取键和值字符串
|
|
|
|
|
+ key, values_str = pair.split('=', 1) # 限制分割次数为1
|
|
|
|
|
+ # 使用split()方法按','切割值字符串,获取值列表
|
|
|
|
|
+ values = values_str.split(',')
|
|
|
|
|
+ # 将键和值列表存储到字典中
|
|
|
|
|
+ result[key] = values
|
|
|
|
|
+
|
|
|
return result
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
@@ -342,6 +355,7 @@ def send_form_data_list(client, i, data=None):
|
|
|
byte_array = bytes([byte_value]) # 将整数转换为单字节的字节串
|
|
byte_array = bytes([byte_value]) # 将整数转换为单字节的字节串
|
|
|
byte_sequence += byte_array
|
|
byte_sequence += byte_array
|
|
|
|
|
|
|
|
|
|
+ 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)
|
|
@@ -532,6 +546,7 @@ def ble_recv_handler():
|
|
|
while g_run:
|
|
while g_run:
|
|
|
data = mq.get("ble_recv")
|
|
data = mq.get("ble_recv")
|
|
|
if not data:
|
|
if not data:
|
|
|
|
|
+ time.sleep(0.1)
|
|
|
continue
|
|
continue
|
|
|
|
|
|
|
|
if g_config["def_cfg"]["recv_detail_print"]:
|
|
if g_config["def_cfg"]["recv_detail_print"]:
|
|
@@ -550,7 +565,7 @@ def ble_recv_handler():
|
|
|
recv_data_str = str(bytes(recv_data))
|
|
recv_data_str = str(bytes(recv_data))
|
|
|
HEAD_SEND = str(g_config["def_cfg"]["head_send"])
|
|
HEAD_SEND = str(g_config["def_cfg"]["head_send"])
|
|
|
HEAD_RECV = str(g_config["def_cfg"]["head_recv"])
|
|
HEAD_RECV = str(g_config["def_cfg"]["head_recv"])
|
|
|
- reve=False
|
|
|
|
|
|
|
+ reve=True
|
|
|
find_head = HEAD_SEND if not reve else HEAD_RECV
|
|
find_head = HEAD_SEND if not reve else HEAD_RECV
|
|
|
index = -1
|
|
index = -1
|
|
|
if find_head != "":
|
|
if find_head != "":
|
|
@@ -590,25 +605,26 @@ def ble_recv_handler():
|
|
|
else:
|
|
else:
|
|
|
dec_data = soc_data
|
|
dec_data = soc_data
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+ dec_str = bytes(dec_data).decode('utf-8')
|
|
|
|
|
+ logger.info("解析:{}".format(dec_str) )
|
|
|
|
|
+
|
|
|
# 特殊处理:升级
|
|
# 特殊处理:升级
|
|
|
- out = batch_split_key_value(dec_data)
|
|
|
|
|
- for key in out:
|
|
|
|
|
- if key == "upgrade_req":
|
|
|
|
|
- values = out[key].split(",")
|
|
|
|
|
- if len(values) == 2:
|
|
|
|
|
- file = values[0]
|
|
|
|
|
- page = values[1]
|
|
|
|
|
- with open(file, "rb", encoding="utf-8") as f:
|
|
|
|
|
- f.seek(page*(1024+4)) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
|
|
|
|
|
- datas = f.read(1024+4)
|
|
|
|
|
- f.close()
|
|
|
|
|
-
|
|
|
|
|
- # 发送
|
|
|
|
|
- byte_array = bytearray("set:upgrade_data=")+bytearray(datas)
|
|
|
|
|
- byte_array = ev_packing(byte_array)
|
|
|
|
|
- send_data = bytes(byte_array)
|
|
|
|
|
- mq.add("ble_send", send_data)
|
|
|
|
|
|
|
+ out = batch_split_key_value(dec_str)
|
|
|
|
|
+ if "upgrade_req" in out.keys():
|
|
|
|
|
+ values = out["upgrade_req"]
|
|
|
|
|
+ if len(values) == 2:
|
|
|
|
|
+ file = str(values[0])
|
|
|
|
|
+ page = int(values[1])
|
|
|
|
|
+ with open(file, "rb") as f:
|
|
|
|
|
+ f.seek(page*(1024+4)) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
|
|
|
|
|
+ datas = f.read(1024+4)
|
|
|
|
|
+ f.close()
|
|
|
|
|
+
|
|
|
|
|
+ # 发送
|
|
|
|
|
+ byte_array = bytearray("set:upgrade_data=", encoding="utf8")+bytearray(datas)
|
|
|
|
|
+ byte_array = ev_packing(byte_array)
|
|
|
|
|
+ send_data = bytes(byte_array)
|
|
|
|
|
+ mq.add("ble_send", send_data)
|
|
|
|
|
|
|
|
# 清理
|
|
# 清理
|
|
|
recv_data.clear()
|
|
recv_data.clear()
|
|
@@ -617,11 +633,23 @@ def ble_recv_handler():
|
|
|
|
|
|
|
|
logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
|
|
logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
|
|
|
|
|
|
|
|
-def main_key_handler():
|
|
|
|
|
|
|
+def key_handler():
|
|
|
|
|
+ global g_run
|
|
|
|
|
+ input_wait_time = 0
|
|
|
|
|
+
|
|
|
|
|
+ while g_run:
|
|
|
|
|
+ if g_config["def_cfg"]["input_interval"]>0 and time.time()-input_wait_time>=g_config["def_cfg"]["input_interval"]:
|
|
|
|
|
+ input_wait_time = time.time()
|
|
|
|
|
+ input_call(None)
|
|
|
|
|
+
|
|
|
|
|
+ time.sleep(0.1)
|
|
|
|
|
+
|
|
|
|
|
+ logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
|
|
|
|
|
+
|
|
|
|
|
+def main_handler():
|
|
|
global g_run
|
|
global g_run
|
|
|
read_config_time = 0
|
|
read_config_time = 0
|
|
|
heart_beart_time = 0
|
|
heart_beart_time = 0
|
|
|
- input_wait_time = 0
|
|
|
|
|
|
|
|
|
|
while g_run:
|
|
while g_run:
|
|
|
if time.time()-read_config_time >= 3:
|
|
if time.time()-read_config_time >= 3:
|
|
@@ -635,10 +663,6 @@ def main_key_handler():
|
|
|
heart_beart_time = time.time()
|
|
heart_beart_time = time.time()
|
|
|
heart_beat_call(None)
|
|
heart_beat_call(None)
|
|
|
|
|
|
|
|
- elif g_config["def_cfg"]["input_interval"]>0 and time.time()-input_wait_time>=g_config["def_cfg"]["input_interval"]:
|
|
|
|
|
- input_wait_time = time.time()
|
|
|
|
|
- input_call(None)
|
|
|
|
|
-
|
|
|
|
|
time.sleep(0.1)
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
|
|
logger.info("结束:{}".format(inspect.currentframe().f_code.co_name))
|
|
@@ -663,7 +687,8 @@ if __name__ == "__main__":
|
|
|
mq = MessageBase()
|
|
mq = MessageBase()
|
|
|
ws = WebServer("0.0.0.0", 11100, mq)
|
|
ws = WebServer("0.0.0.0", 11100, mq)
|
|
|
|
|
|
|
|
- thread_list.append(threading.Thread(target=main_key_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=ble_recv_handler, args=()))
|
|
thread_list.append(threading.Thread(target=ble_recv_handler, args=()))
|
|
|
for thread in thread_list:
|
|
for thread in thread_list:
|
|
|
thread.start()
|
|
thread.start()
|