yuewantong.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # coding = utf-8
  2. import struct
  3. from apps.web.constant import Const
  4. class DefaultParams(object):
  5. """
  6. 常量参数、默认参数
  7. """
  8. STATUS_MAP = {
  9. 0x00: Const.DEV_WORK_STATUS_IDLE,
  10. 0x01: Const.DEV_WORK_STATUS_WORKING,
  11. 0x02: Const.DEV_WORK_STATUS_APPOINTMENT,
  12. 0x03: Const.DEV_WORK_STATUS_FAULT, # 故障
  13. 0x04: Const.DEV_WORK_STATUS_FAULT, # 烟感
  14. 0x05: Const.DEV_WORK_STATUS_FAULT, # 雨感
  15. 0x06: Const.DEV_WORK_STATUS_FAULT # 设备锁定
  16. }
  17. REASON_MAP = {
  18. 0x00: u"充满自停",
  19. 0x01: u"未检测到充电设备",
  20. 0x02: u"过载结束",
  21. 0x03: u"电压输出故障",
  22. 0x04: u"刷卡结束",
  23. 0x05: u"充满自停",
  24. 0x06: u"密码开锁结束订单"
  25. }
  26. class LineInfo(object):
  27. def __init__(self, orderNo, port, voltage, current, power, elec, chargeTime, stayTime, temperature, status=0x00):
  28. """
  29. 粤万通的端口信息
  30. :param orderNo: 订单编号
  31. :param port: 端口号
  32. :param voltage: 电压
  33. :param current: 电流
  34. :param power: 功率
  35. :param elec: 电量
  36. :param chargeTime: 充电时间
  37. :param stayTime: 占位时间
  38. :param temperature: 当前端口的温度
  39. """
  40. self._orderNo = orderNo
  41. self._port = port
  42. self._voltage = voltage
  43. self._current = current
  44. self._power = power
  45. self._elec = elec
  46. self._chargeTime = chargeTime
  47. self._stayTime = stayTime
  48. self._temperature = temperature
  49. self._status = status
  50. @property
  51. def orderNo(self):
  52. """主板与服务器之间的唯一键 目前使用订单编号代替 后期可能会使用流水号"""
  53. return str(self._orderNo)
  54. @property
  55. def port(self):
  56. return str(self._port)
  57. @property
  58. def voltage(self):
  59. """充电电压 单位V"""
  60. return int(self._voltage)
  61. @property
  62. def current(self):
  63. """充电电流 单位A"""
  64. return int(self.current) / 1000.0
  65. @property
  66. def power(self):
  67. """功率 单位w"""
  68. return int(self._power)
  69. @property
  70. def elec(self):
  71. """消耗的电量 单位kw·h"""
  72. return int(self._elec) / 1000.0
  73. @property
  74. def chargeTime(self):
  75. """充电时间 单位分钟"""
  76. return int(self._chargeTime)
  77. @property
  78. def stayTime(self):
  79. """占位时间 单位分钟"""
  80. return int(self._stayTime)
  81. @property
  82. def temperature(self):
  83. """温度 单位 ℃"""
  84. return int(self._temperature) / 10.0
  85. @property
  86. def status(self):
  87. """端口状态 需要经过映射转换"""
  88. return DefaultParams.STATUS_MAP.get(self._status, Const.DEV_WORK_STATUS_FAULT)
  89. class BaseUartData(object):
  90. """
  91. 解析来自主板的串口数据
  92. """
  93. def __init__(self, uartStr):
  94. self._source = uartStr
  95. @property
  96. def head(self):
  97. """帧头 2个字节"""
  98. return self._source[:2]
  99. @property
  100. def tail(self):
  101. """帧尾"""
  102. return self._source[-2:]
  103. @property
  104. def length(self):
  105. """数据长度 从起始到结束"""
  106. return int(self._source[2: 6], 16)
  107. @property
  108. def cmd(self):
  109. """指令关键字"""
  110. return self._source[6: 8]
  111. @property
  112. def check(self):
  113. """校验字节 """
  114. return self._source[-4: -2]
  115. @property
  116. def data(self):
  117. """数据包"""
  118. return self._source[8: -4]
  119. class UartC0(BaseUartData):
  120. def __init__(self, uartStr):
  121. super(UartC0, self).__init__(uartStr)
  122. data = self.data
  123. orderNo = struct.unpack("<H", "")
  124. port = int(data[14: 16], 16)
  125. voltage = int(data[16: 20], 16)
  126. current = int(data[20: 24], 16)
  127. power = int(data[24: 28], 16)
  128. elec = int(data[28: 36], 16)
  129. chargeTime = int(data[36: 40], 16)
  130. stayTime = int(data[40: 44], 16)
  131. temperature = int(data[44: 48], 16)
  132. status = int(data[48: 50], 16)
  133. self._lineInfo = LineInfo(orderNo, port, voltage, current, power, elec, chargeTime, stayTime, temperature, status)
  134. @property
  135. def lineInfo(self):
  136. """端口的信息"""
  137. return self._lineInfo
  138. class UartC1(BaseUartData):
  139. def __init__(self, uartStr):
  140. super(UartC1, self).__init__(uartStr)
  141. data = self.data
  142. orderNo = int(data[:14], 16)
  143. port = int(data[14: 16], 16)
  144. voltage = int(data[16: 20], 16)
  145. current = int(data[20: 24], 16)
  146. power = int(data[24: 28], 16)
  147. elec = int(data[28: 36], 16)
  148. chargeTime = int(data[36: 40], 16)
  149. stayTime = int(data[40: 44], 16)
  150. temperature = int(data[44: 48], 16)
  151. reasonCode = int(data[48: 50], 16)
  152. self._lineInfo = LineInfo(orderNo, port, voltage, current, power, elec, chargeTime, stayTime, temperature)
  153. self._reasonCode = reasonCode
  154. @property
  155. def reasonCode(self):
  156. """结束原因的code"""
  157. return self._reasonCode
  158. @property
  159. def reason(self):
  160. """结束原因的文字描述"""
  161. return DefaultParams.REASON_MAP.get(self.reasonCode, u"未知原因")
  162. class UartB3(BaseUartData):
  163. # TODO
  164. def __init__(self, uartStr):
  165. super(UartB3, self).__init__(uartStr)
  166. class UartB4(BaseUartData):
  167. # TODO
  168. def __init__(self, uartStr):
  169. super(UartB4, self).__init__(uartStr)