Browse Source

start pipboy api

main
gsd 6 months ago
parent
commit
a76a2b525a
  1. 1
      pipboyAPI/.gitignore
  2. 4
      pipboyAPI/api_models.py
  3. 64
      pipboyAPI/api_server.py
  4. 1
      pipboyAPI/system/.gitignore
  5. 1
      pipboyAPI/test/.gitignore
  6. 28
      pipboyAPI/test/pingpong.py
  7. 8
      pipboyIO/next/serialAX.py

1
pipboyAPI/.gitignore

@ -0,0 +1 @@
__pycache__

4
pipboyAPI/api_models.py

@ -0,0 +1,4 @@
from fastapi import FastAPI
class API_CORE:
app: FastAPI

64
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()

1
pipboyAPI/system/.gitignore

@ -0,0 +1 @@
__pycache__

1
pipboyAPI/test/.gitignore

@ -0,0 +1 @@
__pycache__

28
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]

8
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):

Loading…
Cancel
Save