diff --git a/config.py b/config.py index eaa0437..e75acc8 100644 --- a/config.py +++ b/config.py @@ -56,7 +56,7 @@ class Config: ret_val.append( TriggerTuple( name=name, - interval_in_seconds=interval_in_seconds + interval_in_seconds=int(interval_in_seconds) ) ) diff --git a/cpu_governor_auto_adjust.py b/cpu_governor_auto_adjust.py index 30466af..1cfd0ea 100755 --- a/cpu_governor_auto_adjust.py +++ b/cpu_governor_auto_adjust.py @@ -1,23 +1,27 @@ #!/usr/bin/env python3 +import asyncio from pathlib import Path from logger import getLogger from mapping import trigger_mapping from config import Config from governor import Governor +from schedule import TriggerScheduler -def main() -> None: +async def main() -> None: basepath = Path(__file__).parent.resolve() config = Config(basepath) log = getLogger('main', loglevel=config.loglevel.upper()) if config.testmode: log.warning("starting in testmode, cpu adjustments have been disabled") - triggers = [] - for triggertuple in config.triggertuples: - triggers.append( - trigger_mapping[triggertuple.name](triggertuple.name, config) - ) governor = Governor(config) + scheduler = TriggerScheduler(config) + for triggertuple in config.triggertuples: + _trigger = trigger_mapping[triggertuple.name](config) + scheduler.start_trigger(_trigger) + + await scheduler.run() + if __name__ == "__main__": - main() + asyncio.run(main()) diff --git a/cpu_governor_auto_adjust.xml b/cpu_governor_auto_adjust.xml index 3044197..89d8cd9 100644 --- a/cpu_governor_auto_adjust.xml +++ b/cpu_governor_auto_adjust.xml @@ -4,8 +4,8 @@ true - roon - 1 + Roon + 2 diff --git a/governor.py b/governor.py index 9f32036..06dcfde 100644 --- a/governor.py +++ b/governor.py @@ -9,18 +9,21 @@ class Governor(AppClass): super().__init__(_config) @cached_property - def cpufreq(self) -> cpuFreq: + def _cpufreq(self) -> cpuFreq: return cpuFreq() def set_governor(self, governor_name: str) -> None: - self.log.debug("setting governor to %s by using command self.cpufreq.set_governors(%s)", governor_name) + self.log.debug( + "setting cpu governor to %s by using command self._cpufreq.set_governors(%s)", + governor_name, governor_name + ) if self._config.testmode: - self.log.warning("application is running in testmode, governor not set") + self.log.warning("application is running in testmode, cpu governor not set") return None - if governor_name not in self.cpufreq.available_governors: + if governor_name not in self._cpufreq.available_governors: self.log.error("governor %s not supported by cpu", governor_name) return None - self.cpufreq.set_governors(governor_name) + self._cpufreq.set_governors(governor_name) diff --git a/mapping.py b/mapping.py index 8ac7bdb..1ffc331 100644 --- a/mapping.py +++ b/mapping.py @@ -2,6 +2,6 @@ from types import MappingProxyType from triggers.roon import RoonTrigger _dict_mapping = { - 'roon': RoonTrigger + 'Roon': RoonTrigger } trigger_mapping = MappingProxyType(_dict_mapping) \ No newline at end of file diff --git a/schedule.py b/schedule.py index e331cf4..f6801bb 100644 --- a/schedule.py +++ b/schedule.py @@ -1,6 +1,36 @@ +from app_class import AppClass +from config import Config +from trigger import Trigger import asyncio -class TriggerScheduler: - def __init__(self): - pass +class TriggerScheduler(AppClass): + def __init__(self, _config: Config) -> None: + super().__init__(_config) + self.tasks = [] + + async def trigger(self, _trigger: Trigger) -> None: + """Run a trigger with a specific name at a given interval.""" + while True: + self.log.info("Trigger %s is running", _trigger.name) + _trigger.run() + await asyncio.sleep(_trigger.config.interval_in_seconds) + + def start_trigger(self, _trigger: Trigger) -> None: + """Start a new trigger.""" + task = asyncio.create_task(self.trigger(_trigger)) + self.tasks.append(task) + + async def stop_triggers(self) -> None: + """Stop all triggers.""" + for task in self.tasks: + task.cancel() + await asyncio.gather(*self.tasks, return_exceptions=True) + + async def run(self) -> None: + """Run the scheduler and keep it alive until stopped.""" + try: + while True: + await asyncio.sleep(1) # Keep the main function alive + except KeyboardInterrupt: + await self.stop_triggers() diff --git a/trigger.py b/trigger.py index b2c78dd..bf50e4d 100644 --- a/trigger.py +++ b/trigger.py @@ -2,9 +2,9 @@ from app_class import AppClass from config import Config, TriggerTuple, MissingConfig class Trigger(AppClass): - def __init__(self, name: str, _config: Config) -> None: + def __init__(self, _config: Config) -> None: super().__init__(_config) - self.name = name + self.name = "name has to be filled in inherited class and this name has to match the name from config file" @property def config(self) -> TriggerTuple: @@ -12,3 +12,7 @@ class Trigger(AppClass): if _config is None: raise MissingConfig(f"Trigger {self.name} hasn't been properly configured") return _config + + def run(self) -> bool: + # return True if Trigger is valid + return False diff --git a/triggers/roon.py b/triggers/roon.py index fb3c948..7e0344a 100644 --- a/triggers/roon.py +++ b/triggers/roon.py @@ -2,6 +2,11 @@ from trigger import Trigger from config import Config class RoonTrigger(Trigger): - def __init__(self, name: str, _config: Config) -> None: - super().__init__(name, _config) + def __init__(self, _config: Config) -> None: + super().__init__(_config) + self.name = "Roon" + + def run(self) -> bool: + self.log.info("run check code") + return False \ No newline at end of file