remove unseed code
This commit is contained in:
62
network.py
62
network.py
@@ -19,21 +19,10 @@ import config
|
||||
|
||||
from hardware import hardware_manager
|
||||
from power import get_bus_voltage, voltage_to_percent
|
||||
# from laser import laser_manager
|
||||
# from ota import ota_manager
|
||||
from logger_manager import logger_manager
|
||||
from wifi import wifi_manager
|
||||
|
||||
|
||||
def _calculate_tcp_ssl_password(device_id, iccid):
|
||||
"""
|
||||
与服务器 calculatePassword(deviceId, iccid) 一致:
|
||||
hex(md5(hex(md5(deviceId)) + iccid)),iccid 为空则不拼接。
|
||||
"""
|
||||
md5_device_hex = hashlib.md5(device_id.encode("utf-8")).hexdigest()
|
||||
if iccid:
|
||||
md5_device_hex = md5_device_hex + iccid
|
||||
return hashlib.md5(md5_device_hex.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def _wifi_tls_would_block(exc):
|
||||
@@ -219,10 +208,10 @@ class NetworkManager:
|
||||
iccid = self.get_4g_mccid()
|
||||
iccid = iccid if iccid else ""
|
||||
print(f"iccid: {iccid}")
|
||||
self._password = _calculate_tcp_ssl_password(device_id, iccid)
|
||||
self._password = self._netcore.calculate_tcp_ssl_password(device_id, iccid)
|
||||
else:
|
||||
self._password = device_id + "."
|
||||
print(f"self._password: {self._password}")
|
||||
self.logger.error("[SSL] TCP SSL NOT enabled! exit!")
|
||||
exit(1)
|
||||
|
||||
try:
|
||||
with open("/device_key", "r") as f:
|
||||
@@ -652,46 +641,9 @@ class NetworkManager:
|
||||
"""线程安全地将消息加入队列(公共方法)"""
|
||||
self._enqueue((msg_type, data_dict), high)
|
||||
|
||||
def make_packet(self, msg_type: int, body_dict: dict) -> bytes:
|
||||
"""打包 TCP 数据包:头部(长度+类型+校验)+ JSON 正文"""
|
||||
body = json.dumps(body_dict).encode()
|
||||
body_len = len(body)
|
||||
checksum = body_len + msg_type
|
||||
header = struct.pack(">III", body_len, msg_type, checksum)
|
||||
return header + body
|
||||
|
||||
def parse_packet(self, data: bytes):
|
||||
"""解析 TCP 数据包,返回 (类型, 正文字典)"""
|
||||
if len(data) < 12:
|
||||
return None, None
|
||||
body_len, msg_type, checksum = struct.unpack(">III", data[:12])
|
||||
|
||||
expected_len = 12 + body_len
|
||||
|
||||
# 防御性检查:如果 data 比预期长,说明可能有粘包
|
||||
if len(data) > expected_len:
|
||||
self.logger.warning(
|
||||
f"[TCP] parse_packet: data length ({len(data)}) > expected ({expected_len}), "
|
||||
f"possible packet concatenation. body_len={body_len}, msg_type={msg_type}"
|
||||
)
|
||||
# 只解析第一个包,忽略多余数据(或者可以返回剩余部分)
|
||||
# data = data[:expected_len]
|
||||
# TODO: 是否需要解析剩余部分?
|
||||
|
||||
# 如果 data 比预期短,说明包不完整(半包)
|
||||
if len(data) < expected_len:
|
||||
self.logger.warning(
|
||||
f"[TCP] parse_packet: data length ({len(data)}) < expected ({expected_len}), "
|
||||
f"incomplete packet. body_len={body_len}, msg_type={msg_type}"
|
||||
)
|
||||
return None, None
|
||||
|
||||
body = data[12:12 + body_len]
|
||||
try:
|
||||
return msg_type, json.loads(body.decode())
|
||||
except:
|
||||
return msg_type, {"raw": body.decode(errors="ignore")}
|
||||
|
||||
|
||||
def connect_server(self):
|
||||
"""
|
||||
连接到服务器(自动选择WiFi或4G)
|
||||
@@ -1512,12 +1464,6 @@ class NetworkManager:
|
||||
"reason": str(e)[:100]
|
||||
}, 2)
|
||||
|
||||
def generate_token(self, device_id):
|
||||
"""生成用于 HTTP 接口鉴权的 Token(HMAC-SHA256)"""
|
||||
SALT = "shootMessageFire"
|
||||
SALT2 = "shoot"
|
||||
return "Arrow_" + hmac.new((SALT + device_id).encode(), SALT2.encode(), hashlib.sha256).hexdigest()
|
||||
|
||||
def tcp_main(self):
|
||||
"""TCP 主通信循环:登录、心跳、处理指令、发送数据"""
|
||||
import _thread
|
||||
|
||||
Reference in New Issue
Block a user