# -*- coding: utf-8 -*- # !/usr/bin/env python from apps.web.constant import DeviceCmdCode, Const, MQTT_TIMEOUT from apps.web.core.adapter.base import SmartBox, fill_2_hexByte, hexbyte_2_bin from apps.web.core.exceptions import ServiceException from apps.web.core.networking import MessageSender from apps.web.device.models import Device class ChargingCYBox(SmartBox): def __init__(self, device): super(ChargingCYBox, self).__init__(device) self.portDetail = {} def test(self, coins): coins = 1 cTemp = fill_2_hexByte(hex(int(coins) * 100)) devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC, payload = {'IMEI': self._device['devNo'], "funCode": '42', 'data': cTemp}) return devInfo def start_device(self, package, openId, attachParas): coins = int(package['coins']) cTemp = fill_2_hexByte(hex(int(coins) * 100)) devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC, payload = { 'IMEI': self._device['devNo'], "funCode": '42', 'data': cTemp }, 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'充电桩正在忙,无响应,您的金币还在,请稍后再试哦'}) if devInfo['data'][6:8] == '4F': # 表示成功 pass else: raise ServiceException( {'result': 2, 'description': u'支付失败,请您先在充电站上按下需要使用的线路端口按钮,然后再付款。如果还不能解决,请检查设备问题,或者联系老板'}) devInfo['finishedTime'] = self.get_duration(package) return devInfo def get_dev_setting(self): devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC, payload = {'IMEI': self._device['devNo'], "funCode": '45', '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'][6::] bigTime = int(data[0:4], 16) midTime = int(data[4:8], 16) smallTime = int(data[8:12], 16) littlePower = int(data[12:14], 16) preFee = int(data[14:18], 16) voice = int(data[18:20], 16) return {'bigTime': bigTime, 'midTime': midTime, 'smallTime': smallTime, 'littlePower': littlePower, 'preFee': round(preFee / 100.0, 2), 'voice': voice} def clear_dev_feecount(self): devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC, payload = {'IMEI': self._device['devNo'], "funCode": '46', 'data': ''}) if devInfo.has_key('rst') and devInfo['rst'] != 0: if devInfo['rst'] == -1: raise ServiceException( {'result': 2, 'description': u'当前充电桩正在玩命找网络,请您稍候再试'}) elif devInfo['rst'] == 1: raise ServiceException( {'result': 2, 'description': u'当前充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'}) if devInfo['data'][6:8] == '4F': # 表示成功 pass else: raise ServiceException( {'result': 2, 'description': u'清除设备统计信息失败,请重试看能否解决'}) def set_dev_setting(self, configDict): data = '' data += fill_2_hexByte(hex(int(configDict['bigTime']))) data += fill_2_hexByte(hex(int(configDict['midTime']))) data += fill_2_hexByte(hex(int(configDict['smallTime']))) data += fill_2_hexByte(hex(int(configDict['littlePower'])), 2) data += fill_2_hexByte(hex(int(configDict['preFee']))) data += fill_2_hexByte(hex(int(configDict['voice'])), 2) devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC, payload = {'IMEI': self._device['devNo'], "funCode": '41', 'data': data}) if devInfo.has_key('rst') and devInfo['rst'] != 0: if devInfo['rst'] == -1: raise ServiceException( {'result': 2, 'description': u'当前充电桩正在玩命找网络,请您稍候再试'}) elif devInfo['rst'] == 1: raise ServiceException( {'result': 2, 'description': u'当前充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'}) if devInfo['data'][6:8] == '4F': # 表示成功 pass else: raise ServiceException( {'result': 2, 'description': u'设置设备参数失败,请重试看能否解决'}) def get_dev_consume_count(self): devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC, payload = {'IMEI': self._device['devNo'], "funCode": '43', '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'][6::] cardFee = int(data[0:6], 16) / 100.0 coinFee = int(data[6:12], 16) / 100.0 mobileFee = int(data[12:18], 16) / 100.0 return {'cardFee': cardFee, 'coinFee': coinFee, 'mobileFee': mobileFee} def get_port_status(self, force = False): devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC, payload = {'IMEI': self._device['devNo'], "funCode": '44', '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'][6::] resultDict = {} workInfo = (hexbyte_2_bin(data[0:2]) + hexbyte_2_bin(data[2:4]))[::-1] index = 0 for wi in workInfo: index += 1 if wi == '0': resultDict[str(index)] = {'status': Const.DEV_WORK_STATUS_IDLE, 'index': str(index)} else: resultDict[str(index)] = {'status': Const.DEV_WORK_STATUS_WORKING, 'index': str(index)} workInfo = (hexbyte_2_bin(data[4:6]) + hexbyte_2_bin(data[6:8]))[::-1] index = 0 for wi in workInfo: index += 1 if wi == '0': resultDict[str(index)].update({'mode': 'time'}) else: resultDict[str(index)] = {'status': 'preFee'} workInfo = data[8:48] for ii in range(10): left = int(workInfo[ii * 4:ii * 4 + 4], 16) if resultDict[str(ii + 1)]['mode'] == 'time': resultDict[str(ii + 1)]['leftTime'] = left elif resultDict[str(ii + 1)]['mode'] == 'preFee': resultDict[str(ii + 1)]['leftMoney'] = round(left / 100, 2) allPorts, usedPorts, usePorts = self.get_port_static_info(resultDict) Device.update_dev_control_cache(self._device['devNo'], {'allPorts': allPorts, 'usedPorts': usedPorts, 'usePorts': usePorts}) self.portDetail = resultDict return resultDict def get_port_info(self, line): if self.portDetail.has_key(line): return self.portDetail[line] else: self.portDetail = self.get_port_status() return self.portDetail.get(line, {}) def set_device_function_param(self, request, lastSetConf): bigTime = request.POST.get('bigTime', None) midTime = request.POST.get('midTime', None) smallTime = request.POST.get('smallTime', None) littlePower = request.POST.get('littlePower', None) preFee = request.POST.get('preFee', None) voice = request.POST.get('voice', None) if bigTime: lastSetConf.update({'bigTime': int(bigTime)}) if midTime: lastSetConf.update({'midTime': int(midTime)}) if smallTime: lastSetConf.update({'smallTime': int(smallTime)}) if littlePower: lastSetConf.update({'littlePower': int(littlePower)}) if preFee: lastSetConf.update({'preFee': 100 * int(preFee)}) if voice: lastSetConf.update({'voice': int(voice)}) self.set_dev_setting(lastSetConf)