feat: add background task to log state transitions
Co-authored-by: aider (gemini/gemini-2.5-pro-preview-05-06) <aider@aider.chat>
This commit is contained in:
43
server.py
43
server.py
@@ -31,6 +31,7 @@ REQUEST_TIMEOUT_SECONDS = 5
|
|||||||
UNSURE_CONFIDENCE_THRESHOLD = 0.97
|
UNSURE_CONFIDENCE_THRESHOLD = 0.97
|
||||||
PREDICTION_HISTORY = []
|
PREDICTION_HISTORY = []
|
||||||
PREDICTION_HISTORY_MAX_LENGTH = 3
|
PREDICTION_HISTORY_MAX_LENGTH = 3
|
||||||
|
PREVIOUS_STATE = "unknown"
|
||||||
|
|
||||||
# --- Model Inference ---
|
# --- Model Inference ---
|
||||||
def get_prediction(model, image_bytes, device):
|
def get_prediction(model, image_bytes, device):
|
||||||
@@ -126,21 +127,44 @@ async def monitor_garage_door(app):
|
|||||||
await asyncio.sleep(5)
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
|
async def monitor_state_transitions(app):
|
||||||
|
"""Periodically checks for state transitions and logs them."""
|
||||||
|
global PREVIOUS_STATE
|
||||||
|
logging.info("Starting state transition monitoring task.")
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
await asyncio.sleep(5)
|
||||||
|
current_state = get_derived_state()
|
||||||
|
if current_state != "unknown" and current_state != PREVIOUS_STATE:
|
||||||
|
logging.info(f"State transitioned from '{PREVIOUS_STATE}' to '{current_state}'.")
|
||||||
|
PREVIOUS_STATE = current_state
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
logging.info("State transition monitoring task cancelled.")
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"An unexpected error occurred in the state monitoring task: {e}", exc_info=True)
|
||||||
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
# --- Web Server ---
|
# --- Web Server ---
|
||||||
|
def get_derived_state():
|
||||||
|
"""Derives the state from the prediction history."""
|
||||||
|
state = "unknown"
|
||||||
|
if len(PREDICTION_HISTORY) == PREDICTION_HISTORY_MAX_LENGTH:
|
||||||
|
if all(s == "open" for s in PREDICTION_HISTORY):
|
||||||
|
state = "open"
|
||||||
|
elif all(s == "closed" for s in PREDICTION_HISTORY):
|
||||||
|
state = "closed"
|
||||||
|
return state
|
||||||
|
|
||||||
|
|
||||||
async def handle_root(request):
|
async def handle_root(request):
|
||||||
"""Handler for the root GET request."""
|
"""Handler for the root GET request."""
|
||||||
return web.Response(text="hello world")
|
return web.Response(text="hello world")
|
||||||
|
|
||||||
async def handle_state(request):
|
async def handle_state(request):
|
||||||
"""Handler for the /state GET request."""
|
"""Handler for the /state GET request."""
|
||||||
state = "unknown"
|
state = get_derived_state()
|
||||||
|
|
||||||
if len(PREDICTION_HISTORY) == PREDICTION_HISTORY_MAX_LENGTH:
|
|
||||||
if all(s == "open" for s in PREDICTION_HISTORY):
|
|
||||||
state = "open"
|
|
||||||
elif all(s == "closed" for s in PREDICTION_HISTORY):
|
|
||||||
state = "closed"
|
|
||||||
|
|
||||||
return web.Response(text=state)
|
return web.Response(text=state)
|
||||||
|
|
||||||
async def on_startup(app):
|
async def on_startup(app):
|
||||||
@@ -162,13 +186,16 @@ async def on_startup(app):
|
|||||||
|
|
||||||
# Start background task
|
# Start background task
|
||||||
app['monitor_task'] = asyncio.create_task(monitor_garage_door(app))
|
app['monitor_task'] = asyncio.create_task(monitor_garage_door(app))
|
||||||
|
app['state_monitor_task'] = asyncio.create_task(monitor_state_transitions(app))
|
||||||
|
|
||||||
async def on_cleanup(app):
|
async def on_cleanup(app):
|
||||||
"""Actions to perform on application cleanup."""
|
"""Actions to perform on application cleanup."""
|
||||||
logging.info("Cleaning up...")
|
logging.info("Cleaning up...")
|
||||||
app['monitor_task'].cancel()
|
app['monitor_task'].cancel()
|
||||||
|
app['state_monitor_task'].cancel()
|
||||||
try:
|
try:
|
||||||
await app['monitor_task']
|
await app['monitor_task']
|
||||||
|
await app['state_monitor_task']
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
pass
|
pass
|
||||||
await app['client_session'].close()
|
await app['client_session'].close()
|
||||||
|
Reference in New Issue
Block a user