Refactoring everything
This commit is contained in:
parent
6bc569b794
commit
62709b6698
11 changed files with 102 additions and 29 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -160,3 +160,6 @@ cython_debug/
|
|||
#.idea/
|
||||
.vscode/settings.json
|
||||
.vscode/launch.json
|
||||
|
||||
|
||||
hue_bridge_registered.txt
|
7
example.conf.yaml
Normal file
7
example.conf.yaml
Normal file
|
@ -0,0 +1,7 @@
|
|||
modules:
|
||||
hue:
|
||||
- ip: "192.168.178.23"
|
||||
matrixclock:
|
||||
- name: wfwfo
|
||||
- ip: "192.168.178.23"
|
||||
|
|
@ -3,4 +3,10 @@ phue
|
|||
|
||||
# API
|
||||
fastapi
|
||||
uvicorn[standard]
|
||||
uvicorn[standard]
|
||||
|
||||
# Clients
|
||||
requests
|
||||
|
||||
# Config file
|
||||
pyyaml
|
||||
|
|
26
src/core/mash.py
Normal file
26
src/core/mash.py
Normal file
|
@ -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()
|
9
src/core/module.py
Normal file
9
src/core/module.py
Normal file
|
@ -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
|
|
@ -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:
|
|
@ -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"])
|
|
@ -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
|
12
src/main.py
12
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()
|
||||
|
|
13
src/matrix_clock/matrix_clock_adapter.py
Normal file
13
src/matrix_clock/matrix_clock_adapter.py
Normal file
|
@ -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"))
|
6
src/matrix_clock/matrix_clock_module.py
Normal file
6
src/matrix_clock/matrix_clock_module.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from core.module import Module
|
||||
|
||||
|
||||
class MatrixClockModule(Module):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("matrixclock")
|
Loading…
Reference in a new issue