Fixed action queue

This commit is contained in:
Maximilian Giller 2023-12-23 18:57:55 +01:00
parent 123ee48529
commit fa16909572
4 changed files with 29 additions and 23 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
__pycache__

View file

@ -10,15 +10,12 @@ class QueueState(Enum):
class ActionQueue: class ActionQueue:
def __init__(self, idle_action=None) -> None: def __init__(self) -> None:
self.queued_actions: list = [] self.queued_actions: list = []
self.idle_action: Optional[any] = None self.idle_action: Optional[any] = None
self.state = QueueState.STOPPED self.state = QueueState.IDLE
self.background_task = None self.background_task = None
if idle_action:
self.set_idle_action(idle_action)
async def run_queue(self): async def run_queue(self):
while len(self.queued_actions) > 0: while len(self.queued_actions) > 0:
self.state = QueueState.POPPING self.state = QueueState.POPPING
@ -26,7 +23,8 @@ class ActionQueue:
await action[0](*(action[1]), **(action[2])) await action[0](*(action[1]), **(action[2]))
self.state = QueueState.IDLE self.state = QueueState.IDLE
await self.idle_action() if self.idle_action:
await self.idle_action()
async def stop_queue(self): async def stop_queue(self):
if self.background_task is None: if self.background_task is None:

View file

@ -9,7 +9,9 @@ from config import climate_log_file
from handler.action_queue import ActionQueue from handler.action_queue import ActionQueue
from handler.history import get_recent_entries from handler.history import get_recent_entries
queue = ActionQueue(display_time) queue = ActionQueue()
queue.set_idle_action(display_time)
app = FastAPI() app = FastAPI()
origins = [ origins = [

View file

@ -1,39 +1,44 @@
import asyncio import asyncio
from handler.action_queue import ActionQueue from handler.action_queue import ActionQueue
import logging
logging.getLogger().setLevel(logging.INFO)
async def idle_a(): async def idle_a():
while True: while True:
print("Idleling ...") logging.info("Idleling ...")
await asyncio.sleep(1) await asyncio.sleep(1)
async def action_a(): async def action_a():
print("Starting action A") logging.info("Starting action A")
await asyncio.sleep(1) await asyncio.sleep(1)
print("Ended action A") logging.info("Ended action A")
async def action_b(): async def action_b():
print("Starting action B") logging.info("Starting action B")
await asyncio.sleep(1) await asyncio.sleep(1)
print("Ended action B") logging.info("Ended action B")
async def main(): 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 asyncio.sleep(7)
await queue.add_action_to_queue(action_a) await queue.add_action_to_queue(action_a)
await queue.add_action_to_queue(action_b) await queue.add_action_to_queue(action_b)
await asyncio.sleep(10) await asyncio.sleep(10)
asyncio.run(main)
asyncio.run(main())