hanzhen.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. from apps.web.constant import MQTT_TIMEOUT, DeviceCmdCode
  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 FuncCode(object):
  9. CHECK_DEV = "00"
  10. PAY_DEV = "05"
  11. START_DEV = "85"
  12. class HanzhenBox(SmartBox):
  13. def __init__(self, device):
  14. super(HanzhenBox, self).__init__(device)
  15. def _send_data(self, funCode, data, cmd = DeviceCmdCode.OPERATE_DEV_SYNC, timeout = MQTT_TIMEOUT.NORMAL,
  16. orderNo = None):
  17. result = MessageSender.send(device = self.device, cmd = cmd, payload = {
  18. "IMEI": self._device["devNo"],
  19. "funCode": funCode,
  20. "data": data
  21. }, timeout = timeout)
  22. if result.has_key('rst') and result['rst'] != 0:
  23. if result['rst'] == -1:
  24. raise ServiceException({
  25. 'result': 2,
  26. 'description': u'汗蒸机正在玩命找网络,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦'
  27. })
  28. elif result['rst'] == 1:
  29. raise ServiceException({
  30. 'result': 2,
  31. 'description': u'汗蒸机正在忙,无响应,您的金币还在,请试试其他线路,或者请稍后再试哦'
  32. })
  33. return result
  34. def __get_dev_status(self):
  35. """
  36. 获取设备状态 数据域一共9个字节 第三个字节表示是否允许消费 00 允许 第5个字节表示设备是否正常 00 表示正常
  37. :return:
  38. """
  39. result = self._send_data(funCode = FuncCode.CHECK_DEV, data = "00")
  40. data = result['data']
  41. canUse = True if data[10:12] == '00' else False
  42. isDevNormal = True if data[16:18] == '00' else False
  43. return {'canUse': canUse, 'isDevNormal': isDevNormal}
  44. @staticmethod
  45. def reverse_hex(hexCoins):
  46. """
  47. 硬币使用高低位
  48. :param hexCoins:
  49. :return:
  50. """
  51. return hexCoins[2:4] + hexCoins[:2]
  52. def start_device(self, package, openId, attachParas):
  53. """
  54. 启动设备 首先下发支付指令启动设备
  55. 得到设备回应之后 解析回应数据 要求回应指令为85 并且 回应币数和下发币数一致后 再次下发85启动指令 回应数据域为0000
  56. :param openId:
  57. :param attachParas:
  58. :return:
  59. """
  60. if attachParas is None:
  61. raise ServiceException({'result': 2, 'description': u'请您选择合适的商品哦'})
  62. # hexCoins = self.reverse_hex(fill_2_hexByte(hex(int(coins))))
  63. # 协议里面的 币数实际是分钟 真TM坑爹
  64. unit = package['unit']
  65. if unit != u"分钟":
  66. raise ServiceException({'result': 2, 'description': u'套餐单位错误,请联系经销商!'})
  67. sendTime = int(package['time'])
  68. sendTimeHex = self.reverse_hex(fill_2_hexByte(hex(sendTime), 4))
  69. # 首先将币数更新入缓存
  70. Device.update_dev_control_cache(self._device["devNo"], {"sendTime": sendTimeHex, "openId": openId})
  71. result = self._send_data(funCode = FuncCode.PAY_DEV, data = sendTimeHex, timeout = MQTT_TIMEOUT.START_DEVICE)
  72. return result
  73. def analyze_event_data(self, data):
  74. """
  75. 设备上抛事件
  76. 设备上抛事件无效
  77. :param data:
  78. :return:
  79. """
  80. funCode = data[2:4]
  81. sendTime = data[6:10]
  82. return {"sendTime": sendTime, "funCode": funCode}
  83. def check_dev_status(self, attachParas = None):
  84. info = self.__get_dev_status()
  85. if not all([info["canUse"], info["isDevNormal"]]):
  86. raise ServiceException({
  87. 'result': 2,
  88. 'description': u'设备故障无法启动'
  89. })