This commit is contained in:
2026-04-27 18:02:26 +03:30
parent 7c2ec2144d
commit 190a668355
19 changed files with 193 additions and 825 deletions
+3 -23
View File
@@ -1,25 +1,9 @@
from __future__ import annotations
from typing import Any
from django.apps import apps
from farm_data.models import SensorData
def build_water_stress_summary(sensor: Any) -> dict[str, Any]:
moisture = float(getattr(sensor, "soil_moisture", None) or 0.0)
water_stress = max(0, min(100, round(35 - (moisture / 2))))
return {
"waterStressIndex": water_stress,
"level": "پایین" if water_stress <= 20 else "متوسط" if water_stress <= 45 else "بالا",
"sourceMetric": {
"soilMoisture": round(moisture, 2),
"formula": "clamp(round(35 - (soil_moisture / 2)), 0, 100)",
},
}
class WaterStressService:
def get_water_stress(self, *, farm_uuid: str, plant_name: str | None = None) -> dict[str, Any]:
sensor = SensorData.objects.filter(farm_uuid=farm_uuid).first()
@@ -32,10 +16,6 @@ class WaterStressService:
plant_name=plant_name,
)
except Exception as exc:
fallback = {
"farm_uuid": str(sensor.farm_uuid),
**build_water_stress_summary(sensor),
}
fallback["sourceMetric"]["engine"] = "sensor_fallback"
fallback["sourceMetric"]["fallbackReason"] = str(exc)
return fallback
raise RuntimeError(
f"Water stress simulation failed for farm {sensor.farm_uuid}: {exc}"
) from exc