2026-03-22 03:08:27 +03:30
|
|
|
from irrigation.evapotranspiration import calculate_forecast_water_needs, resolve_crop_profile
|
2026-03-22 01:09:09 +03:30
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_water_need_prediction(sensor_id: str, context: dict | None = None, ai_bundle: dict | None = None) -> dict:
|
2026-03-22 03:08:27 +03:30
|
|
|
context = context or {}
|
|
|
|
|
forecasts = context.get("forecasts", [])
|
|
|
|
|
location = context.get("location")
|
|
|
|
|
plants = context.get("plants", [])
|
|
|
|
|
irrigation_methods = context.get("irrigation_methods", [])
|
|
|
|
|
|
|
|
|
|
if not forecasts or location is None:
|
|
|
|
|
return {
|
|
|
|
|
"totalNext7Days": 0,
|
|
|
|
|
"unit": "mm",
|
|
|
|
|
"categories": [],
|
|
|
|
|
"series": [],
|
|
|
|
|
"dailyBreakdown": [],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
plant = plants[0] if plants else None
|
|
|
|
|
crop_profile = resolve_crop_profile(plant)
|
|
|
|
|
efficiency = getattr(irrigation_methods[0], "water_efficiency_percent", None) if irrigation_methods else None
|
|
|
|
|
daily = calculate_forecast_water_needs(
|
|
|
|
|
forecasts=forecasts[:7],
|
|
|
|
|
latitude_deg=float(location.latitude),
|
|
|
|
|
crop_profile=crop_profile,
|
|
|
|
|
growth_stage=crop_profile.get("current_stage"),
|
|
|
|
|
irrigation_efficiency_percent=efficiency,
|
|
|
|
|
)
|
|
|
|
|
daily_requirements = [round(item["gross_irrigation_mm"], 2) for item in daily]
|
2026-03-22 01:09:09 +03:30
|
|
|
|
|
|
|
|
return {
|
2026-03-22 03:08:27 +03:30
|
|
|
"totalNext7Days": round(sum(daily_requirements), 2),
|
|
|
|
|
"unit": "mm",
|
|
|
|
|
"categories": [f"روز {index}" for index in range(1, len(daily_requirements) + 1)],
|
|
|
|
|
"series": [{"name": "نیاز آبی تعدیلشده", "data": daily_requirements}],
|
|
|
|
|
"dailyBreakdown": daily,
|
2026-03-22 01:09:09 +03:30
|
|
|
}
|