# -*- coding: utf-8 -*- from apps.web.core.adapter.base import * from apps.web.device.models import Device class PedicureBox3(SmartBox): def __init__(self, device): super(PedicureBox3, self).__init__(device) def start_device(self, package, openId, attachParas): #: 首先检查设备是否在线 unit = package.get('unit',u'分钟') coins = int(package['coins']) if unit == u'分钟': duration = int(package['time'])*60 else: raise ServiceException({'result': 2, 'description': u'经销商配置的套餐信息错误,单位只能配置为分钟,请您重试'}) hexDuration = fill_2_hexByte(hex(duration), 4) devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC, payload = {'IMEI': self._device['devNo'], "funCode": '01', 'data': hexDuration}, timeout = MQTT_TIMEOUT.START_DEVICE) 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'足疗机正在忙,无响应,您的金币还在,重试不需要重新付款,请试试其他线路,或者请稍后再试哦'}) start_timestamp = int(time.time()) devInfo['finishedTime'] = start_timestamp + int(duration) Device.update_dev_control_cache(self._device['devNo'], { 'status': Const.DEV_WORK_STATUS_WORKING, 'finishedTime': devInfo['finishedTime'], 'openId': openId, 'startTime': timestamp_to_dt(start_timestamp).strftime('%Y-%m-%d %H:%M:%S'), 'needTime': duration, 'coins': coins, 'vCardId': self._vcard_id }) return devInfo def stop(self,port=None): devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '03', 'data': '0303'}) if devInfo.has_key('rst') and devInfo['rst'] != 0: return JsonResponse({"result": 0, "description": u'网络连接异常,停止设备失败,请您重新尝试停掉设备', "payload": ''}) devInfo['remainder_time'] = 0.0 return devInfo def support_count_down(self,openId=None,port=None): return True def count_down(self,request,dev,agent,group,devType,lastOpenId,port=None): surplus, sumtime = self.get_left_time() orderProcessing = False if surplus == 0.0 and sumtime == 0.0: orderProcessing = True return JsonResponse( { 'result': 1, 'description': '', 'payload': { 'surplus': surplus, 'sum': sumtime, 'name': group['groupName'], 'address': group['address'], 'code': devType.get('code'), 'orderProcessing': orderProcessing, 'logicalCode': dev['logicalCode'], 'user': 'me' if lastOpenId == request.user.openId else 'notme', 'agentFeatures': agent.features, } }) def get_left_time(self): ctrInfo = Device.get_dev_control_cache(self._device['devNo']) if (ctrInfo is None) or (not ctrInfo.has_key('status')) or (ctrInfo['status'] == Const.DEV_WORK_STATUS_IDLE): return 0.0, 0.0 needTime = ctrInfo.get('needTime', 0.0) devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], 'funCode': '04', 'data': '0404'}) if devInfo.has_key('rst') and devInfo['rst'] != 0: if ctrInfo.has_key('startTime') and ctrInfo.has_key('needTime'): startTime = to_datetime(ctrInfo['startTime']) needTime = ctrInfo['needTime'] / 60.0 leftTime = needTime - (datetime.datetime.now() - startTime).total_seconds() / 60.0 return leftTime, needTime if devInfo['rst'] == -1: description = u'足疗机正在玩命找网络,建议您试试旁边其他设备,或者试试投硬币,或者稍后再试哦' raise ServiceException({'result': 2, 'description': description}) description = u'足疗机正在忙,无响应,请稍后再试哦' raise ServiceException({'result': 2, 'description': description}) leftTime = int(devInfo['data'][4:8], 16) if leftTime == 0.0: # 顺便清理下状态 Device.invalid_device_control_cache(self._device['devNo']) if needTime < leftTime: needTime = leftTime return leftTime/60.0,needTime/60.0