62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
# command_heartbeat.py
|
|
import struct
|
|
import logging
|
|
import time
|
|
|
|
class CommandHeartbeat:
|
|
def build_0b_heartbeat(self, pile_id):
|
|
frame = bytearray([0x4A, 0x58, 0x0B]) # 帧头 + 命令
|
|
frame.extend(pile_id) # 桩号
|
|
frame.append(0x01) # 数据加密方式(不加密)
|
|
|
|
# 数据域:时间标识
|
|
data = bytearray()
|
|
current_time = time.localtime()
|
|
time_bytes = bytearray([
|
|
current_time.tm_year - 2000,
|
|
current_time.tm_mon,
|
|
current_time.tm_mday,
|
|
current_time.tm_hour,
|
|
current_time.tm_min,
|
|
current_time.tm_sec
|
|
])
|
|
data.extend(time_bytes)
|
|
frame.extend(struct.pack('<H', len(data))) # 数据域长度
|
|
frame.extend(data)
|
|
|
|
# 计算校验码
|
|
checksum = 0
|
|
for b in frame[2:-1]:
|
|
checksum ^= b
|
|
frame.append(checksum)
|
|
return frame
|
|
|
|
def build_0c_heartbeat(self, pile_id):
|
|
frame = bytearray([0x4A, 0x58, 0x0C]) # 帧头 + 命令
|
|
frame.extend(pile_id) # 桩号
|
|
frame.append(0x01) # 数据加密方式(不加密)
|
|
|
|
# 数据域:时间标识
|
|
data = bytearray()
|
|
current_time = time.localtime()
|
|
time_bytes = bytearray([
|
|
current_time.tm_year - 2000,
|
|
current_time.tm_mon,
|
|
current_time.tm_mday,
|
|
current_time.tm_hour,
|
|
current_time.tm_min,
|
|
current_time.tm_sec
|
|
])
|
|
data.extend(time_bytes)
|
|
frame.extend(struct.pack('<H', len(data))) # 数据域长度
|
|
frame.extend(data)
|
|
|
|
# 计算校验码
|
|
checksum = 0
|
|
for b in frame[2:-1]:
|
|
checksum ^= b
|
|
frame.append(checksum)
|
|
return frame
|
|
|
|
def process_0c_heartbeat(self, data, conn):
|
|
logging.info(f"收到0CH心跳响应: {data.hex().upper()}") |