# -*- coding: utf-8 -*- # !/usr/bin/env python import time from apilib.utils_json import JsonResponse from apps.web.constant import Const, DeviceCmdCode, MQTT_TIMEOUT from apps.web.core.adapter.base import SmartBox, fill_2_hexByte from apps.web.core.exceptions import ServiceException from apps.web.core.networking import MessageSender from apps.web.device.models import Device class PedicureHongPaiBox(SmartBox): def __init__(self, device): super(PedicureHongPaiBox, self).__init__(device) def analyze_event_data(self, data): pass def support_count_down(self, openId = None, port = None): return True def send_dev_runtime(self, openId, duration): hexTime = fill_2_hexByte(hex(int(duration / 60)), 2) devInfo = MessageSender.send(self.device, self.make_random_cmdcode(), {'IMEI': self._device['devNo'], "funCode": '05', 'data': hexTime}) 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['data'][4:6] == '00': raise ServiceException({'result': 2, 'description': u'足疗机启动失败,金币还在,请您稍后重试'}) Device.update_dev_control_cache( self._device['devNo'], { 'needTime': duration, 'status': Const.DEV_WORK_STATUS_WORKING, 'finishedTime': int(time.time()) + duration }) return devInfo def start_from_api(self, record): return super(PedicureHongPaiBox, self).start_from_api(record) def start_device(self, package, openId, attachParas): # 微信支付 # 先检查足疗机是否正在运行,如果正在运行,就返回错误 devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.GET_DEVINFO, payload = { 'IMEI': self._device['devNo'] }, 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'足疗机正在忙,无响应,请稍后再试哦'}) if not devInfo.has_key('hongpai_status'): raise ServiceException({'result': 2, 'description': u'当前足疗机没有返回正确的结果,可能是内部接触不良,请您重试,或者联系客服解决'}) if devInfo['hongpai_status'][4:6] == '01': raise ServiceException({'result': 2, 'description': u'当前足疗机正在工作中,不能操作哦,等设备空闲后,您再试试吧'}) needTime = int(package['time']) hexTime = fill_2_hexByte(hex(needTime), 2) devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_NO_RESPONSE, {'IMEI': self._device['devNo'], "funCode": '05', 'data': hexTime}) 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'足疗机正在忙,无响应,您的金币还在,请试试其他线路,或者请稍后再试哦'}) Device.update_dev_control_cache( self._device['devNo'], { 'needTime': needTime * 60, 'status': Const.DEV_WORK_STATUS_WORKING, 'finishedTime': int(time.time()) + needTime * 60 }) return devInfo def test(self, coins): return MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_NO_RESPONSE, {'IMEI': self._device['devNo'], "funCode": '05', 'data': '02'}) def get_left_time(self): serviceInfo = Device.get_dev_control_cache(self._device['devNo']) if (serviceInfo is None) or (not serviceInfo.has_key('status')) or ( serviceInfo['status'] == Const.DEV_WORK_STATUS_IDLE): return 0.0, 0.0 nowTime = int(time.time()) leftTime = (serviceInfo['finishedTime'] - nowTime) / 60.0 needTime = serviceInfo['needTime'] / 60.0 if leftTime <= 0: return needTime, 0.0 return leftTime, needTime 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_dev_setting(self): devObj = Device.objects.get(devNo = self._device['devNo']) return {'freeHours': devObj.otherConf.get('freeHours', 10)} def set_device_function_param(self, request, lastSetConf): if request.POST.has_key('freeHours'): devObj = Device.objects.get(devNo = self._device['devNo']) devObj.otherConf['freeHours'] = int(request.POST.get('freeHours', 10)) devObj.save()