# -*- coding: utf-8 -*- # !/usr/bin/env python import datetime import logging import time from decimal import Decimal from typing import TYPE_CHECKING from apilib.monetary import RMB from apilib.utils_string import split_str, cn from apps.web.constant import Const, DeviceCmdCode, ErrorCode, DeviceErrorCodeDesc, MQTT_TIMEOUT from apps.web.core.adapter.base import SmartBox, fill_2_hexByte, make_six_bytes_session_id from apps.web.core.exceptions import ServiceException from apps.web.core.networking import MessageSender from apps.web.device.models import Device logger = logging.getLogger(__name__) if TYPE_CHECKING: from apps.web.device.models import GroupDict class ChargingDIANCHUANBox(SmartBox): def __init__(self, device): super(ChargingDIANCHUANBox, self).__init__(device) def _check_package(self, package): """ 获取设备启动的发送数据 根据设备的当前模式以及套餐获取 :param package: :return: """ consumeModule = self._device.get("otherConf", dict()).get("consumeModule", 0) unit = package.get("unit", u"分钟") _time = float(package.get("time", 0)) # 按时间计费 if consumeModule == 0: billingType = "time" if unit == u"小时": _time = _time * 60 elif unit == u"天": _time = _time * 24 * 60 elif unit == u"秒": _time = _time / 60 elif unit == u"分钟": _time = _time else: raise ServiceException({"result": 2, "description": u"套餐单位错误,请联系经销商"}) # 按电量计费 else: billingType = "elec" if unit != u"度": raise ServiceException({"result": 2, "description": u"套餐单位错误,请联系经销商"}) else: _time = _time * 100 return _time, unit, billingType def translate_funcode(self, funCode): funCodeDict = { '01': u'查询设备每个端口当前的状态', '02': u'移动支付', '0C': u'获取设备设置', '06': u'获取设备端口详情', '07': u'获取刷卡投币统计数据', '08': u'设置设备参数', '09': u'设置刷卡投币使能', '0A': u'端口使能', '0B': u'端口关闭', '16': u'设置设备参数', '20': u'设备重启', '13': u'设置卡充满退费', '15': u'获取功率费率配置', '14': u'设置功率费率配置', '12': u'充值', '22': u'回复卡充值', '27': u'设置免费充电模式、音量调节', '29': u'设置刷卡充电时间', '2B': u'查询软件版本', '34': u'查询整机所有端口的状态(状态,剩余时间,功率,冲电电量,电流)', '35': u'查询设备其他动态参数(设备温度,烟雾报警器状态,设备电压)', '36': u'查询设备所有设置数据', '37': u'设置浮充功率,浮充时间,充电检测时间', '38': u'设置消费流程、计费方式', '40': u'重启主板', '43': u'温度保护阈值设置', '45': u'设置充电站3档计费功率、比例', } return funCodeDict.get(funCode, '') def translate_event_cmdcode(self, cmdCode): cmdDict = { '03': u'投币上报', '04': u'刷卡上报', '05': u'充电结束', '16': u'充电结束', '10': u'刷卡上报', '0D': u'故障', '20': u'启动设备', '11': u'刷卡使用', '12': u'卡充值', '22': u'ID卡扣费', '23': u'ID卡扣费状态', '17': u'IC卡回收余额成功', } return cmdDict.get(cmdCode, '') def is_port_can_use(self, port, canAdd=False): # 电川的在启动的时候去判断是否需要续充 return True, '' def check_dev_status(self, attachParas=None): """ 如果超过两个心跳周期没有报心跳,并且最后一次更新时间在2个小时内,需要从设备获取状态 否则以缓存状态为准。 :param attachParas: :return: """ if attachParas is None: raise ServiceException({'result': 0, 'description': u'请您选择合适的充电端口、电池类型信息'}) if not attachParas.has_key('chargeIndex'): raise ServiceException({'result': 0, 'description': u'请您选择合适的充电端口'}) if not self.device.need_fetch_online: raise ServiceException( {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL)}) self.get_port_status_from_dev() group = self.device.group # type: GroupDict if group.is_free: logger.debug('{} is free. no need to check continue pay.'.format(repr(self.device))) return # 处理是否能够续充 portDict = self.get_port_status() port = str(attachParas['chargeIndex']) if port in portDict: isCanAdd = self.device['devType'].get('payableWhileBusy', False) if portDict[port]['status'] == Const.DEV_WORK_STATUS_IDLE: return elif portDict[port]['status'] == Const.DEV_WORK_STATUS_FAULT: raise ServiceException({'result': 0, 'description': u'该端口故障,暂时不能使用'}) elif portDict[port]['status'] == Const.DEV_WORK_STATUS_WORKING: if isCanAdd: return else: raise ServiceException({'result': 0, 'description': u'该端口正在工作不能使用,请您使用其他端口'}) elif portDict[port]['status'] == Const.DEV_WORK_STATUS_FORBIDDEN: raise ServiceException({'result': 0, 'description': u'该端口已被禁止使用,请您使用其他端口'}) elif portDict[port]['status'] == Const.DEV_WORK_STATUS_CONNECTED: return else: raise ServiceException({'result': 0, 'description': u'端口状态未知,暂时不能使用'}) else: raise ServiceException({'result': 0, 'description': u'未知端口,暂时不能使用'}) def test(self, port=1, coins=1): hexPort = fill_2_hexByte(hex(int(port)), 2) hexTime = fill_2_hexByte(hex(coins)) devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '02', 'data': hexPort + '0000' + hexTime}) return devInfo def port_is_busy(self, port_dict): if not port_dict: return False if 'billingType' not in port_dict: return False if 'status' not in port_dict: return False if 'coins' not in port_dict: return False if 'price' not in port_dict: return False if port_dict['billingType'] not in ['time', 'elec']: return False if port_dict['billingType'] == 'time': if 'needTime' not in port_dict: return False else: if 'needElec' not in port_dict: return False if port_dict['status'] == Const.DEV_WORK_STATUS_WORKING: return True else: return False 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'请您选择合适的充电线路'}) price = float(package['price']) dev = Device.objects.get(devNo=self._device['devNo']) refundProtection = dev.otherConf.get('refundProtection', 0) refundProtectionTime = dev.otherConf.get('refundProtectionTime', 5) port = hex(int(attachParas['chargeIndex'])) hexPort = fill_2_hexByte(port, 2) _time, unit, billingType = self._check_package(package) coins = float(package['coins']) hexTime = fill_2_hexByte(hex(int(_time))) unit = package['unit'] if unit == '度': hexTime = fill_2_hexByte(hex(int(_time))) orderNo = attachParas.get('orderNo') devInfo = MessageSender.send( device=self.device, cmd=DeviceCmdCode.OPERATE_DEV_SYNC, payload={ 'IMEI': self._device['devNo'], "funCode": '02', 'data': hexPort + '0000' + hexTime }, timeout=MQTT_TIMEOUT.START_DEVICE) usePort = int(attachParas['chargeIndex']) if 'rst' not in devInfo: raise ServiceException( { 'result': 2, 'description': u'启动设备失败,您的支付金额已经退还,请重新扫码设备试试(1001)' }) if devInfo['rst'] != 0: if devInfo['rst'] == -1: raise ServiceException( { 'result': 2, 'description': u'充电桩正在玩命找网络,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦' }) elif devInfo['rst'] == 1: self.check_serial_port_for_startcmd(attachParas['chargeIndex']) else: raise ServiceException( { 'result': 2, 'description': u'启动设备失败,您的支付金额已经退回,请重新扫码设备({})'.format(devInfo['rst']) }) else: if 'data' not in devInfo: logger.warning('no data in success response. result = {}'.format(str(devInfo))) raise ServiceException( { 'result': 2, 'description': u'启动设备失败,您的支付金额已经退回,请重试' }) else: data = devInfo['data'][18::] result = data[2:4] if result == '01': # 成功 pass elif result == '02': 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'充电站故障'}) elif result == '03': newValue = {str(usePort): {'status': Const.DEV_WORK_STATUS_WORKING, 'statusInfo': u''}} Device.update_dev_control_cache(self._device['devNo'], newValue) raise ServiceException({'result': 2, 'description': u'该端口正在使用中'}) else: raise ServiceException({ 'result': 2, 'description': u'启动设备失败({}),您的支付金额已经退回,请重新扫码设备'.format(result) }) portDict = { 'startTime': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'status': Const.DEV_WORK_STATUS_WORKING, 'coins': coins, 'price': price, 'billingType': billingType, 'isStart': True, 'openId': openId, 'refunded': False, 'vCardId': self._vcard_id, 'refundProtection': refundProtection, 'refundProtectionTime': refundProtectionTime, 'doPowerLevel': False, 'payInfo': list() } ctrInfo = Device.get_dev_control_cache(self._device.devNo) lastPortInfo = ctrInfo.get(str(usePort), {}) if self.port_is_busy(lastPortInfo) and \ lastPortInfo.get('billingType') == billingType and \ lastPortInfo.get('openId') == openId: is_continus = True portDict['coins'] = float(coins) + lastPortInfo['coins'] portDict['price'] = float(price) + lastPortInfo['price'] else: is_continus = False portDict['coins'] = float(coins) portDict['price'] = float(price) if 'linkedRechargeRecordId' in attachParas and attachParas.get('isQuickPay', False): if is_continus: payInfo = lastPortInfo.get("payInfo", list()) if not payInfo: logger.warning("miss payInfo! {}-{}".format(self._device["devNo"], usePort)) else: payInfo = list() payInfo.append({'rechargeRcdId': str(attachParas['linkedRechargeRecordId'])}) portDict['payInfo'] = payInfo else: if is_continus: portDict['payInfo'] = lastPortInfo.get("payInfo", list()) else: portDict['payInfo'] = list() if billingType == 'time': if is_continus: portDict['needTime'] = _time + lastPortInfo['needTime'] else: portDict['needTime'] = _time finishedTime = int(time.time()) + int(portDict['needTime'] * 60) else: if is_continus: portDict['needElec'] = _time / 100.0 + lastPortInfo['needElec'] else: portDict['needElec'] = _time / 100.0 finishedTime = int(time.time()) + 60 * 60 * 12 portDict.update({'finishedTime': finishedTime}) if 'orderNo' in attachParas: portDict.update({'orderNo': attachParas['orderNo']}) Device.update_dev_control_cache( self._device['devNo'], { str(usePort): portDict }) devInfo['finishedTime'] = finishedTime return devInfo def analyze_event_data(self, data): cmdCode = data[4:6] if cmdCode == '05': port = int(data[18:20], 16) leftTime = int(data[20:24], 16) reason = data[24:26] if reason == '00': desc = u'购买的充电时间或者电量已经用完' elif 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'远烟雾报警停止' else: desc = u'' # 刷卡充电的结束 if len(data) > 34: cardNo = str(int(data[26:34], 16)) backMoney = int(data[34:36], 16) / 10.0 cardType = 'ID' if data[36:40] == 'AA33' else 'IC' if cardNo != str(0): return {'status': Const.DEV_WORK_STATUS_IDLE, 'cmdCode': cmdCode, 'port': port, 'leftTime': leftTime, 'reason': desc, 'isStart': False, 'cardNo': cardNo, 'backMoney': backMoney, 'cardType': cardType, 'endType': reason, 'reasonCode': reason, 'uartData': data} else: return {'status': Const.DEV_WORK_STATUS_IDLE, 'cmdCode': cmdCode, 'port': port, 'leftTime': leftTime, 'reason': desc, 'isStart': False, 'endType': reason, 'reasonCode': reason, 'uartData': data} # 非刷卡充电的结束 else: return {'status': Const.DEV_WORK_STATUS_IDLE, 'cmdCode': cmdCode, 'port': port, 'leftTime': leftTime, 'reason': desc, 'isStart': False, 'endType': reason, 'reasonCode': reason, 'uartData': data} elif cmdCode == '0D': port = int(data[18:20], 16) errCode = data[20:22] if errCode == '01': return {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'端口输出故障,保险丝熔断', 'cmdCode': cmdCode, 'port': port, 'FaultCode': errCode} elif errCode == '02': return {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'继电器黏连', 'cmdCode': cmdCode, 'port': port, 'FaultCode': errCode} elif cmdCode == '03': # 投币上报 coin = int(data[18:20], 16) port = int(data[20:22], 16) return {'cmdCode': cmdCode, 'coins': coin, 'port': port} elif cmdCode == '11': # 刷卡后,按下端口,上报的报文 cardNo = int(data[18:26], 16) cardNo = str(cardNo) fee = int(data[26:28], 16) / 10.0 balance = int(data[28:32], 16) / 10.0 cardType = 'ID' if data[32:36] == 'AA33' else 'IC' port = int(data[36:38], 16) status = data[38:40] return {'cardNo': cardNo, 'fee': fee, 'balance': balance, 'cardType': cardType, 'port': port, 'status': status, 'cmdCode': cmdCode} elif cmdCode == '12': cardNo = data[18:26] cardNo = str(int(cardNo, 16)) balance = int(data[26:30], 16) / 10.0 cardType = data[30:34] return {'cmdCode': cmdCode, 'balance': balance, 'cardType': cardType, 'cardNo': cardNo} elif cmdCode == '17': """ IC卡回收余额成功,上报回收的数目 样例 660F170048614800052DDA5A0D000001DD """ cardNo = int(data[18:26], 16) cardNo = str(cardNo) backMoney = int(data[26:30], 16) / 10.0 return {'cmdCode': cmdCode, 'cardNo': cardNo, 'backMoney': backMoney} elif cmdCode == '22': # ID卡,上报上来扣费 cardNo = int(data[18:26], 16) cardNo = str(cardNo) fee = int(data[26:28], 16) / 10.0 cardType = 'ID' if data[28:32] == 'AA33' else 'IC' port = int(data[32:34], 16) or -1 return {'cmdCode': cmdCode, 'cardNo': cardNo, 'fee': fee, 'cardType': cardType, 'port': port} elif cmdCode == '23': # ID卡,上报上来扣费 cardNo = int(data[18:26], 16) cardNo = str(cardNo) status = data[26:28] return {'cmdCode': cmdCode, 'cardNo': cardNo, 'status': status} elif cmdCode == '41': # 烟感报警 smokeWarning = True if data[18:20] == '01' else False return {'cmdCode': cmdCode, 'smokeWarning': smokeWarning, "faultCode": cmdCode} elif cmdCode == '47': # 温度超过阀值 temperatureLast = str(int(data[18:20], 16)) return {'cmdCode': cmdCode, 'temperatureLast': temperatureLast, "faultCode": cmdCode} elif cmdCode == '48': data = data nowPowerRatio = int(data[18:20], 16) leftTime = int(data[20:24], 16) nowPower = int(data[24:28], 16) / 10.0 return {'cmdCode': cmdCode, 'nowPowerRatio': nowPowerRatio, 'leftTime': leftTime, 'nowPower': nowPower} def get_dev_consume_count(self): devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '07', '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'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'}) data = devInfo['data'][18::] cardFee = int(data[2:6], 16) / 10.0 # 以角为单位 coinFee = int(data[6:10], 16) # 以元为单位 return {'cardFee': cardFee, 'coinFee': coinFee} # 查询单个充电状态 def get_port_info(self, port): data = fill_2_hexByte(hex(int(port)), 2) devInfo = MessageSender.send(self.device, self.make_random_cmdcode(), {'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'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'}) data = devInfo['data'][18::] leftTime = int(data[2:6], 16) if data[6:10] == 'FFFF': power = 0 else: power = int(data[6:10], 16) / 10.0 data = {"port": port, "power": power} # 电量计费模式 if not self._device.get("otherConf", dict()).get("consumeModule", 0): data.update({"leftTime": leftTime}) else: data.update({"leftElec": leftTime / 100.0}) return data def __get_all_port_status_detail(self, returnType='list'): """ 查询所有端口的详细(状态,剩余时间,功率,冲电电量,电流) returnType : list dict """ devInfo = MessageSender.send(self.device, self.make_random_cmdcode(), {'IMEI': self._device['devNo'], "funCode": '34', 'data': '00'}) if devInfo.has_key('rst') and devInfo['rst'] != 0: if devInfo['rst'] == -1: raise ServiceException( {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试', 'rst': -1}) elif devInfo['rst'] == 1: raise ServiceException( {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能', 'rst': 1}) data = devInfo['data'][18:] import re portStatus = map(lambda d: d, re.findall(r'..', data[:20])) portLeftTime = map(lambda d: int(d, 16), re.findall(r'....', data[20:60])) portPower = map(lambda d: int(d, 16) / 10.0, re.findall(r'....', data[60:100])) portUsedElec = map(lambda d: int(d, 16) / 100.0, re.findall(r'....', data[100:140])) ampere = map(lambda d: int(d, 16) / 10.0, re.findall(r'..', data[140:160])) if returnType == 'list': lis = [] for i, x in enumerate(portStatus): item = { 'index': str(i + 1), 'portStatus': str(x), 'leftTime': str(portLeftTime[i]), 'power': str(portPower[i]), 'usedElec': str(portUsedElec[i]), 'ampere': str(ampere[i]), } lis.append(item) return lis if returnType == 'dict': lis = [] for i, x in enumerate(portStatus): item = (str(i + 1), { 'index': str(i + 1), 'portStatus': str(x), 'leftTime': str(portLeftTime[i]), 'power': str(portPower[i]), 'usedElec': str(portUsedElec[i]), 'ampere': str(ampere[i]), }) lis.append(item) result = dict(lis) return result def dealer_get_device_port(self): port_detail_dict = self.__get_all_port_status_detail() parse_status_code = { '00': u'端口正常', '01': u'端口无输出', '02': u'端口粘连', } ctrInfo = Device.get_dev_control_cache(self.device.devNo) lis = [] for item in port_detail_dict: if float(item['ampere']) == 0 and float(item['power']) == 0: item = { 'index': item['index'], 'status': 'idle', 'desc': parse_status_code[item['portStatus']] } elif item['portStatus'] != '00': item = { 'index': item['index'], 'status': 'fault', 'desc': parse_status_code[item['portStatus']] } else: item['status'] = 'busy' item['desc'] = parse_status_code[item['portStatus']] lineInfo = ctrInfo.get(str(item['index']), {}) if not lineInfo: lineInfo = {} lineInfo.update(item) item = self.format_port_using_detail(lineInfo) lis.append(item) return lis 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'充电桩正在玩命找网络,请您稍候再试', 'rst': -1}) elif devInfo['rst'] == 1: raise ServiceException( {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能', 'rst': 1}) data = devInfo['data'][18::] result = {} portNum = int(data[:2], 16) portData = data[2:-2] ii = 0 while ii < portNum: statusTemp = portData[ii * 2:ii * 2 + 2] if statusTemp == '01': status = {'status': Const.DEV_WORK_STATUS_IDLE} elif statusTemp == '02': status = {'status': Const.DEV_WORK_STATUS_WORKING} elif statusTemp == '03': status = {'status': Const.DEV_WORK_STATUS_FORBIDDEN} elif statusTemp == '04': status = {'status': Const.DEV_WORK_STATUS_FAULT} # 不再上述状态之列的 统一为故障状态 else: status = {'status': Const.DEV_WORK_STATUS_FAULT} ii += 1 result[str(ii)] = status allPorts, usedPorts, usePorts = self.get_port_static_info(result) # 这里存在多线程更新缓存的场景,可能性比较低,但是必须先取出来,然后逐个更新状态,然后再记录缓存 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 ctrInfo.update({ 'allPorts': allPorts, 'usedPorts': usedPorts, 'usePorts': usePorts }) Device.update_dev_control_cache(self.device.devNo, ctrInfo) return result def get_default_port_nums(self): default_num = 10 return default_num 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) if 'allPorts' in ctrInfo and ctrInfo['allPorts'] > 0: allPorts = ctrInfo['allPorts'] else: allPorts = self.get_default_port_nums() statusDict = {} 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} return statusDict def lock_unlock_port(self, port, lock=True): lockStr = '00' if lock else '01' portStr = fill_2_hexByte(hex(int(port)), 2) devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '0A', 'data': portStr + lockStr}) 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'][18::] if data[0:2] == '01': # 表示成功 pass else: 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 active_deactive_port(self, port, active): if active: raise ServiceException({'result': 2, 'description': u'该设备不支持直接打开端口'}) 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, 'needTime': 0, 'leftTime': 0, 'endTime': datetime.datetime.now().strftime(Const.DATETIME_FMT)}) newValue = {str(port): portCtrInfo} Device.update_dev_control_cache(self._device['devNo'], newValue) def stop_charging_port(self, port): portStr = fill_2_hexByte(hex(int(port)), 2) devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '0B', 'data': portStr}) 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'][18::] port = int(data[2:4], 16) leftTime = int(data[4:8], 16) data = {"port": port} # 电量计费模式 if not self._device.get("otherConf", dict()).get("consumeModule", 0): data.update({"remainder_time": leftTime}) else: data.update({"remainder_time": leftTime / 100.0}) return data def stop(self, port=None): return self.stop_charging_port(port) @property def isHaveStopEvent(self): return True # 获取IC卡、投币、最大功率设置 def get_IC_coin_power_config(self): devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '0C', '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'][18::] # maxPower = int(confData[0:4], 16) # 新版本移除maxPower icMoney = int(confData[4:6], 16) if len(confData) > 6: time1 = int(confData[6:10], 16) time2 = int(confData[10:14], 16) time3 = int(confData[14:18], 16) else: time1, time2, time3 = 0, 0, 0 return {'icMoney': icMoney, 'time1': time1, 'time2': time2, 'time3': time3} def set_IC_coin_power_config(self, infoDict): data = '0000' # data += fill_2_hexByte(hex(int(infoDict['maxPower'])), 4) # 新协议移除maxPower 用0000填充 data += fill_2_hexByte(hex(int(infoDict['icMoney'])), 2) data += fill_2_hexByte(hex(int(infoDict['time1'])), 4) data += fill_2_hexByte(hex(int(infoDict['time2'])), 4) data += fill_2_hexByte(hex(int(infoDict['time3'])), 4) devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '08', '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'充电桩忙,无响应,请您稍候再试'}) def set_IC_coin_power_config_elec(self, infoDict): data = '0000' # data += fill_2_hexByte(hex(int(infoDict['maxPower'])), 4) # 新协议移除maxPower 用0000填充 data += fill_2_hexByte(hex(int(infoDict['icMoney'])), 2) data += fill_2_hexByte(hex(int(infoDict['elec1'])), 4) data += fill_2_hexByte(hex(int(infoDict['elec2'])), 4) data += fill_2_hexByte(hex(int(infoDict['elec3'])), 4) devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '08', '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'充电桩忙,无响应,请您稍候再试'}) def get_coin_card_enable(self): devs = Device.get_collection().find({'devNo': self._device['devNo']}) if devs.count == 0: raise ServiceException( {'result': 2, 'description': u'没有找到设备哦'}) return {'putCoins': devs[0].get('otherConf', {}).get('putCoins', False), 'icCard': devs[0].get('otherConf', {}).get('icCard', False)} def set_coin_card_enable(self, infoDict): data = '' if infoDict['putCoins']: data += '01' else: data += '00' if infoDict['icCard']: data += '01' else: data += '00' devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '09', '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'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'}) try: conf = Device.objects.get(devNo=self._device['devNo']).otherConf conf.update({'putCoins': infoDict['putCoins'], 'icCard': infoDict['icCard']}) Device.get_collection().update_one({'devNo': self._device['devNo']}, {'$set': {'otherConf': conf}}) except Exception, e: logger.error('update dev=%s coin enable ic enable e=%s' % (self._device['devNo'], e)) def get_dev_all_settings(self): """ 电川PCB- DC_Crg_10L_C4.0及以上版本 支持全部参数一起查询 多参数消费流程 消费方式 :return: """ result = MessageSender.send( device=self.device, cmd=DeviceCmdCode.OPERATE_DEV_SYNC, payload={ "IMEI": self._device["devNo"], "data": "00", "funCode": "36" } ) rst = result.get("rst") if rst == -1: raise ServiceException({'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'}) elif rst == 1: raise ServiceException({'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'}) elif rst == 0: data = split_str(result.get('data'), lens="4444444242424224442222", startIndex=18, toInt=True) resultDict = { "time1": data[0], "time2": data[1], "time3": data[2], "card1Time": data[3], "card2Time": data[4], "card3Time": data[5], "icMoney": data[6], # 前台单位为角 "cardRefund": bool(data[7]), "power1": data[8], "power1ratio": data[9], "power2": data[10], "power2ratio": data[11], "power3": data[12], "temperatureMax": data[13], "autoStop": bool(data[14]), "fuchongPower": data[15] * 0.1, "fuchongTime": data[16], "noPowerTime": data[17], "chargeFree": data[18], "volume": data[19], "consumeProcess": data[20], "consumeModule": data[21] } try: sensorParam = self.get_sensor_status() resultDict.update(sensorParam) except Exception: pass resultDict['maxPower'] = resultDict['power3'] # 温度开关 if int(resultDict['temperatureMax']) == 255: resultDict['temperatureMaxSwitch'] = '0' resultDict['temperatureMax'] = '--' else: resultDict['temperatureMaxSwitch'] = '1' else: raise ServiceException({'result': 2, 'description': u"数据读取失败,请您稍后再试"}) # 协议上没有不显示 # coinCardEnable = self.get_coin_card_enable() # resultDict['putCoins'] = coinCardEnable['putCoins'] # resultDict['icCard'] = coinCardEnable['icCard'] return resultDict # 获取设备配置参数 def get_dev_setting(self): driverCode = self._device.get("devType", dict()).get("code", "") resultDict = self.get_dev_all_settings() dev = Device.objects.get(devNo=self._device['devNo']) refundProtection = dev.otherConf.get('refundProtection', 0) refundProtectionTime = dev.otherConf.get('refundProtectionTime', 5) cardNoRangeStart = dev.otherConf.get('cardNoRangeStart', '') cardNoRangeEnd = dev.otherConf.get('cardNoRangeEnd', '') # resultDict.update({'volume': volume}) # resultDict.update({'fuchongPower': fuchongPower}) # resultDict.update({'fuchongTime': fuchongTime}) resultDict.update({'driverCode': driverCode}) resultDict.update({'refundProtection': refundProtection}) resultDict.update({'refundProtectionTime': refundProtectionTime}) resultDict.update({'cardNoRangeStart': cardNoRangeStart}) resultDict.update({'cardNoRangeEnd': cardNoRangeEnd}) if resultDict['consumeModule'] == 1: resultDict['elec1'] = round(float(resultDict['time1']) / 100,2) resultDict['elec2'] = round(float(resultDict['time2']) / 100,2) resultDict['elec3'] = round(float(resultDict['time3']) / 100,2) resultDict['card1Elec'] = round(float(resultDict['card1Time']) / 100,2) resultDict['card2Elec'] = round(float(resultDict['card2Time']) / 100,2) resultDict['card3Elec'] = round(float(resultDict['card3Time']) / 100,2) return resultDict # 获取设备配置参数 def set_dev_setting(self, setConf): keys = setConf.keys() if 'putCoins' in keys or 'icCard' in keys: self.set_coin_card_enable(setConf) if 'maxPower' in keys or 'icMoney' in keys or 'time1' in keys or 'time2' in keys or 'time3' in keys: self.set_IC_coin_power_config(setConf) # 暂无此功能 def get_IC_card_password(self): devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '1A', 'data': '00'}, timeout=8) 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'][18::] print confData result = confData[0:2] if result == '00': cardPassword = '' else: cardPassword = str(int(confData[2:12])) return {'cardPassword': cardPassword} def get_fullstop_cardrefund(self): devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '0C', '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'][18::] maxPower = int(confData[0:4], 16) icMoney = int(confData[4:6], 16) if len(confData) > 6: time1 = int(confData[6:10], 16) time2 = int(confData[10:14], 16) time3 = int(confData[14:18], 16) else: time1, time2, time3 = 0, 0, 0 return {'autoStop': True if confData[20:22] == u'01' else False, 'cardRefund': True if confData[18:20] == u'01' else False} def set_fullstop_cardrefund(self, infoDict): data = '' if infoDict['autoStop']: data += '01' else: data += '00' if infoDict['cardRefund']: data += '01' else: data += '00' devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '13', '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'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'}) try: conf = Device.objects.get(devNo=self._device['devNo']).otherConf conf.update({'autoStop': infoDict['autoStop'], 'cardRefund': infoDict['cardRefund']}) Device.get_collection().update({'devNo': self._device['devNo']}, {'$set': {'otherConf': conf}}) except Exception, e: logger.error('update dev=%s coin enable ic enable e=%s' % (self._device['devNo'], e)) def recharge_ic_card_realiable(self, cardNo, money, order_no): # type:(str,RMB,str)->dict data = 'EE1012{}'.format(make_six_bytes_session_id()) cardNo = fill_2_hexByte(hex(int(cardNo)), 8) data += cardNo + fill_2_hexByte(hex(int(money * 10)), 4) + '0001' payload = { 'IMEI': self.device.devNo, 'data': data, 'funCode': '12', 'order_id': order_no, 'order_type': 'ic_recharge'} MessageSender.send_no_wait(device=self.device, cmd=DeviceCmdCode.PASSTHROUGH_OPERATE_DEV_SYNC, payload=payload, timeout=180) # 给实体卡充值 def recharge_card(self, cardNo, money, orderNo=None): # type:(str,RMB)->(dict, RMB) try: data = 'EE1012{}'.format(make_six_bytes_session_id()) cardNo = fill_2_hexByte(hex(int(cardNo)), 8) data += cardNo + fill_2_hexByte(hex(int(money * 10)), 4) + '0001' devInfo = MessageSender.send(device=self.device, cmd=DeviceCmdCode.PASSTHROUGH_OPERATE_DEV_SYNC, payload={'IMEI': self._device['devNo'], 'data': data, 'funCode': '12'}, timeout=180) if devInfo['rst'] != 0: if devInfo['rst'] == ErrorCode.DEVICE_CONN_FAIL: # 离线无法判断是否成功, 认为充值成功, 走售后解决 return { 'result': ErrorCode.DEVICE_CONN_FAIL, 'description': u'当前充电桩正在玩命找网络,请您稍候再试' }, None elif devInfo['rst'] == ErrorCode.BOARD_UART_TIMEOUT: return { 'result': ErrorCode.BOARD_UART_TIMEOUT, 'description': u'当前充电桩忙,无响应,请您稍候再试' }, None else: return { 'result': devInfo['rst'], 'description': u'系统异常' }, None resultData = devInfo['data'] if resultData[4:6] != '16': return { 'result': ErrorCode.PARAMETER_ERROR_TO_BOX, 'description': u'充值返回报文命令码不为16' }, None balance = RMB(int(resultData[26:30], 16)) * Decimal('0.1') result = True if resultData[34:36] == '01' else False if result: return { 'result': ErrorCode.SUCCESS, 'description': '' }, balance else: return { 'result': ErrorCode.IC_RECHARGE_FAIL, 'description': u'充值失败' }, balance except Exception as e: logger.exception(e) return { 'result': ErrorCode.EXCEPTION, 'description': e.message }, None def get_mcu_version(self): devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '25', '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'][18::] mcuVersion = int(confData[0:4], 16) return {'mcuVersion': mcuVersion} def set_freemode_volume_config(self, infoDict): data = '01' if infoDict['chargeFree'] == '1' else '00' data += fill_2_hexByte(hex(int(infoDict.get('volume', 5))), 2) devInfo = MessageSender.send(self.device, self.make_random_cmdcode(), {'IMEI': self._device['devNo'], "funCode": '27', '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'充电桩忙,无响应,请您稍候再试'}) def set_card_time_config(self, infoDict): data = fill_2_hexByte(hex(int(infoDict['card1Time'])), 4) data += fill_2_hexByte(hex(int(infoDict['card2Time'])), 4) data += fill_2_hexByte(hex(int(infoDict['card3Time'])), 4) devInfo = MessageSender.send(self.device, self.make_random_cmdcode(), {'IMEI': self._device['devNo'], "funCode": '29', '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'充电桩忙,无响应,请您稍候再试'}) def set_card_elec_config(self, infoDict): data = fill_2_hexByte(hex(int(infoDict['card1Elec'])), 4) data += fill_2_hexByte(hex(int(infoDict['card2Elec'])), 4) data += fill_2_hexByte(hex(int(infoDict['card3Elec'])), 4) devInfo = MessageSender.send(self.device, self.make_random_cmdcode(), {'IMEI': self._device['devNo'], "funCode": '29', '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'充电桩忙,无响应,请您稍候再试'}) # 暂无此功能 def set_IC_card_password(self, infoDict): passwordStr = str(infoDict['cardPassword']) if len(passwordStr) < 6 or len(passwordStr) > 12: raise ServiceException({'result': 2, 'description': u'密码长度不合法, 请重新设置'}) data = passwordStr.rjust(12, '0') devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], 'funCode': '18', 'data': data}, timeout=120) # 暂无此功能 def set_IC_card_range(self, infoDict): cardNoRangeStart = str(infoDict['cardNoRangeStart']) cardNoRangeEnd = str(infoDict['cardNoRangeEnd']) if int(cardNoRangeStart) > 4294967295 or int(cardNoRangeEnd) > 4294967295 or int(cardNoRangeStart) > int( cardNoRangeEnd): raise ServiceException({'result': 2, 'description': u'卡号设置不合法, 请重新设置'}) cardNoRangeStart = cardNoRangeStart.rjust(10, '0') cardNoRangeEnd = cardNoRangeEnd.rjust(10, '0') data = cardNoRangeStart + cardNoRangeEnd devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], 'funCode': '19', 'data': data}, timeout=120) dev = Device.objects.get(devNo=self._device['devNo']) dev.otherConf.update({'cardNoRangeStart': str(infoDict['cardNoRangeStart'])}) dev.otherConf.update({'cardNoRangeEnd': str(infoDict['cardNoRangeEnd'])}) dev.save() def set_device_function(self, request, lastSetConf): if request.POST.has_key('putCoins'): # putCoins = True if request.POST.get('putCoins') == 'true' else False putCoins = request.POST.get("putCoins", False) lastSetConf.update({'putCoins': putCoins}) self.set_coin_card_enable(lastSetConf) if request.POST.has_key('icCard'): # icCard = True if request.POST.get('icCard') == 'true' else False icCard = request.POST.get("icCard", False) lastSetConf.update({'icCard': icCard}) self.set_coin_card_enable(lastSetConf) if request.POST.has_key('autoStop'): # autoStop = True if request.POST.get('autoStop') == 'true' else False autoStop = request.POST.get("autoStop", False) lastSetConf.update({'autoStop': autoStop}) self.set_fullstop_cardrefund(lastSetConf) if request.POST.has_key('cardRefund'): # cardRefund = True if request.POST.get('cardRefund') == 'true' else False cardRefund = request.POST.get("cardRefund", False) lastSetConf.update({'cardRefund': cardRefund}) self.set_fullstop_cardrefund(lastSetConf) if request.POST.has_key('chargeFree'): # chargeFree = True if request.POST.get('chargeFree') == 'true' else False chargeFree = request.POST.get("chargeFree", False) lastSetConf.update({'chargeFree': chargeFree}) self.set_freemode_volume_config(lastSetConf) if request.POST.has_key('reboot'): reboot = request.POST.get("reboot", False) if reboot: self.reboot_device() def set_device_function_param(self, request, lastSetConf): if request.POST.has_key('time1') and request.POST.has_key('time2') and request.POST.has_key('time3'): lastSetConf.update({'icMoney': int(request.POST.get('icMoney', 0))}) lastSetConf.update({'time1': int(request.POST.get('time1', 0))}) lastSetConf.update({'time2': int(request.POST.get('time2', 0))}) lastSetConf.update({'time3': int(request.POST.get('time3', 0))}) self.set_IC_coin_power_config(lastSetConf) if 'elec1' in request.POST and 'elec2' in request.POST and 'elec3' in request.POST: lastSetConf.update({'icMoney': int(request.POST.get('icMoney', 0))}) lastSetConf.update({'elec1': int(float(request.POST.get('elec1', 0)) * 100)}) lastSetConf.update({'elec2': int(float(request.POST.get('elec2', 0)) * 100)}) lastSetConf.update({'elec3': int(float(request.POST.get('elec3', 0)) * 100)}) self.set_IC_coin_power_config_elec(lastSetConf) if request.POST.has_key('power1') and request.POST.has_key('power1ratio'): lastSetConf.update({'power1': int(request.POST.get('power1', 0))}) lastSetConf.update({'power1ratio': int(request.POST.get('power1ratio', 0))}) lastSetConf.update({'power2': int(request.POST.get('power2', 0))}) lastSetConf.update({'power2ratio': int(request.POST.get('power2ratio', 0))}) lastSetConf.update({'power3': int(request.POST.get('power3', 0))}) lastSetConf.update({'power3ratio': int(request.POST.get('power3ratio', 0))}) self.set_power_level(lastSetConf) # TODO 最大功率就是三档的功率 if request.POST.has_key('maxPower'): if int(lastSetConf.get('maxPower', 0)) != int(request.POST.get('maxPower', 0)): lastSetConf.update({'power3': int(request.POST.get('maxPower', 0))}) self.set_power_level(lastSetConf) if request.POST.has_key('card1Time') and request.POST.has_key('card2Time') and request.POST.has_key( 'card3Time'): lastSetConf.update({'card1Time': int(request.POST.get('card1Time', 0))}) lastSetConf.update({'card2Time': int(request.POST.get('card2Time', 0))}) lastSetConf.update({'card3Time': int(request.POST.get('card3Time', 0))}) self.set_card_time_config(lastSetConf) if 'card1Elec' in request.POST and 'card2Elec' in request.POST and 'card3Elec' in request.POST: lastSetConf.update({'card1Elec': int(float(request.POST.get('card1Elec', 0)) * 100)}) lastSetConf.update({'card2Elec': int(float(request.POST.get('card2Elec', 0)) * 100)}) lastSetConf.update({'card3Elec': int(float(request.POST.get('card3Elec', 0)) * 100)}) self.set_card_elec_config(lastSetConf) if 'fuchongPower' in request.POST and 'fuchongTime' in request.POST: fuchongPower = 0 if request.POST.get('fuchongPower') == '' else int(request.POST.get('fuchongPower')) fuchongTime = 0 if request.POST.get('fuchongTime') == '' else int(request.POST.get('fuchongTime')) noPowerTime = 0 if request.POST.get('noPowerTime') == '' else int(request.POST.get('noPowerTime')) lastSetConf.update({'fuchongPower': fuchongPower}) lastSetConf.update({'fuchongTime': fuchongTime}) lastSetConf.update({'noPowerTime': noPowerTime}) self.set_float_charge_settings(lastSetConf) if "consumeProcess" in request.POST and "consumeModule" in request.POST: lastSetConf.update({"consumeProcess": request.POST.get("consumeProcess")}) lastSetConf.update({"consumeModule": request.POST.get("consumeModule")}) self.set_consume_module(lastSetConf) if request.POST.has_key('refundProtection'): lastSetConf.update({'refundProtection': int(request.POST.get('refundProtection', 0))}) dev = Device.objects.get(devNo=self._device['devNo']) dev.otherConf.update({'refundProtection': lastSetConf['refundProtection']}) dev.save() if request.POST.has_key('refundProtectionTime'): lastSetConf.update({'refundProtectionTime': int(request.POST.get('refundProtectionTime', 5))}) dev = Device.objects.get(devNo=self._device['devNo']) dev.otherConf.update({'refundProtectionTime': lastSetConf['refundProtectionTime']}) dev.save() if request.POST.has_key('volume'): if request.POST.get('volume') != lastSetConf['volume']: lastSetConf.update({'volume': int(request.POST.get('volume', 0))}) self.set_freemode_volume_config(lastSetConf) if request.POST.has_key('temperatureMax') and request.POST.has_key('temperatureMaxSwitch'): temperatureMaxSwitch = request.POST.get('temperatureMaxSwitch') temperatureMax = request.POST.get('temperatureMax') s1 = temperatureMaxSwitch != str(lastSetConf['temperatureMaxSwitch']) s2 = temperatureMax != str(lastSetConf['temperatureMax']) if s1: lastSetConf.update({'temperatureMaxSwitch': int(request.POST.get('temperatureMaxSwitch', 0))}) lastSetConf.update({'temperatureMax': int(request.POST.get('temperatureMax', 0))}) self.set_warning_temperature(lastSetConf) else: if s2: if temperatureMaxSwitch == '0': raise ServiceException({'result': 2, 'description': u'温控阀值开关需要打开'}) else: lastSetConf.update({'temperatureMaxSwitch': int(request.POST.get('temperatureMaxSwitch', 0))}) lastSetConf.update({'temperatureMax': int(request.POST.get('temperatureMax', 0))}) self.set_warning_temperature(lastSetConf) if 'cardPassword' in request.POST: lastSetConf.update({'cardPassword': int(request.POST.get('cardPassword', 0))}) self.set_IC_card_password(lastSetConf) if 'cardNoRangeStart' in request.POST and 'cardNoRangeEnd' in request.POST: lastSetConf.update({'cardNoRangeStart': int(request.POST.get('cardNoRangeStart', 0))}) lastSetConf.update({'cardNoRangeEnd': int(request.POST.get('cardNoRangeEnd', 0))}) self.set_IC_card_range(lastSetConf) def response_card_status(self, cardNo, balance, status): data = 'EE1123{}'.format(make_six_bytes_session_id()) + fill_2_hexByte(hex(int(cardNo)), 4) + fill_2_hexByte( hex(int(balance * 10)), 4) + 'AA33' + status devInfo = MessageSender.send(self.device, DeviceCmdCode.PASSTHROUGH_OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '23', '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: # 等于1的时候,说明服务器和远程模块通讯OK,响应已经收到,不需要异常处理 raise ServiceException( {'result': 2, 'description': u'充电桩没有响应,可能是版本不对或者串口接线不良'}) resultData = devInfo['data'] if resultData[4:6] != '23': return {'status': '00'} backCardNo = int(resultData[18:26], 16) if backCardNo != int(cardNo): return {'status': '00'} return {'status': resultData[26:28]} def set_consume_module(self, infoDict): """ 设置消费模式以及消费流程 包含两个参数 consumeProcess 消费流程 consumeModule 消费方式 :param infoDict: :return: """ consumeProcess = infoDict.get("consumeProcess", 0) consumeModule = infoDict.get("consumeModule", 0) consumeProcessHex = fill_2_hexByte(hex(int(consumeProcess)), 2) consumeModuleHex = fill_2_hexByte(hex(int(consumeModule)), 2) result = MessageSender.send( device=self.device, cmd=DeviceCmdCode.OPERATE_DEV_SYNC, payload={ "IMEI": self._device["devNo"], "funCode": "38", "data": consumeProcessHex + consumeModuleHex } ) rst = result.get("rst") data = result.get("data") if rst == 0 and data[-4:-2] == "01": # OtherConf中也保留一份 方便启动的时候随时查询 otherConf = self._device.get("otherConf", dict()) otherConf.update({ "consumeProcess": int(consumeProcess), "consumeModule": int(consumeModule) }) Device.objects.filter(devNo=self._device["devNo"]).update(otherConf=otherConf) Device.invalid_device_cache(self._device["devNo"]) return elif data[-4: -2] != "01": raise ServiceException({'result': 2, 'description': u'设置失败,请重新试试'}) elif rst == -1: raise ServiceException({'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'}) elif rst == 1: raise ServiceException({'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试'}) else: raise ServiceException({'result': 2, 'description': u'未知错误'}) def reboot_device(self): devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '40', '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'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'}) # 查询设备温度 烟雾报警器状态 设备电压 def get_sensor_status(self): devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '35', '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'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'}) temperature = '%s' % int(devInfo['data'][18:20], 16) smoke = '%s' % int(devInfo['data'][20:22], 16) voltage = '%s' % int(devInfo['data'][22:26], 16) isYangan = True if smoke == '1' else False return {'temperature': temperature, 'isYangan': isYangan, 'voltage': voltage} # 查看版本 def get_versions(self): devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '2B', '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'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'}) data = devInfo['data'][20:-2] import re data = re.findall(r'..', data) data = map(lambda x: chr(int(x, 16)), data) versions = ''.join(data) return {'versions': versions} # 浮充功率,浮充时间,充电检测时间 def set_float_charge_settings(self, infoDict): fuchongPower = infoDict.get('fuchongPower', None) fuchongTime = infoDict.get('fuchongTime', None) noPowerTime = infoDict.get('noPowerTime', None) if not fuchongPower or not fuchongTime or not noPowerTime: raise ServiceException({'result': 2, 'description': u'参数不完整'}) if float(fuchongTime) < 120: raise ServiceException({'result': 2, 'description': u'浮充检测时间最小值为120'}) if float(fuchongPower) < 0 or float(fuchongPower) > 99: raise ServiceException({'result': 2, 'description': u'浮充功率检为0-99W'}) if float(noPowerTime) < 5 or float(noPowerTime) > 999: raise ServiceException({'result': 2, 'description': u'充电拔出检测时间未5-999秒'}) floatPowerHex = fill_2_hexByte(hex(int(fuchongPower) * 10), 4) floatTimeHex = fill_2_hexByte(hex(int(fuchongTime)), 4) noPowerTimeHex = fill_2_hexByte(hex(int(noPowerTime)), 4) result = MessageSender.send( device=self.device, cmd=DeviceCmdCode.OPERATE_DEV_SYNC, payload={ "IMEI": self._device["devNo"], "funCode": "37", "data": floatPowerHex + floatTimeHex + noPowerTimeHex } ) rst = result.get("rst") data = result.get("data") if rst == 0 and data[-4:-2] == "01": dev = Device.objects.get(devNo=self._device['devNo']) dev.otherConf.update({'fuchongPower': fuchongPower}) dev.otherConf.update({'fuchongTime': fuchongTime}) dev.otherConf.update({'noPowerTime': noPowerTime}) dev.save() return elif data[-4: -2] != "01": raise ServiceException({'result': 2, 'description': u'设置失败,请重新试试'}) elif rst == -1: raise ServiceException({'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'}) elif rst == 1: raise ServiceException({'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试'}) else: raise ServiceException({'result': 2, 'description': u'未知错误'}) # 设置温度阀值 def set_warning_temperature(self, infoDict): temperatureMaxSwitch = infoDict.get('temperatureMaxSwitch') if temperatureMaxSwitch == 0: temperatureMax = 255 else: temperatureMax = infoDict.get('temperatureMax', 60) if float(temperatureMax) < 0 or float(temperatureMax) > 254: raise ServiceException({'result': 2, 'description': u'报警温度范围为0-254'}) temperatureHex = fill_2_hexByte(hex(int(temperatureMax)), 2) result = MessageSender.send( device=self.device, cmd=DeviceCmdCode.OPERATE_DEV_SYNC, payload={ "IMEI": self._device["devNo"], "funCode": "43", "data": temperatureHex } ) rst = result.get("rst") data = result.get("data") if rst == 0 and data[-4:-2] == "01": return elif data[-4: -2] != "01": raise ServiceException({'result': 2, 'description': u'设置失败,请重新试试'}) elif rst == -1: raise ServiceException({'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'}) elif rst == 1: raise ServiceException({'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试'}) else: raise ServiceException({'result': 2, 'description': u'未知错误'}) # 设置3档计费功率、比例 def set_power_level(self, infoDict): power1 = infoDict.get('power1') power1ratio = infoDict.get('power1ratio') power2 = infoDict.get('power2') power2ratio = infoDict.get('power2ratio') power3 = infoDict.get('power3') if float(power1) < 0 or float(power2) < float(power1) or float(power3) < float(power2): raise ServiceException({'result': 2, 'description': u'功率参数填写错误'}) power1Hex = fill_2_hexByte(hex(int(power1)), 4) power1ratioHex = fill_2_hexByte(hex(int(power1ratio)), 2) power2Hex = fill_2_hexByte(hex(int(power2)), 4) power2ratioHex = fill_2_hexByte(hex(int(power2ratio)), 2) power3Hex = fill_2_hexByte(hex(int(power3)), 4) result = MessageSender.send( device=self.device, cmd=DeviceCmdCode.OPERATE_DEV_SYNC, payload={ "IMEI": self._device["devNo"], "funCode": "45", "data": power1Hex + power1ratioHex + power2Hex + power2ratioHex + power3Hex } ) rst = result.get("rst") data = result.get("data") if rst == 0 and data[-4:-2] == "01": return elif data[-4: -2] != "01": raise ServiceException({'result': 2, 'description': u'设置失败,请重新试试'}) elif rst == -1: raise ServiceException({'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'}) elif rst == 1: raise ServiceException({'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试'}) else: raise ServiceException({'result': 2, 'description': u'未知错误'}) # 进入升级模式 ## 禁用 def set_update_model(self): devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], "funCode": '26', 'data': '01'}) 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'][18:] print data verNum = data[:4] updateStatus = data[4:] return {'verNum': verNum, 'updateStatus': updateStatus} def format_upload_power(self, power): return float(power / 10) def get_server_setting(self): if self.device.devTypeCode == Const.DEVICE_TYPE_CODE_CHARGING_DIANCHUAN_HIGH: default_refund_protection_time = 3 else: default_refund_protection_time = 5 return { 'refundProtectionTime': int( self.device.get_other_conf_item('refundProtectionTime', default_refund_protection_time)), } def set_server_setting(self, payload): refundProtectionTime = int(payload['refundProtectionTime']) self.device.update_other_conf(refundProtectionTime=refundProtectionTime)