duoliyuan.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import datetime
  4. import logging
  5. from apps.web.constant import DeviceCmdCode, Const, MQTT_TIMEOUT
  6. from apps.web.core.adapter.base import SmartBox, fill_2_hexByte, hexbyte_2_bin
  7. from apps.web.core.exceptions import ServiceException
  8. from apps.web.core.networking import MessageSender
  9. from apps.web.device.models import Device
  10. logger = logging.getLogger(__name__)
  11. class ChargingDLYBox(SmartBox):
  12. def __init__(self, device):
  13. super(ChargingDLYBox, self).__init__(device)
  14. def start_device(self, package, openId, attachParas):
  15. if attachParas is None:
  16. raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路、电池类型信息'})
  17. if not attachParas.has_key('chargeIndex'):
  18. raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路'})
  19. port = hex(int(attachParas['chargeIndex']))
  20. hexPort = fill_2_hexByte(port, 2)
  21. coins = int(package['coins'])
  22. hexCoins = fill_2_hexByte(hex(coins), 2)
  23. devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC, payload = {
  24. 'IMEI': self._device['devNo'],
  25. "funCode": '21',
  26. 'data': hexCoins + hexPort
  27. }, timeout = MQTT_TIMEOUT.START_DEVICE)
  28. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  29. if devInfo['rst'] == -1:
  30. raise ServiceException({'result': 2, 'description': u'充电桩正在玩命找网络,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦'})
  31. elif devInfo['rst'] == 1:
  32. raise ServiceException({'result': 2, 'description': u'充电桩繁忙,无响应,您的金币还在,请试试其他线路,或者稍后再试哦'})
  33. data = devInfo['data'][6::]
  34. if data[20:22] == '01': # 表示成功
  35. pass
  36. else:
  37. raise ServiceException({'result': 2, 'description': u'启动充电失败,请重试看能否解决'})
  38. power = int(data[0:4], 16)
  39. needTime = int(data[4:8], 16)
  40. port = int(data[18:20], 16)
  41. Device.update_dev_control_cache(self._device['devNo'],
  42. {str(port): {'startTime': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
  43. 'power': power, 'coins': float(coins), 'needTime': needTime,
  44. 'isStart': True,
  45. 'openId': openId, 'refunded': False, 'vCardId': self._vcard_id}})
  46. return devInfo
  47. def active_deactive_port(self, port, active):
  48. if not active:
  49. self.stop_port(port)
  50. devInfo = Device.get_dev_control_cache(self._device['devNo'])
  51. portCtrInfo = devInfo.get(str(port), {})
  52. portCtrInfo.update({'isStart': False, 'status': Const.DEV_WORK_STATUS_IDLE, 'needTime': 0, 'leftTime': 0,
  53. 'endTime': datetime.datetime.now().strftime(Const.DATETIME_FMT)})
  54. newValue = {str(port): portCtrInfo}
  55. Device.update_dev_control_cache(self._device['devNo'], newValue)
  56. else:
  57. raise ServiceException({'result': 2, 'description': u'此设备不支持直接启动端口'})
  58. def stop_port(self, port):
  59. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  60. {'IMEI': self._device['devNo'], "funCode": '22',
  61. 'data': fill_2_hexByte(hex(int(port)), 2)})
  62. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  63. if devInfo['rst'] == -1:
  64. raise ServiceException(
  65. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  66. elif devInfo['rst'] == 1:
  67. raise ServiceException(
  68. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  69. data = devInfo['data'][6::]
  70. leftTime = int(data[0:4], 16)
  71. port = int(data[2:4], 16)
  72. return {'leftTime': leftTime, 'port': port}
  73. def get_port_status(self, force = False):
  74. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  75. {'IMEI': self._device['devNo'], "funCode": '23', 'data': ''})
  76. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  77. if devInfo['rst'] == -1:
  78. raise ServiceException(
  79. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  80. elif devInfo['rst'] == 1:
  81. raise ServiceException(
  82. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  83. data = devInfo['data'][6::]
  84. binStatus = hexbyte_2_bin(data[0:4])
  85. result = {}
  86. for ii in range(10):
  87. status = Const.DEV_WORK_STATUS_IDLE if binStatus[ii] == '0' else Const.DEV_WORK_STATUS_WORKING
  88. result[str(ii)] = {'status': status}
  89. allPorts, usedPorts, usePorts = self.get_port_static_info(result)
  90. Device.update_dev_control_cache(self._device['devNo'],
  91. {'allPorts': allPorts, 'usedPorts': usedPorts, 'usePorts': usePorts})
  92. return result
  93. def analyze_event_data(self, data):
  94. cmdCode = data[4:6]
  95. if cmdCode == '12':
  96. data = data[6::]
  97. leftTime = int(data[0:4], 16)
  98. port = int(data[4:6], 16)
  99. finishType = int(data[6:8], 16)
  100. reason = ''
  101. status = Const.DEV_WORK_STATUS_IDLE
  102. if finishType == 3:
  103. status = Const.DEV_WORK_STATUS_FAULT
  104. reason = u'异常结束'
  105. elif finishType == 2:
  106. reason = u'充满结束'
  107. elif finishType == 1:
  108. reason = u'正常结束'
  109. return {'status': status, 'statusInfo': reason, 'cmdCode': '12', 'leftTime': leftTime, 'port': port,
  110. 'reason': reason}
  111. return None