Update backend/api/routes/sensors.py
Browse files
backend/api/routes/sensors.py
CHANGED
|
@@ -64,3 +64,68 @@ async def sensors_history(
|
|
| 64 |
except Exception as exc:
|
| 65 |
log.error("Sensor history failed: %s", exc, exc_info=True)
|
| 66 |
raise HTTPException(status_code=502, detail=f"Sensor history unavailable: {exc}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
except Exception as exc:
|
| 65 |
log.error("Sensor history failed: %s", exc, exc_info=True)
|
| 66 |
raise HTTPException(status_code=502, detail=f"Sensor history unavailable: {exc}")
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
@router.get("/soil-moisture")
|
| 70 |
+
async def soil_moisture_history(
|
| 71 |
+
hours: int = Query(168, ge=1, le=8760, description="Hours of history"),
|
| 72 |
+
):
|
| 73 |
+
"""Hourly soil moisture time-series from ThingsBoard (treatment Soil1)."""
|
| 74 |
+
try:
|
| 75 |
+
from datetime import datetime, timezone, timedelta
|
| 76 |
+
from src.data.thingsboard_client import ThingsBoardClient
|
| 77 |
+
|
| 78 |
+
client = ThingsBoardClient()
|
| 79 |
+
end = datetime.now(tz=timezone.utc)
|
| 80 |
+
start = end - timedelta(hours=hours)
|
| 81 |
+
|
| 82 |
+
df = client.get_timeseries(
|
| 83 |
+
"Soil1", ["soilMoisture", "soilMoisture2"],
|
| 84 |
+
start=start, end=end,
|
| 85 |
+
interval_ms=3_600_000, agg="AVG", limit=2000,
|
| 86 |
+
)
|
| 87 |
+
if df.empty:
|
| 88 |
+
return []
|
| 89 |
+
rows = []
|
| 90 |
+
for ts, row in df.iterrows():
|
| 91 |
+
r = {"timestamp": ts.isoformat()}
|
| 92 |
+
for col in df.columns:
|
| 93 |
+
val = row[col]
|
| 94 |
+
if val is not None and val == val: # not NaN
|
| 95 |
+
r[col] = round(float(val), 1)
|
| 96 |
+
rows.append(r)
|
| 97 |
+
return rows
|
| 98 |
+
except Exception as exc:
|
| 99 |
+
log.error("Soil moisture history failed: %s", exc)
|
| 100 |
+
return []
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
@router.get("/rain")
|
| 104 |
+
async def rain_history(
|
| 105 |
+
hours: int = Query(168, ge=1, le=8760, description="Hours of history"),
|
| 106 |
+
):
|
| 107 |
+
"""Hourly rain time-series from ThingsBoard ambient station (Air1)."""
|
| 108 |
+
try:
|
| 109 |
+
from datetime import datetime, timezone, timedelta
|
| 110 |
+
from src.data.thingsboard_client import ThingsBoardClient
|
| 111 |
+
|
| 112 |
+
client = ThingsBoardClient()
|
| 113 |
+
end = datetime.now(tz=timezone.utc)
|
| 114 |
+
start = end - timedelta(hours=hours)
|
| 115 |
+
|
| 116 |
+
df = client.get_timeseries(
|
| 117 |
+
"Air1", ["rain"],
|
| 118 |
+
start=start, end=end,
|
| 119 |
+
interval_ms=3_600_000, agg="SUM", limit=2000,
|
| 120 |
+
)
|
| 121 |
+
if df.empty:
|
| 122 |
+
return []
|
| 123 |
+
rows = []
|
| 124 |
+
for ts, row in df.iterrows():
|
| 125 |
+
val = row.get("rain")
|
| 126 |
+
if val is not None and val == val:
|
| 127 |
+
rows.append({"timestamp": ts.isoformat(), "rain_mm": round(float(val), 2)})
|
| 128 |
+
return rows
|
| 129 |
+
except Exception as exc:
|
| 130 |
+
log.error("Rain history failed: %s", exc)
|
| 131 |
+
return []
|