zuliao1.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import time
  4. import uuid
  5. from apilib.utils_json import JsonResponse
  6. from apps.web.constant import Const, DeviceCmdCode, ErrorCode, DeviceErrorCodeDesc
  7. from apps.web.core.adapter.base import hexbyte_2_bin, SmartBox
  8. from apps.web.core.exceptions import ServiceException
  9. from apps.web.core.networking import MessageSender
  10. from apps.web.device.models import Device
  11. class PedicureBox(SmartBox):
  12. def __init__(self, device):
  13. super(PedicureBox, self).__init__(device)
  14. # 串口设备的状态检测,需要单独进行,和非串口的不一样
  15. def check_dev_status(self, attachParas = None):
  16. devInfo = self.get_dev_info()
  17. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  18. if devInfo['rst'] == ErrorCode.DEVICE_CONN_FAIL:
  19. raise ServiceException(
  20. {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL)})
  21. elif devInfo['rst'] == ErrorCode.BOARD_UART_TIMEOUT:
  22. raise ServiceException(
  23. {'result': 2, 'description': u'当前足疗机忙,无响应,请您稍候再试'})
  24. if devInfo['isStart']:
  25. Device.update_dev_control_cache(
  26. self._device['devNo'],
  27. {
  28. 'status': Const.DEV_WORK_STATUS_WORKING,
  29. 'finishedTime': (int(time.time()) + 60)
  30. })
  31. raise ServiceException({'result': 2, 'description': u'足疗机正在为您服务,结束后,才可以充值哦'})
  32. else:
  33. Device.update_dev_control_cache(self._device['devNo'], {'status': Const.DEV_WORK_STATUS_IDLE})
  34. if devInfo['status'] == Const.DEV_WORK_STATUS_FAULT:
  35. raise ServiceException({'result': 2, 'description': u'足疗机出现故障:%s,您找下管理员吧' % devInfo['statusInfo']})
  36. # 足疗机的事件报文和get上来的报文一样,直接调用即可
  37. def analyze_event_data(self, data):
  38. return self.analyze_get_data(data)
  39. # 解析get到的报文信息
  40. def analyze_get_data(self, data):
  41. result = {}
  42. if 'F2F2F008' not in data:
  43. return result
  44. status = Const.DEV_WORK_STATUS_WORKING
  45. fault = ''
  46. # 解析返回的报文,报文格式F2F2...
  47. resultData = data[8:24]
  48. if resultData[0:2] == '00':
  49. isStart = False
  50. else:
  51. isStart = True
  52. hTime = resultData[2:4]
  53. mTime = resultData[4:6]
  54. sTime = resultData[6:8]
  55. leftTime = int(hTime, 16) * 3600 + int(mTime, 16) * 60 + int(sTime, 16)
  56. totalTime = int(resultData[8:12], 16) * 3600 + int(resultData[12:13], 16)
  57. hexFault = resultData[14:16]
  58. binFault = hexbyte_2_bin(hexFault)
  59. if binFault[0] == '1':
  60. status = Const.DEV_WORK_STATUS_FAULT
  61. fault = u'堵转故障'
  62. if binFault[1] == '1':
  63. fault = u'按摩脚霍尔传感器不归位'
  64. if binFault[2] == '1':
  65. fault = u'付款没有开机故障'
  66. if leftTime == 0 and fault == '':
  67. status = Const.DEV_WORK_STATUS_IDLE
  68. return {'status': status, 'fault': fault, 'isStart': isStart, 'totalTime': totalTime,
  69. 'leftTime': leftTime}
  70. def get_dev_info(self):
  71. result = MessageSender.send(self.device, DeviceCmdCode.PASSTHROUGH_OPERATE_DEV_SYNC,
  72. {'IMEI': self._device['devNo'], 'funCode': 'F0', 'data': 'F2F2F00100'})
  73. if result['rst'] != 0:
  74. return {'rst': -1, 'isOffline': 1}
  75. result.update(self.analyze_get_data(result['data']))
  76. return result
  77. def send_time(self, time):
  78. oxTime = hex(time)[2::]
  79. oxTime = '0000' + oxTime
  80. oxTime = oxTime[-4::]
  81. uuId = str(uuid.uuid4()).replace('-', '')[0:16]
  82. data = uuId + oxTime
  83. result = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  84. {'IMEI': self._device['devNo'], 'funCode': '05', 'data': data})
  85. if 'rst' in result and result['rst'] != 0:
  86. current_dev_type_name = self._device['devType']['name']
  87. if current_dev_type_name == u'其他':
  88. current_dev_type_name = u'自助设备'
  89. description = u'当前' + current_dev_type_name + u'充值失败,可能是当前网络状况差,请稍候再试'
  90. raise ServiceException({'result': 2, 'description': description})
  91. status = result['data'][24:26]
  92. if status == '00':
  93. raise ServiceException({'result': 2, 'description': u'设备启动异常,请联系客服'})
  94. hour = int(result['data'][26:28], 16)
  95. minute = int(result['data'][28:30], 16)
  96. second = int(result['data'][30:32], 16)
  97. left_time = 3600 * hour + 60 * minute + second
  98. return result, left_time
  99. def start_device(self, package, openId, attachParas):
  100. unit = package.get('unit', u'分钟')
  101. if unit == u'分钟':
  102. duration = int(package['time'])
  103. elif unit == u'小时':
  104. duration = int(package['time']) * 60
  105. elif unit == u'天':
  106. duration = int(package['time']) * 1440
  107. else:
  108. duration = int(package['time'])
  109. dev_info, left_time = self.send_time(duration)
  110. if left_time == 0:
  111. Device.update_dev_control_cache(self._device['devNo'], {'status': Const.DEV_WORK_STATUS_IDLE})
  112. return dev_info
  113. need_time = duration * 60
  114. start_time = int(time.time())
  115. service_cache = Device.get_dev_control_cache(self._device['devNo'])
  116. if 'finishedTime' not in service_cache or service_cache['finishedTime'] > int(time.time()):
  117. if 'startTime' in service_cache:
  118. start_time = service_cache['startTime']
  119. if 'needTime' in service_cache:
  120. need_time = need_time + int(service_cache['needTime'])
  121. finished_time = start_time + need_time
  122. Device.update_dev_control_cache(
  123. self._device['devNo'],
  124. {
  125. 'openId': openId,
  126. 'status': Const.DEV_WORK_STATUS_WORKING,
  127. 'startTime': start_time,
  128. 'finishedTime': finished_time,
  129. 'leftTime': left_time
  130. })
  131. return dev_info
  132. def support_count_down(self, openId = None, port = None):
  133. return True
  134. def count_down(self, request, dev, agent, group, devType, lastOpenId, port = None):
  135. dev_info = self.get_dev_info()
  136. if dev_info['rst'] != 0:
  137. return JsonResponse({'result': 0, 'description': u'足疗机网络不太好,再试试哦'})
  138. result = {
  139. 'result': 1,
  140. 'description': '',
  141. 'payload': {
  142. 'surplus': 0.0,
  143. 'sum': 0.0,
  144. 'name': group['groupName'],
  145. 'address': group['address'],
  146. 'code': devType.get('code'),
  147. 'orderProcessing': False,
  148. 'logicalCode': dev['logicalCode'],
  149. 'user': 'me' if lastOpenId == request.user.openId else 'notme',
  150. 'agentFeatures': agent.features,
  151. }
  152. }
  153. if dev_info['leftTime'] == 0:
  154. Device.invalid_device_control_cache(self._device['devNo'])
  155. return JsonResponse(result)
  156. service_cache = Device.get_dev_control_cache(dev['devNo'])
  157. left_time = dev_info['leftTime']
  158. need_time = left_time
  159. if 'startTime' in service_cache and 'finishedTime' in service_cache:
  160. need_time = service_cache['finishedTime'] - service_cache['startTime']
  161. result.update({
  162. 'surplus': left_time / 60.0,
  163. 'sum': need_time / 60.0,
  164. })
  165. return result