From 30571d2439a94038950fee4ede0d650b522586ba Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Tue, 30 Jan 2024 20:43:25 +0100 Subject: [PATCH] Implemented start up actions and tasks --- src/handler/action_queue.py | 12 +++---- src/main.py | 11 ++---- src/testing.py | 68 ------------------------------------- 3 files changed, 7 insertions(+), 84 deletions(-) delete mode 100644 src/testing.py diff --git a/src/handler/action_queue.py b/src/handler/action_queue.py index 48d30fa..0b06284 100644 --- a/src/handler/action_queue.py +++ b/src/handler/action_queue.py @@ -2,9 +2,9 @@ import asyncio import logging class ActionQueue: - def __init__(self) -> None: + def __init__(self, idle_action = None, *args, **kwargs) -> None: self.queued_actions: asyncio.Queue = asyncio.Queue() - self.idle_action: tuple = None + self.idle_action: tuple = (idle_action, args, kwargs) self.queue_task = asyncio.create_task(self.run_queue()) self.idle_action_task = None @@ -17,7 +17,7 @@ class ActionQueue: async def run_queue(self): while True: try: - if self.queued_actions.empty() and self.idle_action is not None: + if self.queued_actions.empty() and self.idle_action[0] is not None: self.idle_action_task = asyncio.create_task(self.run_action(self.idle_action)) action = await self.queued_actions.get() @@ -43,10 +43,6 @@ class ActionQueue: await self.queued_actions.put((action, args, kwargs)) async def set_idle_action(self, action, *args, **kwargs): - if action is None: - self.idle_action = None - else: - self.idle_action = (action, args, kwargs) - + self.idle_action = (action, args, kwargs) await self.queued_actions.put(None) diff --git a/src/main.py b/src/main.py index 0331dcf..f00b77f 100644 --- a/src/main.py +++ b/src/main.py @@ -12,7 +12,7 @@ from handler.history import get_recent_entries logging.getLogger().setLevel(logging.INFO) -queue = ActionQueue() +queue = ActionQueue(matrix_display.show_current_time) app = FastAPI() origins = [ @@ -105,10 +105,5 @@ async def display_message(body: dict): return {"message": "Message displayed"} -# async def main(): -# await queue.set_idle_action(display_time) - - -# if __name__ == "__main__": -# asyncio.create_task(main()) -# asyncio.create_task(log_temperature()) +if __name__ == "__main__": + asyncio.create_task(log_temperature()) diff --git a/src/testing.py b/src/testing.py deleted file mode 100644 index 75b93cc..0000000 --- a/src/testing.py +++ /dev/null @@ -1,68 +0,0 @@ -import asyncio - -from fastapi import FastAPI -from fastapi.middleware.cors import CORSMiddleware -from handler.action_queue import ActionQueue -import logging - -logging.getLogger().setLevel(logging.INFO) - -queue = ActionQueue() -app = FastAPI() - -origins = [ - "http://localhost", - "http://localhost:8000", - "http://raspberrypi", - "http://192.168.178.84:8000", - "http://192.168.178.84", -] - -app.add_middleware( - CORSMiddleware, - allow_origins=origins, - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], -) - - - -async def idle_a(): - while True: - logging.info("Idleling AAA ...") - await asyncio.sleep(1) - - -async def idle_b(): - while True: - logging.info("Idleling BBB ...") - await asyncio.sleep(1) - - -async def action_a(): - logging.info("Starting action A") - await asyncio.sleep(1) - logging.info("Ended action A") - - -async def action_b(): - logging.info("Starting action B") - await asyncio.sleep(1) - logging.info("Ended action B") - -@app.post("/idle_a") -async def api_idle_a(): - await queue.set_idle_action(idle_a) - -@app.post("/idle_b") -async def api_idle_b(): - await queue.set_idle_action(idle_b) - -@app.post("/action_a") -async def api_action_a(): - await queue.add_action_to_queue(action_a) - -@app.post("/action_b") -async def api_action_b(): - await queue.add_action_to_queue(action_b)