binnuo.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. from apps.web.core.adapter.base import *
  3. from apps.web.device.models import Device
  4. class PedicureBox3(SmartBox):
  5. def __init__(self, device):
  6. super(PedicureBox3, self).__init__(device)
  7. def start_device(self, package, openId, attachParas):
  8. #: 首先检查设备是否在线
  9. unit = package.get('unit',u'分钟')
  10. coins = int(package['coins'])
  11. if unit == u'分钟':
  12. duration = int(package['time'])*60
  13. else:
  14. raise ServiceException({'result': 2, 'description': u'经销商配置的套餐信息错误,单位只能配置为分钟,请您重试'})
  15. hexDuration = fill_2_hexByte(hex(duration), 4)
  16. devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
  17. payload = {'IMEI': self._device['devNo'], "funCode": '01', 'data': hexDuration},
  18. timeout = MQTT_TIMEOUT.START_DEVICE)
  19. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  20. if devInfo['rst'] == -1:
  21. raise ServiceException({'result': 2, 'description': u'足疗机正在玩命找网络,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦'})
  22. elif devInfo['rst'] == 1:
  23. raise ServiceException({'result': 2, 'description': u'足疗机正在忙,无响应,您的金币还在,重试不需要重新付款,请试试其他线路,或者请稍后再试哦'})
  24. start_timestamp = int(time.time())
  25. devInfo['finishedTime'] = start_timestamp + int(duration)
  26. Device.update_dev_control_cache(self._device['devNo'],
  27. {
  28. 'status': Const.DEV_WORK_STATUS_WORKING,
  29. 'finishedTime': devInfo['finishedTime'],
  30. 'openId': openId,
  31. 'startTime': timestamp_to_dt(start_timestamp).strftime('%Y-%m-%d %H:%M:%S'),
  32. 'needTime': duration,
  33. 'coins': coins,
  34. 'vCardId': self._vcard_id
  35. })
  36. return devInfo
  37. def stop(self,port=None):
  38. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  39. {'IMEI': self._device['devNo'], "funCode": '03', 'data': '0303'})
  40. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  41. return JsonResponse({"result": 0, "description": u'网络连接异常,停止设备失败,请您重新尝试停掉设备', "payload": ''})
  42. devInfo['remainder_time'] = 0.0
  43. return devInfo
  44. def support_count_down(self,openId=None,port=None):
  45. return True
  46. def count_down(self,request,dev,agent,group,devType,lastOpenId,port=None):
  47. surplus, sumtime = self.get_left_time()
  48. orderProcessing = False
  49. if surplus == 0.0 and sumtime == 0.0:
  50. orderProcessing = True
  51. return JsonResponse(
  52. {
  53. 'result': 1,
  54. 'description': '',
  55. 'payload': {
  56. 'surplus': surplus,
  57. 'sum': sumtime,
  58. 'name': group['groupName'],
  59. 'address': group['address'],
  60. 'code': devType.get('code'),
  61. 'orderProcessing': orderProcessing,
  62. 'logicalCode': dev['logicalCode'],
  63. 'user': 'me' if lastOpenId == request.user.openId else 'notme',
  64. 'agentFeatures': agent.features,
  65. }
  66. })
  67. def get_left_time(self):
  68. ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
  69. if (ctrInfo is None) or (not ctrInfo.has_key('status')) or (ctrInfo['status'] == Const.DEV_WORK_STATUS_IDLE):
  70. return 0.0, 0.0
  71. needTime = ctrInfo.get('needTime', 0.0)
  72. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  73. {'IMEI': self._device['devNo'], 'funCode': '04', 'data': '0404'})
  74. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  75. if ctrInfo.has_key('startTime') and ctrInfo.has_key('needTime'):
  76. startTime = to_datetime(ctrInfo['startTime'])
  77. needTime = ctrInfo['needTime'] / 60.0
  78. leftTime = needTime - (datetime.datetime.now() - startTime).total_seconds() / 60.0
  79. return leftTime, needTime
  80. if devInfo['rst'] == -1:
  81. description = u'足疗机正在玩命找网络,建议您试试旁边其他设备,或者试试投硬币,或者稍后再试哦'
  82. raise ServiceException({'result': 2, 'description': description})
  83. description = u'足疗机正在忙,无响应,请稍后再试哦'
  84. raise ServiceException({'result': 2, 'description': description})
  85. leftTime = int(devInfo['data'][4:8], 16)
  86. if leftTime == 0.0: # 顺便清理下状态
  87. Device.invalid_device_control_cache(self._device['devNo'])
  88. if needTime < leftTime:
  89. needTime = leftTime
  90. return leftTime/60.0,needTime/60.0