generated from farm-ng/amiga-app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
132 lines (104 loc) · 4.42 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright (c) farm-ng, inc.
#
# Licensed under the Amiga Development Kit License (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://github.com/farm-ng/amiga-dev-kit/blob/main/LICENSE
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import argparse
import asyncio
import json
from pathlib import Path
import uvicorn
from farm_ng.core.event_client import EventClient
from farm_ng.core.event_service_pb2 import EventServiceConfigList
from farm_ng.core.event_service_pb2 import SubscribeRequest
from farm_ng.core.events_file_reader import proto_from_json_file
from farm_ng.core.uri_pb2 import Uri
from fastapi import FastAPI
from fastapi import WebSocket
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from google.protobuf.json_format import MessageToJson
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
# to store the events clients
clients: dict[str, EventClient] = {}
@app.get("/list_uris")
async def list_uris() -> JSONResponse:
"""Coroutine to list all the uris from all the event services
Returns:
JSONResponse: the list of uris as a json.
Usage:
curl -X GET "http://localhost:8042/list_uris"
"""
all_uris = {}
for service_name, client in clients.items():
# get the list of uris from the event service
uris: list[Uri] = []
try:
# NOTE: some services may not be available, so we need to handle the timeout
uris = await asyncio.wait_for(client.list_uris(), timeout=0.1)
except asyncio.TimeoutError:
continue
# convert the uris to a dict, where the key is the uri full path
# and the value is the uri proto as a json string
for uri in uris:
all_uris[f"{service_name}{uri.path}"] = json.loads(MessageToJson(uri))
return JSONResponse(content=all_uris, status_code=200)
@app.websocket("/subscribe/{service_name}/{uri_path}")
async def subscribe(websocket: WebSocket, service_name: str, uri_path: str, every_n: int = 1):
"""Coroutine to subscribe to an event service via websocket.
Args:
websocket (WebSocket): the websocket connection
service_name (str): the name of the event service
uri_path (str): the uri path to subscribe to
every_n (int, optional): the frequency to receive events. Defaults to 1.
Usage:
ws = new WebSocket("ws://localhost:8042/subscribe/oak0/left
"""
client: EventClient = clients[service_name]
await websocket.accept()
async for _, message in client.subscribe(
request=SubscribeRequest(uri=Uri(path=f"/{uri_path}"), every_n=every_n), decode=True
):
await websocket.send_json(MessageToJson(message))
await websocket.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=Path, required=True, help="config file")
parser.add_argument("--port", type=int, default=8042, help="port to run the server")
parser.add_argument("--debug", action="store_true", help="debug mode")
args = parser.parse_args()
# NOTE: we only serve the react app in debug mode
if not args.debug:
react_build_directory = Path(__file__).parent / "ts" / "dist"
app.mount(
"/",
StaticFiles(directory=str(react_build_directory.resolve()), html=True),
)
# config list with all the configs
config_list: EventServiceConfigList = proto_from_json_file(args.config, EventServiceConfigList())
for config in config_list.configs:
if config.port == 0:
continue # skip invalid service configs
# create the event client
client = EventClient(config=config)
# add the client to the clients dict
clients[config.name] = client
# run the server
uvicorn.run(app, host="0.0.0.0", port=args.port) # noqa: S104