UPDATE
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
"""
|
||||
Management command to seed a fixed demo farm-data record.
|
||||
Run: python manage.py seed_farm_data
|
||||
"""
|
||||
from uuid import UUID
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from farm_data.models import SensorData
|
||||
from location_data.models import SoilLocation
|
||||
from plant.models import Plant
|
||||
from weather.models import WeatherForecast
|
||||
|
||||
|
||||
DEMO_FARM_UUID = UUID("11111111-1111-1111-1111-111111111111")
|
||||
DEMO_LATITUDE = "50.000000"
|
||||
DEMO_LONGITUDE = "50.000000"
|
||||
DEMO_SENSOR_PAYLOAD = {
|
||||
"sensor-7-1": {
|
||||
"soil_moisture": 42.3,
|
||||
"soil_temperature": 21.4,
|
||||
"soil_ph": 6.9,
|
||||
"electrical_conductivity": 1.1,
|
||||
"nitrogen": 28.0,
|
||||
"phosphorus": 14.0,
|
||||
"potassium": 19.0,
|
||||
}
|
||||
}
|
||||
DEMO_PLANT_NAMES = [
|
||||
"گوجهفرنگی",
|
||||
"خیار",
|
||||
]
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Seed a fixed farm-data row with farm_uuid=11111111-1111-1111-1111-111111111111."
|
||||
|
||||
def handle(self, *args, **options):
|
||||
location, _ = SoilLocation.objects.get_or_create(
|
||||
latitude=DEMO_LATITUDE,
|
||||
longitude=DEMO_LONGITUDE,
|
||||
)
|
||||
weather_forecast = (
|
||||
WeatherForecast.objects.filter(location=location)
|
||||
.order_by("-forecast_date", "-id")
|
||||
.first()
|
||||
)
|
||||
|
||||
farm_data, created = SensorData.objects.update_or_create(
|
||||
farm_uuid=DEMO_FARM_UUID,
|
||||
defaults={
|
||||
"center_location": location,
|
||||
"weather_forecast": weather_forecast,
|
||||
"sensor_payload": DEMO_SENSOR_PAYLOAD,
|
||||
},
|
||||
)
|
||||
plants = list(Plant.objects.filter(name__in=DEMO_PLANT_NAMES).order_by("name"))
|
||||
if plants:
|
||||
farm_data.plants.set(plants)
|
||||
|
||||
status_text = "Created" if created else "Updated"
|
||||
weather_text = weather_forecast.id if weather_forecast else "None"
|
||||
plant_count = len(plants)
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
f"{status_text} farm-data {farm_data.farm_uuid} for center_location_id={location.id} weather_forecast_id={weather_text} plants={plant_count}"
|
||||
)
|
||||
)
|
||||
self.stdout.write(self.style.SUCCESS("\nDone seeding farm_data demo record."))
|
||||
@@ -0,0 +1,70 @@
|
||||
"""
|
||||
Management command to seed the 7 initial sensor parameters.
|
||||
Run: python manage.py seed_sensor_parameters
|
||||
"""
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from farm_data.models import (
|
||||
DEFAULT_SENSOR_DATA_TYPE,
|
||||
DEFAULT_SENSOR_KEY,
|
||||
ParameterUpdateLog,
|
||||
SensorParameter,
|
||||
)
|
||||
|
||||
|
||||
INITIAL_PARAMETERS = [
|
||||
("soil_moisture", "رطوبت خاک", "%"),
|
||||
("soil_temperature", "دما خاک", "°C"),
|
||||
("soil_ph", "pH خاک", ""),
|
||||
("electrical_conductivity", "هدایت الکتریکی", "dS/m"),
|
||||
("nitrogen", "ازت (N)", "mg/kg"),
|
||||
("phosphorus", "فسفر", "mg/kg"),
|
||||
("potassium", "پتاسیم", "mg/kg"),
|
||||
]
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Seed 7 initial sensor parameters (soil_moisture, soil_temperature, etc.)"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
"--sensor-key",
|
||||
default=DEFAULT_SENSOR_KEY,
|
||||
help='کلید سنسور مثل "sensor-7-1" یا "leaf-sensor"',
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
sensor_key = options["sensor_key"]
|
||||
created_count = 0
|
||||
for code, name_fa, unit in INITIAL_PARAMETERS:
|
||||
param, created = SensorParameter.objects.get_or_create(
|
||||
sensor_key=sensor_key,
|
||||
code=code,
|
||||
defaults={
|
||||
"name_fa": name_fa,
|
||||
"unit": unit,
|
||||
"data_type": DEFAULT_SENSOR_DATA_TYPE,
|
||||
},
|
||||
)
|
||||
if created:
|
||||
ParameterUpdateLog.objects.create(
|
||||
parameter=param,
|
||||
action="added",
|
||||
payload={
|
||||
"sensor_key": sensor_key,
|
||||
"code": code,
|
||||
"name_fa": name_fa,
|
||||
"unit": unit,
|
||||
},
|
||||
)
|
||||
created_count += 1
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
f" Created: {sensor_key}.{code} ({name_fa})"
|
||||
)
|
||||
)
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
f"\nDone. Created {created_count} new parameters for {sensor_key}."
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user