Skip to content

Commit

Permalink
Add routes to enable/disable fake states
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Oct 28, 2024
1 parent 7822e37 commit 4b43b45
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
57 changes: 56 additions & 1 deletion src/lvmapi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import os

import taskiq_fastapi
from fastapi import FastAPI, Request
from fastapi import FastAPI, HTTPException, Request

from lvmapi import auth
from lvmapi.broker import broker, broker_shutdown, broker_startup
Expand Down Expand Up @@ -73,11 +73,66 @@ async def get_id_route(request: Request):
app.state.fake_states = {
"wind_alert": False,
"humidity_alert": False,
"rain_alert": False,
"door_alert": False,
"is_day": False,
}


@app.get("/fake-states/enable", include_in_schema=False)
async def route_get_enable_states():
"""Enable fake states."""

app.state.use_fake_states = True

return {"use_fake_states": app.state.use_fake_states}


@app.get("/fake-states/disable", include_in_schema=False)
async def route_get_disable_states():
"""Disable fake states."""

app.state.use_fake_states = False

return {"use_fake_states": app.state.use_fake_states}


@app.get("/fake-states/set/{state}/{value}", include_in_schema=False)
async def route_get_set_fake_state(state: str, value: bool):
"""Sets a fake state."""

if state not in app.state.fake_states:
raise HTTPException(400, f"Invalid state {state!r}")

app.state.fake_states[state] = value

return {"state": state, "value": app.state.fake_states[state]}


@app.get("/fake-states/get", include_in_schema=False)
async def route_get_get_fake_state_all():
"""Queries the value of all fake states."""

return {
"use_fake_states": app.state.use_fake_states,
"fake_States": app.state.fake_states,
}


@app.get("/fake-states/get/{state}", include_in_schema=False)
async def route_get_get_fake_state(state: str):
"""Queries the value of a fake state."""

if state not in app.state.fake_states:
raise HTTPException(400, f"Invalid state {state!r}")

return {
"use_fake_states": app.state.use_fake_states,
"state": state,
"value": app.state.fake_states[state],
}


@app.get("/")
def root(request: Request):
return {}
11 changes: 9 additions & 2 deletions src/lvmapi/routers/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import warnings

import polars
from fastapi import APIRouter
from fastapi import APIRouter, Request
from pydantic import BaseModel

from lvmapi.tools.alerts import enclosure_alerts, spec_temperature_alerts
Expand Down Expand Up @@ -42,7 +42,7 @@ class AlertsSummary(BaseModel):
@router.get("")
@router.get("/")
@router.get("/summary")
async def summary() -> AlertsSummary:
async def summary(request: Request) -> AlertsSummary:
"""Summary of alerts."""

tasks: list[asyncio.Task] = []
Expand Down Expand Up @@ -100,6 +100,13 @@ async def summary() -> AlertsSummary:
rain_sensor_alarm = enclosure_alerts_response.get("rain_sensor_alarm", None)
door_alert = enclosure_alerts_response.get("door_alert", None)

# These fake states are just for testing.
if request.app.state.use_fake_states:
humidity_alert = request.app.state.fake_states["humidity_alert"]
wind_alert = request.app.state.fake_states["wind_alert"]
rain_sensor_alarm = request.app.state.fake_states["rain_alert"]
door_alert = request.app.state.fake_states["door_alert"]

return AlertsSummary(
humidity_alert=humidity_alert,
dew_point_alert=dew_point_alert,
Expand Down

0 comments on commit 4b43b45

Please sign in to comment.