chongdian.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import logging
  4. import time
  5. from apilib.utils_datetime import timestamp_to_dt
  6. from apps.web.constant import Const, MQTT_TIMEOUT, DeviceCmdCode
  7. from apps.web.core.adapter.base import MqttSmartBox, fill_2_hexByte
  8. from apps.web.core.exceptions import ServiceException
  9. from apps.web.core.networking import MessageSender
  10. from apps.web.device.models import Device
  11. logger = logging.getLogger(__name__)
  12. class HeshuiChongdian(MqttSmartBox):
  13. def __init__(self, device):
  14. super(HeshuiChongdian, self).__init__(device)
  15. def analyze_event_data(self, data):
  16. if 'FB' == data[0:2]:
  17. return {'cmdCode': 'FB', 'port': 1}
  18. elif 'FD' in data[0:2]:
  19. if len(data) > 2:
  20. leftTime = int(data[2:6], 16)
  21. else:
  22. leftTime = -1
  23. return {'cmdCode': 'FD', 'port': 1, 'leftTime': leftTime}
  24. return None
  25. def get_port_info(self, line):
  26. leftTime = self.get_left_time_from_chargePort()
  27. return {'port': line, 'leftTime': leftTime}
  28. def get_port_status_from_dev(self):
  29. ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
  30. device = Device.objects(devNo = self._device['devNo']).first()
  31. if device.otherConf.get('isMainboardDetection', False) is True:
  32. devInfo = MessageSender.send(self.device, 210,
  33. {'IMEI': self._device['devNo'], 'serialType': 'charger', 'funCode': 'FC',
  34. 'data': ''})
  35. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  36. if devInfo['rst'] == -1:
  37. raise ServiceException({'result': 2, 'description': u'设备正在玩命找网络,建议您试试旁边其他设备,或者稍后再试哦'})
  38. elif devInfo['rst'] == 1:
  39. raise ServiceException({'result': 2, 'description': u'设备忙,无响应,请您稍候再试。建议您试试旁边其他设备,或者稍后再试哦'})
  40. if devInfo['data'] != '0000':
  41. ctrInfo.update({'1': {'status': Const.DEV_WORK_STATUS_WORKING}})
  42. else:
  43. ctrInfo.update({'1': {'status': Const.DEV_WORK_STATUS_IDLE}})
  44. else:
  45. ctrInfo.update({'1': {'status': Const.DEV_WORK_STATUS_IDLE}})
  46. Device.update_dev_control_cache(self._device['devNo'], ctrInfo)
  47. def get_port_status(self, force = False):
  48. if force:
  49. return self.get_port_status_from_dev()
  50. portDict = {}
  51. ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
  52. if ctrInfo.has_key('1') and ctrInfo['1'].has_key('status'):
  53. portDict.update({'1': ctrInfo['1']})
  54. else:
  55. portDict = {'1': {'status': Const.DEV_WORK_STATUS_IDLE}}
  56. return portDict
  57. def start_device(self, package, openId, attachParas):
  58. #: 首先检查设备是否在线
  59. unit = package.get('unit', u'分钟')
  60. coins = package.get('price')
  61. port = int(attachParas.get('chargeIndex'))
  62. if unit in [u'分钟', u'次', u'升', u'杯', u'包', u'度', u'天', u'小时']: # 如果单位为分钟,就认为是充电
  63. needTimeInput = int(package['time'])
  64. needTime = needTimeInput * 60
  65. serialType = 'charger'
  66. funCode = '0A'
  67. data = fill_2_hexByte(hex(needTimeInput), 4)
  68. elif unit == u'秒': # 如果单位是秒,就认为是接水
  69. needTimeInput = int(package['time'])
  70. needTime = needTimeInput
  71. serialType = 'charger'
  72. funCode = '0A'
  73. data = fill_2_hexByte(hex(needTimeInput), 4)
  74. else:
  75. raise ServiceException({'result': 2, 'description': u'套餐单位错误,无法启动哦'})
  76. devInfo = MessageSender.send(self.device, 220,
  77. {'IMEI': self._device['devNo'], 'serialType': serialType, "funCode": funCode,
  78. 'data': data}, timeout = MQTT_TIMEOUT.START_DEVICE)
  79. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  80. if devInfo['rst'] == -1:
  81. raise ServiceException({'result': 2, 'description': u'设备正在玩命找网络,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦'})
  82. elif devInfo['rst'] == 1:
  83. raise ServiceException({'result': 2, 'description': u'设备正在忙,无响应,您的金币还在,重试不需要重新付款,请试试其他线路,或者请稍后再试哦'})
  84. start_timestamp = int(time.time())
  85. if serialType == 'charger':
  86. devInfo['finishedTime'] = (int(time.time()) + int(needTime))
  87. else:
  88. devInfo['finishedTime'] = int(time.time()) + 60
  89. value = {
  90. 'openId': openId,
  91. 'startTime': timestamp_to_dt(start_timestamp).strftime('%Y-%m-%d %H:%M:%S'),
  92. 'serialType': serialType,
  93. 'needTime': needTime,
  94. 'coins': coins,
  95. 'vCardId': self._vcard_id,
  96. 'status': Const.DEV_WORK_STATUS_WORKING,
  97. 'finishedTime': devInfo['finishedTime']
  98. }
  99. if 'linkedRechargeRecordId' in attachParas:
  100. item = {
  101. 'rechargeRcdId': str(attachParas['linkedRechargeRecordId'])
  102. }
  103. value['payInfo'] = [item]
  104. logger.info('port value is %s' % value)
  105. Device.update_dev_control_cache(self._device['devNo'], {str(port): value})
  106. return devInfo
  107. def get_left_time_from_chargePort(self):
  108. devInfo = MessageSender.send(self.device, 210,
  109. {'IMEI': self._device['devNo'], 'serialType': 'charger', 'funCode': 'FC',
  110. 'data': ''})
  111. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  112. raise ServiceException({'result': 2, 'description': u'当前设备正在玩命找网络,建议您试试旁边其他设备,或者试试投硬币,或者稍后再试哦'})
  113. leftTime = int(devInfo['data'], 16)
  114. return leftTime
  115. def set_standard_pulse(self, standardPulse):
  116. standardPulse = fill_2_hexByte(hex(int(standardPulse)))
  117. MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  118. {'IMEI': self._device['devNo'], 'serialType': 'charger', "funCode": '6B',
  119. 'data': standardPulse})
  120. def set_standard_count_and_time(self, standardCount, standardTime):
  121. standardCount = fill_2_hexByte(hex(int(standardCount)), num = 2)
  122. standardTime = fill_2_hexByte(hex(int(standardTime)), num = 2)
  123. MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  124. {'IMEI': self._device['devNo'], "funCode": 'F5', 'serialType': 'charger',
  125. 'data': standardCount + standardTime})
  126. def set_unit_pulse_detection(self):
  127. MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  128. {'IMEI': self._device['devNo'], "funCode": 'B7', 'serialType': 'charger', 'data': 'CACA'})
  129. def set_device_function_param(self, request, lastSetConf):
  130. if 'standardPulse' in request.POST:
  131. standardPulse = request.POST['standardPulse']
  132. if standardPulse != '':
  133. self.set_standard_pulse(standardPulse)
  134. if 'standardCount' in request.POST and 'standardTime' in request.POST:
  135. standardCount = request.POST['standardCount']
  136. standardTime = request.POST['standardTime']
  137. if standardCount != '' and standardTime != '':
  138. self.set_standard_count_and_time(standardCount, standardTime)
  139. def get_dev_setting(self):
  140. device = Device.objects(devNo = self._device['devNo']).first()
  141. return {'isMainboardDetection': device.otherConf.get('isMainboardDetection', False)}
  142. def set_device_function(self, request, lastSetConf):
  143. if 'unitPulseDetection' in request.POST:
  144. self.set_unit_pulse_detection()
  145. if 'isMainboardDetection' in request.POST:
  146. isMainboardDetection = request.POST['isMainboardDetection']
  147. device = Device.objects(devNo = self._device['devNo']).first()
  148. device.otherConf['isMainboardDetection'] = isMainboardDetection
  149. device.save()