| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 | # coding=utf-8import jsonimport timeimport loggingimport typingfrom apps.web.core.adapter.base import SmartBoxfrom apps.web.core.exceptions import ServiceExceptionfrom apps.web.device.models import Devicefrom library.yinkayo import FCardWeb, DoorOnlineStatuslogger = logging.getLogger(__name__)class OneCardGate(SmartBox):    def __init__(self, device):        super(OneCardGate, self).__init__(device)        self._oneCardGate = None        # type: typing.Optional[None, FCardWeb]        self._sn, self._in, self._out = self._load_device()    def _load_device(self):        sn = self.device.devNo        inId = self.device.otherConf.get("IN")        outID = self.device.otherConf.get("OUT")        return sn, inId, outID    def _load_gate(self):        configs = self.device.otherConf        loginParams = configs.get("customer"), configs.get("operator"), configs.get("password")        if not all(loginParams):            raise ServiceException({"result": 2, "description": u"道闸设备登录参数尚未配置,请联系系统管理员!"})        oneCardGate = FCardWeb(*loginParams)        self._oneCardGate = oneCardGate    def _get_device_doors(self):        pageIndex, pageSize = 1, 10        doors = list()        while True:            result = self.oneCardGate.Door.get_doors(1, 10)            total = result["total"]            rows = result["rows"]            for row in rows:                if row["DoorSN"] != self._sn:                    continue                doors.append({"doorName": row["dName"], "doorID": row["DoorID"]})            if pageIndex * pageSize >= total:                break            pageSize += 10        return doors    def _start(self, doorID):        result = self.oneCardGate.Door.open(doorID)        taskID = result['RetData'][0]['tID']        timeout = 15        startTime = int(time.time())        while int(time.time()) - startTime < timeout:            time.sleep(2)            tResult = self.oneCardGate.Door.get_operate_record(taskID)            status = tResult["RetData"][0]["TaskStatus"]            if self.oneCardGate.Door.is_task_init(status) or self.oneCardGate.Door.is_task_ing(status):                continue            if self.oneCardGate.Door.is_task_fail(status):                raise ServiceException({"result": 2, "description": u"启动失败"})            if self.oneCardGate.Door.is_task_timeout(status):                raise ServiceException({"result": 2, "description": u"启动超时(1001)"})            if self.oneCardGate.Door.is_task_done(status):                return True        else:            raise ServiceException({"result": 2, "description": u"启动超时(1002)"})    @property    def oneCardGate(self):        if not self._oneCardGate:            self._load_gate()        return self._oneCardGate    def start(self, packageId, openId=None, attachParas={}):        control = attachParas.get("control", dict())        doorID = control.get("doorID")        if not doorID:            raise ServiceException({"result": 2, "description": u"设备配置错误,请联系经销商"})        return self._start(doorID)    def get_dev_setting(self):        otherConf = self.device.otherConf        priceParam = {            "price": otherConf.get("price"),            "cycle": otherConf.get("cycle"),            "maxFee": otherConf.get("maxFee"),            "freeHour": otherConf.get("freeHour")        }        controlParam = {            "in": otherConf.get("in"),            "out": otherConf.get("out"),        }        try:            gateDoorArray = self._get_device_doors()        except Exception as e:            logger.warning("[get_dev_setting] devNo = {} get device settings error = {}".format(self.device.devNo, e))            gateDoorArray = []        data = {            # 账号配置            "customer": otherConf.get("customer", ""),            "operator": otherConf.get("operator", ""),            "password": otherConf.get("password", ""),            "priceParam": priceParam,            "controlParam": controlParam,            "gateDoorArray": gateDoorArray        }        return data    def check_dev_status(self, attachParas=None):        """        检查设备状态 主要检查设备是否是在线的        """        result = self.oneCardGate.Door.get_sn_details(self._sn)        online = result["RetData"]["Online"]        if online != DoorOnlineStatus.ONLINE:            raise ServiceException({"result": 2, "description": u"当前设备已离线,请联系设备管理员"})    def set_device_function_param(self, request, lastSetConf):        """        设置保存服务器的参数        """        otherConf = self.device.otherConf        # 账号参数设置        if "customer" in request.POST and "operator" in request.POST and "password" in request.POST:            otherConf["customer"] = request.POST["customer"]            otherConf["operator"] = request.POST["operator"]            otherConf["password"] = request.POST["password"]        else:            # 其余的参数设置            priceParam = request.POST.get("priceParam", dict())            otherConf["price"] = priceParam["price"]            otherConf["cycle"] = priceParam["cycle"]            otherConf["maxFee"] = priceParam["maxFee"]            otherConf["freeHour"] = priceParam["freeHour"]            controlParam = request.POST.get("controlParam", dict())            otherConf["in"] = controlParam["in"]            otherConf["out"] = controlParam["out"]        Device.objects.get(devNo=self.device.devNo).update(otherConf=otherConf)        Device.invalid_device_cache(self.device.devNo)        return    def is_port_can_use(self, port, canAdd=False):        if port in ['0', '1']:            return True, ""        else:            return False, u"错误的道闸设备二维码"
 |