From fa16909572f373daa469d6093327ed382975c4f2 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 23 Dec 2023 18:57:55 +0100 Subject: [PATCH] Fixed action queue --- .gitignore | 1 + src/handler/action_queue.py | 10 ++++------ src/main.py | 4 +++- src/testing.py | 37 +++++++++++++++++++++---------------- 4 files changed, 29 insertions(+), 23 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/src/handler/action_queue.py b/src/handler/action_queue.py index 51c3757..72f576e 100644 --- a/src/handler/action_queue.py +++ b/src/handler/action_queue.py @@ -10,15 +10,12 @@ class QueueState(Enum): class ActionQueue: - def __init__(self, idle_action=None) -> None: + def __init__(self) -> None: self.queued_actions: list = [] self.idle_action: Optional[any] = None - self.state = QueueState.STOPPED + self.state = QueueState.IDLE self.background_task = None - if idle_action: - self.set_idle_action(idle_action) - async def run_queue(self): while len(self.queued_actions) > 0: self.state = QueueState.POPPING @@ -26,7 +23,8 @@ class ActionQueue: await action[0](*(action[1]), **(action[2])) self.state = QueueState.IDLE - await self.idle_action() + if self.idle_action: + await self.idle_action() async def stop_queue(self): if self.background_task is None: diff --git a/src/main.py b/src/main.py index 3d78001..650926a 100644 --- a/src/main.py +++ b/src/main.py @@ -9,7 +9,9 @@ from config import climate_log_file from handler.action_queue import ActionQueue from handler.history import get_recent_entries -queue = ActionQueue(display_time) +queue = ActionQueue() +queue.set_idle_action(display_time) + app = FastAPI() origins = [ diff --git a/src/testing.py b/src/testing.py index 5d5dea0..f062b3e 100644 --- a/src/testing.py +++ b/src/testing.py @@ -1,39 +1,44 @@ import asyncio from handler.action_queue import ActionQueue +import logging + +logging.getLogger().setLevel(logging.INFO) async def idle_a(): while True: - print("Idleling ...") + logging.info("Idleling ...") await asyncio.sleep(1) + async def action_a(): - print("Starting action A") + logging.info("Starting action A") await asyncio.sleep(1) - print("Ended action A") - + logging.info("Ended action A") + + async def action_b(): - print("Starting action B") + logging.info("Starting action B") await asyncio.sleep(1) - print("Ended action B") - - - - - - - + logging.info("Ended action B") async def main(): - queue = ActionQueue(idle_a) + logging.info("Starting main") + + await action_a() + + logging.info("Starting action queue") + queue = ActionQueue() + + await queue.set_idle_action(idle_a) await asyncio.sleep(7) await queue.add_action_to_queue(action_a) await queue.add_action_to_queue(action_b) - await asyncio.sleep(10) -asyncio.run(main) + +asyncio.run(main())