This commit is contained in:
2026-05-09 16:55:06 +03:30
parent 1679825ae2
commit cead7dafe2
51 changed files with 7514 additions and 1221 deletions
+14 -18
View File
@@ -38,13 +38,13 @@ def _model_to_data_fields(instance: Model, exclude: set[str] | None = None) -> d
def build_user_soil_text(sensor_uuid: str) -> str | None:
"""
ساخت متن قابل embed برای یک سنسور (کاربر).
از SensorData → SoilLocation → SoilDepthData خوانده می‌شود.
از SensorData → SoilLocation → latest remote sensing snapshots خوانده می‌شود.
Returns:
متن متنی قابل چانک، یا None اگر سنسور یافت نشد.
"""
from farm_data.models import SensorData
from location_data.models import SoilDepthData
from location_data.satellite_snapshot import build_location_block_satellite_snapshots
try:
sensor = SensorData.objects.select_related("center_location").get(
@@ -72,23 +72,19 @@ def build_user_soil_text(sensor_uuid: str) -> str | None:
sensor_lines = [f" {k}: {v}" for k, v in sorted(sensor_fields.items())]
parts.append("خوانش‌های سنسور:\n" + "\n".join(sensor_lines))
# داده‌های خاک به تفکیک عمق
depths = (
SoilDepthData.objects.filter(soil_location=loc)
.order_by("depth_label")
.all()
)
if depths:
depth_parts = []
for d in depths:
d_data = _model_to_data_fields(
d, exclude={"soil_location", "soil_location_id"}
snapshots = build_location_block_satellite_snapshots(loc)
if snapshots:
snapshot_lines = []
for snapshot in snapshots:
metrics = snapshot.get("resolved_metrics") or {}
if not metrics:
continue
lines = [f" {k}: {v}" for k, v in sorted(metrics.items())]
snapshot_lines.append(
f" بلوک {snapshot.get('block_code') or 'farm'}:\n" + "\n".join(lines)
)
if d_data:
lines = [f" {k}: {v}" for k, v in sorted(d_data.items())]
depth_parts.append(f" عمق {d.depth_label}:\n" + "\n".join(lines))
if depth_parts:
parts.append("داده‌های خاک:\n" + "\n".join(depth_parts))
if snapshot_lines:
parts.append("داده‌های ماهواره‌ای:\n" + "\n".join(snapshot_lines))
return "\n\n".join(parts) if len(parts) > 1 else None