Skip to content

Commit

Permalink
Add rain alert to /alerts/summary endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Jul 15, 2024
1 parent 39910a4 commit 23511ec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
18 changes: 14 additions & 4 deletions src/lvmapi/routers/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from fastapi import APIRouter
from pydantic import BaseModel

from lvmapi.tools.alerts import o2_alerts, spec_temperature_alerts
from lvmapi.tools.alerts import enclosure_alerts, spec_temperature_alerts


class AlertsSummary(BaseModel):
Expand All @@ -24,6 +24,7 @@ class AlertsSummary(BaseModel):
o2_alert: bool | None
heater_alert: bool | None
heater_camera_alerts: dict[str, bool] | None
rain: bool | None


router = APIRouter(prefix="/alerts", tags=["alerts"])
Expand All @@ -43,16 +44,25 @@ async def summary() -> AlertsSummary:
ln2_alert = None

try:
o2_camera_alerts = await o2_alerts()
o2_alert = any(o2_camera_alerts.values())
enclosure_alerts_response = await enclosure_alerts()
except Exception as err:
warnings.warn(f"Error getting O2 level alerts: {err}")
warnings.warn(f"Error getting enclosure alerts: {err}")
o2_alert = None

o2_alerts = {
key: value
for key, value in enclosure_alerts_response.items()
if "o2_percentage" in key
}
o2_alert = any(o2_alerts.values())

rain_sensor_alarm = enclosure_alerts_response.get("rain_sensor_alarm", None)

return AlertsSummary(
ln2_alert=ln2_alert,
ln2_camera_alerts=ln2_camera_alerts,
o2_alert=o2_alert,
heater_alert=False,
heater_camera_alerts={},
rain=rain_sensor_alarm,
)
17 changes: 10 additions & 7 deletions src/lvmapi/tools/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from lvmapi.tools.spectrograph import get_spectrograph_temperatures_history


__all__ = ["spec_temperature_alerts", "o2_alerts"]
__all__ = ["spec_temperature_alerts", "enclosure_alerts"]


async def spec_temperature_alerts(
Expand Down Expand Up @@ -61,8 +61,8 @@ async def spec_temperature_alerts(
return temp_alerts_dict


async def o2_alerts(threshold: float = 19.5):
"""Returns an alert if any of the O2 sensors is below the threshold.
async def enclosure_alerts(threshold: float = 19.5):
"""Returns O2 and rain sensor enclosure alerts.
Parameters
----------
Expand All @@ -72,9 +72,9 @@ async def o2_alerts(threshold: float = 19.5):
Returns
-------
o2_alerts_dict
enclosure_alerts_dict
A dictionary of alerts. E.g.,
``{'o2_spec_room': False, 'o2_util_room': False}``.
``{'o2_spec_room': False, 'o2_util_room': False, 'rain_sensor_alarm': True}``.
"""

Expand All @@ -89,9 +89,12 @@ async def o2_alerts(threshold: float = 19.5):
if status.status.did_fail:
raise ValueError("Failed retrieving status from ECP.")

o2_alerts_dict = {
registers = status.replies.get("registers")

enclosure_alerts_dict = {
"o2_spec_room": status.replies.get("o2_percent_spectrograph") < threshold,
"o2_util_room": status.replies.get("o2_percent_utilities") < threshold,
"rain_sensor_alarm": registers["rain_sensor_alarm"],
}

return o2_alerts_dict
return enclosure_alerts_dict

0 comments on commit 23511ec

Please sign in to comment.