# -*- coding: utf-8 -*- # !/usr/bin/env python import logging from apps.web.constant import Const, DeviceErrorCodeDesc, ErrorCode, DeviceOnlineStatus, DeviceCmdCode, MQTT_TIMEOUT import time import simplejson as json from apps.web.core.adapter.base import OnlineSmartBox from apps.web.core.exceptions import ServiceException from apps.web.core.networking import MessageSender logger = logging.getLogger(__name__) class GprsQuickChargeBox(OnlineSmartBox): def __init__(self, device): super(GprsQuickChargeBox, self).__init__(device) def is_port_can_use(self, port, canAdd = False): return True, u'' def get_port_status(self, force = False): return { '1': {'status': Const.DEV_WORK_STATUS_IDLE}, '2': {'status': Const.DEV_WORK_STATUS_IDLE} } def check_dev_status(self, attachParas = None): if 'chargeIndex' not in attachParas: raise ServiceException( {'result': 2, 'description': u'请扫描带有端口编号的二维码图片'}) if not self.device.need_fetch_online: raise ServiceException( {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL)}) if self.device.online == DeviceOnlineStatus.DEV_STATUS_ONLINE: retry = 3 timeout = 12 else: retry = 2 timeout = 10 dev_info = MessageSender.send(device = self.device, cmd = DeviceCmdCode.GET_DEVINFO, payload = {'IMEI': self._device['devNo'], 'fields': ['power1', 'power2']}, timeout = timeout, retry = retry) if 'rst' not in dev_info: raise ServiceException( {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL)}) if dev_info['rst'] == ErrorCode.DEVICE_SUCCESS: port = int(attachParas['chargeIndex']) if port == 1 and dev_info['power1'] == 'false': raise ServiceException( {'result': 0, 'description': u'1号端口空载,请将电瓶接到指定的端口'}) if port == 2 and dev_info['power2'] == 'false': raise ServiceException( {'result': 0, 'description': u'2号端口空载,请将电瓶接到指定的端口'}) else: raise ServiceException( {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL)}) def test(self, coins): if not self._device.online: raise ServiceException({'result': 2, 'description': u'设备离线'}) result = MessageSender.send(device = self.device, cmd = DeviceCmdCode.GET_DEVINFO, payload = {'IMEI': self._device['devNo'], 'fields': ['power1', 'power2']}) if result['rst'] == 0: if result['power1'] == 'true': return MessageSender.net_pay(self.device, coins, port = 1) elif result['power2'] == 'true': return MessageSender.net_pay(self.device, coins, port = 2) else: raise ServiceException({'result': 0, 'description': u'端口空载'}) return result def start_device(self, package, openId, attachParas): pay_count = int(package['coins']) port = attachParas.get('chargeIndex', None) if attachParas else None if not port: raise ServiceException({'result': 2, 'description': u'参数错误'}) result = MessageSender.net_pay(self.device, pay_count, timeout = MQTT_TIMEOUT.START_DEVICE, port = int(port)) if result['rst'] != 0: logger.debug('gprs quick charge failed to start, result = %s' % (json.dumps(result),)) current_dev_type_name = self.device.devTypeName if current_dev_type_name == u'其他': current_dev_type_name = u'自助设备' if result['rst'] == ErrorCode.BOARD_FAULT: description = u'当前' + current_dev_type_name + u'故障,建议您试试旁边其他设备' elif result['rst'] == ErrorCode.PORT_NO_LOAD: description = u'端口空载,请接负载到指定的端口' elif result['rst'] == ErrorCode.DEVICE_CONN_FAIL: description = u'当前' + current_dev_type_name + u'正在玩命找网络,您的金币还在,重试不需要重新付款再试尝试不会扣费,建议您试试旁边其他设备,或者试试投硬币,或者稍后再试哦' else: description = DeviceErrorCodeDesc.get(result['rst']) raise ServiceException({'result': 2, 'description': description}) try: duration = self.get_duration(package = package) result['finishedTime'] = (int(time.time()) + duration) except Exception as e: logger.exception('error = %s' % e) return result