# -*- coding: utf-8 -*- # !/usr/bin/env python import time from apps.web.constant import MQTT_TIMEOUT, DeviceCmdCode, Const from apps.web.core.adapter.base import SmartBox, fill_2_hexByte from apps.web.core.exceptions import ServiceException from apps.web.core.networking import MessageSender from apps.web.device.models import Device class ShengOuBox(SmartBox): def _send_data(self, funcCode, sendData=None, cmd=None, timeout=MQTT_TIMEOUT.NORMAL,orderNo=None): """ 发送 报文 :param funcCode: 串口命令 :param sendData: 发送数据 :param cmd: 报文命令 :param timeout: 超时时间 :return: """ if sendData is None: sendData = "" if cmd is None: cmd = DeviceCmdCode.OPERATE_DEV_SYNC if timeout is None: timeout = MQTT_TIMEOUT.NORMAL result = MessageSender.send(device = self.device, cmd = cmd, payload = { "IMEI": self._device["devNo"], "funCode": funcCode, "data": sendData }, timeout = timeout) if "rst" in result: if result["rst"] == -1: raise ServiceException({'result': 2, 'description': u'网络不通畅,请稍候再试'}) elif result["rst"] == 1: raise ServiceException({'result': 2, 'description': u'主板连接故障,请稍后再试'}) elif result["rst"] == 0: return result else: raise ServiceException({'result': 2, 'description': u'系统错误'}) else: raise ServiceException({'result': 2, 'description': u'系统错误'}) def _trans_time(self, needTime, unit): """ 根据所给的单位将时间转换成分钟 :param needTime: :param unit: :return: """ if unit == u"小时": needTime = needTime * 60 elif unit == u"分钟": needTime = needTime elif unit == u"秒": needTime = int(needTime / 60) else: needTime = None return needTime def _charge(self, chargeIndex, chargeTime=None, chargeFee=None,orderNo=None): """ 下发支付信息 :param chargeIndex: :param chargeTime :param chargeFee :return: """ wechatAddr = "{:0>8}".format(self._device["logicalCode"].replace("G", "")) hostAddr = "{:0>8}".format(0) portHex = fill_2_hexByte(hex(int(chargeIndex)), 4, reverse=True) timeHex = fill_2_hexByte(hex(int(chargeTime)), 4, reverse=True) feeHex = fill_2_hexByte(hex(int(chargeFee)), 4, reverse=True) sendData = wechatAddr + hostAddr + portHex + feeHex + timeHex result = self._send_data(funcCode="01", sendData=sendData,orderNo=orderNo, timeout = MQTT_TIMEOUT.START_DEVICE) data = result.get("data") if data[24:26] != "01": raise ServiceException({"result": 2, "description": u"插座启动失败"}) return result def is_port_can_use(self, port, canAdd=False): """ 判断端口是否可用 :param port: :param canAdd: :return: """ return True, u"" def start_device(self, package, openId, attachParas): """ 开启服务 :param package: :param openId: :param attachParas: :return: """ portStr = attachParas.get("chargeIndex") orderNo = attachParas.get("orderNo") coins = package.get("coins") price = package.get("price") unit = package.get("unit") needTime = package.get("time") if not portStr: raise ServiceException({"result": 2, "description": u"请选择合适充电线路"}) needTime = self._trans_time(needTime, unit) result = self._charge(chargeIndex=portStr, chargeTime=needTime, chargeFee=coins,orderNo = orderNo) finishedTime = int(time.time()) + needTime * 60 result.update({"finishedTime": finishedTime}) dataDict = { "status": Const.DEV_WORK_STATUS_WORKING, "finishedTime": finishedTime, "coins": coins, "price": price, "needTime": needTime, "port": portStr, "openId": openId, "vCardId": self._vcard_id, "isStart": True, } Device.update_dev_control_cache(self._device["devNo"], {str(portStr): dataDict}) return result