实现单例任务,可推送新任务功能,垃圾回收功能。
This commit is contained in:
37
boot.py
37
boot.py
@@ -7,10 +7,17 @@ import urandom
|
||||
import hashlib
|
||||
import _thread
|
||||
import ujson
|
||||
import gc
|
||||
|
||||
import shared_vars
|
||||
from SingletonThreadPool import SingletonThreadPool
|
||||
|
||||
from InterphoneHandler import InterphoneHandler
|
||||
|
||||
|
||||
gc.enable()
|
||||
|
||||
|
||||
# Wi-Fi配置
|
||||
WIFI_SSID = "JULM"
|
||||
WIFI_PASSWORD = "11223344"
|
||||
@@ -127,22 +134,25 @@ def handle_action(action, data):
|
||||
method = getattr(handler, method_part, None)
|
||||
if method and callable(method):
|
||||
print(f"执行 {action} 方法")
|
||||
method(data)
|
||||
shared_vars.handle_task_id = generate_random_hex()
|
||||
#method(data)
|
||||
thread_pool = SingletonThreadPool()
|
||||
|
||||
thread_pool.add_task(method,data)
|
||||
|
||||
else:
|
||||
print(f"处理器 {class_part} 没有方法: {method_part}")
|
||||
except Exception as e:
|
||||
print(f"执行 {action} 失败: {str(e)}")
|
||||
|
||||
|
||||
def handle_new_data(data):
|
||||
print(f"接收到新数据: {data}")
|
||||
# 修改后的处理逻辑
|
||||
try:
|
||||
# 解析JSON数据
|
||||
message = ujson.loads(data)
|
||||
action = message.get('action')
|
||||
params = message.get('data')
|
||||
|
||||
|
||||
if action:
|
||||
print(f"解析到动作指令: {action}")
|
||||
handle_action(action, params)
|
||||
@@ -151,7 +161,6 @@ def handle_new_data(data):
|
||||
except Exception as e:
|
||||
print(f"数据处理异常: {str(e)}")
|
||||
|
||||
|
||||
def receive_message(sock):
|
||||
header = b""
|
||||
while len(header) < 2: # 确保接收到至少2字节头部
|
||||
@@ -233,7 +242,6 @@ def send_text(sock, message):
|
||||
print(f"发送失败:{str(e)}")
|
||||
raise # 抛出异常供上层处理
|
||||
|
||||
|
||||
# 启动接收数据的线程
|
||||
_thread.start_new_thread(websocket_receive_thread, ())
|
||||
|
||||
@@ -261,8 +269,6 @@ def ws_client():
|
||||
time.sleep(5)
|
||||
reset()
|
||||
|
||||
|
||||
|
||||
def force_cleanup():
|
||||
"""强制清理残留资源"""
|
||||
global WS_SOCK
|
||||
@@ -288,4 +294,19 @@ def check_connection_alive():
|
||||
return False
|
||||
raise
|
||||
|
||||
def generate_random_hex():
|
||||
# 初始化一个空的字节数组,用于存储随机字节
|
||||
random_bytes = bytearray()
|
||||
|
||||
# 循环 4 次,每次生成 32 位(4 字节)的随机数
|
||||
for _ in range(4):
|
||||
# 生成 32 位随机数
|
||||
rand_32_bits = urandom.getrandbits(32)
|
||||
# 将 32 位随机数转换为 4 字节,并添加到字节数组中
|
||||
random_bytes.extend(rand_32_bits.to_bytes(4, 'big'))
|
||||
|
||||
# 将随机字节转换为十六进制字符串
|
||||
hex_string = ubinascii.hexlify(random_bytes).decode()
|
||||
return hex_string
|
||||
|
||||
ws_client()
|
||||
|
||||
Reference in New Issue
Block a user