38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
from dashboard_data.card_utils import safe_number
|
|
|
|
|
|
def _to_score(value, lower, upper):
|
|
if value is None:
|
|
return 0
|
|
if value <= lower:
|
|
return 0
|
|
if value >= upper:
|
|
return 100
|
|
return round(((value - lower) / (upper - lower)) * 100)
|
|
|
|
|
|
def build_sensor_radar_chart(sensor_id: str, context: dict | None = None, ai_bundle: dict | None = None) -> dict:
|
|
context = context or {}
|
|
sensor = context.get("sensor")
|
|
forecasts = context.get("forecasts", [])
|
|
if sensor is None:
|
|
return {"labels": [], "series": []}
|
|
|
|
current_weather = forecasts[0] if forecasts else None
|
|
current = [
|
|
_to_score(sensor.soil_temperature, 0, 40),
|
|
_to_score(sensor.soil_moisture, 0, 100),
|
|
_to_score(sensor.soil_ph, 0, 14),
|
|
_to_score(sensor.electrical_conductivity, 0, 5),
|
|
85,
|
|
_to_score(current_weather.wind_speed_max if current_weather else 0, 0, 30),
|
|
]
|
|
|
|
return {
|
|
"labels": ["دما", "رطوبت", "pH", "هدایت الکتریکی", "نور", "باد"],
|
|
"series": [
|
|
{"name": "امروز", "data": current},
|
|
{"name": "ایدهآل", "data": [80, 70, 75, 75, 90, 50]},
|
|
],
|
|
}
|