oneCardGate.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # coding=utf-8
  2. import json
  3. import time
  4. import logging
  5. import typing
  6. from apps.web.core.adapter.base import SmartBox
  7. from apps.web.core.exceptions import ServiceException
  8. from apps.web.device.models import Device
  9. from library.yinkayo import FCardWeb, DoorOnlineStatus
  10. logger = logging.getLogger(__name__)
  11. class OneCardGate(SmartBox):
  12. def __init__(self, device):
  13. super(OneCardGate, self).__init__(device)
  14. self._oneCardGate = None # type: typing.Optional[None, FCardWeb]
  15. self._sn, self._in, self._out = self._load_device()
  16. def _load_device(self):
  17. sn = self.device.devNo
  18. inId = self.device.otherConf.get("IN")
  19. outID = self.device.otherConf.get("OUT")
  20. return sn, inId, outID
  21. def _load_gate(self):
  22. configs = self.device.otherConf
  23. loginParams = configs.get("customer"), configs.get("operator"), configs.get("password")
  24. if not all(loginParams):
  25. raise ServiceException({"result": 2, "description": u"道闸设备登录参数尚未配置,请联系系统管理员!"})
  26. oneCardGate = FCardWeb(*loginParams)
  27. self._oneCardGate = oneCardGate
  28. def _get_device_doors(self):
  29. pageIndex, pageSize = 1, 10
  30. doors = list()
  31. while True:
  32. result = self.oneCardGate.Door.get_doors(1, 10)
  33. total = result["total"]
  34. rows = result["rows"]
  35. for row in rows:
  36. if row["DoorSN"] != self._sn:
  37. continue
  38. doors.append({"doorName": row["dName"], "doorID": row["DoorID"]})
  39. if pageIndex * pageSize >= total:
  40. break
  41. pageSize += 10
  42. return doors
  43. def _start(self, doorID):
  44. result = self.oneCardGate.Door.open(doorID)
  45. taskID = result['RetData'][0]['tID']
  46. timeout = 15
  47. startTime = int(time.time())
  48. while int(time.time()) - startTime < timeout:
  49. time.sleep(2)
  50. tResult = self.oneCardGate.Door.get_operate_record(taskID)
  51. status = tResult["RetData"][0]["TaskStatus"]
  52. if self.oneCardGate.Door.is_task_init(status) or self.oneCardGate.Door.is_task_ing(status):
  53. continue
  54. if self.oneCardGate.Door.is_task_fail(status):
  55. raise ServiceException({"result": 2, "description": u"启动失败"})
  56. if self.oneCardGate.Door.is_task_timeout(status):
  57. raise ServiceException({"result": 2, "description": u"启动超时(1001)"})
  58. if self.oneCardGate.Door.is_task_done(status):
  59. return True
  60. else:
  61. raise ServiceException({"result": 2, "description": u"启动超时(1002)"})
  62. @property
  63. def oneCardGate(self):
  64. if not self._oneCardGate:
  65. self._load_gate()
  66. return self._oneCardGate
  67. def start(self, packageId, openId=None, attachParas={}):
  68. control = attachParas.get("control", dict())
  69. doorID = control.get("doorID")
  70. if not doorID:
  71. raise ServiceException({"result": 2, "description": u"设备配置错误,请联系经销商"})
  72. return self._start(doorID)
  73. def get_dev_setting(self):
  74. otherConf = self.device.otherConf
  75. priceParam = {
  76. "price": otherConf.get("price"),
  77. "cycle": otherConf.get("cycle"),
  78. "maxFee": otherConf.get("maxFee"),
  79. "freeHour": otherConf.get("freeHour")
  80. }
  81. controlParam = {
  82. "in": otherConf.get("in"),
  83. "out": otherConf.get("out"),
  84. }
  85. try:
  86. gateDoorArray = self._get_device_doors()
  87. except Exception as e:
  88. logger.warning("[get_dev_setting] devNo = {} get device settings error = {}".format(self.device.devNo, e))
  89. gateDoorArray = []
  90. data = {
  91. # 账号配置
  92. "customer": otherConf.get("customer", ""),
  93. "operator": otherConf.get("operator", ""),
  94. "password": otherConf.get("password", ""),
  95. "priceParam": priceParam,
  96. "controlParam": controlParam,
  97. "gateDoorArray": gateDoorArray
  98. }
  99. return data
  100. def check_dev_status(self, attachParas=None):
  101. """
  102. 检查设备状态 主要检查设备是否是在线的
  103. """
  104. result = self.oneCardGate.Door.get_sn_details(self._sn)
  105. online = result["RetData"]["Online"]
  106. if online != DoorOnlineStatus.ONLINE:
  107. raise ServiceException({"result": 2, "description": u"当前设备已离线,请联系设备管理员"})
  108. def set_device_function_param(self, request, lastSetConf):
  109. """
  110. 设置保存服务器的参数
  111. """
  112. otherConf = self.device.otherConf
  113. # 账号参数设置
  114. if "customer" in request.POST and "operator" in request.POST and "password" in request.POST:
  115. otherConf["customer"] = request.POST["customer"]
  116. otherConf["operator"] = request.POST["operator"]
  117. otherConf["password"] = request.POST["password"]
  118. else:
  119. # 其余的参数设置
  120. priceParam = request.POST.get("priceParam", dict())
  121. otherConf["price"] = priceParam["price"]
  122. otherConf["cycle"] = priceParam["cycle"]
  123. otherConf["maxFee"] = priceParam["maxFee"]
  124. otherConf["freeHour"] = priceParam["freeHour"]
  125. controlParam = request.POST.get("controlParam", dict())
  126. otherConf["in"] = controlParam["in"]
  127. otherConf["out"] = controlParam["out"]
  128. Device.objects.get(devNo=self.device.devNo).update(otherConf=otherConf)
  129. Device.invalid_device_cache(self.device.devNo)
  130. return
  131. def is_port_can_use(self, port, canAdd=False):
  132. if port in ['0', '1']:
  133. return True, ""
  134. else:
  135. return False, u"错误的道闸设备二维码"