async def ws_start(self, data): a = "wss://" + data["host"] + "/websocket" protocol = "ws://" if not data["ssl"] else "wss://" ssl_context = None if not data["ssl"] else self.state.sslcontext uri = protocol + data["host"] + "/websocket" mlog.debug(f"WSClient: Connecting to {uri}") async with websockets.connect( uri, ssl=ssl_context ) as websocket: self.ws = websocket asyncio.ensure_future(self.ws_login(data)) loop = asyncio.get_event_loop() await loop.create_task(self.ws_recv())
async def camera_request_send(state, src, dst, encoded, objects, ts, error, snapshots, tmp, dbg): msg = util.serialize( "camera_request_done", { "src": src, "dst": dst, "image": encoded, "objects": objects, "timestamp": ts, "error": error, "snapshots": snapshots, "temp": tmp }) mlog.debug(dbg % len(msg)) await state["ws"].send(msg)
async def ws_recv(self): commands = { "account_login_reply" : cloud.login_reply, "camera_request" : cloud.camera_request, "camera_reinit" : cloud.camera_reinit } while(self.state.closed == False): try: msg = await self.ws.recv() mlog.debug("Received: %d bytes" % len(msg)) except websockets.exceptions.ConnectionClosedOK as e: mlog.debug(e) self.state.closed = True break except websockets.exceptions.ConnectionClosed as e: mlog.debug(e) self.state.closed = True break
async def login_reply(state, payload): if "error" in payload and payload["error"] == "ok": state["logged"] = True else: mlog.debug("Login failed") state["closed"] = True
async def ws_login(self, data): if(self.state.closed == True): return msg = cloud.login_msg(data["user"], data["pass"], data["device"]) mlog.debug("Attempt to login") await self.ws.send(msg)