daqiang.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. from apps.web.constant import DeviceCmdCode, Const
  4. from apps.web.core.adapter.base import SmartBox, fill_2_hexByte
  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 DaQiang(SmartBox):
  9. def _send_data(self, funCode, data, cmd=DeviceCmdCode.OPERATE_DEV_SYNC, timeout=30):
  10. result = MessageSender.send(
  11. device=self.device,
  12. cmd=cmd,
  13. payload={"IMEI": self.device.devNo, "funCode": funCode, "data": data},
  14. timeout=timeout
  15. )
  16. if result.get("rst") == -1:
  17. raise ServiceException({'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  18. if result.get("rst") == 1:
  19. raise ServiceException({'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  20. return result
  21. @staticmethod
  22. def _suit_package(package):
  23. pass
  24. def _start(self, coins):
  25. data = fill_2_hexByte(hex(int(coins)), 2)
  26. result = self._send_data("FD", data)
  27. if result['data'][2:4] == 'F1':
  28. raise ServiceException({'result': 2, 'description': u'未知错误,设备启动失败'})
  29. errorCode = result['data'][4:-2]
  30. if errorCode != "02":
  31. raise ServiceException({'result': 2, 'description': u'设备启动失败'})
  32. return result
  33. def _status_check(self):
  34. data = "01"
  35. result = self._send_data("FE", data)
  36. if result['data'][2:4] == 'F1':
  37. raise ServiceException({'result': 2, 'description': u'未知错误,设备状态查询失败'})
  38. sys_st = result["data"][4:-2]
  39. return sys_st
  40. def check_dev_status(self, attachParas=None):
  41. sys_st = self._status_check()
  42. if sys_st != "01":
  43. if sys_st == '02':
  44. raise ServiceException({'result': 2, 'description': u'设备已被启动,请将子弹用完后再试。'})
  45. else:
  46. raise ServiceException({'result': 2, 'description': u'设备无法使用,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  47. washConf = self.device['washConfig']
  48. for _, _info in washConf.items():
  49. if _info.get('unit') != "次":
  50. raise ServiceException(
  51. {'result': 2, 'description': u'设备单位配置错误({}),请联系经销商修改套餐配置(次)'.format(_info.get('unit'))})
  52. return sys_st
  53. def start_device(self, package, openId, attachParas):
  54. times = int(package['time'])
  55. result = self._start(times)
  56. devDict = {
  57. 'status': Const.DEV_WORK_STATUS_IDLE,
  58. 'times': times,
  59. 'openId': openId,
  60. }
  61. Device.update_dev_control_cache(self._device['devNo'], devDict)
  62. result["finishedTime"] = 12 * 60 * 60
  63. return result
  64. def analyze_event_data(self, data):
  65. cmdCode = data[2:4]
  66. if cmdCode == 'FF':
  67. status_code = data[4:6]
  68. if status_code == '01':
  69. desc = u'设备等待使用中'
  70. elif status_code == '02':
  71. desc = u'设备正在使用中'
  72. else:
  73. desc = u'设备状态异常,请稍候再试'
  74. return {
  75. 'cmdCode': 'FF',
  76. 'status': status_code,
  77. }
  78. elif cmdCode == 'F1':
  79. return {
  80. "cmdCode": 'F1'
  81. }
  82. def update_dq_status(self, status):
  83. data = Device.get_dev_control_cache(self._device['devNo'])
  84. data['status'] = status
  85. Device.update_dev_control_cache(self._device['devNo'], data)
  86. def get_port_status_from_dev(self):
  87. return {'status': Const.DEV_WORK_STATUS_IDLE}