# coding = utf-8 import struct from apps.web.constant import Const class DefaultParams(object): """ 常量参数、默认参数 """ STATUS_MAP = { 0x00: Const.DEV_WORK_STATUS_IDLE, 0x01: Const.DEV_WORK_STATUS_WORKING, 0x02: Const.DEV_WORK_STATUS_APPOINTMENT, 0x03: Const.DEV_WORK_STATUS_FAULT, # 故障 0x04: Const.DEV_WORK_STATUS_FAULT, # 烟感 0x05: Const.DEV_WORK_STATUS_FAULT, # 雨感 0x06: Const.DEV_WORK_STATUS_FAULT # 设备锁定 } REASON_MAP = { 0x00: u"充满自停", 0x01: u"未检测到充电设备", 0x02: u"过载结束", 0x03: u"电压输出故障", 0x04: u"刷卡结束", 0x05: u"充满自停", 0x06: u"密码开锁结束订单" } class LineInfo(object): def __init__(self, orderNo, port, voltage, current, power, elec, chargeTime, stayTime, temperature, status=0x00): """ 粤万通的端口信息 :param orderNo: 订单编号 :param port: 端口号 :param voltage: 电压 :param current: 电流 :param power: 功率 :param elec: 电量 :param chargeTime: 充电时间 :param stayTime: 占位时间 :param temperature: 当前端口的温度 """ self._orderNo = orderNo self._port = port self._voltage = voltage self._current = current self._power = power self._elec = elec self._chargeTime = chargeTime self._stayTime = stayTime self._temperature = temperature self._status = status @property def orderNo(self): """主板与服务器之间的唯一键 目前使用订单编号代替 后期可能会使用流水号""" return str(self._orderNo) @property def port(self): return str(self._port) @property def voltage(self): """充电电压 单位V""" return int(self._voltage) @property def current(self): """充电电流 单位A""" return int(self.current) / 1000.0 @property def power(self): """功率 单位w""" return int(self._power) @property def elec(self): """消耗的电量 单位kw·h""" return int(self._elec) / 1000.0 @property def chargeTime(self): """充电时间 单位分钟""" return int(self._chargeTime) @property def stayTime(self): """占位时间 单位分钟""" return int(self._stayTime) @property def temperature(self): """温度 单位 ℃""" return int(self._temperature) / 10.0 @property def status(self): """端口状态 需要经过映射转换""" return DefaultParams.STATUS_MAP.get(self._status, Const.DEV_WORK_STATUS_FAULT) class BaseUartData(object): """ 解析来自主板的串口数据 """ def __init__(self, uartStr): self._source = uartStr @property def head(self): """帧头 2个字节""" return self._source[:2] @property def tail(self): """帧尾""" return self._source[-2:] @property def length(self): """数据长度 从起始到结束""" return int(self._source[2: 6], 16) @property def cmd(self): """指令关键字""" return self._source[6: 8] @property def check(self): """校验字节 """ return self._source[-4: -2] @property def data(self): """数据包""" return self._source[8: -4] class UartC0(BaseUartData): def __init__(self, uartStr): super(UartC0, self).__init__(uartStr) data = self.data orderNo = struct.unpack("