diff --git a/pipboyAPI/.gitignore b/pipboyAPI/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/pipboyAPI/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/pipboyAPI/api_models.py b/pipboyAPI/api_models.py new file mode 100644 index 0000000..387fd3b --- /dev/null +++ b/pipboyAPI/api_models.py @@ -0,0 +1,4 @@ +from fastapi import FastAPI + +class API_CORE: + app: FastAPI diff --git a/pipboyAPI/api_server.py b/pipboyAPI/api_server.py new file mode 100644 index 0000000..8c5b7f3 --- /dev/null +++ b/pipboyAPI/api_server.py @@ -0,0 +1,64 @@ +#fs imports +from fastapi import FastAPI, HTTPException, WebSocket +from fastapi.responses import HTMLResponse +from uvicorn import run as UR +from contextlib import asynccontextmanager + +#sys +import asyncio +from typing import List, Dict +import os, sys + +#internal +from api_models import API_CORE + +class API(API_CORE): + exts = {} + + def __init__(self): + self.WORKDIR = os.path.dirname(os.path.abspath(__file__)) + print(f"Change workdir to {self.WORKDIR}") + os.chdir(self.WORKDIR) + + self.app = FastAPI(lifespan=self.lifespan) + self.extensionLoader(["test", "system"]) + + def run(self):#todo filter + UR(self.app, host="0.0.0.0", port=14088) + + def extensionLoader(self, search_paths = []): + if type(search_paths) == str: + search_paths = [search_paths] + + for path in search_paths: + print(f"Try found extensions in {path}") + if not os.path.exists(path) or not os.path.isdir(path): + print(f"Directory is not exists or not directory, skip") + continue + + sys.path.insert(0, path) + for extension in os.listdir(path): + extension, ext = os.path.splitext(extension) + if ext != ".py": + continue + print(f"Found ext: {extension}") + self.exts[f"{path}/{extension}"] = __import__(extension).API_EXTENSION(self) + sys.path.pop(0) + + print(f"Found {self.exts.keys().__len__()} extension") + for ext_path, ext in self.exts.items(): + print(f"Register routes") + ext.routes() + + @asynccontextmanager + async def lifespan(self, app: FastAPI): + print("server is started") + for ext_path, ext in self.exts.items(): + print(f"start backgrounds tasks in {ext_path}") + for task in ext.tasks: + asyncio.create_task(task()) + yield + print("server is shutdown now") + +if __name__ == "__main__": + API().run() \ No newline at end of file diff --git a/pipboyAPI/system/.gitignore b/pipboyAPI/system/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/pipboyAPI/system/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/pipboyAPI/test/.gitignore b/pipboyAPI/test/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/pipboyAPI/test/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/pipboyAPI/test/pingpong.py b/pipboyAPI/test/pingpong.py new file mode 100644 index 0000000..be52322 --- /dev/null +++ b/pipboyAPI/test/pingpong.py @@ -0,0 +1,28 @@ +from api_models import API_CORE +from time import time +import asyncio + +class API_EXTENSION: + def __init__(self, api): + self.api: API_CORE = api + print("PingPong") + self.response = { + "ping":"pong", + "ts": time() + } + + def routes(self): + @self.api.app.get("/api/ping") + async def call(): + return self.response + + @property + def tasks(self): + async def updateTs(): + while 1: + self.response["ts"] = time() + await asyncio.sleep(1) + + return [updateTs] + + \ No newline at end of file diff --git a/pipboyIO/next/serialAX.py b/pipboyIO/next/serialAX.py index 43e5e11..67043d1 100644 --- a/pipboyIO/next/serialAX.py +++ b/pipboyIO/next/serialAX.py @@ -72,13 +72,13 @@ class QE: def __call__(self, data): if data[2] == MOVEMENT.LEFT: - if DEBUGIO: - print(self.q) - subprocess.Popen(self.q) - if data[2] == MOVEMENT.RIGHT: if DEBUGIO: print(self.e) subprocess.Popen(self.e) + if data[2] == MOVEMENT.RIGHT: + if DEBUGIO: + print(self.q) + subprocess.Popen(self.q) class WS: def __init__(self):