123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- # 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("<H", "")
- port = int(data[14: 16], 16)
- voltage = int(data[16: 20], 16)
- current = int(data[20: 24], 16)
- power = int(data[24: 28], 16)
- elec = int(data[28: 36], 16)
- chargeTime = int(data[36: 40], 16)
- stayTime = int(data[40: 44], 16)
- temperature = int(data[44: 48], 16)
- status = int(data[48: 50], 16)
- self._lineInfo = LineInfo(orderNo, port, voltage, current, power, elec, chargeTime, stayTime, temperature, status)
- @property
- def lineInfo(self):
- """端口的信息"""
- return self._lineInfo
- class UartC1(BaseUartData):
- def __init__(self, uartStr):
- super(UartC1, self).__init__(uartStr)
- data = self.data
- orderNo = int(data[:14], 16)
- port = int(data[14: 16], 16)
- voltage = int(data[16: 20], 16)
- current = int(data[20: 24], 16)
- power = int(data[24: 28], 16)
- elec = int(data[28: 36], 16)
- chargeTime = int(data[36: 40], 16)
- stayTime = int(data[40: 44], 16)
- temperature = int(data[44: 48], 16)
- reasonCode = int(data[48: 50], 16)
- self._lineInfo = LineInfo(orderNo, port, voltage, current, power, elec, chargeTime, stayTime, temperature)
- self._reasonCode = reasonCode
- @property
- def reasonCode(self):
- """结束原因的code"""
- return self._reasonCode
- @property
- def reason(self):
- """结束原因的文字描述"""
- return DefaultParams.REASON_MAP.get(self.reasonCode, u"未知原因")
- class UartB3(BaseUartData):
- # TODO
- def __init__(self, uartStr):
- super(UartB3, self).__init__(uartStr)
- class UartB4(BaseUartData):
- # TODO
- def __init__(self, uartStr):
- super(UartB4, self).__init__(uartStr)
|