feat(edge): env-based Config for detectors and transport
This commit is contained in:
parent
bfff96ddbd
commit
a63a5918cc
2 changed files with 66 additions and 0 deletions
43
edge/config.py
Normal file
43
edge/config.py
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from edge.detectors.fall import FallConfig
|
||||||
|
from edge.detectors.immobility import ImmobilityConfig
|
||||||
|
from edge.detectors.ghost import GhostConfig
|
||||||
|
from edge.detectors.zone import ZoneConfig
|
||||||
|
from edge.engine import EngineConfig
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Config:
|
||||||
|
device_id: str
|
||||||
|
camera_id: str
|
||||||
|
cafe24_url: str
|
||||||
|
secret: str
|
||||||
|
fall: FallConfig
|
||||||
|
immobility: ImmobilityConfig
|
||||||
|
ghost: GhostConfig
|
||||||
|
zone: ZoneConfig
|
||||||
|
engine: EngineConfig
|
||||||
|
|
||||||
|
|
||||||
|
def _f(env: dict, key: str, default: float) -> float:
|
||||||
|
return float(env[key]) if key in env else default
|
||||||
|
|
||||||
|
|
||||||
|
def load_config(env: dict) -> Config:
|
||||||
|
camera_id = env.get("CAMERA_ID", "cam-01")
|
||||||
|
fall = FallConfig(
|
||||||
|
prone_confirm_s=_f(env, "FALL_PRONE_S", 3.0),
|
||||||
|
no_motion_s=_f(env, "FALL_NO_MOTION_S", 10.0),
|
||||||
|
)
|
||||||
|
immobility = ImmobilityConfig(immobile_s=_f(env, "IMMOBILE_S", 120.0))
|
||||||
|
ghost = GhostConfig(reappear_s=_f(env, "GHOST_REAPPEAR_S", 10.0))
|
||||||
|
zone = ZoneConfig(debounce_s=_f(env, "ZONE_DEBOUNCE_S", 0.75))
|
||||||
|
engine = EngineConfig(camera_id=camera_id, cooldown_s=_f(env, "COOLDOWN_S", 30.0))
|
||||||
|
return Config(
|
||||||
|
device_id=env.get("DEVICE_ID", "pi-unknown"),
|
||||||
|
camera_id=camera_id,
|
||||||
|
cafe24_url=env.get("CAFE24_URL", ""),
|
||||||
|
secret=env.get("DEVICE_SECRET", ""),
|
||||||
|
fall=fall, immobility=immobility, ghost=ghost, zone=zone, engine=engine,
|
||||||
|
)
|
||||||
23
tests/test_config.py
Normal file
23
tests/test_config.py
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
from edge.config import load_config
|
||||||
|
|
||||||
|
def test_defaults_when_env_empty():
|
||||||
|
cfg = load_config({})
|
||||||
|
assert cfg.device_id == "pi-unknown"
|
||||||
|
assert cfg.fall.prone_confirm_s == 3.0
|
||||||
|
assert cfg.fall.no_motion_s == 10.0
|
||||||
|
|
||||||
|
def test_env_overrides():
|
||||||
|
env = {
|
||||||
|
"DEVICE_ID": "pi-w1", "CAMERA_ID": "cam-9",
|
||||||
|
"CAFE24_URL": "https://x/api/events", "DEVICE_SECRET": "s",
|
||||||
|
"FALL_PRONE_S": "2.0", "FALL_NO_MOTION_S": "8",
|
||||||
|
"IMMOBILE_S": "90", "COOLDOWN_S": "45",
|
||||||
|
}
|
||||||
|
cfg = load_config(env)
|
||||||
|
assert cfg.device_id == "pi-w1"
|
||||||
|
assert cfg.camera_id == "cam-9"
|
||||||
|
assert cfg.fall.prone_confirm_s == 2.0
|
||||||
|
assert cfg.fall.no_motion_s == 8.0
|
||||||
|
assert cfg.immobility.immobile_s == 90.0
|
||||||
|
assert cfg.engine.cooldown_s == 45.0
|
||||||
|
assert cfg.engine.camera_id == "cam-9"
|
||||||
Loading…
Reference in a new issue