UPDATE
This commit is contained in:
@@ -12,7 +12,7 @@ from farm_data.models import SensorData
|
||||
from plant.gdd import calculate_daily_gdd, resolve_growth_profile
|
||||
from weather.models import WeatherForecast
|
||||
|
||||
from .services import CropSimulationService
|
||||
from .services import CropSimulationService, build_simulation_payload_from_farm
|
||||
|
||||
|
||||
DEFAULT_DYNAMIC_PARAMETERS = ["DVS", "LAI", "TAGP", "TWSO", "SM"]
|
||||
@@ -35,6 +35,7 @@ class GrowthSimulationError(Exception):
|
||||
|
||||
@dataclass
|
||||
class GrowthSimulationContext:
|
||||
farm_uuid: str | None
|
||||
plant_name: str
|
||||
plant: Any
|
||||
dynamic_parameters: list[str]
|
||||
@@ -223,6 +224,7 @@ def build_growth_context(payload: dict[str, Any]) -> GrowthSimulationContext:
|
||||
page_size = min(max(int(payload.get("page_size") or DEFAULT_PAGE_SIZE), 1), MAX_PAGE_SIZE)
|
||||
|
||||
sensor = None
|
||||
resolved_farm_uuid = str(payload["farm_uuid"]) if payload.get("farm_uuid") else None
|
||||
if payload.get("farm_uuid"):
|
||||
sensor = (
|
||||
SensorData.objects.select_related("center_location")
|
||||
@@ -233,6 +235,35 @@ def build_growth_context(payload: dict[str, Any]) -> GrowthSimulationContext:
|
||||
if sensor is None:
|
||||
raise GrowthSimulationError("Farm not found.")
|
||||
|
||||
if resolved_farm_uuid:
|
||||
farm_payload = build_simulation_payload_from_farm(
|
||||
farm_uuid=resolved_farm_uuid,
|
||||
plant_name=plant_name,
|
||||
weather=payload.get("weather"),
|
||||
soil=payload.get("soil_parameters"),
|
||||
crop_parameters=payload.get("crop_parameters"),
|
||||
agromanagement=payload.get("agromanagement"),
|
||||
site_parameters=payload.get("site_parameters"),
|
||||
)
|
||||
weather = farm_payload["weather"]
|
||||
crop_parameters = farm_payload["crop_parameters"]
|
||||
soil_parameters = farm_payload["soil"]
|
||||
site_parameters = farm_payload["site_parameters"]
|
||||
agromanagement = farm_payload["agromanagement"]
|
||||
plant = farm_payload["plant"] or plant
|
||||
return GrowthSimulationContext(
|
||||
farm_uuid=resolved_farm_uuid,
|
||||
plant_name=plant_name,
|
||||
plant=plant,
|
||||
dynamic_parameters=dynamic_parameters,
|
||||
weather=weather,
|
||||
crop_parameters=crop_parameters,
|
||||
soil_parameters=soil_parameters,
|
||||
site_parameters=site_parameters,
|
||||
agromanagement=agromanagement,
|
||||
page_size=page_size,
|
||||
)
|
||||
|
||||
weather = (
|
||||
_normalize_weather_records(payload["weather"])
|
||||
if payload.get("weather")
|
||||
@@ -265,6 +296,7 @@ def build_growth_context(payload: dict[str, Any]) -> GrowthSimulationContext:
|
||||
)
|
||||
|
||||
return GrowthSimulationContext(
|
||||
farm_uuid=resolved_farm_uuid,
|
||||
plant_name=plant_name,
|
||||
plant=plant,
|
||||
dynamic_parameters=dynamic_parameters,
|
||||
@@ -359,6 +391,8 @@ def _run_projection_engine(context: GrowthSimulationContext) -> dict[str, Any]:
|
||||
def _run_simulation(context: GrowthSimulationContext) -> tuple[dict[str, Any], int | None, str | None]:
|
||||
try:
|
||||
response = CropSimulationService().run_single_simulation(
|
||||
farm_uuid=context.farm_uuid,
|
||||
plant_name=context.plant_name,
|
||||
weather=context.weather,
|
||||
soil=context.soil_parameters,
|
||||
crop_parameters=context.crop_parameters,
|
||||
|
||||
Reference in New Issue
Block a user