Implemented start up actions and tasks
This commit is contained in:
parent
91648b739f
commit
30571d2439
3 changed files with 7 additions and 84 deletions
|
@ -2,9 +2,9 @@ import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
class ActionQueue:
|
class ActionQueue:
|
||||||
def __init__(self) -> None:
|
def __init__(self, idle_action = None, *args, **kwargs) -> None:
|
||||||
self.queued_actions: asyncio.Queue = asyncio.Queue()
|
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.queue_task = asyncio.create_task(self.run_queue())
|
||||||
self.idle_action_task = None
|
self.idle_action_task = None
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ class ActionQueue:
|
||||||
async def run_queue(self):
|
async def run_queue(self):
|
||||||
while True:
|
while True:
|
||||||
try:
|
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))
|
self.idle_action_task = asyncio.create_task(self.run_action(self.idle_action))
|
||||||
|
|
||||||
action = await self.queued_actions.get()
|
action = await self.queued_actions.get()
|
||||||
|
@ -43,10 +43,6 @@ class ActionQueue:
|
||||||
await self.queued_actions.put((action, args, kwargs))
|
await self.queued_actions.put((action, args, kwargs))
|
||||||
|
|
||||||
async def set_idle_action(self, action, *args, **kwargs):
|
async def set_idle_action(self, action, *args, **kwargs):
|
||||||
if action is None:
|
self.idle_action = (action, args, kwargs)
|
||||||
self.idle_action = None
|
|
||||||
else:
|
|
||||||
self.idle_action = (action, args, kwargs)
|
|
||||||
|
|
||||||
await self.queued_actions.put(None)
|
await self.queued_actions.put(None)
|
||||||
|
|
||||||
|
|
11
src/main.py
11
src/main.py
|
@ -12,7 +12,7 @@ from handler.history import get_recent_entries
|
||||||
|
|
||||||
logging.getLogger().setLevel(logging.INFO)
|
logging.getLogger().setLevel(logging.INFO)
|
||||||
|
|
||||||
queue = ActionQueue()
|
queue = ActionQueue(matrix_display.show_current_time)
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
origins = [
|
origins = [
|
||||||
|
@ -105,10 +105,5 @@ async def display_message(body: dict):
|
||||||
return {"message": "Message displayed"}
|
return {"message": "Message displayed"}
|
||||||
|
|
||||||
|
|
||||||
# async def main():
|
if __name__ == "__main__":
|
||||||
# await queue.set_idle_action(display_time)
|
asyncio.create_task(log_temperature())
|
||||||
|
|
||||||
|
|
||||||
# if __name__ == "__main__":
|
|
||||||
# asyncio.create_task(main())
|
|
||||||
# asyncio.create_task(log_temperature())
|
|
||||||
|
|
|
@ -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)
|
|
Loading…
Reference in a new issue