class ScanQRCode(threading.Thread): def __init__(self, q): threading.Thread.__init__(self) self.running = True self.queue = q self.vs = None self.cache = Cache(ttl=5) def stopThread(self): self.running = False if not self.vs: self.vs.stop() def run(self): self.vs = VideoStream(usePiCamera=True).start() time.sleep(2) logging.info("scan qrcode thread is running...") while self.running: frame = self.vs.read() frame = imutils.resize(frame, width=400) barcodes = pyzbar.decode(frame) for barcode in barcodes: data = barcode.data if self.cache.has(data): continue self.cache.add(data, None) msg = Msg(msgType=MsgType.SCAN_QR_CODE, load=barcode.data) logging.info("scan qrcode thread put msg: " + str(msg.load)) self.queue.put(msg)
def test_cache_add(cache: Cache): """Test that cache.add() sets a cache key but only if it doesn't exist.""" key, value = ("key", "value") ttl = 2 cache.add(key, value, ttl) assert cache.get(key) == value assert cache.expire_times()[key] == ttl cache.add(key, value, ttl + 1) assert cache.expire_times()[key] == ttl cache.set(key, value, ttl + 1) assert cache.expire_times()[key] == ttl + 1
def main(): gpio_ctrl = GPIOCtrl() # openDoor("dd", "djf", "dkkd") q = queue.Queue(100) scanQRCodeThread = ScanQRCode(q=q) scanQRCodeThread.start() randomStrThread = RandomStr(q=q) randomStrThread.start() web3Thread = None db = LockDB(DB_NAME) info = db.getInfo() cache = Cache(ttl=60 * 5) channel = grpc.insecure_channel("localhost:50000") stub = lock_pb2_grpc.LockStub(channel) if info.get("contractAddr"): web3Thread = Web3Thread(address=info["contractAddr"], q=q) web3Thread.start() userMap = info.get("userMap", {}) logging.info("main thread running...") while True: msg = q.get() if msg.msgType == MsgType.SCAN_QR_CODE: codeStr = msg.load codeInfo = None try: codeInfo = json.loads(str(codeStr, encoding="utf-8")) except BaseException as e: logging.info(e) continue if not isinstance(info, dict): logging.info("continue qr code deal") continue logging.info(codeInfo) salt = codeInfo.get("salt") address = codeInfo.get("addr") sign = codeInfo.get("sign") if gpio_ctrl.is_bind_status and salt and address: info["salt"] = salt info["contractAddr"] = address db.update(info) if web3Thread: web3Thread.stopThread() web3Thread.join() # q.clear() web3Thread = Web3Thread(address=address, q=q) web3Thread.start() elif sign and address: logging.info(userMap) infoList = userMap.get(address) logging.info(infoList) if infoList: logging.warning("here") pubKey = infoList[1] decryptStr = "" resp = "" try: resp = stub.decrypt( lock_pb2.Request(pubKey=pubKey, sign=sign)) except Exception as e: logging.info(e) continue scanRanStr = resp.ranStr logging.info("decrypt str: " + scanRanStr) if cache.has(scanRanStr): openDoor(infoList[0], info.get("contractAddr", ""), info.get("salt", "")) elif msg.msgType == MsgType.ETH_UPDATE: userMap = decodeUserInfo(msg.load) info["userMap"] = userMap db.update(info) elif msg.msgType == MsgType.RandomStr: ranStr = msg.load if info.get("contractAddr"): cache.add(ranStr, None) salt = info["salt"] data = {} data["address"] = info.get("contractAddr", "") data["ranStr"] = ranStr data["salt"] = info.get("salt", "") signStr = "" for k in sorted(data.keys()): signStr = signStr + k + "=" + data[k] + ";" md5 = hashlib.md5() md5.update(signStr.encode(encoding="utf-8")) data["sign"] = md5.hexdigest() data.pop("salt") requests.post(url=BASE_URL + "/api/lock/randomstr", data=data, verify=False)