67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
# command_19_1A.py
|
|
import struct
|
|
import logging
|
|
import time
|
|
|
|
class Command191A:
|
|
def process_and_respond(self, data, conn):
|
|
if len(data) < 14:
|
|
logging.warning("19H数据长度不足")
|
|
return
|
|
|
|
pile_id = data[3:11]
|
|
data_start = 14
|
|
data_len = struct.unpack('<H', data[12:14])[0]
|
|
data_end = data_start + data_len
|
|
if len(data) < data_end + 1:
|
|
logging.warning("19H数据域长度不匹配")
|
|
return
|
|
|
|
timestamp = data[data_start:data_start + 6]
|
|
gun_id = data[data_start + 6]
|
|
card_number = data[data_start + 7:data_start + 23].decode('ascii', errors='ignore').rstrip('\x00')
|
|
balance = struct.unpack('<I', data[data_start + 23:data_start + 27])[0]
|
|
|
|
parsed_data = {
|
|
"pile_id": pile_id.hex().upper(),
|
|
"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,
|
|
"card_number": card_number,
|
|
"balance_cents": balance
|
|
}
|
|
logging.info(f"19H卡鉴权请求: {parsed_data}")
|
|
|
|
# 模拟鉴权逻辑(实际需查询数据库)
|
|
auth_result = 0x01 # 假设通过
|
|
new_balance = balance - 100 # 扣除费用
|
|
|
|
# 构建1AH响应
|
|
frame = bytearray([0x4A, 0x58, 0x1A])
|
|
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)
|
|
data.append(gun_id)
|
|
data.append(auth_result)
|
|
data.extend(struct.pack('<I', new_balance))
|
|
|
|
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"发送1AH鉴权响应: {frame.hex().upper()}") |