init commit
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
"""High-level switch service selecting native/mock backend driver."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from python_app.hardware_full.switch_drivers import (
|
||||
H7992Driver,
|
||||
HMC349ADriver,
|
||||
MockSwitchDriver,
|
||||
SwitchDriverProtocol,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class SwitchService:
|
||||
"""Facade around concrete switch drivers."""
|
||||
|
||||
name: str
|
||||
positions: int
|
||||
mode: str = "mock"
|
||||
driver: str = "h7992"
|
||||
gpio_chip: str = "/dev/gpiochip0"
|
||||
pin_a: int = 17
|
||||
pin_b: int = 27
|
||||
invert_logic: bool = False
|
||||
default_position: int = 0
|
||||
_driver: SwitchDriverProtocol = field(init=False, repr=False)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
"""Create underlying driver based on configured mode and type."""
|
||||
self._driver = self._build_driver()
|
||||
|
||||
def open(self) -> None:
|
||||
"""Open underlying switch driver resources."""
|
||||
self._driver.open()
|
||||
|
||||
def close(self) -> None:
|
||||
"""Close underlying switch driver resources."""
|
||||
self._driver.close()
|
||||
|
||||
def switch_to(self, position: int) -> None:
|
||||
"""Switch to requested position."""
|
||||
self._driver.switch_to(position)
|
||||
|
||||
@property
|
||||
def current_position(self) -> int:
|
||||
"""Return current switch position reported by backend driver."""
|
||||
return self._driver.current_position
|
||||
|
||||
def _build_driver(self) -> SwitchDriverProtocol:
|
||||
"""Instantiate concrete driver according to configured mode and driver kind."""
|
||||
mode = self.mode.strip().lower()
|
||||
driver_kind = self.driver.strip().lower()
|
||||
|
||||
if mode == "mock":
|
||||
return MockSwitchDriver(
|
||||
name=self.name,
|
||||
positions=self.positions,
|
||||
default_position=self.default_position,
|
||||
)
|
||||
|
||||
if mode != "native":
|
||||
raise RuntimeError(f"Unsupported switch mode: {self.mode}")
|
||||
|
||||
if driver_kind == "h7992":
|
||||
return H7992Driver(
|
||||
name=self.name,
|
||||
positions=self.positions,
|
||||
default_position=self.default_position,
|
||||
gpio_chip=self.gpio_chip,
|
||||
pin_a=self.pin_a,
|
||||
pin_b=self.pin_b,
|
||||
)
|
||||
if driver_kind == "hmc349a":
|
||||
return HMC349ADriver(
|
||||
name=self.name,
|
||||
positions=self.positions,
|
||||
default_position=self.default_position,
|
||||
gpio_chip=self.gpio_chip,
|
||||
pin_a=self.pin_a,
|
||||
invert_logic=self.invert_logic,
|
||||
)
|
||||
|
||||
raise RuntimeError(f"Unsupported switch driver: {self.driver}")
|
||||
Reference in New Issue
Block a user