wawaji_maichong.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. import logging
  4. import simplejson as json
  5. from apps.web.constant import DeviceOnlineStatus, DeviceCmdCode, ErrorCode, DeviceErrorCodeDesc, FAULT_CODE
  6. from apps.web.core.adapter.base import SmartBox
  7. from apps.web.core.exceptions import ServiceException, DeviceNetworkTimeoutError
  8. from apps.web.core.networking import MessageSender
  9. logger = logging.getLogger(__name__)
  10. class WawajiMaichongBox(SmartBox):
  11. def __init__(self, device):
  12. super(WawajiMaichongBox, self).__init__(device)
  13. def analyze_event_data(self, data):
  14. if data.has_key('messageType') and data['messageType'] == 'PUT_ITEM':
  15. return {'funCode':'10'}
  16. return None
  17. def check_dev_status(self, attachParas = None):
  18. """
  19. 如果超过两个心跳周期没有报心跳,并且最后一次更新时间在2个小时内,需要从设备获取状态
  20. 否则以缓存状态为准。
  21. :param attachParas:
  22. :return:
  23. """
  24. if self.device.online == DeviceOnlineStatus.DEV_STATUS_ONLINE:
  25. retry = 3
  26. timeout = 35
  27. interval = 10
  28. else:
  29. retry = 2
  30. timeout = 17
  31. interval = 6
  32. operation_result = MessageSender.send(device = self.device,
  33. cmd = DeviceCmdCode.GET_DEVINFO,
  34. payload = {
  35. 'IMEI': self.device.devNo,
  36. 'fields': ['signal', 'pulse_open', 'board_volt', 'board_valid']
  37. },
  38. timeout = timeout, retry = retry)
  39. if operation_result['rst'] != ErrorCode.DEVICE_SUCCESS:
  40. if operation_result['rst'] == ErrorCode.DEVICE_CONN_FAIL:
  41. raise ServiceException(
  42. {
  43. 'result': 2,
  44. 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL)
  45. })
  46. else:
  47. raise ServiceException(
  48. {
  49. 'result': 2,
  50. 'description': u'检测设备状态失败({})'.format(operation_result['rst'])
  51. })
  52. else:
  53. if 'pulse_open' in operation_result and (not operation_result['pulse_open']):
  54. raise ServiceException(
  55. {
  56. 'result': 2,
  57. 'description': u'检测设备状态失败({})'.format(ErrorCode.PULSE_IS_CLOSE)
  58. })
  59. if 'board_valid' in operation_result and 'board_volt' in operation_result and operation_result[
  60. 'board_valid'] != 2:
  61. if operation_result['board_volt'] != operation_result['board_valid']:
  62. raise ServiceException(
  63. {
  64. 'result': 2,
  65. 'description': u'当前设备正在工作,请稍后再试'
  66. })
  67. def test(self, coins):
  68. return MessageSender.net_pay(self.device, 1, timeout = 120)
  69. def start_device(self, package, openId, attachParas):
  70. pay_count = int(package['coins'])
  71. result = MessageSender.net_pay(self.device, pay_count, timeout = 120)
  72. if result['rst'] == ErrorCode.DEVICE_CONN_FAIL:
  73. raise DeviceNetworkTimeoutError()
  74. elif result['rst'] != ErrorCode.DEVICE_SUCCESS:
  75. logger.debug('WawajiMaichongBox() failed to start, result was=%s' % (json.dumps(result),))
  76. raise ServiceException({'result': 2, 'description': DeviceErrorCodeDesc.get(result['rst'])})
  77. return result
  78. def get_total_coin(self):
  79. result = MessageSender.send(self.device, DeviceCmdCode.GET_DEVINFO,
  80. {'cmd': DeviceCmdCode.GET_DEVINFO, 'IMEI': self._device['devNo']})
  81. if result['rst'] != ErrorCode.DEVICE_SUCCESS:
  82. logger.debug('WawajiMaichongBox() failed to get total coin, result was=%s' % (json.dumps(result),))
  83. description = u'当前设备信号弱没有响应,请您稍后重试。'
  84. raise ServiceException({'result': 2, 'description': description})
  85. if not result.has_key('total_coin'):
  86. raise ServiceException({'result': 2, 'description': u'当前设备暂时不支持获取总的硬币数目,待版本自动升级后,会支持'})
  87. return result['total_coin']
  88. # 基类函数,检查告警状态,只能做一个简单的检查,设备是否在线
  89. def check_alarm(self, alarm):
  90. if alarm.faultCode == FAULT_CODE.OFFLINE:
  91. dev_info = MessageSender.send(device = self.device,
  92. cmd = DeviceCmdCode.GET_DEVINFO,
  93. payload = {'IMEI': self.device.devNo, 'fields': []},
  94. timeout = 15)
  95. if dev_info['rst'] == 0:
  96. return u'设备状态检查在线,网络通畅,网络可能出现闪断'
  97. else:
  98. raise ServiceException({'result': 2, 'description': u'设备玩命也无法找到网络,设备可能不在线'})
  99. else:
  100. return u'无法检查该设备的告警状态,建议您用其他方式确认此告警是否正常'