Beispiel #1
0
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    id = uuid.uuid4()
    open_sockets[id] = websocket
    fake_png = create_fake_file()
    await websocket.send_bytes(fake_png.getvalue())
    fake_png.close()

    with get_hardware() as (vid, relay):
        read_task = asyncio.create_task(websocket.receive_text())

        while True:
            try:
                write_task = asyncio.create_task(
                    send_video_frame(vid, websocket))

                done, pending = await asyncio.wait(
                    [read_task, write_task],
                    return_when=asyncio.FIRST_COMPLETED)

                if read_task in done:
                    # if the read task is finished, process the result and start a new read and wait for the current write
                    # otherwise, the write is done and just loop again on the same read
                    result = read_task.result()
                    resource, action = tuple(result.split('|'))
                    if resource == 'light':
                        if action == 'on':
                            relay.on()
                        else:
                            relay.off()
                    read_task = asyncio.create_task(websocket.receive_text())
                    if pending:
                        await asyncio.wait(pending)

            except Exception as e:
                print(e)
                break

    print('Websocket disconnected')
    del open_sockets[id]
Beispiel #2
0
async def ws_stream_iracing_data(
    websocket: WebSocket, ws_connection_manager=Depends(get_ws_manager)):
    """
    Stream current iRacing data over a websocket connection. 
    TODO Get framerate from user config.
    """
    await ws_connection_manager.connect(websocket)

    try:
        while True:
            websocket.receive_text()

            data = get_iracing_data(raw=True)

            if data:
                await ws_connection_manager.send_json(data, websocket)

            await asyncio.sleep(0.03)
    except (WebSocketDisconnect, ConnectionClosedError, ConnectionClosedOK,
            RuntimeError):
        await ws_connection_manager.disconnect(websocket)
        return
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    try:
        if app.camera is None:
            app.camera = cv.VideoCapture(0)
            if app.camera.isOpened():
                # create tracker with chosen algorithm
                app.tracker = Tracker(app.camera)
                await websocket.send_text("LookingFor")
        while app.camera.isOpened():
            # read frame and run step of algorithm
            start = time.time()
            _, frame = app.camera.read()
            gesture = app.tracker.algorithm.run(frame)
            # if the glove is lost
            if gesture is None:
                app.tracker.update_init_loc(app.camera)
            color = app.tracker.color.convert_gesture(gesture)
            # send color | "LookingFor"
            await websocket.send_text(color)
            # confirmation that client recived data, if there is no answer program stopped
            data = await asyncio.wait_for(websocket.receive_text(), timeout=5)
            if data == "FindMyGlove":
                app.tracker.update_init_loc(app.camera)
            elif data != "Received":
                app.tracker.change_algorithm(data, app.camera)
            if app.debug:
                app.tracker.algorithm.draw(frame)
                cv.waitKey(50)
    except Exception:
        print("Connection closed")
    finally:
        # close the websocket and the camera
        await websocket.close()
        if app.camera is None:
            app.camera.release()
            app.camera = None
        if app.debug:
            cv.destroyAllWindows()