simaier.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import time
  4. from apps.web.constant import DeviceOnlineStatus, ErrorCode, Const, DeviceCmdCode, DeviceErrorCodeDesc, MQTT_TIMEOUT
  5. from apps.web.core.adapter.base import fill_2_hexByte, SmartBox
  6. from apps.web.core.exceptions import ServiceException
  7. from apps.web.core.networking import MessageSender
  8. from apps.web.device.models import Device
  9. reason_dict = {
  10. 0x00: u'待机可用',
  11. 0x01: u'预约中',
  12. 0x02: u'消毒清洁中',
  13. 0x03: u'洗衣工作中',
  14. 0x04: u'洗衣机洗涤结束',
  15. 0x05: u'洗衣机自检中',
  16. 0x06: u'进水超时,请检查进水阀或者水龙头',
  17. 0x07: u'排水超时,请检查排水系统',
  18. 0x08: u'脱水时撞桶,请调整衣服位置,然后关上门盖',
  19. 0x09: u'脱水开盖,请关好门盖',
  20. 0x0a: u'水位传感器异常,请检查水位传感器',
  21. 0x0b: u'溢水报警,请检查进水阀或洗衣机控制器',
  22. 0x0c: u'电机故障,请检查电机或洗衣机控制器',
  23. 0x0d: u'通讯故障',
  24. 0xfd: u'IMEI未配置,请重启模块',
  25. 0xfe: u'投币金额不足'
  26. }
  27. class SimaierWasher(SmartBox):
  28. def get_port_status_from_dev(self):
  29. pass
  30. def check_dev_status(self, attachParas = None):
  31. if not self.device.need_fetch_online:
  32. raise ServiceException(
  33. {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL)})
  34. if self.device.online == DeviceOnlineStatus.DEV_STATUS_ONLINE:
  35. retry = 3
  36. timeout = 12
  37. else:
  38. retry = 2
  39. timeout = 10
  40. operation_result = self.get_dev_info(retry = retry, timeout = timeout)
  41. if operation_result['rst'] != ErrorCode.DEVICE_SUCCESS:
  42. if operation_result['rst'] == ErrorCode.DEVICE_CONN_FAIL:
  43. raise ServiceException(
  44. {
  45. 'result': 2,
  46. 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL)
  47. })
  48. elif operation_result['rst'] == ErrorCode.BOARD_UART_TIMEOUT:
  49. raise ServiceException(
  50. {
  51. 'result': 2,
  52. 'description': u'设备忙无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'
  53. })
  54. else:
  55. raise ServiceException(
  56. {
  57. 'result': 2,
  58. 'description': u'检查设备状态失败({})'.format(operation_result['rst'])
  59. })
  60. _result = self.analyze_get_data(operation_result['data'])
  61. if _result['comm_result'] != 'SUCCESS':
  62. raise ServiceException(
  63. {'result': 2,
  64. 'description': _result['comm_desc']})
  65. def __init__(self, device):
  66. super(SimaierWasher, self).__init__(device)
  67. # 足疗机的事件报文和get上来的报文一样,直接调用即可
  68. def analyze_event_data(self, data):
  69. return self.analyze_get_data(data)
  70. # 解析get到的报文信息
  71. def analyze_get_data(self, data):
  72. result = {'comm_result': 'SUCCESS', 'comm_desc': 'SUCCESS', 'status': Const.DEV_WORK_STATUS_IDLE,
  73. 'statusInfo': '', 'left_time': 0}
  74. crc_result = int(data[2:4], 16)
  75. if crc_result != 0x06:
  76. result['comm_result'] = 'FAILURE'
  77. result['comm_desc'] = u'CRC校验错,请联系平台'
  78. return result
  79. status = int(data[8:10], 16)
  80. if status in reason_dict:
  81. if status == 0x01:
  82. result['status'] = Const.DEV_WORK_STATUS_APPOINTMENT
  83. result['statusInfo'] = reason_dict[status]
  84. elif status == 0x03:
  85. result['status'] = Const.DEV_WORK_STATUS_WORKING
  86. result['statusInfo'] = reason_dict[status]
  87. elif status in [0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0xfd]:
  88. result['status'] = Const.DEV_WORK_STATUS_FAULT
  89. result['statusInfo'] = reason_dict[status]
  90. elif status in [0x00, 0x04]:
  91. result['status'] = Const.DEV_WORK_STATUS_IDLE
  92. result['statusInfo'] = reason_dict[status]
  93. elif status in [0x02, 0x05]:
  94. result['status'] = Const.DEV_WORK_STATUS_MAINTENANCE
  95. result['statusInfo'] = reason_dict[status]
  96. else:
  97. result['status'] = Const.DEV_WORK_STATUS_FAULT
  98. result['statusInfo'] = u'未知状态'
  99. result['left_time'] = int(data[10:12], 16)
  100. return result
  101. def get_dev_info(self, retry, timeout):
  102. result = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  103. {'IMEI': self._device['devNo'], 'funCode': '01', 'data': '00'}, timeout = timeout,
  104. retry = retry)
  105. if result['rst'] != 0:
  106. return {'rst': -1, 'isOffline': 1}
  107. result.update(self.analyze_get_data(result['data']))
  108. return result
  109. def start_device(self, package, openId, attachParas):
  110. MODE = {
  111. u'加强洗': 0x1,
  112. u'标准洗': 0x2,
  113. u'快速洗': 0x3,
  114. u'单脱水': 0x4,
  115. u'桶消毒': 0x5,
  116. u'洗衣机自检': 0x6,
  117. }
  118. mode = hex(int(MODE[package['name']]))
  119. operation_result = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  120. {'IMEI': self._device['devNo'], 'funCode': '02',
  121. 'data': fill_2_hexByte(mode, 2)}, timeout = MQTT_TIMEOUT.START_DEVICE)
  122. if operation_result['rst'] != ErrorCode.DEVICE_SUCCESS:
  123. if operation_result['rst'] == ErrorCode.DEVICE_CONN_FAIL:
  124. raise ServiceException(
  125. {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_START_CONN_FAIL)})
  126. elif operation_result['rst'] == ErrorCode.BOARD_UART_TIMEOUT:
  127. raise ServiceException(
  128. {'result': 2,
  129. 'description': u'设备忙无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  130. else:
  131. raise ServiceException(
  132. {'result': 2,
  133. 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_START_FAILURE).format(operation_result['rst'])})
  134. _result = self.analyze_get_data(operation_result['data'])
  135. _result['rst'] = ErrorCode.DEVICE_SUCCESS
  136. if _result['comm_result'] != 'SUCCESS':
  137. raise ServiceException(
  138. {'result': 2,
  139. 'description': _result['comm_desc']})
  140. if _result['status'] != Const.DEV_WORK_STATUS_WORKING:
  141. raise ServiceException(
  142. {'result': 2,
  143. 'description': _result['statusInfo']})
  144. if _result['left_time'] == 0:
  145. Device.invalid_device_control_cache(self._device['devNo'])
  146. return _result
  147. left_time = _result['left_time']
  148. start_time = int(time.time())
  149. finished_time = start_time + left_time * 60
  150. Device.update_dev_control_cache(
  151. self._device['devNo'],
  152. {
  153. 'openId': openId,
  154. 'status': Const.DEV_WORK_STATUS_WORKING,
  155. 'startTime': start_time,
  156. 'finishedTime': finished_time
  157. })
  158. return _result
  159. def support_count_down(self, openId = None, port = None):
  160. return False