hongpai.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import time
  4. from apilib.utils_json import JsonResponse
  5. from apps.web.constant import Const, DeviceCmdCode, MQTT_TIMEOUT
  6. from apps.web.core.adapter.base import SmartBox, fill_2_hexByte
  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. class PedicureHongPaiBox(SmartBox):
  11. def __init__(self, device):
  12. super(PedicureHongPaiBox, self).__init__(device)
  13. def analyze_event_data(self, data):
  14. pass
  15. def support_count_down(self, openId = None, port = None):
  16. return True
  17. def send_dev_runtime(self, openId, duration):
  18. hexTime = fill_2_hexByte(hex(int(duration / 60)), 2)
  19. devInfo = MessageSender.send(self.device, self.make_random_cmdcode(),
  20. {'IMEI': self._device['devNo'], "funCode": '05', 'data': hexTime})
  21. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  22. if devInfo['rst'] == -1:
  23. raise ServiceException({'result': 2, 'description': u'足疗机正在玩命找网络,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦'})
  24. elif devInfo['rst'] == 1:
  25. raise ServiceException({'result': 2, 'description': u'足疗机正在忙,无响应,您的金币还在,请试试其他线路,或者请稍后再试哦'})
  26. if devInfo['data'][4:6] == '00':
  27. raise ServiceException({'result': 2, 'description': u'足疗机启动失败,金币还在,请您稍后重试'})
  28. Device.update_dev_control_cache(
  29. self._device['devNo'],
  30. {
  31. 'needTime': duration,
  32. 'status': Const.DEV_WORK_STATUS_WORKING,
  33. 'finishedTime': int(time.time()) + duration
  34. })
  35. return devInfo
  36. def start_from_api(self, record):
  37. return super(PedicureHongPaiBox, self).start_from_api(record)
  38. def start_device(self, package, openId, attachParas): # 微信支付
  39. # 先检查足疗机是否正在运行,如果正在运行,就返回错误
  40. devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.GET_DEVINFO, payload = {
  41. 'IMEI': self._device['devNo']
  42. }, timeout = MQTT_TIMEOUT.START_DEVICE)
  43. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  44. if devInfo['rst'] == -1:
  45. raise ServiceException({'result': 2, 'description': u'足疗机正在玩命找网络,建议您试试旁边其他设备,或者试试投硬币,或者稍后再试哦'})
  46. elif devInfo['rst'] == 1:
  47. raise ServiceException({'result': 2, 'description': u'足疗机正在忙,无响应,请稍后再试哦'})
  48. if not devInfo.has_key('hongpai_status'):
  49. raise ServiceException({'result': 2, 'description': u'当前足疗机没有返回正确的结果,可能是内部接触不良,请您重试,或者联系客服解决'})
  50. if devInfo['hongpai_status'][4:6] == '01':
  51. raise ServiceException({'result': 2, 'description': u'当前足疗机正在工作中,不能操作哦,等设备空闲后,您再试试吧'})
  52. needTime = int(package['time'])
  53. hexTime = fill_2_hexByte(hex(needTime), 2)
  54. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_NO_RESPONSE,
  55. {'IMEI': self._device['devNo'], "funCode": '05', 'data': hexTime})
  56. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  57. if devInfo['rst'] == -1:
  58. raise ServiceException({'result': 2, 'description': u'足疗机正在玩命找网络,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦'})
  59. elif devInfo['rst'] == 1:
  60. raise ServiceException({'result': 2, 'description': u'足疗机正在忙,无响应,您的金币还在,请试试其他线路,或者请稍后再试哦'})
  61. Device.update_dev_control_cache(
  62. self._device['devNo'],
  63. {
  64. 'needTime': needTime * 60,
  65. 'status': Const.DEV_WORK_STATUS_WORKING,
  66. 'finishedTime': int(time.time()) + needTime * 60
  67. })
  68. return devInfo
  69. def test(self, coins):
  70. return MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_NO_RESPONSE,
  71. {'IMEI': self._device['devNo'], "funCode": '05', 'data': '02'})
  72. def get_left_time(self):
  73. serviceInfo = Device.get_dev_control_cache(self._device['devNo'])
  74. if (serviceInfo is None) or (not serviceInfo.has_key('status')) or (
  75. serviceInfo['status'] == Const.DEV_WORK_STATUS_IDLE):
  76. return 0.0, 0.0
  77. nowTime = int(time.time())
  78. leftTime = (serviceInfo['finishedTime'] - nowTime) / 60.0
  79. needTime = serviceInfo['needTime'] / 60.0
  80. if leftTime <= 0:
  81. return needTime, 0.0
  82. return leftTime, needTime
  83. def count_down(self, request, dev, agent, group, devType, lastOpenId, port = None):
  84. surplus, sumtime = self.get_left_time()
  85. orderProcessing = False
  86. if surplus == 0.0 and sumtime == 0.0:
  87. orderProcessing = True
  88. return JsonResponse(
  89. {
  90. 'result': 1,
  91. 'description': '',
  92. 'payload': {
  93. 'surplus': surplus,
  94. 'sum': sumtime,
  95. 'name': group['groupName'],
  96. 'address': group['address'],
  97. 'code': devType.get('code'),
  98. 'orderProcessing': orderProcessing,
  99. 'logicalCode': dev['logicalCode'],
  100. 'user': 'me' if lastOpenId == request.user.openId else 'notme',
  101. 'agentFeatures': agent.features,
  102. }
  103. })
  104. def get_dev_setting(self):
  105. devObj = Device.objects.get(devNo = self._device['devNo'])
  106. return {'freeHours': devObj.otherConf.get('freeHours', 10)}
  107. def set_device_function_param(self, request, lastSetConf):
  108. if request.POST.has_key('freeHours'):
  109. devObj = Device.objects.get(devNo = self._device['devNo'])
  110. devObj.otherConf['freeHours'] = int(request.POST.get('freeHours', 10))
  111. devObj.save()