changyuan.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. from apps.web.constant import DeviceCmdCode, Const, MQTT_TIMEOUT
  4. from apps.web.core.adapter.base import SmartBox, fill_2_hexByte, hexbyte_2_bin
  5. from apps.web.core.exceptions import ServiceException
  6. from apps.web.core.networking import MessageSender
  7. from apps.web.device.models import Device
  8. class ChargingCYBox(SmartBox):
  9. def __init__(self, device):
  10. super(ChargingCYBox, self).__init__(device)
  11. self.portDetail = {}
  12. def test(self, coins):
  13. coins = 1
  14. cTemp = fill_2_hexByte(hex(int(coins) * 100))
  15. devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
  16. payload = {'IMEI': self._device['devNo'], "funCode": '42', 'data': cTemp})
  17. return devInfo
  18. def start_device(self, package, openId, attachParas):
  19. coins = int(package['coins'])
  20. cTemp = fill_2_hexByte(hex(int(coins) * 100))
  21. devInfo = MessageSender.send(device = self.device,
  22. cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
  23. payload = {
  24. 'IMEI': self._device['devNo'], "funCode": '42', 'data': cTemp
  25. },
  26. timeout = MQTT_TIMEOUT.START_DEVICE)
  27. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  28. if devInfo['rst'] == -1:
  29. raise ServiceException({'result': 2, 'description': u'充电桩正在玩命找网络,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦'})
  30. elif devInfo['rst'] == 1:
  31. raise ServiceException({'result': 2, 'description': u'充电桩正在忙,无响应,您的金币还在,请稍后再试哦'})
  32. if devInfo['data'][6:8] == '4F': # 表示成功
  33. pass
  34. else:
  35. raise ServiceException(
  36. {'result': 2, 'description': u'支付失败,请您先在充电站上按下需要使用的线路端口按钮,然后再付款。如果还不能解决,请检查设备问题,或者联系老板'})
  37. devInfo['finishedTime'] = self.get_duration(package)
  38. return devInfo
  39. def get_dev_setting(self):
  40. devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
  41. payload = {'IMEI': self._device['devNo'], "funCode": '45', 'data': ''})
  42. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  43. if devInfo['rst'] == -1:
  44. raise ServiceException(
  45. {'result': 2, 'description': u'当前充电桩正在玩命找网络,请您稍候再试'})
  46. elif devInfo['rst'] == 1:
  47. raise ServiceException(
  48. {'result': 2, 'description': u'当前充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  49. data = devInfo['data'][6::]
  50. bigTime = int(data[0:4], 16)
  51. midTime = int(data[4:8], 16)
  52. smallTime = int(data[8:12], 16)
  53. littlePower = int(data[12:14], 16)
  54. preFee = int(data[14:18], 16)
  55. voice = int(data[18:20], 16)
  56. return {'bigTime': bigTime, 'midTime': midTime, 'smallTime': smallTime, 'littlePower': littlePower,
  57. 'preFee': round(preFee / 100.0, 2), 'voice': voice}
  58. def clear_dev_feecount(self):
  59. devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
  60. payload = {'IMEI': self._device['devNo'], "funCode": '46', 'data': ''})
  61. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  62. if devInfo['rst'] == -1:
  63. raise ServiceException(
  64. {'result': 2, 'description': u'当前充电桩正在玩命找网络,请您稍候再试'})
  65. elif devInfo['rst'] == 1:
  66. raise ServiceException(
  67. {'result': 2, 'description': u'当前充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  68. if devInfo['data'][6:8] == '4F': # 表示成功
  69. pass
  70. else:
  71. raise ServiceException(
  72. {'result': 2, 'description': u'清除设备统计信息失败,请重试看能否解决'})
  73. def set_dev_setting(self, configDict):
  74. data = ''
  75. data += fill_2_hexByte(hex(int(configDict['bigTime'])))
  76. data += fill_2_hexByte(hex(int(configDict['midTime'])))
  77. data += fill_2_hexByte(hex(int(configDict['smallTime'])))
  78. data += fill_2_hexByte(hex(int(configDict['littlePower'])), 2)
  79. data += fill_2_hexByte(hex(int(configDict['preFee'])))
  80. data += fill_2_hexByte(hex(int(configDict['voice'])), 2)
  81. devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
  82. payload = {'IMEI': self._device['devNo'], "funCode": '41', 'data': data})
  83. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  84. if devInfo['rst'] == -1:
  85. raise ServiceException(
  86. {'result': 2, 'description': u'当前充电桩正在玩命找网络,请您稍候再试'})
  87. elif devInfo['rst'] == 1:
  88. raise ServiceException(
  89. {'result': 2, 'description': u'当前充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  90. if devInfo['data'][6:8] == '4F': # 表示成功
  91. pass
  92. else:
  93. raise ServiceException(
  94. {'result': 2, 'description': u'设置设备参数失败,请重试看能否解决'})
  95. def get_dev_consume_count(self):
  96. devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
  97. payload = {'IMEI': self._device['devNo'], "funCode": '43', 'data': ''})
  98. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  99. if devInfo['rst'] == -1:
  100. raise ServiceException(
  101. {'result': 2, 'description': u'当前充电桩正在玩命找网络,请您稍候再试'})
  102. elif devInfo['rst'] == 1:
  103. raise ServiceException(
  104. {'result': 2, 'description': u'当前充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  105. data = devInfo['data'][6::]
  106. cardFee = int(data[0:6], 16) / 100.0
  107. coinFee = int(data[6:12], 16) / 100.0
  108. mobileFee = int(data[12:18], 16) / 100.0
  109. return {'cardFee': cardFee, 'coinFee': coinFee, 'mobileFee': mobileFee}
  110. def get_port_status(self, force = False):
  111. devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
  112. payload = {'IMEI': self._device['devNo'], "funCode": '44', 'data': ''})
  113. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  114. if devInfo['rst'] == -1:
  115. raise ServiceException(
  116. {'result': 2, 'description': u'当前充电桩正在玩命找网络,请您稍候再试'})
  117. elif devInfo['rst'] == 1:
  118. raise ServiceException(
  119. {'result': 2, 'description': u'当前充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  120. data = devInfo['data'][6::]
  121. resultDict = {}
  122. workInfo = (hexbyte_2_bin(data[0:2]) + hexbyte_2_bin(data[2:4]))[::-1]
  123. index = 0
  124. for wi in workInfo:
  125. index += 1
  126. if wi == '0':
  127. resultDict[str(index)] = {'status': Const.DEV_WORK_STATUS_IDLE, 'index': str(index)}
  128. else:
  129. resultDict[str(index)] = {'status': Const.DEV_WORK_STATUS_WORKING, 'index': str(index)}
  130. workInfo = (hexbyte_2_bin(data[4:6]) + hexbyte_2_bin(data[6:8]))[::-1]
  131. index = 0
  132. for wi in workInfo:
  133. index += 1
  134. if wi == '0':
  135. resultDict[str(index)].update({'mode': 'time'})
  136. else:
  137. resultDict[str(index)] = {'status': 'preFee'}
  138. workInfo = data[8:48]
  139. for ii in range(10):
  140. left = int(workInfo[ii * 4:ii * 4 + 4], 16)
  141. if resultDict[str(ii + 1)]['mode'] == 'time':
  142. resultDict[str(ii + 1)]['leftTime'] = left
  143. elif resultDict[str(ii + 1)]['mode'] == 'preFee':
  144. resultDict[str(ii + 1)]['leftMoney'] = round(left / 100, 2)
  145. allPorts, usedPorts, usePorts = self.get_port_static_info(resultDict)
  146. Device.update_dev_control_cache(self._device['devNo'],
  147. {'allPorts': allPorts, 'usedPorts': usedPorts, 'usePorts': usePorts})
  148. self.portDetail = resultDict
  149. return resultDict
  150. def get_port_info(self, line):
  151. if self.portDetail.has_key(line):
  152. return self.portDetail[line]
  153. else:
  154. self.portDetail = self.get_port_status()
  155. return self.portDetail.get(line, {})
  156. def set_device_function_param(self, request, lastSetConf):
  157. bigTime = request.POST.get('bigTime', None)
  158. midTime = request.POST.get('midTime', None)
  159. smallTime = request.POST.get('smallTime', None)
  160. littlePower = request.POST.get('littlePower', None)
  161. preFee = request.POST.get('preFee', None)
  162. voice = request.POST.get('voice', None)
  163. if bigTime:
  164. lastSetConf.update({'bigTime': int(bigTime)})
  165. if midTime:
  166. lastSetConf.update({'midTime': int(midTime)})
  167. if smallTime:
  168. lastSetConf.update({'smallTime': int(smallTime)})
  169. if littlePower:
  170. lastSetConf.update({'littlePower': int(littlePower)})
  171. if preFee:
  172. lastSetConf.update({'preFee': 100 * int(preFee)})
  173. if voice:
  174. lastSetConf.update({'voice': int(voice)})
  175. self.set_dev_setting(lastSetConf)