Add auth to control endpoints, sigmoid Farquhar transition
Browse filesFiles changed (2)
5 unmodified lines678910112 unmodified lines141516171819204 unmodified lines252627282930314 unmodified lines36373839404142434445464748495051525 unmodified linesfrom fastapi import APIRouter, Depends, HTTPExceptionfrom backend.api.deps import get_datahub, get_redis_clientfrom src.data.data_providers import DataHub2 unmodified lines@router.get("/status")async def control_status():"""Last ControlLoop tick result (stored in Redis by the worker)."""redis = get_redis_client()if redis:4 unmodified lines@router.get("/plan")async def control_plan():"""Current day-ahead plan."""redis = get_redis_client()if redis:4 unmodified linestry:import jsonfrom config.settings import DAILY_PLAN_PATHwith open(DAILY_PLAN_PATH) as f:return json.load(f)except FileNotFoundError:return {"status": "waiting", "message": "No plan generated yet", "slots": []}except Exception as exc:log.error("Failed to load plan from file: %s", exc)raise HTTPException(status_code=500, detail="Plan loading failed")@router.get("/budget")async def control_budget():"""Current energy budget state."""redis = get_redis_client()if redis:5 unmodified lines67891011122 unmodified lines151617181920214 unmodified lines262728293031324 unmodified lines3738394041424344454647484950515253545556575859605 unmodified linesfrom fastapi import APIRouter, Depends, HTTPExceptionfrom backend.api.auth import optional_authfrom backend.api.deps import get_datahub, get_redis_clientfrom src.data.data_providers import DataHub2 unmodified lines@router.get("/status")async def control_status(user: dict = Depends(optional_auth)):"""Last ControlLoop tick result (stored in Redis by the worker)."""redis = get_redis_client()if redis:4 unmodified lines@router.get("/plan")async def control_plan(user: dict = Depends(optional_auth)):"""Current day-ahead plan."""redis = get_redis_client()if redis:4 unmodified linestry:import jsonfrom config.settings import DAILY_PLAN_PATHfrom pathlib import Pathplan_path = Path(DAILY_PLAN_PATH)if not plan_path.is_relative_to(plan_path.parent.parent):raise HTTPException(status_code=400, detail="Invalid plan path")with open(plan_path) as f:return json.load(f)except FileNotFoundError:return {"status": "waiting", "message": "No plan generated yet", "slots": []}except HTTPException:raiseexcept Exception as exc:log.error("Failed to load plan from file: %s", exc)raise HTTPException(status_code=500, detail="Plan loading failed")@router.get("/budget")async def control_budget(user: dict = Depends(optional_auth)):"""Current energy budget state."""redis = get_redis_client()if redis:
271 unmodified lines272273274275276277278279280281282283271 unmodified linesAc, Aj, Rd = self._compute_rates(PAR, Tleaf, CO2, VPD, CWSI or 0.0)An = min(Ac, Aj) - Rdif Tleaf < transition_temp:state = "RuBP_Limited"shading_helps = Falseelse:state = "Rubisco_Limited"shading_helps = (Aj > Ac)return float(max(0.0, An)), state, shading_helps271 unmodified lines272273274275276277278279280281282283284285286287288289290291292293271 unmodified linesAc, Aj, Rd = self._compute_rates(PAR, Tleaf, CO2, VPD, CWSI or 0.0)An = min(Ac, Aj) - Rd# Smooth sigmoid transition (28–32°C) instead of hard threshold.# rubisco_weight = 0 below 28°C, 1 above 32°C, sigmoid in between.import math_TRANSITION_WIDTH = 2.0 # °C half-width of sigmoid zonerubisco_weight = 1.0 / (1.0 + math.exp(-(Tleaf - transition_temp) / (_TRANSITION_WIDTH / 2.5)))if rubisco_weight < 0.3:state = "RuBP_Limited"elif rubisco_weight > 0.7:state = "Rubisco_Limited"else:state = "Transition"# shading_helps is weighted: only meaningful when Rubisco-limited# AND light is abundant relative to enzyme capacity (Aj > Ac)shading_helps = rubisco_weight > 0.5 and (Aj > Ac)return float(max(0.0, An)), state, shading_helps
backend/api/routes/control.py+12-4
src/models/farquhar_model.py+14-4