2026-03-22 01:09:09 +03:30
|
|
|
from datetime import date
|
|
|
|
|
|
|
|
|
|
|
2026-04-25 17:22:41 +03:30
|
|
|
def load_farm_context(sensor_id: str) -> dict | None:
|
2026-03-22 01:09:09 +03:30
|
|
|
from irrigation.models import IrrigationMethod
|
2026-05-09 16:55:06 +03:30
|
|
|
from location_data.satellite_snapshot import build_location_block_satellite_snapshots
|
2026-04-06 23:50:24 +03:30
|
|
|
from farm_data.models import SensorData
|
2026-05-05 01:46:10 +03:30
|
|
|
from farm_data.services import get_farm_plant_snapshots
|
2026-03-22 01:09:09 +03:30
|
|
|
from weather.models import WeatherForecast
|
|
|
|
|
|
|
|
|
|
try:
|
2026-05-05 01:46:10 +03:30
|
|
|
sensor = SensorData.objects.select_related("center_location").prefetch_related("plant_assignments__plant").get(
|
2026-04-06 23:50:24 +03:30
|
|
|
farm_uuid=sensor_id
|
2026-03-22 01:09:09 +03:30
|
|
|
)
|
|
|
|
|
except SensorData.DoesNotExist:
|
|
|
|
|
return None
|
|
|
|
|
|
2026-04-06 23:50:24 +03:30
|
|
|
location = sensor.center_location
|
2026-05-09 16:55:06 +03:30
|
|
|
satellite_snapshots = build_location_block_satellite_snapshots(location)
|
2026-03-22 01:09:09 +03:30
|
|
|
forecasts = list(
|
2026-04-25 17:22:41 +03:30
|
|
|
WeatherForecast.objects.filter(location=location, forecast_date__gte=date.today()).order_by("forecast_date")[:7]
|
2026-03-22 01:09:09 +03:30
|
|
|
)
|
2026-05-05 01:46:10 +03:30
|
|
|
plants = get_farm_plant_snapshots(sensor)
|
2026-03-22 01:09:09 +03:30
|
|
|
irrigation_methods = list(IrrigationMethod.objects.all()[:5])
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"sensor": sensor,
|
|
|
|
|
"location": location,
|
2026-05-09 16:55:06 +03:30
|
|
|
"satellite_snapshots": satellite_snapshots,
|
2026-03-22 01:09:09 +03:30
|
|
|
"forecasts": forecasts,
|
2026-04-06 23:50:24 +03:30
|
|
|
"history": [],
|
2026-03-22 01:09:09 +03:30
|
|
|
"plants": plants,
|
|
|
|
|
"irrigation_methods": irrigation_methods,
|
|
|
|
|
}
|