# -*- coding: utf-8 -*- #!/usr/bin/env python import logging import simplejson as json from apps.web.constant import DeviceOnlineStatus, DeviceCmdCode, ErrorCode, DeviceErrorCodeDesc, FAULT_CODE from apps.web.core.adapter.base import SmartBox from apps.web.core.exceptions import ServiceException, DeviceNetworkTimeoutError from apps.web.core.networking import MessageSender logger = logging.getLogger(__name__) class WawajiMaichongBox(SmartBox): def __init__(self, device): super(WawajiMaichongBox, self).__init__(device) def analyze_event_data(self, data): if data.has_key('messageType') and data['messageType'] == 'PUT_ITEM': return {'funCode':'10'} return None def check_dev_status(self, attachParas = None): """ 如果超过两个心跳周期没有报心跳,并且最后一次更新时间在2个小时内,需要从设备获取状态 否则以缓存状态为准。 :param attachParas: :return: """ if self.device.online == DeviceOnlineStatus.DEV_STATUS_ONLINE: retry = 3 timeout = 35 interval = 10 else: retry = 2 timeout = 17 interval = 6 operation_result = MessageSender.send(device = self.device, cmd = DeviceCmdCode.GET_DEVINFO, payload = { 'IMEI': self.device.devNo, 'fields': ['signal', 'pulse_open', 'board_volt', 'board_valid'] }, timeout = timeout, retry = retry) if operation_result['rst'] != ErrorCode.DEVICE_SUCCESS: if operation_result['rst'] == ErrorCode.DEVICE_CONN_FAIL: raise ServiceException( { 'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL) }) else: raise ServiceException( { 'result': 2, 'description': u'检测设备状态失败({})'.format(operation_result['rst']) }) else: if 'pulse_open' in operation_result and (not operation_result['pulse_open']): raise ServiceException( { 'result': 2, 'description': u'检测设备状态失败({})'.format(ErrorCode.PULSE_IS_CLOSE) }) if 'board_valid' in operation_result and 'board_volt' in operation_result and operation_result[ 'board_valid'] != 2: if operation_result['board_volt'] != operation_result['board_valid']: raise ServiceException( { 'result': 2, 'description': u'当前设备正在工作,请稍后再试' }) def test(self, coins): return MessageSender.net_pay(self.device, 1, timeout = 120) def start_device(self, package, openId, attachParas): pay_count = int(package['coins']) result = MessageSender.net_pay(self.device, pay_count, timeout = 120) if result['rst'] == ErrorCode.DEVICE_CONN_FAIL: raise DeviceNetworkTimeoutError() elif result['rst'] != ErrorCode.DEVICE_SUCCESS: logger.debug('WawajiMaichongBox() failed to start, result was=%s' % (json.dumps(result),)) raise ServiceException({'result': 2, 'description': DeviceErrorCodeDesc.get(result['rst'])}) return result def get_total_coin(self): result = MessageSender.send(self.device, DeviceCmdCode.GET_DEVINFO, {'cmd': DeviceCmdCode.GET_DEVINFO, 'IMEI': self._device['devNo']}) if result['rst'] != ErrorCode.DEVICE_SUCCESS: logger.debug('WawajiMaichongBox() failed to get total coin, result was=%s' % (json.dumps(result),)) description = u'当前设备信号弱没有响应,请您稍后重试。' raise ServiceException({'result': 2, 'description': description}) if not result.has_key('total_coin'): raise ServiceException({'result': 2, 'description': u'当前设备暂时不支持获取总的硬币数目,待版本自动升级后,会支持'}) return result['total_coin'] # 基类函数,检查告警状态,只能做一个简单的检查,设备是否在线 def check_alarm(self, alarm): if alarm.faultCode == FAULT_CODE.OFFLINE: dev_info = MessageSender.send(device = self.device, cmd = DeviceCmdCode.GET_DEVINFO, payload = {'IMEI': self.device.devNo, 'fields': []}, timeout = 15) if dev_info['rst'] == 0: return u'设备状态检查在线,网络通畅,网络可能出现闪断' else: raise ServiceException({'result': 2, 'description': u'设备玩命也无法找到网络,设备可能不在线'}) else: return u'无法检查该设备的告警状态,建议您用其他方式确认此告警是否正常'