# -*- coding: utf-8 -*- #!/usr/bin/env python import time import logging import datetime from apilib.utils_datetime import timestamp_to_dt 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.dealer.models import Dealer from apps.web.device.models import Device, Group from apps.web.core.device_define.water_despenser import CMD_CODE from apps.web.helpers import get_wechat_manager_mp_proxy from apps.web.user.models import MyUser logger = logging.getLogger(__name__) class WaterDispenserBox(SmartBox): def __init__(self, device): super(WaterDispenserBox, self).__init__(device) def analyze_event_data(self, data): cmdCode = data[12:14] if cmdCode == CMD_CODE.WATER_DISPENSE_ALERT_EVENT_CODE: return { 'cmdCode': cmdCode, 'isError': False if data[14:16] == '00' else True, 'tempreature': int(data[16:18], 16), 'noWater': False if data[18:20] == '00' else True, 'moreWater': False if data[20:22] == '00' else True, 'port1Leak': False if data[22:24] == '00' else True, 'port2Leak': False if data[24:26] == '00' else True, 'port1Error': False if data[26:28] == '00' else True, 'port2Error': False if data[28:30] == '00' else True, 'eventKey': data[30:32], 'deviceId': data[32:36] } elif cmdCode == CMD_CODE.WATER_DISPENSE_SETTING_EVENT_CODE: return { 'cmdCode': cmdCode, 'boardSoftVer': data[24:28], } elif cmdCode == CMD_CODE.WATER_DISPENSE_WORK_STATUS_EVENT_CODE: return { 'cmdCode': cmdCode, 'port': str(int(data[14:16], 16)), 'consumeMode': data[16:18], 'isPause': data[18:20] } elif cmdCode == CMD_CODE.WATER_DISPENSE_FINISH_EVENT_CODE: port = str(int(data[14:16], 16)) consumeMode = str(int(data[16:18], 16)) reason = data[18:20] duration = int(data[38:42], 16) spendLitre = int(data[44:48], 16) if reason == '01': desc = u'客户停止加水' elif reason == '02': desc = u'管理员停止加水' elif reason == '03': desc = u'管理员取消' elif reason == '04': desc = u'按键终止加水' elif reason == '05': desc = u'超时终止加水' elif reason == '06': desc = u'设备故障终止加水' elif reason == '07': desc = u'加水完成' elif reason == '08': desc = u'违规使用停止加水' elif reason == '09': desc = u'套餐不符停止加水' else: desc = u'空' return {'port': port, 'cmdCode': cmdCode, 'consumeMode': consumeMode, 'reason': reason, 'desc': desc, 'duration': duration, 'spendLitre': spendLitre} def init_dev_settings(self, boardSoftVer): deviceId = self.get_device_id() device = Device.objects(devNo=self._device['devNo']).first() workMode = device.otherConf.get('workMode', '1') maxDuration = fill_2_hexByte(hex(int(device.otherConf.get('maxDuration', 60))), 2) minConsumeRatio = device.otherConf.get('minConsumeRatio', 100) pauseTime = fill_2_hexByte(hex(int(device.otherConf.get('pauseTime', 60))), 2) flowTimes = fill_2_hexByte(hex(int(device.otherConf.get('flowTimes', 1))), 4) adStartTime = device.otherConf.get('adStartTime', '19:00') nightVolume = fill_2_hexByte(hex(int(device.otherConf.get('nightVolume', 3))), 2) adEndTime = device.otherConf.get('adEndTime', '7:00') dayVolume = fill_2_hexByte(hex(int(device.otherConf.get('dayVolume', 7))), 2) adStartHours = fill_2_hexByte(hex(int(adStartTime.split(':')[0])), 2) adStartMinutes = fill_2_hexByte(hex(int(adStartTime.split(':')[1])), 2) adEndHours = fill_2_hexByte(hex(int(adEndTime.split(':')[0])), 2) adEndMinutes = fill_2_hexByte(hex(int(adEndTime.split(':')[1])), 2) firstStartCode = '00' ctrlInfo = Device.get_dev_control_cache(self._device['devNo']) if ctrlInfo.get('1', None) is not None and ctrlInfo.get('2', None) is not None: port1Error = u'电磁阀故障' in ctrlInfo['1'].get('statusErrorInfo', '') port2Error = u'电磁阀故障' in ctrlInfo['2'].get('statusErrorInfo', '') if port1Error: firstStartCode = '01' if port2Error: firstStartCode = '02' if port1Error and port2Error: firstStartCode = '03' data = '02' + maxDuration + pauseTime + flowTimes + boardSoftVer + '00' + firstStartCode + deviceId + adStartHours + adStartMinutes + nightVolume + adEndHours + adEndMinutes + dayVolume MessageSender.send( self.device, DeviceCmdCode.OPERATE_DEV_NO_RESPONSE, {'IMEI': self._device['devNo'], "funCode": "06", 'data': data}, timeout=MQTT_TIMEOUT.CHECK_DEVICE_STATUS ) tempAdStartHours = str(int(adStartHours, 16)) tempAdStartMinutes = str(int(adStartMinutes, 16)) tempAdEndHours = str(int(adEndHours, 16)) tempAdEndMinutes = str(int(adEndMinutes, 16)) adStartTime = '{}{}:{}{}'.format('0' if len(tempAdStartHours) == 1 else '', tempAdStartHours, '0' if len(tempAdStartMinutes) == 1 else '', tempAdStartMinutes) adEndTime = '{}{}:{}{}'.format('0' if len(tempAdEndHours) == 1 else '', tempAdEndHours, '0' if len(tempAdEndMinutes) == 1 else '', tempAdEndMinutes) device.otherConf.update({ 'workMode': workMode, 'maxDuration': int(maxDuration, 16), 'minConsumeRatio': minConsumeRatio, 'pauseTime': int(pauseTime, 16), 'flowTimes': int(flowTimes, 16), 'adStartTime': adStartTime, 'nightVolume': int(nightVolume, 16), 'adEndTime': adEndTime, 'dayVolume': int(dayVolume, 16), 'boardSoftVer': boardSoftVer, 'clearFault': 0 }) device.save() Device.invalid_device_cache(device.devNo) def get_device_id(self): devNo = self._device['devNo'] deviceId = Device.get_dev_control_cache(devNo).get('deviceId', '') if deviceId == '': raise ServiceException({'result': 2, 'description': u'未获取到设备ID,设备故障,请联系经销商'}) return deviceId def get_port_status_from_dev(self): # 获取端口状态, 播放语音 deviceId = self.get_device_id() # TODO 目前只有一种语音类型,后续业务完成后,增加警告以及拒绝的语音类型。 voiceType = '01' orderNo = '000000000000' data = '00' + voiceType + '00' + orderNo + deviceId + '000000' + '000000' devInfo = MessageSender.send( self.device, self.make_random_cmdcode(), {'IMEI': self._device['devNo'], "funCode": "04", 'data': data}, timeout=MQTT_TIMEOUT.CHECK_DEVICE_STATUS ) 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'当前设备忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'}) portStatus = devInfo['data'][18:20] if portStatus == '00': result = {'1':{'status': Const.DEV_WORK_STATUS_IDLE}, '2':{'status': Const.DEV_WORK_STATUS_IDLE}} elif portStatus == '01': result = {'1': {'status': Const.DEV_WORK_STATUS_IDLE}, '2': {'status': Const.DEV_WORK_STATUS_WORKING}} elif portStatus == '02': result = {'1': {'status': Const.DEV_WORK_STATUS_IDLE}, '2': {'status': Const.DEV_WORK_STATUS_FAULT}} elif portStatus == '03': result = {'1': {'status': Const.DEV_WORK_STATUS_WORKING}, '2': {'status': Const.DEV_WORK_STATUS_IDLE}} elif portStatus == '04': result = {'1': {'status': Const.DEV_WORK_STATUS_FAULT}, '2': {'status': Const.DEV_WORK_STATUS_IDLE}} elif portStatus == '05': result = {'1': {'status': Const.DEV_WORK_STATUS_WORKING}, '2': {'status': Const.DEV_WORK_STATUS_WORKING}} elif portStatus == '07': result = {'1': {'status': Const.DEV_WORK_STATUS_FAULT}, '2': {'status': Const.DEV_WORK_STATUS_WORKING}} elif portStatus == '08': result = {'1': {'status': Const.DEV_WORK_STATUS_WORKING}, '2': {'status': Const.DEV_WORK_STATUS_FAULT}} else: result = {'1': {'status': Const.DEV_WORK_STATUS_FAULT}, '2': {'status': Const.DEV_WORK_STATUS_FAULT}} allPorts, usedPorts, usePorts = self.get_port_static_info(result) Device.update_dev_control_cache(self._device['devNo'], {'allPorts': allPorts, 'usedPorts': usedPorts, 'usePorts': usePorts}) # 这里存在多线程更新缓存的场景,可能性比较低,但是必须先取出来,然后逐个更新状态,然后再记录缓存 ctrInfo = Device.get_dev_control_cache(self._device['devNo']) for strPort, info in result.items(): if ctrInfo.has_key(strPort): ctrInfo[strPort].update({'status': info['status']}) else: ctrInfo[strPort] = info Device.update_dev_control_cache(self._device['devNo'], ctrInfo) return result def get_port_status(self, force=True): return self.get_port_status_from_dev() def start_device(self, package, openId, attachParas): if attachParas is None: raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路、电池类型信息'}) if not attachParas.has_key('chargeIndex'): raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路'}) device = Device.objects(devNo=self._device['devNo']).first() port = '0' + attachParas['chargeIndex'] workMode = device.otherConf.get('workMode', '1') hexWorkMode = fill_2_hexByte(hex(int(workMode)), 2) orderNo = '000000000000' deviceId = self.get_device_id() agentId = Dealer.objects(id=device.ownerId).first().agentId # 同步用户 users = MyUser.objects(openId=openId, agentId=agentId) breakRuleTimesList = [] voiceType = '00' for _ in users: breakRuleTimesList.append(_.blacklistConfig.get('breakRuleTimes', 0)) if 1 in breakRuleTimesList: for u in users: u.blacklistConfig['breakRuleTimes'] = 1 u.blacklistConfig['orderRemindType'] = 0 u.save() voiceType = '02' if 2 in breakRuleTimesList: for u in users: u.blacklistConfig['breakRuleTimes'] = 2 u.blacklistConfig['orderRemindType'] = 0 u.save() voiceType = '03' # 播放不同语音 vData = '00' + voiceType + '00' + orderNo + deviceId + '000000' + '000000' devInfo = MessageSender.send( self.device, self.make_random_cmdcode(), {'IMEI': self._device['devNo'], "funCode": "04", 'data': vData}, timeout=MQTT_TIMEOUT.CHECK_DEVICE_STATUS ) 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 voiceType == '03': raise ServiceException({'result': 2, 'description': u'黑名单用户,拒绝提供服务。'}) price = float(package['price']) coins = float(package['coins']) needTime = 0 needLitre = 0 unit = package['unit'] if unit in [u'升', u'毫升']: needLitre = int(package['time']) hexLitre = fill_2_hexByte(hex(needLitre)) hexTime = '0000' consumeMode = '2' hexConsumeMode = '02' elif unit == u'秒': needTime = int(package['time']) hexTime = fill_2_hexByte(hex(needTime)) hexLitre = '0000' consumeMode = '1' hexConsumeMode = '01' else: raise ServiceException({'result': 2, 'description': u'套餐单位错误,请重新设置'}) data = port + hexConsumeMode + hexWorkMode + orderNo + deviceId + '00' + hexTime + '00' + hexLitre devInfo = MessageSender.send( device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC, payload = { 'IMEI': self._device['devNo'], "funCode": "01", 'data': data }, 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'当前设备正在忙,无响应,您的金币还在,请试试其他线路,或者请稍后再试哦'}) result = devInfo['data'][14::] resultPort = int(result[0:2], 16) resultStatus = data[4:6] if resultStatus == '02': # 代表失败 raise ServiceException({'result': 2, 'description': u'设备正在工作中,不能重复启动。'}) start_timestamp = int(time.time()) portDict = { 'startTime': timestamp_to_dt(start_timestamp).strftime('%Y-%m-%d %H:%M:%S'), 'status': Const.DEV_WORK_STATUS_WORKING, 'price': price, 'coins': coins, 'isStart': True, 'needTime': needTime, 'needLitre': needLitre, 'consumeMode':consumeMode, 'workMode': workMode, 'openId': openId, 'refunded': False, 'vCardId': self._vcard_id } if 'linkedRechargeRecordId' in attachParas and attachParas.get('isQuickPay', False): item = { 'rechargeRcdId': str(attachParas['linkedRechargeRecordId']) } payInfo = list() payInfo.append(item) portDict['payInfo'] = payInfo Device.update_dev_control_cache(self._device['devNo'], {str(resultPort): portDict}) # 推送的首要判断条件是用户是属于哪一类用户 user = MyUser.objects(openId=openId, groupId=self._device['groupId']).first() if user.is_push_live(float(str(price)), float(device.otherConf.get('liveLimitedPrice', '0.0'))) is True: group = Group.objects(id=device.groupId).first() dealer = Dealer.objects(id=device.ownerId).first() liveUrl = device.otherConf.get('liveUrl', 'https://open.ys7.com/view/h5/942c931863f04fddb9c9411a1a95a818') wechat_mp_proxy = get_wechat_manager_mp_proxy(dealer) wechat_mp_proxy.notify( dealer.managerialOpenId or '', 'dev_start', liveUrl, **{ 'title': u'{}'.format(group.groupName), 'things': u'加水机设备{}正在使用!\\n'.format(device.logicalCode), 'time': datetime.datetime.now().strftime(Const.DATETIME_FMT), 'remark': liveUrl }) return devInfo def lock_unlock_port(self, port, lock = True): if port == 1: lockStr = '04' if lock else '02' elif port == 2: lockStr = '05' if lock else '03' else: raise ServiceException({'result': 2, 'description': u'系统异常'}) deviceId = self.get_device_id() data = '02' + '00' + '00' + '0000' + '0000' + lockStr + '00' + deviceId + '00' + '00' + '00' + '00' + '00' + '00' devInfo = MessageSender.send( self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": "05", 'data': data} ) 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'][28:30] == '06': raise ServiceException({'result': 2, 'description': u'操作端口失败,繁忙中禁用'}) if lock: Device.update_dev_control_cache(self._device['devNo'], {str(port): {'status': Const.DEV_WORK_STATUS_FORBIDDEN}}) else: Device.update_dev_control_cache(self._device['devNo'], {str(port): {'status': Const.DEV_WORK_STATUS_IDLE}}) def stop(self, port = None): infoDict = self.stop_charging_port(port) infoDict['remainder_time'] = 0 return infoDict def active_deactive_port(self, port, active): if not active: self.stop_charging_port(port) devInfo = Device.get_dev_control_cache(self._device['devNo']) portCtrInfo = devInfo.get(str(port), {}) portCtrInfo.update({'isStart': False, 'status': Const.DEV_WORK_STATUS_IDLE, 'endTime': datetime.datetime.now().strftime(Const.DATETIME_FMT)}) newValue = {str(port): portCtrInfo} Device.update_dev_control_cache(self._device['devNo'], newValue) else: raise ServiceException({'result': 2, 'description': u'此设备不支持直接打开端口'}) def stop_charging_port(self, port): portStr = fill_2_hexByte(hex(int(port)), 2) lineInfo = Device.get_dev_control_cache(self.device['devNo'])[str(port)] consumeMode = lineInfo.get('consumeMode', '1') hexConsumeMode = fill_2_hexByte(hex(int(consumeMode)), 2) stopType = lineInfo.get('stopType', '000002') if stopType == 'dealerStopUsingPort': stopType = '000002' elif stopType == 'dealerCancelOrder': stopType = '000003' elif stopType == 'illegalUseStop': stopType = '000004' elif stopType == 'packageMismatchStop': stopType = '000005' else: stopType = '000001' deviceId = self.get_device_id() data = portStr + hexConsumeMode + '00' + '000000000000' + deviceId + stopType + '000000' devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": "02", 'data': data}) 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'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'}) result = devInfo['data'][36:42] if result == '000006': raise ServiceException({'result': 2, 'description': u'设备正在工作中,不能重复启动。'}) return {} def pauseToUseDevice(self, port, oper): lineInfo = Device.get_dev_control_cache(self._device['devNo']) isPause = lineInfo[str(port)].get('isPause', False) PauseCode = '01' if isPause is False else '02' consumeMode = lineInfo[str(port)].get('consumeMode', '1') hexConsumeMode = fill_2_hexByte(hex(int(consumeMode)), 2) orderNo = '000000000000' deviceId = self.get_device_id() pauseType = '000001' hexPort = fill_2_hexByte(hex(int(port)), 2) data = hexPort + hexConsumeMode + PauseCode + orderNo + deviceId + pauseType + '000000' devInfo = MessageSender.send( self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": "03", 'data': data} ) 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'当前设备正在忙,无响应,您的金币还在,请试试其他线路,或者请稍后再试哦'}) devInfoData = devInfo['data'] result = devInfoData[18:20] if result == '03': raise ServiceException({'result': 2, 'description': u'操作失败,请重试'}) def get_dev_setting(self): data = '03' + '00000000000000000000000000000000' devInfo = MessageSender.send( self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": "06", 'data': data} ) 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'当前设备正在忙,无响应,您的金币还在,请试试其他线路,或者请稍后再试哦'}) devInfoData = devInfo['data'] if devInfoData[14:16] != '04': raise ServiceException({'result': 2, 'description': u'获取参数失败,系统异常'}) maxDuration = int(devInfoData[16:18], 16) pauseTime = int(devInfoData[18:20], 16) flowTimes = int(devInfoData[20:24], 16) boardSoftVer = devInfoData[24:28] adStartHours = str(int(devInfoData[36:38], 16)) adStartMinutes = str(int(devInfoData[38:40], 16)) nightVolume = int(devInfoData[40:42], 16) adEndHours = str(int(devInfoData[42:44], 16)) adEndMinutes = str(int(devInfoData[44:46], 16)) dayVolume = int(devInfoData[46:48], 16) adStartTime = '{}{}:{}{}'.format('0' if len(adStartHours) == 1 else '', adStartHours, '0' if len(adStartMinutes) == 1 else '', adStartMinutes) adEndTime = '{}{}:{}{}'.format('0' if len(adEndHours) == 1 else '', adEndHours, '0' if len(adEndMinutes) == 1 else '', adEndMinutes) device = Device.objects(devNo=self._device['devNo']).first() device.otherConf.update({ 'maxDuration': maxDuration, 'pauseTime': pauseTime, 'flowTimes': flowTimes, 'boardSoftVer': boardSoftVer, 'adStartTime': adStartTime, 'nightVolume': nightVolume, 'adEndTime': adEndTime, 'dayVolume': dayVolume, }) device.save() Device.invalid_device_cache(device.devNo) clearFault = device.otherConf.get('clearFault', 0) workMode = device.otherConf.get('workMode', '1') minConsumeRatio = device.otherConf.get('minConsumeRatio', 5) return { 'maxDuration': maxDuration, 'pauseTime': pauseTime, 'flowTimes': flowTimes, 'boardSoftVer': boardSoftVer, 'adStartTime': adStartTime, 'nightVolume': nightVolume, 'adEndTime': adEndTime, 'dayVolume': dayVolume, 'clearFault': clearFault, 'workMode': workMode, 'minConsumeRatio': minConsumeRatio } def set_device_function_param(self, request, lastSetConf): tempSetConf = {} if 'maxDuration' in request.POST: maxDuration = request.POST['maxDuration'] pauseTime = request.POST['pauseTime'] minConsumeRatio = request.POST['minConsumeRatio'] boardSoftVer = request.POST['boardSoftVer'] flowTimes = request.POST['flowTimes'] workMode = request.POST['workMode'] clearFault = 0 tempSetConf.update({ 'maxDuration': maxDuration, 'pauseTime': pauseTime, 'minConsumeRatio': minConsumeRatio, 'boardSoftVer': boardSoftVer, 'flowTimes': flowTimes, 'workMode': workMode, 'clearFault': clearFault }) self.set_dev_setting(tempSetConf) elif 'adStartTime' in request.POST: adStartTime = request.POST['adStartTime'] adEndTime = request.POST['adEndTime'] dayVolume = request.POST['dayVolume'] nightVolume = request.POST['nightVolume'] adStartHours = str(int(adStartTime.split(':')[0])) adStartMinutes = str(int(adStartTime.split(':')[1])) adEndHours = str(int(adEndTime.split(':')[0])) adEndMinutes = str(int(adEndTime.split(':')[1])) tempSetConf.update({ 'dayVolume': dayVolume, 'nightVolume': nightVolume, 'adStartHours': adStartHours, 'adStartMinutes': adStartMinutes, 'adEndHours': adEndHours, 'adEndMinutes': adEndMinutes }) self.set_dev_setting(tempSetConf) def set_dev_setting(self, setConf): maxDuration = pauseTime = flowTimes = boardSoftVer = clearFault = adStartHours = adStartMinutes = nightVolume = adEndHours = adEndMinutes = dayVolume = '00' if 'maxDuration' in setConf: maxDuration = fill_2_hexByte(hex(int(setConf['maxDuration'])), 2) pauseTime = fill_2_hexByte(hex(int(setConf['pauseTime'])), 2) flowTimes = fill_2_hexByte(hex(int(setConf['flowTimes'])), 4) boardSoftVer = setConf['boardSoftVer'] clearFault = fill_2_hexByte(hex(int(setConf['clearFault'])), 2) setType = '03' elif 'adStartHours' in setConf: adStartHours = fill_2_hexByte(hex(int(setConf['adStartHours'])), 2) adStartMinutes = fill_2_hexByte(hex(int(setConf['adStartMinutes'])), 2) nightVolume = fill_2_hexByte(hex(int(setConf['nightVolume'])), 2) adEndHours = fill_2_hexByte(hex(int(setConf['adEndHours'])), 2) adEndMinutes = fill_2_hexByte(hex(int(setConf['adEndMinutes'])), 2) dayVolume = fill_2_hexByte(hex(int(setConf['dayVolume'])), 2) setType = '01' else: raise ServiceException({'result': 2, 'description': u'参数判断错误'}) deviceId = self.get_device_id() if setType == '03': data = setType + maxDuration + pauseTime + flowTimes + boardSoftVer + clearFault + '00' + deviceId + '00' + '00' + '00' + '00' + '00' + '00' else: data = setType + '00' + '00' + '0000' + '0000' + '00' + '00' + deviceId + adStartHours + adStartMinutes + nightVolume + adEndHours + adEndMinutes + dayVolume devInfo = MessageSender.send( self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": "05", 'data': data} ) 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'][14:16] == '04': raise ServiceException({'result': 2, 'description': u'参数修改失败,运行中和暂停中修改参数'}) device = Device.objects(devNo=self._device['devNo']).first() if setType == '03': device.otherConf.update({ 'maxDuration': int(maxDuration, 16), 'pauseTime': int(pauseTime, 16), 'flowTimes': int(flowTimes, 16), 'boardSoftVer': boardSoftVer, 'workMode': setConf['workMode'], 'minConsumeRatio': int(setConf['minConsumeRatio']), 'clearFault': 0 }) else: tempAdStartHours = str(int(adStartHours, 16)) tempAdStartMinutes = str(int(adStartMinutes, 16)) tempAdEndHours = str(int(adEndHours, 16)) tempAdEndMinutes = str(int(adEndMinutes, 16)) adStartTime = '{}{}:{}{}'.format('0' if len(tempAdStartHours) == 1 else '', tempAdStartHours, '0' if len(tempAdStartMinutes) == 1 else '', tempAdStartMinutes) adEndTime = '{}{}:{}{}'.format('0' if len(tempAdEndHours) == 1 else '', tempAdEndHours, '0' if len(tempAdEndMinutes) == 1 else '', tempAdEndMinutes) device.otherConf.update({ 'adStartTime': adStartTime, 'adEndTime': adEndTime, 'nightVolume': int(nightVolume, 16), 'dayVolume': int(dayVolume, 16), }) device.save() Device.invalid_device_cache(device.devNo)