zhiyan.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. from apps.web.core.adapter.base import *
  3. from apps.web.device.models import Device
  4. class ChargingZhiYanBox(SmartBox):
  5. def __init__(self, device):
  6. super(ChargingZhiYanBox, self).__init__(device)
  7. def get_port_status_from_dev(self):
  8. devInfo = MessageSender.send(self.device, self.make_random_cmdcode(),
  9. {'IMEI': self._device['devNo'], "funCode": '02', 'data': '01'})
  10. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  11. if devInfo['rst'] == -1:
  12. raise ServiceException(
  13. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  14. elif devInfo['rst'] == 1:
  15. raise ServiceException(
  16. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  17. data = devInfo['zhiyan_status'][2::]
  18. result = {}
  19. portNum = 8
  20. ii = 0
  21. while ii < portNum:
  22. port = ii + 1
  23. statusTemp = data[ii * 2:ii * 2 + 2]
  24. if statusTemp == '31':
  25. status = {'status': Const.DEV_WORK_STATUS_IDLE}
  26. elif statusTemp == '32':
  27. status = {'status': Const.DEV_WORK_STATUS_WORKING}
  28. elif statusTemp == '30':
  29. status = {'status': Const.DEV_WORK_STATUS_FORBIDDEN}
  30. ii += 1
  31. result[str(port)] = status
  32. allPorts, usedPorts, usePorts = self.get_port_static_info(result)
  33. Device.update_dev_control_cache(self._device['devNo'],
  34. {'allPorts': allPorts, 'usedPorts': usedPorts, 'usePorts': usePorts})
  35. # 这里存在多线程更新缓存的场景,可能性比较低,但是必须先取出来,然后逐个更新状态,然后再记录缓存
  36. ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
  37. for strPort, info in result.items():
  38. if ctrInfo.has_key(strPort):
  39. ctrInfo[strPort].update({'status': info['status']})
  40. else:
  41. ctrInfo[strPort] = info
  42. Device.update_dev_control_cache(self._device['devNo'], ctrInfo)
  43. return result
  44. # 获取端口状态(缓存)
  45. def get_port_status(self, force = False):
  46. if force:
  47. return self.get_port_status_from_dev()
  48. ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
  49. statusDict = {}
  50. allPorts = ctrInfo.get('allPorts', 8)
  51. for ii in range(allPorts):
  52. tempDict = ctrInfo.get(str(ii + 1), {})
  53. if tempDict.has_key('status'):
  54. statusDict[str(ii + 1)] = {'status': tempDict.get('status')}
  55. elif tempDict.has_key('isStart'):
  56. if tempDict['isStart']:
  57. statusDict[str(ii + 1)] = {'status': Const.DEV_WORK_STATUS_WORKING}
  58. else:
  59. statusDict[str(ii + 1)] = {'status': Const.DEV_WORK_STATUS_IDLE}
  60. else:
  61. statusDict[str(ii + 1)] = {'status': Const.DEV_WORK_STATUS_IDLE}
  62. allPorts, usedPorts, usePorts = self.get_port_static_info(statusDict)
  63. Device.update_dev_control_cache(self._device['devNo'],
  64. {'allPorts': allPorts, 'usedPorts': usedPorts, 'usePorts': usePorts})
  65. return statusDict
  66. # 锁定、解锁某一端口
  67. def lock_unlock_port(self, port, lock=True):
  68. if lock:
  69. Device.update_dev_control_cache(self._device['devNo'],
  70. {str(port): {'status': Const.DEV_WORK_STATUS_FORBIDDEN}})
  71. else:
  72. Device.update_dev_control_cache(self._device['devNo'],
  73. {str(port): {'status': Const.DEV_WORK_STATUS_IDLE}})
  74. # 微信支付
  75. def start_device(self, package, openId, attachParas):
  76. if attachParas is None:
  77. raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路、电池类型信息'})
  78. if not attachParas.has_key('chargeIndex'):
  79. raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路'})
  80. port = hex(int('1' + attachParas['chargeIndex']))
  81. funCode = fill_2_hexByte(port, 2)
  82. time = package['time']
  83. hexTimeStr = str(hex(int(time)))[2:]
  84. data = '0' + hexTimeStr if len(hexTimeStr) < 2 else hexTimeStr
  85. devInfo = MessageSender.send(device = self.device, cmd = 220, payload = {
  86. 'IMEI': self._device['devNo'],
  87. "funCode": funCode,
  88. 'data': data
  89. }, timeout = MQTT_TIMEOUT.START_DEVICE)
  90. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  91. if devInfo['rst'] == -1:
  92. raise ServiceException(
  93. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  94. elif devInfo['rst'] == 1:
  95. raise ServiceException(
  96. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  97. return devInfo