60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
# command_1F_20.py
|
|
import struct
|
|
import logging
|
|
import time
|
|
|
|
class Command1F20:
|
|
def process_1f(self, pile_id, conn):
|
|
frame = bytearray([0x4A, 0x58, 0x1F])
|
|
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)
|
|
gun_id = 0x01
|
|
data.append(gun_id)
|
|
order_number = "ORDER1234567890".encode('ascii') + b"\x00" * 2
|
|
data.extend(order_number)
|
|
balance = 1000
|
|
data.extend(struct.pack('<I', balance))
|
|
data.append(0x01) # 充电模式:自动充满
|
|
|
|
frame.extend(struct.pack('<H', len(data)))
|
|
frame.extend(data)
|
|
|
|
checksum = 0
|
|
for b in frame[2:-1]:
|
|
checksum ^= b
|
|
frame.append(checksum)
|
|
|
|
conn.send(frame)
|
|
logging.info(f"发送1FH启动充电: {frame.hex().upper()}")
|
|
return True
|
|
|
|
def parse_20h(self, data):
|
|
if len(data) < 14:
|
|
logging.warning("20H数据长度不足")
|
|
return
|
|
|
|
data_start = 14
|
|
timestamp = data[data_start:data_start + 6]
|
|
gun_id = data[data_start + 6]
|
|
result = data[data_start + 7]
|
|
reason = data[data_start + 8]
|
|
|
|
parsed_data = {
|
|
"timestamp": f"20{timestamp[0]:02X}-{timestamp[1]:02X}-{timestamp[2]:02X} {timestamp[3]:02X}:{timestamp[4]:02X}:{timestamp[5]:02X}",
|
|
"gun_id": gun_id,
|
|
"result": "Success" if result == 0x01 else "Failed",
|
|
"reason": reason if result == 0x02 else None
|
|
}
|
|
logging.info(f"20H启动充电结果: {parsed_data}") |