more work

This commit is contained in:
John Lancaster
2024-10-20 21:48:19 +00:00
parent 050fe75e71
commit 502c218c35
2 changed files with 34 additions and 61 deletions

View File

@@ -19,26 +19,24 @@ class ADSubsystem:
def __post_init__(self) -> None:
name = f'_{self.__class__.__name__.lower()}'
self.logger = getLogger(f'AppDaemon.{name}')
self.AD.starts.append(self.start)
if start_func := getattr(self, 'start', False):
self.AD.starts.append(start_func)
@property
def stopping(self) -> bool:
return self.stop.is_set()
def start(self):
raise NotImplementedError('Need to implement start for subsystem')
@dataclass
class Utility(ADSubsystem):
loop_rate: float = 1.0
loop_rate: float = 0.5
def start(self):
self.AD.create_task(self.loop(), 'Utility loop')
async def loop(self):
while not self.stopping:
self.logger.info('Looping...')
self.logger.debug('Looping...')
await asyncio.sleep(self.loop_rate)
self.logger.debug('Stopped utility loop')
@@ -46,14 +44,15 @@ class Utility(ADSubsystem):
@dataclass
class Plugin(ADSubsystem):
state: dict[str, int] = field(default_factory=dict)
update_rate: float = 30.0
update_rate: float = 5.0
def __post_init__(self) -> None:
super().__post_init__()
self.state['update_count'] = 0
def start(self):
self.AD.create_task(self.periodic_self_udpate(), 'Periodic plugin update')
self.AD.create_task(self.periodic_self_udpate(),
'Periodic plugin update')
async def periodic_self_udpate(self):
while not self.stopping:
@@ -73,6 +72,7 @@ class AppDaemon:
starts: list[Callable] = field(default_factory=list)
def __post_init__(self) -> None:
self.logger = logging.getLogger('AppDaemon')
self.utility = Utility(self, self.context.stop_event)
self.plugins['dummy'] = Plugin(self, self.context.stop_event)
@@ -81,6 +81,8 @@ class AppDaemon:
def start(self):
for start in self.starts:
subsystem = start.__qualname__.split('.')[0]
self.logger.debug(f'Starting {subsystem}')
start()