diff --git a/.gitignore b/.gitignore index ef5bac5..c866a9b 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,6 @@ cython_debug/ #.idea/ .vscode/settings.json .vscode/launch.json + + +hue_bridge_registered.txt \ No newline at end of file diff --git a/example.conf.yaml b/example.conf.yaml new file mode 100644 index 0000000..444105b --- /dev/null +++ b/example.conf.yaml @@ -0,0 +1,7 @@ +modules: + hue: + - ip: "192.168.178.23" + matrixclock: + - name: wfwfo + - ip: "192.168.178.23" + diff --git a/requirements.txt b/requirements.txt index 3fd5bc7..e0195a9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,10 @@ phue # API fastapi -uvicorn[standard] \ No newline at end of file +uvicorn[standard] + +# Clients +requests + +# Config file +pyyaml diff --git a/src/core/mash.py b/src/core/mash.py new file mode 100644 index 0000000..b26630e --- /dev/null +++ b/src/core/mash.py @@ -0,0 +1,26 @@ +import yaml +from fastapi import FastAPI + +from core.module import Module + + +class MaSH: + def __init__(self, config_path: str) -> None: + self.server: FastAPI = FastAPI() + self.config: dict = None + + self._load_config_(config_path) + + def _load_config_(self, config_path: str) -> None: + try: + with open(config_path, "r", encoding="UTF-8") as fp: + self.config = yaml.safe_load(fp) + except FileNotFoundError: + raise f"Config file for MaSH server could not be opened at [{config_path}]." + + def add_module(self, module: Module) -> None: + module.add_routes(self.server) + self.server.include_router(module.get_router()) + + def run(self): + self.server.run() diff --git a/src/core/module.py b/src/core/module.py new file mode 100644 index 0000000..3120f67 --- /dev/null +++ b/src/core/module.py @@ -0,0 +1,9 @@ +from fastapi import FastAPI + + +class Module: + def __init__(self, module_id: str) -> None: + self.module_id = module_id + + def add_routes(self, server: FastAPI) -> None: + pass diff --git a/src/endpoints/handlers/hue.py b/src/hue/hue_adapter.py similarity index 90% rename from src/endpoints/handlers/hue.py rename to src/hue/hue_adapter.py index dd85153..0089ae9 100644 --- a/src/endpoints/handlers/hue.py +++ b/src/hue/hue_adapter.py @@ -3,7 +3,7 @@ from phue import Bridge from pathlib import Path -class HueHandler: +class HueAdapter: """Handler for Hue API calls.""" registered_ips_file = "hue_bridge_registered.txt" @@ -35,15 +35,15 @@ class HueHandler: def get_registered_ips(self) -> list: """Get a list of registered bridge IPs.""" - if not Path(HueHandler.registered_ips_file).is_file(): + if not Path(HueAdapter.registered_ips_file).is_file(): return [] - with open(HueHandler.registered_ips_file, "r") as f: - return f.readlines() + with open(HueAdapter.registered_ips_file, "r") as f: + return [ad.strip() for ad in f.readlines()] def register_bridge(self, bridge_ip: str): """Register a bridge IP.""" - with open(HueHandler.registered_ips_file, "a") as f: + with open(HueAdapter.registered_ips_file, "a") as f: f.write(bridge_ip + "\n") def list_scenes(self) -> dict: diff --git a/src/endpoints/hue.py b/src/hue/hue_module.py similarity index 69% rename from src/endpoints/hue.py rename to src/hue/hue_module.py index 74fd6d2..86c63c4 100644 --- a/src/endpoints/hue.py +++ b/src/hue/hue_module.py @@ -1,9 +1,25 @@ +from fastapi import FastAPI, APIRouter + +from hue.hue_adapter import HueAdapter +from ..core.module import Module from fastapi import APIRouter from fastapi.responses import HTMLResponse -from .handlers.hue import HueHandler -router = APIRouter() -hue = HueHandler("192.168.178.85") +router = APIRouter(tags=["hue"]) +hue = HueAdapter("192.168.178.85") + +########## Module ########## + + +class HueModule(Module): + def __init__(self) -> None: + super().__init__("hue") + + def add_routes(self, server: FastAPI) -> None: + server.include_router(router, prefix="/hue") + + +########## Routes ########## @router.get("/scenes", tags=["scene"]) diff --git a/src/hue_bridge_registered.txt b/src/hue_bridge_registered.txt deleted file mode 100644 index a73b77a..0000000 --- a/src/hue_bridge_registered.txt +++ /dev/null @@ -1,15 +0,0 @@ -192.168.178.85 -192.168.178.85 -192.168.178.85 -192.168.178.85 -192.168.178.85 -192.168.178.85 -192.168.178.85 -192.168.178.85 -192.168.178.85 -192.168.178.85 -192.168.178.85 -192.168.178.85 -192.168.178.85 -192.168.178.85 -192.168.178.85 diff --git a/src/main.py b/src/main.py index f88263b..f715e03 100644 --- a/src/main.py +++ b/src/main.py @@ -1,9 +1,11 @@ -from fastapi import FastAPI -from endpoints.hue import router as hue_router +from core.mash import MaSH +from hue.hue_module import HueModule +from matrix_clock.matrix_clock_module import MatrixClockModule -app = FastAPI() +mash: MaSH = MaSH("config.yaml") -app.include_router(hue_router, prefix="/hue", tags=["hue"]) +mash.add_module(HueModule()) +mash.add_module(MatrixClockModule()) if __name__ == "__main__": - app.run() + mash.run() diff --git a/src/matrix_clock/matrix_clock_adapter.py b/src/matrix_clock/matrix_clock_adapter.py new file mode 100644 index 0000000..204e9e1 --- /dev/null +++ b/src/matrix_clock/matrix_clock_adapter.py @@ -0,0 +1,13 @@ +import asyncio +import requests as r + + +class MatrixClockAdapter: + def __init__(self, ip_address: str) -> None: + self.ip_address = ip_address.strip("/") + + if not self.ip_address.startswith("http"): + self.ip_address = f"http://{self.ip_address}" + + async def turn_off(self): + await asyncio.run(r.post(f"{self.ip_address}/off")) diff --git a/src/matrix_clock/matrix_clock_module.py b/src/matrix_clock/matrix_clock_module.py new file mode 100644 index 0000000..526acc8 --- /dev/null +++ b/src/matrix_clock/matrix_clock_module.py @@ -0,0 +1,6 @@ +from core.module import Module + + +class MatrixClockModule(Module): + def __init__(self) -> None: + super().__init__("matrixclock")