Fix NDVI: use Air1+Crop3, cap at 168h
Browse files
backend/api/routes/sensors.py
CHANGED
|
@@ -166,16 +166,19 @@ async def temp_humidity(
|
|
| 166 |
async def ndvi_history(
|
| 167 |
hours: int = Query(168, ge=1, le=8760, description="Hours of history"),
|
| 168 |
):
|
| 169 |
-
"""Hourly NDVI from ThingsBoard — treatment (
|
| 170 |
try:
|
| 171 |
rows = []
|
| 172 |
-
for device, label in [("
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
| 179 |
return rows
|
| 180 |
except Exception as exc:
|
| 181 |
log.error("NDVI failed: %s", exc)
|
|
|
|
| 166 |
async def ndvi_history(
|
| 167 |
hours: int = Query(168, ge=1, le=8760, description="Hours of history"),
|
| 168 |
):
|
| 169 |
+
"""Hourly NDVI from ThingsBoard — treatment (Crop3) and ambient (Air1)."""
|
| 170 |
try:
|
| 171 |
rows = []
|
| 172 |
+
for device, label in [("Air1", "ambient"), ("Crop3", "treatment")]:
|
| 173 |
+
try:
|
| 174 |
+
df = _tb_timeseries(device, ["NDVI"], min(hours, 168))
|
| 175 |
+
if not df.empty and "NDVI" in df.columns:
|
| 176 |
+
for ts, row in df.iterrows():
|
| 177 |
+
v = row.get("NDVI")
|
| 178 |
+
if v is not None and v == v:
|
| 179 |
+
rows.append({"timestamp": ts.isoformat(), "device": label, "ndvi": round(float(v), 4)})
|
| 180 |
+
except Exception:
|
| 181 |
+
pass
|
| 182 |
return rows
|
| 183 |
except Exception as exc:
|
| 184 |
log.error("NDVI failed: %s", exc)
|