safraeli commited on
Commit
10bb15d
·
verified ·
1 Parent(s): bdffbe8

Fix photosynthesis/current: route to predict_fvcb/predict_ml with live conditions

Browse files
backend/api/routes/photosynthesis.py CHANGED
@@ -2,6 +2,7 @@
2
 
3
  from __future__ import annotations
4
 
 
5
  from enum import Enum
6
 
7
  from fastapi import APIRouter, Depends, Query
@@ -9,6 +10,7 @@ from fastapi import APIRouter, Depends, Query
9
  from backend.api.deps import get_datahub
10
  from src.data.data_providers import DataHub
11
 
 
12
  router = APIRouter()
13
 
14
 
@@ -17,12 +19,36 @@ class PSModel(str, Enum):
17
  ml = "ml"
18
 
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  @router.get("/current")
21
  async def ps_current(
22
  model: PSModel = Query(PSModel.fvcb, description="Model: fvcb or ml"),
23
  hub: DataHub = Depends(get_datahub),
24
  ):
25
- return hub.photosynthesis.get_current(model=model.value)
 
 
 
26
 
27
 
28
  @router.get("/forecast")
 
2
 
3
  from __future__ import annotations
4
 
5
+ import logging
6
  from enum import Enum
7
 
8
  from fastapi import APIRouter, Depends, Query
 
10
  from backend.api.deps import get_datahub
11
  from src.data.data_providers import DataHub
12
 
13
+ log = logging.getLogger(__name__)
14
  router = APIRouter()
15
 
16
 
 
19
  ml = "ml"
20
 
21
 
22
+ def _live_conditions(hub: DataHub) -> dict:
23
+ """Try to get live sensor conditions; return sensible defaults if unavailable."""
24
+ defaults = {"PAR": 1000.0, "Tleaf": 30.0, "CO2": 400.0, "VPD": 1.5, "Tair": 30.0}
25
+ try:
26
+ snap = hub.vine_sensors.get_snapshot(light=True)
27
+ if isinstance(snap, dict) and "error" not in snap:
28
+ tair = snap.get("air_temperature_c") or snap.get("temperature")
29
+ if tair is not None:
30
+ defaults["Tair"] = float(tair)
31
+ defaults["Tleaf"] = float(tair) # approximate
32
+ ghi = snap.get("ghi") or snap.get("solar_radiation")
33
+ if ghi is not None:
34
+ defaults["PAR"] = float(ghi) * 2.04 # W/m² → µmol/m²/s approx
35
+ rh = snap.get("relative_humidity")
36
+ if rh is not None:
37
+ defaults["VPD"] = max(0.1, (1 - float(rh) / 100) * 3.0)
38
+ except Exception as exc:
39
+ log.debug("Could not fetch live conditions for FvCB: %s", exc)
40
+ return defaults
41
+
42
+
43
  @router.get("/current")
44
  async def ps_current(
45
  model: PSModel = Query(PSModel.fvcb, description="Model: fvcb or ml"),
46
  hub: DataHub = Depends(get_datahub),
47
  ):
48
+ if model == PSModel.fvcb:
49
+ conds = _live_conditions(hub)
50
+ return hub.photosynthesis.predict_fvcb(**conds)
51
+ return hub.photosynthesis.predict_ml()
52
 
53
 
54
  @router.get("/forecast")