# -*- coding: utf-8 -*- # !/usr/bin/env python from apps.web.core.adapter.base import * from apps.web.device.models import Device, StockRecord class TissueBox(SmartBox): def __init__(self, device): super(TissueBox, self).__init__(device) def analyze_event_data(self,data): if data['faultCode'] == 1: return {'status':Const.DEV_WORK_STATUS_FAULT,'statusInfo':u'缺纸告警'} elif data['faultCode'] ==Const.DEV_WORK_STATUS_FAULT: return {'status':Const.DEV_WORK_STATUS_FAULT,'statusInfo':u'电机故障'} return {'status':Const.DEV_WORK_STATUS_IDLE,'statusInfo':''} def check_dev_status(self,attachParas=None): 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 devInfo = MessageSender.send(self.device, DeviceCmdCode.GET_DEVINFO, {'IMEI': self._device['devNo']}, timeout = timeout, retry = retry) if devInfo.has_key('rst') and devInfo['rst'] != 0: if devInfo['rst'] == -1: raise ServiceException( {'result': 2, 'description': u'当前纸巾机正在玩命找网络,请您稍候再试'}) elif devInfo['rst'] == 1: raise ServiceException( {'result': 2, 'description': u'当前纸巾机忙,无响应,请您稍候再试'}) if devInfo.get('isTissueEmpty', False): Device.update_field( dev_no = self._device['devNo'], update = True, quantity = 0) raise ServiceException( {'result': 2, 'description': u'您来迟了一步,当前纸巾机已经没有纸巾了,建议您试试周围其他机子哦'}) #请求设备出纸巾,count表示出的数目 def sendOutPaper(self,count): result = MessageSender.send(self.device, DeviceCmdCode.PAY_MONEY, {'app_pay': int(count), 'IMEI': self._device['devNo']}, timeout = MQTT_TIMEOUT.START_DEVICE) if result.has_key('rst') and result['rst'] != 0: if result['rst'] == -1: description = u'当前纸巾机开小差了,可能是网络信号差,您稍候试试,或者找找周边的其他纸巾机' raise ServiceException({'result': 2, 'description': description}) elif result['rst'] == 1: description = u'当前纸巾机繁忙无响应,您稍后再试' raise ServiceException({'result': 2, 'description': description}) return {'rst':0,'IMEI':self._device['devNo'],'out':count} #免费领纸巾 def start_free(self, token, count = 1): result = self.sendOutPaper(count) #更新设备的库存信息 self._device['consumptionQuantity'] += result['out'] self._device['quantity'] -= result['out'] if self._device['quantity'] <= 0: self._device['quantity'] = 0 Device.update_field(dev_no = self._device['devNo'], update = True, consumptionQuantity = self._device['consumptionQuantity'], quantity = self._device['quantity']) # 用户成功消耗纸巾,需要把库存记录到数据库中 payload = { 'token': token, 'logicCode': self._device['logicalCode'], 'imei': self._device['devNo'], 'stockType':'adFree', 'stockTime': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'number':count, 'more':u'免费送' } StockRecord(**payload).save() return result def start_device(self, package, openId, attachParas): #: 首先检查设备是否在线,是否有库存 coins = int(package['coins']) devInfo = self.sendOutPaper(coins) # 更新设备的库存信息 self._device['consumptionQuantity'] += devInfo['out'] self._device['quantity'] -= devInfo['out'] if self._device['quantity'] <= 0: self._device['quantity'] = 0 Device.update_field( dev_no = self._device['devNo'], update = True, consumptionQuantity = self._device['consumptionQuantity'], quantity = self._device['quantity']) #用户成功消耗纸巾,需要把库存记录到数据库中 try: stockTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') StockRecord.get_collection().insert({'logicCode':self._device['logicalCode'],'imei':self._device['devNo'], 'stockType':'consume','stockTime':stockTime,'number':devInfo['out']}) except Exception ,e: logger.info('record tissue device=%s consume record exception=%s' % (self._device['devNo'],e)) Device.update_dev_control_cache(self._device['devNo'], {'openId':openId,'vCardId':self._vcard_id}) return devInfo def get_dev_setting(self): devInfo = MessageSender.send(self.device, DeviceCmdCode.GET_DEVINFO, {'IMEI': self._device['devNo']}) if devInfo.has_key('rst') and devInfo['rst'] != 0: if devInfo['rst'] == -1: raise ServiceException( {'result': 2, 'description': u'当前纸巾机正在玩命找网络,请您稍候再试'}) elif devInfo['rst'] == 1: raise ServiceException( {'result': 2, 'description': u'当前纸巾机忙,无响应,请您稍候再试'}) return {'stop_delay': devInfo['stop_delay'], 'tissue_timeout': devInfo['tissue_timeout']} def set_dev_setting(self, stop_delay, tissue_timeout): result = MessageSender.send(self.device, 202, { "cmd": 202, "IMEI": self._device['devNo'], "motor_set": { "stop_delay": stop_delay, "tissue_timeout": tissue_timeout } }) if result['rst'] == -1: raise ServiceException( {'result': 2, 'description': u'当前纸巾机正在玩命找网络,请您稍候再试'}) elif result['rst'] == 1: raise ServiceException( {'result': 2, 'description': u'当前纸巾机忙,无响应,请您稍候再试'}) return def set_device_function_param(self,request,lastSetConf): setting = { 'stop_delay': request.POST.get('stop_delay'), 'tissue_timeout': request.POST.get('tissue_timeout') } from apps.web.device.validation import DeviceSettingSchema setting = DeviceSettingSchema.TISSUE_BOX(setting) self.set_dev_setting(**setting)