123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import datetime
- import logging
- import time
- from decimal import Decimal
- from typing import Optional
- from apilib.monetary import RMB
- from apilib.utils_datetime import timestamp_to_dt
- from apps.web.constant import DeviceCmdCode, MQTT_TIMEOUT, Const, ErrorCode
- 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__)
- class Aiyaxin5Box(SmartBox):
- def __init__(self, device):
- super(Aiyaxin5Box, self).__init__(device)
- def translate_funcode(self, funCode):
- funCodeDict = {
- '01': u'获取端口数量',
- '02': u'移动支付',
- '0C': u'获取设备设置',
- '08': u'设置设备参数',
- '06': u'获取设备端口详情',
- '07': u'获取刷卡投币统计数据',
- '09': u'设置刷卡投币使能',
- '0A': u'端口使能',
- '0B': u'端口关闭',
- '16': u'设置设备参数',
- '20': u'设备重启',
- '13': u'设置卡充满退费',
- '15': u'获取功率费率配置',
- '14': u'设置功率费率配置',
- '12': u'充值',
- }
- 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'卡充值',
- '21': u'电流上报',
- '22': u'温度上报',
- '23': u'烟感上报',
- }
- return cmdDict.get(cmdCode, '')
- def test(self, coins):
- hexPort = fill_2_hexByte(hex(int(1)), 2)
- hexTime = fill_2_hexByte(hex(5))
- devInfo = MessageSender.send(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- 'IMEI': self._device['devNo'],
- "funCode": '02',
- 'data': hexPort + '0000' + hexTime
- })
- return devInfo
- 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'请您选择合适的充电线路'})
- port = hex(int(attachParas['chargeIndex']))
- hexPort = fill_2_hexByte(port, 2)
- needTime = int(package['time'])
- unit = package.get('unit', u'分钟')
- if unit == u'小时':
- needTime = int(package['time']) * 60
- elif unit == u'天':
- needTime = int(package['time']) * 1440
- coins = float(package['coins'])
- hexTime = fill_2_hexByte(hex(needTime))
- 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)
- 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'][16::]
- usePort = int(attachParas['chargeIndex'])
- result = data[4:6]
- 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'该端口正在使用中'})
- start_timestamp = int(time.time())
- devInfo['finishedTime'] = start_timestamp + needTime * 60
- Device.update_dev_control_cache(
- self._device['devNo'],
- {
- str(usePort): {
- 'startTime': timestamp_to_dt(start_timestamp).strftime('%Y-%m-%d %H:%M:%S'),
- 'status': Const.DEV_WORK_STATUS_WORKING,
- 'finishedTime': devInfo['finishedTime'],
- 'coins': coins,
- 'needTime': needTime,
- 'isStart': True,
- 'openId': openId,
- 'refunded': False,
- 'vCardId': self._vcard_id
- }
- })
- return devInfo
- def analyze_event_data(self, data):
- cmdCode = data[4:6]
- if cmdCode in ['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'已经开始充电,但是没有检测到电池,建议检查插座是否连接正常'
- else:
- desc = u''
- return {'status': Const.DEV_WORK_STATUS_IDLE, 'cmdCode': cmdCode, 'port': port, 'leftTime': leftTime,
- 'reason': desc, 'isStart': False}
- elif cmdCode == '0D':
- port = int(data[16:18], 16)
- errCode = int(data[18:20], 16)
- 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 errCode == '03':
- return {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'电源故障', 'cmdCode': cmdCode, 'port': port,
- 'FaultCode': errCode}
- elif cmdCode == '03':
- coin = int(data[18:20], 16)
- return {'cmdCode': cmdCode, 'coins': coin}
- elif cmdCode == '11':
- cardNo = data[18:26]
- cardNo = str(int(cardNo, 16))
- money = int(data[26:28], 16) / 10.0
- balance = int(data[28:32], 16) / 10.0
- port = int(data[36:38], 16)
- status = data[38:40]
- return {'cmdCode': cmdCode, 'cardNo': cardNo, 'coins': money, 'balance': balance, 'status': status,
- 'port': port}
- 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 == '04': # 旧版本的刷卡报文
- money = int(data[18:20], 16)
- return {'cmdCode': cmdCode, 'money': money}
- elif cmdCode == '30': # 宇泽家的,主动上报电流
- portData = data[18:48]
- port1 = int(portData[2:4])
- port2 = int(portData[4:6])
- port3 = int(portData[6:8])
- port4 = int(portData[8:10])
- port5 = int(portData[10:12])
- port6 = int(portData[12:14])
- port7 = int(portData[14:16])
- port8 = int(portData[16:18])
- port9 = int(portData[18:20])
- port10 = int(portData[20:22])
- device = int(portData[22:24])
- return {
- 'cmdCode': cmdCode,
- 'port1': port1,
- 'port2': port2,
- 'port3': port3,
- 'port4': port4,
- 'port5': port5,
- 'port6': port6,
- 'port7': port7,
- 'port8': port8,
- 'port9': port9,
- 'port10': port10,
- 'device': device,
- }
- elif cmdCode == '31': # 宇泽家的,主动上报温度
- portData = data[18:48]
- port1 = int(portData[2:4])
- port2 = int(portData[4:6])
- port3 = int(portData[6:8])
- port4 = int(portData[8:10])
- port5 = int(portData[10:12])
- port6 = int(portData[12:14])
- port7 = int(portData[14:16])
- port8 = int(portData[16:18])
- port9 = int(portData[18:20])
- port10 = int(portData[20:22])
- device = int(portData[22:24])
- return {
- 'cmdCode': cmdCode,
- 'port1': port1,
- 'port2': port2,
- 'port3': port3,
- 'port4': port4,
- 'port5': port5,
- 'port6': port6,
- 'port7': port7,
- 'port8': port8,
- 'port9': port9,
- 'port10': port10,
- 'device': device,
- }
- elif cmdCode == '32': # 宇泽家的,主动上报温度
- if data[18:20] == '01':
- return {'isYangan': True, 'cmdCode': cmdCode}
- else:
- return {'isYangan': False, 'cmdCode': cmdCode}
- def get_dev_consume_count(self):
- devInfo = MessageSender.send(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- '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'][16::]
- cardFee = int(data[2:6], 16) / 10.0 # 以角为单位
- coinFee = int(data[6:10], 16) # 以元为单位
- return {'cardFee': cardFee, 'coinFee': coinFee}
- def get_port_info(self, line):
- data = fill_2_hexByte(hex(int(line)), 2)
- devInfo = MessageSender.send(device = self.device,
- cmd = self.make_random_cmdcode(),
- payload = {
- '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'][16::]
- leftTime = int(data[4:8], 16)
- if data[8:12] == 'FFFF':
- power = 0
- else:
- power = int(data[8:12], 16) / 10.0
- return {'port': line, 'leftTime': leftTime, 'power': power}
- # 访问设备电流
- def get_port_elec_from_dev(self):
- devInfo = MessageSender.send(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- 'IMEI': self._device['devNo'], "funCode": '21', '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'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
- portData = devInfo['data'][18::]
- port1 = int(portData[2:4], 16)
- port2 = int(portData[4:6], 16)
- port3 = int(portData[6:8], 16)
- port4 = int(portData[8:10], 16)
- port5 = int(portData[10:12], 16)
- port6 = int(portData[12:14], 16)
- port7 = int(portData[14:16], 16)
- port8 = int(portData[16:18], 16)
- port9 = int(portData[18:20], 16)
- port10 = int(portData[20:22], 16)
- device = int(portData[22:24], 16)
- return {
- 'port1': port1,
- 'port2': port2,
- 'port3': port3,
- 'port4': port4,
- 'port5': port5,
- 'port6': port6,
- 'port7': port7,
- 'port8': port8,
- 'port9': port9,
- 'port10': port10,
- 'device': device,
- }
- # 访问设备
- def get_port_temperature_from_dev(self):
- devInfo = MessageSender.send(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- 'IMEI': self._device['devNo'], "funCode": '22', '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'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
- portData = devInfo['data'][18::]
- port1 = int(portData[2:4], 16)
- port2 = int(portData[4:6], 16)
- port3 = int(portData[6:8], 16)
- port4 = int(portData[8:10], 16)
- port5 = int(portData[10:12], 16)
- port6 = int(portData[12:14], 16)
- port7 = int(portData[14:16], 16)
- port8 = int(portData[16:18], 16)
- port9 = int(portData[18:20], 16)
- port10 = int(portData[20:22], 16)
- device = int(portData[22:24], 16)
- return {
- 'port1': port1,
- 'port2': port2,
- 'port3': port3,
- 'port4': port4,
- 'port5': port5,
- 'port6': port6,
- 'port7': port7,
- 'port8': port8,
- 'port9': port9,
- 'port10': port10,
- 'device': device,
- }
- # 访问设备,获取设备端口信息
- def get_port_status_from_dev(self):
- devInfo = MessageSender.send(device = self.device,
- cmd = self.make_random_cmdcode(),
- payload = {
- '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'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
- data = devInfo['data'][16::]
- result = {}
- portNum = int(data[2:4], 16)
- portData = data[4::]
- 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}
- ii += 1
- result[str(ii)] = status
- 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'])
- if not ctrInfo.has_key('allPorts'):
- self.get_port_status_from_dev()
- ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
- allPorts = ctrInfo.get('allPorts', 10)
- 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}
- 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 lock_unlock_port(self, port, lock = True):
- lockStr = '00' if lock else '01'
- portStr = fill_2_hexByte(hex(int(port)), 2)
- devInfo = MessageSender.send(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- '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(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- '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'][16::]
- port = int(data[2:4], 16)
- leftTime = int(data[4:8], 16)
- return {'port': port, 'leftTime': leftTime}
- # 获取IC卡、投币、最大功率设置
- def get_IC_coin_power_config(self):
- devInfo = MessageSender.send(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- '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 {'maxPower': maxPower, 'icMoney': icMoney, 'time1': time1,
- 'time2': time2, 'time3': time3}
- def set_IC_coin_power_config(self, infoDict):
- data = ''
- data += fill_2_hexByte(hex(int(infoDict['maxPower'])), 4)
- 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(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- '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(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- '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({'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_setting(self):
- resultDict = self.get_IC_coin_power_config()
- tempDict = self.get_coin_card_enable()
- resultDict.update(tempDict)
- tempDict = self.get_gear_conf()
- resultDict.update(tempDict)
- tempDict = self.get_fullstop_cardrefund()
- resultDict.update(tempDict)
- # tempDict = self.get_freemode_volume_andsoon_config()
- # resultDict.update(tempDict)
- # tempDict = self.get_mcu_version()
- # resultDict.update(tempDict)
- 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_fullstop_cardrefund(self):
- try:
- dev = Device.objects.get(devNo = self._device['devNo'])
- except Exception, e:
- logger.error('get dev error = %s' % e)
- return {'autoStop': False, 'cardRefund': False}
- return {'autoStop': dev['otherConf'].get('autoStop', False),
- 'cardRefund': dev['otherConf'].get('cardRefund', 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(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- '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 get_gear_conf(self):
- resultDict = {'power1': 0, 'power1ratio': 0, 'power2': 0,
- 'power2ratio': 0, 'power3': 0, 'power3ratio': 0}
- devInfo = MessageSender.send(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- 'IMEI': self._device['devNo'], "funCode": '15', '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:
- return resultDict
- confData = devInfo['data'][18::]
- power1 = int(confData[0:4], 16)
- power1ratio = int(confData[4:6], 16) * 10
- power2 = int(confData[6:10], 16)
- power2ratio = int(confData[10:12], 16) * 10
- power3 = int(confData[12:16], 16)
- power3ratio = int(confData[16:18], 16) * 10
- result = {
- 'power1': power1, 'power1ratio': power1ratio, 'power2': power2,
- 'power2ratio': power2ratio, 'power3': power3, 'power3ratio': power3ratio
- }
- if len(confData) > 31:
- power4 = int(confData[18:22], 16)
- power4ratio = int(confData[22:24], 16) * 10
- power5 = int(confData[24:28], 16)
- power5ratio = int(confData[28:30], 16) * 10
- result.update({'power4': power4, 'power4ratio': power4ratio,
- 'power5': power5, 'power5ratio': power5ratio})
- return result
- def set_gear_conf(self, infoDict):
- data = ''
- data += fill_2_hexByte(hex(int(infoDict['power1'])), 4)
- data += fill_2_hexByte(hex(int(infoDict['power1ratio']) / 10), 2)
- data += fill_2_hexByte(hex(int(infoDict['power2'])), 4)
- data += fill_2_hexByte(hex(int(infoDict['power2ratio']) / 10), 2)
- data += fill_2_hexByte(hex(int(infoDict['power3'])), 4)
- data += fill_2_hexByte(hex(int(infoDict['power3ratio']) / 10), 2)
- if infoDict.has_key('power4'):
- data += fill_2_hexByte(hex(int(infoDict['power4'])), 4)
- data += fill_2_hexByte(hex(int(infoDict['power4ratio']) / 10), 2)
- data += fill_2_hexByte(hex(int(infoDict['power5'])), 4)
- data += fill_2_hexByte(hex(int(infoDict['power5ratio']) / 10), 2)
- devInfo = MessageSender.send(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- 'IMEI': self._device['devNo'], "funCode": '14', '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 recharge_card(self, cardNo, money, orderNo = None):
- # type:(str,RMB,Optional[str])->(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 = MQTT_TIMEOUT.LONGEST)
- 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
- # 获取IC卡、投币、最大功率设置
- def get_freemode_volume_andsoon_config(self):
- devInfo = MessageSender.send(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- 'IMEI': self._device['devNo'], "funCode": '2A', '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::]
- card1Time = int(confData[0:4], 16)
- card2Time = int(confData[4:8], 16)
- card3Time = int(confData[8:12], 16)
- chargeFree = True if confData[12:14] == '01' else False
- volume = int(confData[14:16], 16)
- FuchongPower = int(confData[16:20], 16)
- FuchongTime = int(confData[20:24], 16)
- return {'card1Time': card1Time, 'card2Time': card2Time, 'card3Time': card3Time,
- 'chargeFree': chargeFree, 'volume': volume, 'FuchongPower': FuchongPower,
- 'FuchongTime': FuchongTime}
- def get_mcu_version(self):
- devInfo = MessageSender.send(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- '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'] else '00'
- data += fill_2_hexByte(hex(int(infoDict.get('volume', 5))), 2)
- devInfo = MessageSender.send(device = self.device,
- cmd = self.make_random_cmdcode(),
- payload = {
- '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_fuchong_config(self, infoDict):
- data = fill_2_hexByte(hex(int(infoDict['fuchongPower'])), 4)
- data += fill_2_hexByte(hex(int(infoDict['fuchongTime'])), 4)
- devInfo = MessageSender.send(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- 'IMEI': self._device['devNo'], "funCode": '28', '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(device = self.device,
- cmd = self.make_random_cmdcode(),
- payload = {
- '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_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")
- 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)
- def set_device_function_param(self, request, lastSetConf):
- if request.POST.has_key('maxPower') and request.POST.has_key('icMoney'):
- lastSetConf.update({'maxPower': int(request.POST.get('maxPower', 0))})
- lastSetConf.update({'icMoney': int(request.POST.get('icMoney', 0))})
- self.set_IC_coin_power_config(lastSetConf)
- if request.POST.has_key('time1') and request.POST.has_key('time2') and request.POST.has_key('time3'):
- 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 request.POST.has_key('power1'):
- lastSetConf.update({'power1': int(request.POST.get('power1', 0))})
- lastSetConf.update({'power2': int(request.POST.get('power2', 0))})
- lastSetConf.update({'power3': int(request.POST.get('power3', 0))})
- lastSetConf.update({'power4': int(request.POST.get('power4', 0))})
- lastSetConf.update({'power5': int(request.POST.get('power5', 0))})
- self.set_gear_conf(lastSetConf)
- if request.POST.has_key('power1ratio'):
- lastSetConf.update({'power1ratio': int(request.POST.get('power1ratio', 0))})
- lastSetConf.update({'power2ratio': int(request.POST.get('power2ratio', 0))})
- lastSetConf.update({'power3ratio': int(request.POST.get('power3ratio', 0))})
- lastSetConf.update({'power4ratio': int(request.POST.get('power4ratio', 0))})
- lastSetConf.update({'power5ratio': int(request.POST.get('power5ratio', 0))})
- self.set_gear_conf(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 request.POST.has_key('fuchongPower') and request.POST.has_key('fuChongTime'):
- lastSetConf.update({'fuchongPower': int(request.POST.get('fuchongPower', 0))})
- lastSetConf.update({'fuChongTime': int(request.POST.get('fuChongTime', 0))})
- self.set_fuchong_config(lastSetConf)
|