import struct import logging import time class Command02: def build_02h_response(self, pile_id, allow=True, gun_count=1): frame = bytearray([0x4A, 0x58, 0x02]) # 帧头 + 命令 frame.extend(pile_id) # 桩号 frame.append(0x01) # 数据加密方式(不加密) # 数据域 data = bytearray() current_time = time.gmtime() # 使用 UTC 时间 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) data.append(0x01 if allow else 0x02) # 请求结果 data.append(0x00) # 拒绝原因(无) if allow: # 二维码字段,使用桩号生成唯一 URL qr_fixed = f"http://example.com/{pile_id.hex().upper()}".encode('ascii') + b"\x00" * (100 - len(f"http://example.com/{pile_id.hex().upper()}")) data.extend(qr_fixed) num_guns = gun_count # 动态设置枪数量 data.append(num_guns) # 动态生成每把枪的二维码 for i in range(num_guns): qr_gun = f"http://example.com/gun{i+1}_{pile_id.hex().upper()}".encode('ascii') + b"\x00" * (20 - len(f"http://example.com/gun{i+1}_{pile_id.hex().upper()}")) data.extend(qr_gun) frame.extend(struct.pack('