123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import datetime
- import logging
- import re
- import threading
- import time
- from decimal import Decimal
- from django.core.cache import cache
- from apilib.monetary import RMB
- from apilib.utils_AES import EncryptDate
- from apps import serviceCache
- from apps.web.common.proxy import ClientConsumeModelProxy
- from apps.web.constant import DeviceCmdCode, ErrorCode, Const, MQTT_TIMEOUT
- from apps.web.core.adapter.base import SmartBox
- from apps.web.core.exceptions import ServiceException, DeviceNetworkTimeoutError, StartDeviceError, UartTimeoutError
- from apps.web.core.networking import MessageSender
- from apps.web.device.models import Device, DeviceType
- from apps.web.user.models import ServiceProgress, Card, MyUser, ConsumeRecord
- cardKey = 'FR4e1OFCnDdrYA7u'
- logger = logging.getLogger(__name__)
- class ChargingWeiFuLeCarBox(SmartBox):
- def __init__(self, device):
- super(ChargingWeiFuLeCarBox, self).__init__(device)
- self.PORT = '1'
- def analyze_event_data(self, device_data):
- uart_data = device_data.get('data')
- if not uart_data:
- return
- fun_code = uart_data.get('fun_code')
- if not fun_code:
- return
- billingType = 'elec'
- cmd = fun_code
- if cmd == '32':
- return uart_data.get('order')
- def disable_app_device(self, switch=True):
- # type:(bool) -> None
- otherConf = self.device.get('otherConf', {})
- otherConf['disableDevice'] = switch
- Device.objects.filter(devNo=self.device['devNo']).update(otherConf=otherConf)
- Device.invalid_device_cache(self.device['devNo'])
- @staticmethod
- def check_params_range(params, minData=None, maxData=None, desc=''):
- # type:(str,float,float,str) -> str
- """
- 检查参数,返回字符串参数
- """
- if params is None:
- raise ServiceException({'result': 2, 'description': u'参数错误.'})
- if not isinstance(params, Decimal):
- params = Decimal(params)
- if not minData and maxData:
- if not isinstance(maxData, Decimal):
- maxData = Decimal(maxData)
- if params <= maxData:
- return '%g' % params
- else:
- raise ServiceException({'result': 2, 'description': u'%s超出可选范围,可选最大值为%g' % (desc, maxData)})
- if not maxData and minData:
- if not isinstance(minData, Decimal):
- minData = Decimal(minData)
- if minData <= params:
- return '%g' % params
- else:
- raise ServiceException({'result': 2, 'description': u'%s超出可选范围,可选最小值为%g' % (desc, minData)})
- if not minData and not maxData:
- return '%g' % params
- else:
- if not isinstance(minData, Decimal):
- minData = Decimal(minData)
- if not isinstance(maxData, Decimal):
- maxData = Decimal(maxData)
- if minData <= params <= maxData:
- return '%g' % params
- else:
- raise ServiceException(
- {'result': 2, 'description': u'%s参数超出可选范围,可取范围为%g-%g' % (desc, minData, maxData)})
- def send_mqtt(self, data=None, cmd=DeviceCmdCode.OPERATE_DEV_SYNC, otherData=None):
- """
- 发送mqtt 指令默认210 返回data
- """
- if data:
- payload = {'data': data}
- else:
- payload = otherData
- result = MessageSender.send(self.device, cmd,
- payload)
- if 'rst' in result and result['rst'] != 0:
- if result['rst'] == -1:
- raise ServiceException(
- {'result': 2, 'description': u'该设备正在玩命找网络,请您稍候再试', 'rst': -1})
- elif result['rst'] == 1:
- raise ServiceException(
- {'result': 2, 'description': u'该设备忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能', 'rst': 1})
- else:
- if cmd in [DeviceCmdCode.OPERATE_DEV_NO_RESPONSE, DeviceCmdCode.PASSTHROUGH_OPERATE_DEV_NO_RESPONSE]:
- return
- if result.get('data') == '00':
- raise ServiceException({'result': 2, 'description': u'设备操作失败.请重试'})
- else:
- return result.get('data', 'ok')
- def _get_all_info(self):
- data = {
- 'funCode': 2,
- 'all': True
- }
- data = self.send_mqtt(data)
- return data
- def _do_update_configs(self, updateDict):
- dev = Device.objects.get(devNo=self.device.devNo)
- deviceConfigs = dev.otherConf.get('deviceConfigs', {})
- deviceConfigs.update(updateDict)
- dev.otherConf['deviceConfigs'] = deviceConfigs
- dev.save()
- Device.invalid_device_cache(self.device.devNo)
- def _get_device_configs(self):
- return Device.objects.get(devNo=self.device.devNo).otherConf.get('deviceConfigs', {})
- def _start(self, money, order_id):
- amount = int(float(money) * 100)
- data = {
- 'fun_code': 7,
- 'amount': amount,
- 'order_id': order_id
- }
- payload = {'data': data}
- return MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, payload,
- timeout=MQTT_TIMEOUT.START_DEVICE)
- def do_ack_order_32(self, order_id):
- data = {
- 'fun_code': 32,
- 'order_id': order_id
- }
- self.send_mqtt(data)
- def do_ack_remove_order_from_device_34(self, order_id):
- data = {
- 'fun_code': 34,
- 'order_id': order_id
- }
- self.send_mqtt(data)
- def _check_dev_status(self):
- data = {
- 'fun_code': 2,
- 'status': True,
- 'exec_orders': True,
- 'wait_orders': True,
- }
- data = self.send_mqtt(data)
- exec_orders = data.get('exec_orders') or []
- wait_orders = data.get('wait_orders') or []
- order_list = []
- if exec_orders:
- order_list.append(exec_orders[0]['id'])
- if wait_orders:
- order_list += list(map(lambda x: x['id'], wait_orders))
- result = False
- status = data.get('status')
- if status == 'fault':
- raise ServiceException({'result': 2, 'description': u'该充电桩出现故障,请使用其他充电桩'})
- elif status == 'idle':
- raise ServiceException({'result': 2, 'description': u'请先连接充电枪'})
- elif status == 'link':
- result = True
- elif status == 'estop':
- raise ServiceException({'result': 2, 'description': u'该充电桩处于 紧急停止 状态,请确认充电桩安全状态后,再次扫码使用或选择其他充电桩'})
- elif status == 'ready':
- result = True
- elif status == 'busy':
- result = True
- else:
- pass
- return {'result': result, 'order_list': order_list}
- def _check_package(self, package):
- """
- 获取设备启动的发送数据 根据设备的当前模式以及套餐获取
- :param package:
- :return:
- """
- unit = package.get('unit')
- _time = package.get('time')
- billingType = 'elec'
- if unit != u'度':
- raise ServiceException({'result': 2, 'description': u'套餐单位错误,应选取单位(度),请联系经销商'})
- else:
- _time = _time
- return _time, unit, billingType
- def _transform_prices(self, prices, mode='send'):
- if mode == 'send':
- send_list = []
- default_price = round(float(prices.get('default_price', 0)), 2)
- item = ['default', default_price]
- send_list.append(item)
- if all([prices.get('time1_start_m'), prices.get('time1_start_h'), prices.get('time1_end_m'),
- prices.get('time1_end_h'), prices.get('price1')]):
- time1_start_m = '{0:0>2}'.format(prices.get('time1_start_m'))
- time1_start_h = '{0:0>2}'.format(prices.get('time1_start_h'))
- time1_end_m = '{0:0>2}'.format(prices.get('time1_end_m'))
- time1_end_h = '{0:0>2}'.format(prices.get('time1_end_h'))
- price1 = round(float(prices.get('price1', 0)), 2)
-
- if time1_end_h + ':' + time1_end_m <= time1_start_h + ':' + time1_start_m:
- raise ServiceException({'result': 2, 'description': u'时段1结束时间必须大于起始时间'})
-
- str = time1_start_h + ':' + time1_start_m + '-' + time1_end_h + ':' + time1_end_m
- item = [str, price1]
- send_list.append(item)
- else:
- raise ServiceException({'result': 2, 'description': u'时段1参数不完整'})
- if all([prices.get('time2_start_m'), prices.get('time2_start_h'), prices.get('time2_end_m'),
- prices.get('time2_end_h'), prices.get('price2')]):
- time2_start_m = '{0:0>2}'.format(prices.get('time2_start_m'))
- time2_start_h = '{0:0>2}'.format(prices.get('time2_start_h'))
- time2_end_m = '{0:0>2}'.format(prices.get('time2_end_m'))
- time2_end_h = '{0:0>2}'.format(prices.get('time2_end_h'))
- price2 = round(float(prices.get('price2', 0)), 2)
-
- if time2_end_h + ':' + time2_end_m <= time2_start_h + ':' + time2_start_m:
- raise ServiceException({'result': 2, 'description': u'时段2结束时间必须大于起始时间'})
-
- str = time2_start_h + ':' + time2_start_m + '-' + time2_end_h + ':' + time2_end_m
- item = [str, price2]
- send_list.append(item)
- else:
- raise ServiceException({'result': 2, 'description': u'时段2参数不完整'})
- if all([prices.get('time3_start_m'), prices.get('time3_start_h'), prices.get('time3_end_m'),
- prices.get('time3_end_h'), prices.get('price3')]):
- time3_start_m = '{0:0>2}'.format(prices.get('time3_start_m'))
- time3_start_h = '{0:0>2}'.format(prices.get('time3_start_h'))
- time3_end_m = '{0:0>2}'.format(prices.get('time3_end_m'))
- time3_end_h = '{0:0>2}'.format(prices.get('time3_end_h'))
- price3 = round(float(prices.get('price3', 0)), 2)
-
- if time3_end_h + ':' + time3_end_m <= time3_start_h + ':' + time3_start_m:
- raise ServiceException({'result': 2, 'description': u'时段3结束时间必须大于起始时间'})
-
- str = time3_start_h + ':' + time3_start_m + '-' + time3_end_h + ':' + time3_end_m
- item = [str, price3]
- send_list.append(item)
- else:
- raise ServiceException({'result': 2, 'description': u'时段3参数不完整'})
-
- if not (time3_start_h + ':' + time3_start_m >= time2_end_h + ':' + time2_end_m and time2_start_h + ':' + time2_start_m >= time1_end_h + ':' + time1_end_m):
- raise ServiceException({'result': 2, 'description': u'时段的其实时间不能小于上一个时段的结束时间'})
-
- return send_list
- else:
- price_dict = {}
- index = 1
- for price in prices:
- if price[0] == 'default':
- price_dict.update({'default_price': price[1]})
- else:
- time1_start, time1_end = price[0].split('-')
- price_dict.update({
- 'time{}_start_h'.format(index): time1_start.split(':')[0],
- 'time{}_start_m'.format(index): time1_start.split(':')[1],
- 'time{}_end_h'.format(index): time1_end.split(':')[0],
- 'time{}_end_m'.format(index): time1_end.split(':')[1],
- 'price{}'.format(index): price[1]
- })
- index += 1
- return price_dict
- def _reboot_device(self):
- data = {
- 'fun_code': 11,
- 'reset_mcu': True,
- }
- self.send_mqtt(data)
- def _restart_device(self):
- data = {
- 'restart': True,
- }
- self.send_mqtt(otherData=data, cmd=DeviceCmdCode.SET_DEVINFO)
- def _factory_set_device(self):
- data = {
- 'factory_set': True,
- }
- self.send_mqtt(otherData=data, cmd=DeviceCmdCode.SET_DEVINFO)
- def _get_current_order(self):
- data = {
- 'fun_code': 2,
- 'exec_orders': True,
- }
- data = self.send_mqtt(data)
- return data.get('exec_orders', [])
- def _get_total_card(self):
- data = {
- 'fun_code': 8
- }
- data = self.send_mqtt(data)
- return {'total_card': int(data.get('total_card', 0)) / 100.0}
- def _reset_total_card(self):
- data = {
- 'fun_code': 9
- }
- self.send_mqtt(data)
- def is_port_can_use(self, port, canAdd=False):
- return True, ''
- # data = {
- # 'fun_code': 2,
- # 'status': True,
- # 'exec_orders': True,
- # 'wait_orders': True,
- # }
- # data = self.send_mqtt(data)
- # exec_orders = data.get('exec_orders')
- # wait_orders = data.get('wait_orders')
- #
- # order_list = []
- # if exec_orders:
- # order_list.append(exec_orders[0]['id'])
- # if wait_orders:
- # order_list += list(map(lambda x: x['id'], wait_orders))
- #
- # status = data.get('status')
- # if status == 'fault':
- # return False, '该充电桩出现故障,请使用其他充电桩'
- # elif status == 'idle':
- # return False, '请先连接充电枪'
- # elif status == 'link':
- # return True, ''
- # elif status == 'estop':
- # raise False, '该充电桩处于 紧急停止 状态,请确认充电桩安全状态后,再次扫码使用或选择其他充电桩'
- # elif status == 'ready':
- # return True, ''
- # elif status == 'busy':
- # return True, ''
- # else:
- # return False, '该充电桩出现故障,请使用其他充电桩'
- def check_dev_status(self, attachParas=None):
- if attachParas.get('isTempPackage') == True:
- washConfig = self.device.get('tempWashConfig', {})
- else:
- washConfig = self.device.get('washConfig', {})
- packageId = attachParas.get('packageId', '1')
- package = washConfig.get(packageId)
- # self._check_package(package)
- data = self._check_dev_status()
- openId = attachParas.get('openId')
- order_list = data.get('order_list')
- result = data.get('result')
- if not order_list:
- return result
- else:
- consumeRcd = ConsumeRecord.objects.filter(devNo=self.device.devNo, orderNo=order_list[0]).first()
- if not consumeRcd: # 没有找到订单 证明是刷卡启动的 或则经销商远程上分
- raise ServiceException({'result': 2, 'description': u'当前设备已经有其他人启动充电,请不要操作设备或充电枪, 小心触电!'})
- else:
- if consumeRcd.openId != openId:
- raise ServiceException({'result': 2, 'description': u'当前设备已有人在使用,请您选择空闲的设备进行操作'})
- else:
- return result
- def get_port_status(self, force=False):
- if force:
- self._get_port_status()
- ctrInfo = Device.get_dev_control_cache(self.device['devNo'])
- if not ctrInfo.get(self.PORT, {}).get('status'):
- self._get_port_status()
- ctrInfo = Device.get_dev_control_cache(self.device['devNo'])
- result = {}
- for port, info in ctrInfo.items():
- if port.isdigit():
- result[port] = info
- return result
- def async_update_portinfo_from_dev(self):
- class Sender(threading.Thread):
- def __init__(self, smartBox):
- super(Sender, self).__init__()
- self._smartBox = smartBox
- def run(self):
- try:
- self._smartBox._get_port_status()
- except Exception as e:
- logger.info('get port stats from dev,e=%s' % e)
- sender = Sender(self)
- sender.start()
- 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'请您选择合适的充电线路'})
- onPoints = attachParas.get('onPoints')
- if onPoints: # 远程上分
- self._check_dev_status()
- orderNo = ConsumeRecord.make_no()
- logger.info('dealer onPoints package<{}> order<{}>'.format(package, orderNo))
- else:
- orderNo = str(attachParas.get('orderNo'))
- coins = package.get('coins')
- result = self._start(coins, orderNo)
- if result['rst'] == 0:
- consumeOrder = {
- 'orderNo': orderNo,
- 'coin': package.get('coins'),
- 'consumeType': 'coin',
- }
- self.register_service_progress(openId, orderNo, consumeOrder, attachParas)
- elif result['rst'] == -1: # 网络丢失
- raise DeviceNetworkTimeoutError()
- elif result['rst'] == 1: # 充电桩已被禁用(模块没有单)
- raise StartDeviceError('充电桩已被禁用')
- elif result['rst'] == 2: # 计量器故障(模块没有单)
- raise StartDeviceError('计量器故障')
- elif result['rst'] == 3: # 串口超时(模块没有单)
- raise UartTimeoutError()
- elif result['rst'] == 4: # 充电桩故障, 请联系服务商(模块没有单)
- raise StartDeviceError('充电桩故障, 请联系服务商')
- elif result['rst'] == 5: # 未连接充电枪(模块没有单)
- raise StartDeviceError('未连接充电枪')
- elif result['rst'] == 6: # 设备急停状态(模块没有单)
- raise StartDeviceError('设备急停状态')
- elif result['rst'] == 7: # 充电启动失败(模块没有单)
- raise StartDeviceError('充电启动失败')
- elif result['rst'] == 9: # 充电启动失败(车辆侧充电器开关未闭合)
- raise StartDeviceError('车辆侧充电器开关未闭合')
- event_info = {
- 'cmd': 100,
- 'data': {
- 'fun_code': 32,
- 'order': {
- 'order_type': 'apps_start',
- 'id': orderNo,
- 'status': 'running',
- 'adapter': True
- }
- },
- 'IMEI': '862167056713559',
- 'rst': 0
- }
- return event_info
- def register_service_progress(self, openId, weifuleOrderNo, consumeOrder=None, attachParas=None):
- return ServiceProgress.objects.create(
- open_id=openId,
- device_imei=self.device.devNo,
- devTypeCode='100257',
- port=1,
- attachParas=attachParas if attachParas else {},
- start_time=int(time.time()),
- finished_time=int(time.time() + 24 * 60 * 60),
- consumeOrder=consumeOrder if consumeOrder else {},
- weifuleOrderNo=weifuleOrderNo,
- expireAt = datetime.datetime.now() + datetime.timedelta(days = 91)
- ).id
- def get_port_info(self, port):
- if not port or port not in [1, '1']:
- return {}
- else:
- data = cache.get('port_info_{}'.format(self.device.devNo))
- if data:
- return data
- else:
- data = {
- 'fun_code': 2,
- 'all': True
- }
- data = self.send_mqtt(data)
- exec_orders = data.get('exec_orders')
- ctrInfo = Device.get_dev_control_cache(self.device.devNo)
- lineInfo = ctrInfo.get(port, {})
- if exec_orders and lineInfo.get('order_id') == exec_orders[0]['id']:
- elec = sum(map(lambda x: x['elec'], exec_orders))
- data = {
- 'power': '%.2f' % (data.get('watt', 0)),
- 'voltage': '%.2f' % (data.get('volt', 0)),
- 'elec': '%.4f' % (elec / 1000000.0),
- 'ampere': '%.2f' % (data.get('ampr', 0) / 1000.0),
- }
- cache.set('port_info_{}'.format(self.device.devNo), data, 5)
- return data
- else:
- return {}
- def recharge_card(self, cardNo, money, orderNo=None):
- data = {
- 'card_no': cardNo,
- 'fun_code': 37,
- 'result': 1,
- 'charge': int(money * 100),
- 'order_id': orderNo
- }
- self.send_mqtt(data)
- card = Card.objects.filter(cardNo=cardNo, dealerId=self.device.ownerId).first()
- balance = card.balance + money
- return {
- 'result': ErrorCode.SUCCESS,
- 'description': ''
- }, balance
- def set_device_function(self, request, lastSetConf):
- update_dict = {'fun_code': 11}
- if request.POST.get('pile_disable') == True or request.POST.get('pile_disable') == False:
- update_dict.update({'pile_disable': request.POST.get('pile_disable')})
- self.disable_app_device(request.POST.get('pile_disable'))
- if request.POST.get('card_disable') == True or request.POST.get('card_disable') == False:
- update_dict.update({'card_disable': request.POST.get('card_disable')})
- if request.POST.get('scan_disable') == True or request.POST.get('scan_disable') == False:
- update_dict.update({'scan_disable': request.POST.get('scan_disable')})
- if request.POST.get('sys_pwd') == True or request.POST.get('sys_pwd') == False:
- update_dict.update({'sys_pwd': request.POST.get('sys_pwd')})
- if request.POST.get('card_refund') == True or request.POST.get('card_refund') == False:
- update_dict.update({'card_refund': request.POST.get('card_refund')})
- if request.POST.get('reboot') == True:
- self._reboot_device()
- return
- if request.POST.get('restart') == True:
- self._restart_device()
- return
- if request.POST.get('factory_set') == True:
- self._factory_set_device()
- return
- if request.POST.get('czskze') == True:
- self._reset_total_card()
- return
- self.send_mqtt(update_dict)
- def set_device_function_param(self, request, lastSetConf):
- update_dict = {'fun_code': 11}
- if request.POST.get('volume'):
- self.check_params_range(request.POST.get('volume'), minData=0, maxData=7, desc='音量')
- update_dict.update({'volume': int(request.POST.get('volume'))})
- if request.POST.get('one_card'):
- update_dict.update({'one_card': int(float(request.POST.get('one_card')) * 100)})
- if request.POST.get('max_watt'):
- update_dict.update({'max_watt': int(request.POST.get('max_watt'))})
- if request.POST.get('max_volt'):
- update_dict.update({'max_volt': int(request.POST.get('max_volt'))})
- if request.POST.get('max_ampr'):
- update_dict.update({'max_ampr': int(request.POST.get('max_ampr'))})
- if request.POST.get('card_timeout'):
- update_dict.update({'card_timeout': int(request.POST.get('card_timeout'))})
- if request.POST.get('max_time'):
- update_dict.update({'max_time': int(request.POST.get('max_time'))})
- if request.POST.get('price1'):
- prices = self._transform_prices(request.POST)
- update_dict.update({'prices': prices})
- if request.POST.get('sys_old_pwd'):
- password = serviceCache.get(self.cache_key())
- if not password:
- raise ServiceException({'result': 2, 'description': u'当前页面已过期,请刷新后重试'})
- enObj = EncryptDate(cardKey)
- password = enObj.decrypt(password)
- sys_old_pwd = request.POST.get('sys_old_pwd')
- sys_pwd = request.POST.get('sys_pwd')
- sys_pwd2 = request.POST.get('sys_pwd2')
- if sys_pwd != sys_pwd2:
- raise ServiceException({'result': 2, 'description': u'两次新密码输入不一致'})
- if not sys_pwd:
- raise ServiceException({'result': 2, 'description': u'请输入新密码'})
- if sys_old_pwd == sys_pwd:
- raise ServiceException({'result': 2, 'description': u'新旧密码不能为一致'})
- if password != sys_old_pwd:
- raise ServiceException({'result': 2, 'description': u'旧密码输入错误'})
- result = re.findall(r'[a-zA-Z0-9]{6,10}', sys_pwd)
- if not result or len(result[0]) != len(sys_pwd) or len(result[0]) < 6 or len(result[0]) > 10:
- raise ServiceException({'result': 2, 'description': u'新密码只能是6-10位,由字母或者数字'})
- update_dict.update({'sys_pwd': enObj.encrypt(sys_pwd)})
- self.send_mqtt(update_dict)
- serviceCache.set(self.cache_key(), enObj.encrypt(sys_pwd), 600)
- if request.POST.get('card_dft_pwd'):
- card_dft_pwd = str(request.POST.get('card_dft_pwd'))
- result = re.findall(r'[a-zA-Z0-9]{6}', card_dft_pwd)
- if not result or len(result[0]) != len(card_dft_pwd):
- raise ServiceException({'result': 2, 'description': u'卡片密码只能是6位,由字母或者数字组成'})
- update_dict.update({'card_dft_pwd': card_dft_pwd})
- if request.POST.get('card_dft_mny'):
- update_dict.update({'card_dft_mny': int(float(request.POST.get('card_dft_mny')) * 100)})
- if request.POST.get('card_token'):
- card_token = str(request.POST.get('card_token'))
- result = re.findall(r'[a-zA-Z0-9]{6}', card_token)
- if not result or len(result[0]) != len(card_token):
- raise ServiceException({'result': 2, 'description': u'设备刷卡标签只能是6位,由字母或者数字组成'})
- update_dict.update({'card_token': card_token})
- if request.POST.get('temp_threshold'):
- update_dict.update({'temp_threshold': int(request.POST.get('temp_threshold'))})
- if len(update_dict) > 1:
- self.send_mqtt(update_dict)
- if request.POST.get('csh_tel', None) != None:
- data = {
- 'csh_tel': request.POST.get('csh_tel')
- }
- self.send_mqtt(otherData=data, cmd=DeviceCmdCode.SET_DEVINFO)
- self._do_update_configs({'csh_tel': request.POST.get('csh_tel')})
- def get_dev_setting(self):
- data = {
- 'fun_code': 12,
- 'all': True,
- }
- data = self.send_mqtt(data)
- data['one_card'] = data.get('one_card', 0) / 100.0
- data['card_dft_mny'] = data.get('card_dft_mny', 0) / 100.0
- price = data.pop('prices')
- price_dict = self._transform_prices(price, 'get')
- sys_pwd = data.pop('sys_pwd')
- serviceCache.set(self.cache_key(), sys_pwd, 600)
- data.update(price_dict)
- total_card = self._get_total_card()
- data.update(total_card)
- deviceConfigs = self._get_device_configs()
- if deviceConfigs:
- data.update(deviceConfigs)
- return data
- def get_port_status_from_dev(self):
- data = {
- 'fun_code': 2,
- 'all': True
- }
- data = self.send_mqtt(data)
- exec_orders = data.get('exec_orders') or []
- wait_orders = data.get('wait_orders') or []
- status = data.get('status')
- item = {
- 'index': 1,
- 'power': '%.2f' % (data.get('watt', 0)),
- 'voltage': '%.2f' % (data.get('volt', 0)),
- 'ampere': '%.2f' % (data.get('ampr', 0) / 1000.0),
- 'status': 'fault',
- 'devTemp': '%.2f' % data.get('temp', 0),
- }
- if status == 'busy' or status == 'ready':
- item.update({'status': status})
- exec_order = exec_orders[0]
- start_time = exec_order.get('create_time')
- elec = exec_order.get('elec', 0)
- time = exec_order.get('time', 0)
- start_time = datetime.datetime.fromtimestamp(start_time)
- order_id = exec_order.get('id')
- consumeOrder = ConsumeRecord.objects.filter(devNo=self.device.devNo, orderNo=order_id).first()
- order_type = exec_order.get('order_type', 'apps_start')
- if order_type == 'apps_start':
- _info = {
- 'consumeType': 'mobile',
- 'coins': '{}{}'.format(str(RMB.fen_to_yuan(exec_order.get('amount', 0))), self.show_pay_unit),
- 'leftMoney': '{}{}'.format(str(RMB.fen_to_yuan(exec_order.get('left_money', 0))),
- self.show_pay_unit),
- 'consumeMoney': '{}{}'.format(str(RMB.fen_to_yuan(exec_order.get('amount', 0)) - RMB.fen_to_yuan(
- exec_order.get('left_money', 0))), self.show_pay_unit),
- 'startTime': start_time.strftime('%m-%d %H:%M:%S'),
- 'elec': '%.4f' % (elec / 1000000.0),
- 'usedTime': '%.2f' % (time / 60.0),
- }
- # 远程上分没有订单号
- if not consumeOrder:
- people = MyUser(nickname='经销商远程上分')
- else:
- people = MyUser.objects.filter(openId=consumeOrder.openId, groupId=self.device['groupId']).first()
-
- #后付费
- payAfterUse = consumeOrder.attachParas.get('payAfterUse')
- if payAfterUse:
- _info['consumeType'] = 'postpaid'
- _info.pop('leftMoney', None)
- _info.pop('coins', None)
-
- _info['nickName'] = people.nickname
- if wait_orders:
- waittingOrder = []
- for _this in wait_orders:
- one = {}
- if _this['order_type'] == 'apps_start':
- one['consumeType'] = 'mobile'
- try:
- consumeOne = ConsumeRecord.objects.filter(orderNo=order_id).first()
- if consumeOne.attachParas.get('payAfterUse'):
- one['consumeType'] = 'postpaid'
- except:
- pass
- elif _this['order_type'] == 'card_start':
- one['consumeType'] = 'card'
- one['cardNo'] = _this.get('card_no')
- one['cardBalance'] = '{}{}'.format(str(RMB.fen_to_yuan(_this.get('balance', 0))),
- self.show_pay_unit)
- one['coins'] = '{}{}'.format(str(RMB.fen_to_yuan(_this.get('amount', 0))), self.show_pay_unit)
- createTime = datetime.datetime.fromtimestamp(_this['create_time'])
- one['createTime'] = createTime.strftime('%m-%d %H:%M:%S')
- waittingOrder.append(one)
- _info['waittingOrder'] = waittingOrder
- elif order_type == 'card_start':
- card_no = exec_order.get('card_no')
- _info = {
- 'consumeType': 'card',
- 'cardNo': card_no,
- 'coins': '{}{}'.format(str(RMB.fen_to_yuan(exec_order.get('amount', 0))), self.show_pay_unit),
- 'cardLeftMoney': '{}{}'.format(str(RMB.fen_to_yuan(exec_order.get('left_money', 0))),
- self.show_pay_unit),
- 'cardBalance': '{}{}'.format(str(RMB.fen_to_yuan(exec_order.get('balance', 0))),
- self.show_pay_unit),
- 'cardConsumeMoney': '{}{}'.format(
- str(RMB.fen_to_yuan(exec_order.get('amount', 0)) - RMB.fen_to_yuan(
- exec_order.get('left_money', 0))), self.show_pay_unit),
- 'startTime': start_time.strftime('%m-%d %H:%M:%S'),
- 'elec': '%.4f' % (elec / 1000000.0),
- }
- card = Card.objects.filter(cardNo=card_no, dealerId=self.device.ownerId).first()
- if card:
- _info.update({'nickName': card.nickName})
- else:
- _info.update({'nickName': '匿名用户'})
- if wait_orders:
- waittingOrder = []
- for _this in wait_orders:
- one = {}
- if _this['order_type'] == 'apps_start':
- one['consumeType'] = 'mobile'
- elif _this['order_type'] == 'card_start':
- one['consumeType'] = 'card'
- one['cardNo'] = _this.get('card_no')
- one['cardBalance'] = '{}{}'.format(str(RMB.fen_to_yuan(_this.get('balance', 0))),
- self.show_pay_unit)
- one['coins'] = '{}{}'.format(str(RMB.fen_to_yuan(_this.get('amount', 0))), self.show_pay_unit)
- createTime = datetime.datetime.fromtimestamp(_this['create_time'])
- one['createTime'] = createTime.strftime('%m-%d %H:%M:%S')
- waittingOrder.append(one)
- _info['waittingOrder'] = waittingOrder
- else:
- _info = {}
- item.update(_info)
- elif status == 'fault':
- item.update({'status': status})
- elif status == 'estop':
- item.update({'status': status})
- elif status == 'idle':
- item.update({'status': status})
- elif status == 'link':
- item.update({'status': 'connected'})
- return [item, ]
- def stop(self, port=None):
- data = {
- 'fun_code': 6,
- 'operator': 'dealer'
- }
- self.send_mqtt(data)
- def active_deactive_port(self, port, active):
- if active == False:
- self.stop()
- def stop_by_order(self, port=None, orderNo=''):
- order = ConsumeRecord.objects.filter(orderNo=orderNo, isNormal=True).first()
- if not order:
- logger.info('no this order <{}>'.format(orderNo))
- return
- people = MyUser.objects.filter(openId=order.openId, groupId=self.device['groupId']).first()
- data = {
- 'fun_code': 4,
- 'operator': 'user',
- 'operator_id': str(people.id),
- 'order_id': orderNo
- }
- data = self.send_mqtt(data)
- reback_order_id = data.get('order_id')
- if reback_order_id and reback_order_id == orderNo:
- return
- else:
- # 数据库中有但是没有结束的异常单
- try:
- order.update(isNormal=False,
- errorDesc='异常结束,设备丢失此订单code:3,',
- finishedTime=datetime.datetime.now())
- ServiceProgress.objects.filter(device_imei = self.device.devNo,
- weifuleOrderNo = order.orderNo).update(
- isFinished = True,
- finished_time = int(time.time()),
- expireAt = datetime.datetime.now())
- except Exception:
- pass
- @property
- def isHaveStopEvent(self):
- return True
- def cache_key(self):
- return 'sys_pwd_{}'.format(self.device.devNo)
- def _get_port_status(self):
- data = {
- 'fun_code': 2,
- 'status': True
- }
- data = self.send_mqtt(data)
- status = data.get('status')
- if status == 'fault':
- res = {'status': Const.DEV_WORK_STATUS_FAULT, 'port': self.PORT}
- elif status == 'idle':
- res = {'status': Const.DEV_WORK_STATUS_IDLE, 'port': self.PORT}
- elif status == 'link':
- res = {'status': Const.DEV_WORK_STATUS_CONNECTED, 'port': self.PORT}
- elif status == 'estop':
- res = {'status': Const.DEV_WORK_STATUS_ESTOP, 'port': self.PORT}
- elif status == 'ready':
- res = {'status': Const.DEV_WORK_STATUS_READY, 'port': self.PORT}
- elif status == 'busy':
- res = {'status': Const.DEV_WORK_STATUS_WORKING, 'port': self.PORT}
- else:
- res = {'status': Const.DEV_WORK_STATUS_FAULT, 'port': self.PORT}
- Device.update_port_control_cache(self.device['devNo'], res)
- def get_current_use(self, **kw):
- base_data = kw.get('base_data')
- spDict = kw.get('spDict')
- # sp = ServiceProgress.objects.filter(device_imei=self.device.devNo, weifuleOrderNo=spDict.get('weifuleOrderNo')).first()
- portInfo = serviceCache.get('_all_orders_{}'.format(self.device.devNo))
- if not portInfo:
- data = {'fun_code': 2, 'all': True}
- portInfo = self.send_mqtt(data)
- serviceCache.set('_all_orders_{}'.format(self.device.devNo), portInfo, 5)
- exec_orders = portInfo.get('exec_orders') or []
- wait_orders = portInfo.get('wait_orders') or []
- all_orders = exec_orders + wait_orders
- unit = self.show_pay_unit
- result_list = [] # 数据整理返回
- for order in all_orders:
- if order['id'] != spDict['weifuleOrderNo']:
- continue
- item = {}
- item.update(base_data)
- item['orderNo'] = order['id']
- if order['status'] == 'running':
- item['voltage'] = round(portInfo.get('volt', 0), 2)
- item['power'] = round(portInfo.get('watt', 0), 2)
- item['ampere'] = round((portInfo.get('ampr', 0) * 0.001), 2)
- item['elec'] = round(order.get('elec', 0) * 0.000001, 4)
- item['usedTime'] = round(order.get('time', 0) / 60.0, 2)
- consumeType = 'card' if 'card_no' in order else 'mobile'
- item['consumeMoney'] = '{}{}'.format(round(order.get('money', 0) * 0.01, 2), unit)
- item['leftMoney'] = '{}{}'.format(round(order.get('left_money', 0) * 0.01, 2), unit)
- item['order'] = {
- 'orderNo': order['id'], # 停止按钮传订单停单用
- 'coin': '{}{}'.format(round(order.get('amount', 0) * 0.01, 2), unit),
- 'consumeType': consumeType,
- }
- if 'card_no' in order:
- item['cardNo'] = order['card_no']
- elif order['status'] == 'waiting':
- item['desc'] = '此订单已经下发到设备上,上一单运行完毕就会自动运行此订单'
- consumeType = 'card' if 'card_no' in order else 'mobile'
- item['order'] = {
- 'orderNo': order['id'], # 停止按钮传订单停单用
- 'coin': '{}{}'.format(round(order.get('amount', 0) * 0.01, 2), unit),
- 'consumeType': consumeType,
- }
- if 'cardNo' in order:
- item['cardNo'] = order['card_no']
- try:
- consumeRcd = ConsumeRecord.objects.filter(orderNo=order['id']).first()
- # 问题单兼容
- payAfterUse = consumeRcd.attachParas.get('payAfterUse')
- if payAfterUse:
- item['order'] = {
- 'orderNo': order['id'], # 停止按钮传订单停单用
- 'coin': '{}{}'.format(0, unit),
- 'consumeType': 'postpaid',
- }
- item.pop('leftMoney', None)
- except:
- pass
- item.update(DeviceType.get_services_button(self.device['devType']['id']))
- result_list.append(item)
- break
- if not result_list:
- ServiceProgress.objects.filter(device_imei=self.device.devNo,
- weifuleOrderNo=spDict.get('weifuleOrderNo')).update(
- isFinished=True,
- expireAt = datetime.datetime.now())
- ConsumeRecord.objects.filter(devNo=self.device.devNo, isNormal=True,
- orderNo=spDict.get('weifuleOrderNo')).update(isNormal=False,
- errorDesc='异常结束,设备订单丢失 code:4',
- finishedTime=datetime.datetime.now())
- return result_list
- @property
- def show_pay_unit(self):
- """
- 前台显示付费的时候,目前有不同的客户希望 显示不同的单位 有的显示金币 有的显示元, 这个地方处理下
- :return:
- """
- if self.device['otherConf'].get('pay_unit'):
- return self.device['otherConf'].get('pay_unit')
- return u'币'
- def dealer_get_port_status(self):
- """
- 远程上分的时候获取端口状态
- :return:
- """
- return self.get_port_status(force=True)
- def check_order_state(self, openId):
- dealerId = self.device.ownerId
- return ClientConsumeModelProxy.get_not_finished_record(ownerId=dealerId, openId=openId,
- devTypeCode=self._device['devType']['code'],
- attachParas__payAfterUse=True)
- def force_stop_order(self,order, **kwargs):
- if not order.attachParas.get('payAfterUse'):
- pass
-
- else:
- errorDesc = order.errorDesc + '经销商手动强制关单<{}>'.format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
- order.update(status='finished', errorDesc=errorDesc, isNormal=False)
-
- def get_policy_infos(self):
- data = {
- 'fun_code': 12,
- 'all': True,
- }
- data = self.send_mqtt(data)
- price = data.pop('prices')
- price_dict = self._transform_prices(price, 'get')
- return price_dict
-
- def set_policy_infos(self,payload):
- update_dict = {'fun_code': 11}
- if payload.get('price1'):
- prices = self._transform_prices(payload)
- update_dict.update({'prices': prices})
-
- return self.send_mqtt(update_dict)
-
- def get_policy_for_user(self):
- data = {
- 'fun_code': 12,
- 'all': True,
- }
- data = self.send_mqtt(data)
- prices = data.get('prices')
-
- # 用默认时间填满缝隙
- defaultPrice = 1.5
- priceList = []
- for price in prices:
- if price[0] == 'default':
- defaultPrice = price[1]
- else:
- priceList.append(price)
-
- fillList = []
- for ii in range(len(priceList)-1):
- price = priceList[ii]
- if price[0] == 'default':
- continue
- endTime= price[0].split('-')[1]
- nextPrice = priceList[ii+1]
- nextStartTime = nextPrice[0].split('-')[0]
- if nextStartTime>endTime:
- fillList.append(('%s-%s' % (endTime,nextStartTime),defaultPrice))
-
- # 头尾加上时间
- firstTime = priceList[0][0].split('-')[0]
- if firstTime != '00:00':
- priceList.insert(0, ('00:00-%s'% firstTime,defaultPrice))
- tailTime = priceList[-1][0].split('-')[1]
- if tailTime != '24:00':
- priceList.append(('%s-24:00'% tailTime,defaultPrice))
-
- # 合并价格一样的时间区间
-
- startTime = priceList[0][0].split('-')[0]
- elecPrice = priceList[0][1]
- resultList = [{'startTime':startTime,'elecPrice':elecPrice}]
- for ii in range(len(priceList)-1):
- nextPrice = priceList[ii+1]
- if nextPrice[1] == resultList[-1]['elecPrice']:
- continue
- resultList.append({'startTime':nextPrice[0].split('-')[0],'elecPrice':nextPrice[1],'sevicePrice':0})
-
- return resultList
-
- def get_customize_score_unit(self):
- return u'元'
-
- def start_customize_point(self,pointNum,openId,port):
- package = {'name':'customizePoint','price':pointNum,'coins':pointNum,'unit':u'次','time':1}
- attachParas = {'chargeIndex':port,'onPoints':True}
- return self.start_device(package, openId, attachParas)
|