# -*- coding: utf-8 -*- # !/usr/bin/env python import time import logging from apilib.utils_datetime import timestamp_to_dt 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 from apps.web.constant import Const, DeviceCmdCode, MQTT_TIMEOUT logger = logging.getLogger(__name__) class ChargingHaiNiaoDoubleBox(SmartBox): def __init__(self, device): super(ChargingHaiNiaoDoubleBox, self).__init__(device) def translate_funcode(self, funCode): funCodeDict = { '01': u'获取端口状态', '02': u'获取设备参数', '03': u'操作端口', '04': u'设置参数', } return funCodeDict.get(funCode, '') def translate_event_cmdcode(self, cmdCode): cmdDict = { '86': u'设备上报信息', } return cmdDict.get(cmdCode, '') def get_port_status_from_dev(self): # 获取端口状态 devInfo = MessageSender.send(self.device, self.make_random_cmdcode(), {'IMEI': self._device['devNo'], "funCode": '01', 'data': '00'}) 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 = {} if devInfo['data'][10:12] == '00': result['1'] = {'status': Const.DEV_WORK_STATUS_IDLE} result['2'] = {'status': Const.DEV_WORK_STATUS_IDLE} elif devInfo['data'][10:12] == '01': result['1'] = {'status': Const.DEV_WORK_STATUS_WORKING} result['2'] = {'status': Const.DEV_WORK_STATUS_IDLE} elif devInfo['data'][10:12] == '02': result['1'] = {'status': Const.DEV_WORK_STATUS_IDLE} result['2'] = {'status': Const.DEV_WORK_STATUS_WORKING} elif devInfo['data'][10:12] == '03': result['1'] = {'status': Const.DEV_WORK_STATUS_WORKING} result['2'] = {'status': Const.DEV_WORK_STATUS_WORKING} 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=False): # 获取端口状态(缓存) if force: return self.get_port_status_from_dev() ctrInfo = Device.get_dev_control_cache(self._device['devNo']) statusDict = {} allPorts = ctrInfo.get('allPorts', 1) for ii in range(allPorts): tempDict = ctrInfo.get(str(ii + 1), {}) if tempDict.has_key('status'): statusDict[str(ii + 1)] = {'status': tempDict.get('status')} elif tempDict.has_key('isStart'): if tempDict['isStart']: statusDict[str(ii + 1)] = {'status': Const.DEV_WORK_STATUS_WORKING} else: statusDict[str(ii + 1)] = {'status': Const.DEV_WORK_STATUS_IDLE} else: statusDict[str(ii + 1)] = {'status': Const.DEV_WORK_STATUS_IDLE} allPorts, usedPorts, usePorts = self.get_port_static_info(statusDict) Device.update_dev_control_cache(self._device['devNo'], {'allPorts': allPorts, 'usedPorts': usedPorts, 'usePorts': usePorts}) return statusDict 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'请您选择合适的充电线路'}) hexPort = '000' + attachParas['chargeIndex'] if 'linkedRechargeRecordId' in attachParas and attachParas.get('isQuickPay', False): needPrice = float(package['price']) else: needPrice = float(package['coins']) hexNeedPrice = fill_2_hexByte(hex(int(needPrice * 100))) needTime = int(package['time']) hexTime = fill_2_hexByte(hex(needTime)) timeUnit = package.get('unit', u'分钟') data = hexPort + '01' + hexTime + hexNeedPrice portStatus = self.get_port_status_from_dev() if timeUnit == u'秒': data = hexPort + hexTime + hexNeedPrice devInfo = MessageSender.send( device=self.device, cmd=DeviceCmdCode.OPERATE_DEV_SYNC, payload={ 'IMEI': self._device['devNo'], "funCode": '0A', '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'当前设备正在忙,无响应,您的金币还在,请试试其他线路,或者请稍后再试哦'}) data = devInfo['data'][8::] binUsePort = str(bin(int(data[2:4])))[2:] while len(binUsePort) < 2: binUsePort = '0' + binUsePort if binUsePort == '01': usePort = 1 elif binUsePort == '10': usePort = 2 result = data[4:8] if result == '0000': # 代表失败 newValue = {str(usePort): {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'充电站故障'}} Device.update_dev_control_cache(self._device['devNo'], newValue) raise ServiceException({'result': 2, 'description': u'充电站故障'}) start_timestamp = int(time.time()) finishedTime = start_timestamp + needTime * 60 portDict = { 'startTime': timestamp_to_dt(start_timestamp).strftime('%Y-%m-%d %H:%M:%S'), 'status': Const.DEV_WORK_STATUS_WORKING, 'finishedTime': finishedTime, 'needPrice': needPrice, # 个人中心断电需要用到 'coins': needPrice, 'isStart': True, 'needTime': needTime, 'openId': openId, 'refunded': False, 'vCardId': self._vcard_id, 'timeUnit': timeUnit } ctrInfo = Device.get_dev_control_cache(self._device['devNo']) lastPortInfo = ctrInfo.get(str(usePort), None) is_continue = False if lastPortInfo is not None and \ lastPortInfo.get('status', '') == Const.DEV_WORK_STATUS_WORKING and \ lastPortInfo.get('openId', '') == openId: is_continue = True if 'needPrice' in lastPortInfo: portDict['needPrice'] = float(needPrice) + lastPortInfo['needPrice'] if 'needTime' in lastPortInfo: portDict['needTime'] = needTime + lastPortInfo['needTime'] finishedTime = int(time.time()) + portDict['needTime'] * 60 portDict.update({'finishedTime': finishedTime}) if 'linkedRechargeRecordId' in attachParas and attachParas.get('isQuickPay', False): item = { 'rechargeRcdId': str(attachParas['linkedRechargeRecordId']) } if is_continue: payInfo = lastPortInfo.get('payInfo', list()) else: payInfo = list() payInfo.append(item) portDict['payInfo'] = payInfo else: if portStatus[attachParas['chargeIndex']]['status'] == Const.DEV_WORK_STATUS_WORKING: # 续充07指令, 字节不同 data = data[0:4] + data[6:10] devInfo = MessageSender.send( device=self.device, cmd=DeviceCmdCode.OPERATE_DEV_SYNC, payload={ 'IMEI': self._device['devNo'], "funCode": '09', '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'当前设备正在忙,无响应,您的金币还在,请试试其他线路,或者请稍后再试哦'}) data = devInfo['data'][8::] binUsePort = str(bin(int(data[2:4])))[2:] while len(binUsePort) < 2: binUsePort = '0' + binUsePort if binUsePort == '01': usePort = 1 elif binUsePort == '10': usePort = 2 result = data[4:8] if result == '0000': # 代表失败 newValue = {str(usePort): {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'充电站故障'}} Device.update_dev_control_cache(self._device['devNo'], newValue) raise ServiceException({'result': 2, 'description': u'充电站故障'}) start_timestamp = int(time.time()) finishedTime = start_timestamp + needTime * 60 portDict = { 'startTime': timestamp_to_dt(start_timestamp).strftime('%Y-%m-%d %H:%M:%S'), 'status': Const.DEV_WORK_STATUS_WORKING, 'finishedTime': finishedTime, 'needPrice': needPrice, # 个人中心断电需要用到 'coins': needPrice, 'isStart': True, 'needTime': needTime, 'openId': openId, 'refunded': False, 'vCardId': self._vcard_id, 'timeUnit': timeUnit } ctrInfo = Device.get_dev_control_cache(self._device['devNo']) lastPortInfo = ctrInfo.get(str(usePort), None) is_continue = False if lastPortInfo is not None and \ lastPortInfo.get('status', '') == Const.DEV_WORK_STATUS_WORKING and \ lastPortInfo.get('openId', '') == openId: is_continue = True if 'needPrice' in lastPortInfo: portDict['needPrice'] = float(needPrice) + lastPortInfo['needPrice'] if 'needTime' in lastPortInfo: portDict['needTime'] = needTime + lastPortInfo['needTime'] finishedTime = int(time.time()) + portDict['needTime'] * 60 portDict.update({'finishedTime': finishedTime}) if 'linkedRechargeRecordId' in attachParas and attachParas.get('isQuickPay', False): item = { 'rechargeRcdId': str(attachParas['linkedRechargeRecordId']) } if is_continue: payInfo = lastPortInfo.get('payInfo', list()) else: payInfo = list() payInfo.append(item) portDict['payInfo'] = payInfo else: devInfo = MessageSender.send( device=self.device, cmd=DeviceCmdCode.OPERATE_DEV_SYNC, payload={ 'IMEI': self._device['devNo'], "funCode": '08', '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'当前设备正在忙,无响应,您的金币还在,请试试其他线路,或者请稍后再试哦'}) data = devInfo['data'][8::] binUsePort = str(bin(int(data[2:4])))[2:] while len(binUsePort) < 2: binUsePort = '0' + binUsePort if binUsePort == '01': usePort = 1 elif binUsePort == '10': usePort = 2 result = data[4:8] if result == '0000': # 代表失败 newValue = {str(usePort): {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'充电站故障'}} Device.update_dev_control_cache(self._device['devNo'], newValue) raise ServiceException({'result': 2, 'description': u'充电站故障'}) start_timestamp = int(time.time()) finishedTime = start_timestamp + needTime * 60 portDict = { 'startTime': timestamp_to_dt(start_timestamp).strftime('%Y-%m-%d %H:%M:%S'), 'status': Const.DEV_WORK_STATUS_WORKING, 'finishedTime': finishedTime, 'needPrice': needPrice, # 个人中心断电需要用到 'coins': needPrice, 'isStart': True, 'needTime': needTime, 'openId': openId, 'refunded': False, 'vCardId': self._vcard_id, 'timeUnit': timeUnit } if 'linkedRechargeRecordId' in attachParas: item = { 'rechargeRcdId': str(attachParas['linkedRechargeRecordId']) } portDict['payInfo'] = [item] Device.update_dev_control_cache(self._device['devNo'], {str(usePort): portDict}) return devInfo def get_dev_setting(self): devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '02', 'data': '00'}) 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'当前设备忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'}) confData = devInfo['data'][8::] channelParams = int(confData[0:2], 16) accumulatedCoins = int(confData[2:6], 16) versionNumber = int(confData[6:10], 16) charge1PriceForH0 = int(confData[10:12], 16) charge1TimeForH0 = int(confData[12:16], 16) power1PriceForCharge2 = int(confData[22:24], 16) power1TimeForCharge2 = int(confData[24:28], 16) power2PriceForCharge2 = int(confData[28:30], 16) power2TimeForCharge2 = int(confData[30:34], 16) power3PriceForCharge2 = int(confData[34:36], 16) power3TimeForCharge2 = int(confData[36:40], 16) power4PriceForCharge2 = int(confData[40:42], 16) power4TimeForCharge2 = int(confData[42:46], 16) binStr = str(bin(channelParams))[2:] while len(binStr) < 8: binStr = '0' + binStr playSound = True if int(binStr[7]) else False if binStr[4:7] == '001': clearMode = '1minutes' elif binStr[4:7] == '010': clearMode = '2minutes' elif binStr[4:7] == '011': clearMode = '3minutes' else: clearMode = 'restart' pauseSwitch = True if int(binStr[2]) else False # clearMode = '10minutes' if int(binStr[4]) else '1minutes' workingMode = 'public' if int(binStr[3]) else 'private' infraredMode = True if int(binStr[1]) else False coinLaunchMode = 'button' if int(binStr[0]) else 'direct' device = Device.objects(devNo=self._device['devNo']).first() minConsumptionLimit = device.otherConf.get('minConsumptionLimit', 0) return { 'playSound': playSound, 'pauseSwitch': pauseSwitch, 'clearMode': clearMode, 'workingMode': workingMode, 'infraredMode': infraredMode, 'coinLaunchMode': coinLaunchMode, 'accumulatedCoins': accumulatedCoins, 'versionNumber': versionNumber, 'charge1PriceForH0': charge1PriceForH0, 'charge1TimeForH0': charge1TimeForH0, 'power1PriceForCharge2': power1PriceForCharge2, 'power1TimeForCharge2': power1TimeForCharge2, 'power2PriceForCharge2': power2PriceForCharge2, 'power2TimeForCharge2': power2TimeForCharge2, 'power3PriceForCharge2': power3PriceForCharge2, 'power3TimeForCharge2': power3TimeForCharge2, 'power4PriceForCharge2': power4PriceForCharge2, 'power4TimeForCharge2': power4TimeForCharge2, 'minConsumptionLimit': minConsumptionLimit } def send_setting_data(self, data): devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '04', '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] == '01': raise ServiceException( {'result': 2, 'description': u'设置失败'}) def set_pauseSwitch_mode(self, pauseSwitch): data = '08' + ('0001' if pauseSwitch else '0000') self.send_setting_data(data) def set_playSound_mode(self, playSound): data = '07' + ('0001' if playSound else '0000') self.send_setting_data(data) def set_infraredMode_mode(self, infraredMode): data = '0B' + ('0001' if infraredMode else '0000') self.send_setting_data(data) def set_base_settings(self, baseSettingsDict): # workingModeData = '0A' + ('0001' if baseSettingsDict['workingMode'] == 'multiple' else '0000') workingModeData = '0A0000' self.send_setting_data(workingModeData) coinLaunchModeData = '09' + ('0001' if baseSettingsDict['coinLaunchMode'] == 'button' else '0000') self.send_setting_data(coinLaunchModeData) # clearMode = '0C' + ('0001' if baseSettingsDict['clearMode'] == '10minutes' else '0000') # self.send_setting_data(clearMode) if baseSettingsDict['clearMode'] == '1minutes': clearMode = '0C0001' elif baseSettingsDict['clearMode'] == '2minutes': clearMode = '0C0002' elif baseSettingsDict['clearMode'] == '3minutes': clearMode = '0C0003' else: clearMode = '0C0004' self.send_setting_data(clearMode) device = Device.objects(devNo=self._device['devNo']).first() device.otherConf['minConsumptionLimit'] = baseSettingsDict['minConsumptionLimit'] device.save() def set_private_settings(self, powerPriceDict): charge1PriceForH0 = '14' + fill_2_hexByte(hex(int(powerPriceDict['charge1PriceForH0']))) self.send_setting_data(charge1PriceForH0) charge1TimeForH0 = '15' + fill_2_hexByte(hex(int(powerPriceDict['charge1TimeForH0']))) self.send_setting_data(charge1TimeForH0) def set_public_settings(self, powerMatterDict): power1PriceForCharge2 = '18' + fill_2_hexByte(hex(int(powerMatterDict['power1PriceForCharge2']))) self.send_setting_data(power1PriceForCharge2) power1TimeForCharge2 = '19' + fill_2_hexByte(hex(int(powerMatterDict['power1TimeForCharge2']))) self.send_setting_data(power1TimeForCharge2) power2PriceForCharge2 = '1A' + fill_2_hexByte(hex(int(powerMatterDict['power2PriceForCharge2']))) self.send_setting_data(power2PriceForCharge2) power2TimeForCharge2 = '1B' + fill_2_hexByte(hex(int(powerMatterDict['power2TimeForCharge2']))) self.send_setting_data(power2TimeForCharge2) power3PriceForCharge2 = '1C' + fill_2_hexByte(hex(int(powerMatterDict['power3PriceForCharge2']))) self.send_setting_data(power3PriceForCharge2) power3TimeForCharge2 = '1D' + fill_2_hexByte(hex(int(powerMatterDict['power3TimeForCharge2']))) self.send_setting_data(power3TimeForCharge2) power4PriceForCharge2 = '1E' + fill_2_hexByte(hex(int(powerMatterDict['power4PriceForCharge2']))) self.send_setting_data(power4PriceForCharge2) power4TimeForCharge2 = '1F' + fill_2_hexByte(hex(int(powerMatterDict['power4TimeForCharge2']))) self.send_setting_data(power4TimeForCharge2) def set_device_function(self, request, lastSetConf): if 'infraredMode' in request.POST: infraredMode = request.POST['infraredMode'] self.set_infraredMode_mode(infraredMode) if 'playSound' in request.POST: playSound = request.POST['playSound'] self.set_playSound_mode(playSound) if 'pauseSwitch' in request.POST: pauseSwitch = request.POST['pauseSwitch'] self.set_pauseSwitch_mode(pauseSwitch) def set_device_function_param(self, request, lastSetConf): # if 'coinLaunchMode' in request.POST \ # and 'workingMode' in request.POST \ # and 'minConsumptionLimit' in request.POST \ # and 'clearMode' in request.POST: if 'coinLaunchMode' in request.POST \ and 'minConsumptionLimit' in request.POST \ and 'clearMode' in request.POST: coinLaunchMode = request.POST['coinLaunchMode'] # workingMode = request.POST['workingMode'] minConsumptionLimit = request.POST['minConsumptionLimit'] clearMode = request.POST['clearMode'] self.set_base_settings({ 'coinLaunchMode': coinLaunchMode, # 'workingMode': workingMode, 'minConsumptionLimit': minConsumptionLimit, 'clearMode': clearMode }) if 'charge1PriceForH0' in request.POST and 'charge1TimeForH0' in request.POST: charge1PriceForH0 = request.POST['charge1PriceForH0'] charge1TimeForH0 = request.POST['charge1TimeForH0'] self.set_private_settings({ 'charge1PriceForH0': charge1PriceForH0, 'charge1TimeForH0': charge1TimeForH0 }) if 'power1PriceForCharge2' in request.POST \ and 'power1TimeForCharge2' in request.POST \ and 'power2PriceForCharge2' in request.POST \ and 'power2TimeForCharge2' in request.POST \ and 'power3PriceForCharge2' in request.POST \ and 'power3TimeForCharge2' in request.POST \ and 'power4PriceForCharge2' in request.POST \ and 'power4TimeForCharge2' in request.POST: power1PriceForCharge2 = request.POST['power1PriceForCharge2'] power1TimeForCharge2 = request.POST['power1TimeForCharge2'] power2PriceForCharge2 = request.POST['power2PriceForCharge2'] power2TimeForCharge2 = request.POST['power2TimeForCharge2'] power3PriceForCharge2 = request.POST['power3PriceForCharge2'] power3TimeForCharge2 = request.POST['power3TimeForCharge2'] power4PriceForCharge2 = request.POST['power4PriceForCharge2'] power4TimeForCharge2 = request.POST['power4TimeForCharge2'] self.set_public_settings({ 'power1PriceForCharge2': power1PriceForCharge2, 'power1TimeForCharge2': power1TimeForCharge2, 'power2PriceForCharge2': power2PriceForCharge2, 'power2TimeForCharge2': power2TimeForCharge2, 'power3PriceForCharge2': power3PriceForCharge2, 'power3TimeForCharge2': power3TimeForCharge2, 'power4PriceForCharge2': power4PriceForCharge2, 'power4TimeForCharge2': power4TimeForCharge2 }) def get_port_info(self, line): devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '01', 'data': '00'}) 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 str(line) == '1': leftTimeSec = int(devInfo['data'][12:20], 16) leftTime = leftTimeSec / 60 elif str(line) == '2': leftTimeSec = int(devInfo['data'][20:28], 16) leftTime = leftTimeSec / 60 else: leftTime = 0 return {'port': line, 'leftTime': leftTime} def active_deactive_port(self, port, active): if active: return else: data = '000' + str(port) + '00' devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '03', 'data': data + '0000'}) if devInfo.has_key('rst') and devInfo['rst'] != 0: if devInfo['rst'] == -1: raise ServiceException( {'result': 2, 'description': u'当前设备正在玩命找网络,请您稍候再试'}) elif devInfo['rst'] == 1: # 不处理 pass def analyze_event_data(self, data): cmdCode = data[6:8] if cmdCode == '86': # data = data[8:] channelChangeStatus = data[10:12] channelOperateStatus = data[14:16] binChannelChangeStatus = str(bin(int(channelChangeStatus, 16)))[2:] while len(binChannelChangeStatus) < 8: binChannelChangeStatus = '0' + binChannelChangeStatus binChannelOperateStatus = str(bin(int(channelOperateStatus, 16)))[2:] while len(binChannelOperateStatus) < 8: binChannelOperateStatus = '0' + binChannelOperateStatus channel1Status = False if binChannelChangeStatus[-1] == '1' and binChannelOperateStatus[-1] == '0' else True channel2Status = False if binChannelChangeStatus[-2] == '1' and binChannelOperateStatus[-2] == '0' else True channel1Start = False if binChannelChangeStatus[-1] == '1' and binChannelOperateStatus[-1] == '1' else True channel2Start = False if binChannelChangeStatus[-2] == '1' and binChannelOperateStatus[-2] == '1' else True if channel1Status is False: port = '1' reason = data[16:18] leftTime = int(data[36:44], 16) / 60 leftPrice = float(int(data[44:48], 16)) / 100 elif channel2Status is False: port = '2' reason = data[18:20] leftTime = int(data[48:56], 16) / 60 leftPrice = float(int(data[56:60], 16)) / 100 else: port = -1 reason = '' leftTime = -1 leftPrice = -1 if reason == '00': desc = u'购买的充电时间或者电量用完了。' elif reason in ['04', '06', '07']: desc = u'管理人员可能远程断电了,或是插头被拔掉, 建议您根据已经充电的时间评估是否需要到现场换到其他端口充电。' elif reason == '05': desc = u'空载断电。' else: desc = u'' return { 'status': Const.DEV_WORK_STATUS_IDLE, 'leftPrice': leftPrice, 'leftTime': leftTime, 'cmdCode': cmdCode, 'reason': desc, 'port': port, 'channel1Start':channel1Start, 'channel2Start':channel2Start } else: return {} def stop(self, port=None): self.stop_charging_port(port) infoDict = dict() infoDict['remainder_time'] = 0 return infoDict def stop_charging_port(self, port): self.active_deactive_port(port, False) def pauseToUseDevice(self, port, oper): MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_NO_RESPONSE, {'IMEI': self._device['devNo'], "funCode": '03', 'data': '000' + port + oper + '0000'}) if oper == '02': Device.update_dev_control_cache(self._device['devNo'], {port: {'pausePort': True}}) elif oper == '01': Device.update_dev_control_cache(self._device['devNo'], {port: {'pausePort': False}}) else: pass @property def isHaveStopEvent(self): return True