Files
Ai/sensor_data/models.py
T

124 lines
4.3 KiB
Python
Raw Normal View History

import uuid
from django.db import models
class SensorData(models.Model):
"""
داده‌های خوانش سنسور برای یک location.
هنگام آپدیت، نسخه قبلی در SensorDataHistory ذخیره می‌شود.
"""
uuid_sensor = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False,
help_text="شناسه یکتای سنسور",
)
location = models.ForeignKey(
"soil_data.SoilLocation",
on_delete=models.CASCADE,
related_name="sensor_data",
db_column="location_id",
help_text="همان location_id در soil_data",
)
soil_moisture = models.FloatField(null=True, blank=True, help_text="رطوبت خاک")
soil_temperature = models.FloatField(null=True, blank=True, help_text="دما خاک")
soil_ph = models.FloatField(null=True, blank=True, help_text="pH خاک")
electrical_conductivity = models.FloatField(
null=True, blank=True, help_text="هدایت الکتریکی"
)
nitrogen = models.FloatField(null=True, blank=True, help_text="ازت (N)")
phosphorus = models.FloatField(null=True, blank=True, help_text="فسفر")
potassium = models.FloatField(null=True, blank=True, help_text="پتاسیم")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ["-updated_at"]
verbose_name = "داده سنسور"
verbose_name_plural = "داده‌های سنسور"
def __str__(self):
return f"SensorData({self.uuid_sensor}, location={self.location_id})"
class SensorDataHistory(models.Model):
"""
تاریخچه خوانش‌های سنسور. کپی از SensorData هنگام آپدیت.
"""
uuid_sensor = models.UUIDField(help_text="شناسه سنسور")
location_id = models.IntegerField(help_text="location_id از soil_data")
soil_moisture = models.FloatField(null=True, blank=True)
soil_temperature = models.FloatField(null=True, blank=True)
soil_ph = models.FloatField(null=True, blank=True)
electrical_conductivity = models.FloatField(null=True, blank=True)
nitrogen = models.FloatField(null=True, blank=True)
phosphorus = models.FloatField(null=True, blank=True)
potassium = models.FloatField(null=True, blank=True)
recorded_at = models.DateTimeField(
auto_now_add=True, help_text="زمان ثبت در تاریخچه"
)
class Meta:
ordering = ["-recorded_at"]
verbose_name = "تاریخچه داده سنسور"
verbose_name_plural = "تاریخچه داده‌های سنسور"
def __str__(self):
return f"SensorDataHistory({self.uuid_sensor}, {self.recorded_at})"
class SensorParameter(models.Model):
"""
تعریف پارامترهای سنسور (مثلاً رطوبت خاک، pH، ...).
"""
code = models.CharField(
max_length=64,
unique=True,
db_index=True,
help_text="کد یکتا (مثلاً soil_moisture)",
)
name_fa = models.CharField(max_length=128, help_text="نام فارسی")
unit = models.CharField(max_length=32, blank=True, help_text="واحد اندازه‌گیری")
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ["code"]
verbose_name = "پارامتر سنسور"
verbose_name_plural = "پارامترهای سنسور"
def __str__(self):
return f"{self.code} ({self.name_fa})"
class ParameterUpdateLog(models.Model):
"""
لاگ آپدیت لیست پارامترها.
"""
ACTION_ADDED = "added"
ACTION_MODIFIED = "modified"
ACTION_CHOICES = [
(ACTION_ADDED, "اضافه شده"),
(ACTION_MODIFIED, "ویرایش شده"),
]
parameter = models.ForeignKey(
SensorParameter,
on_delete=models.CASCADE,
related_name="update_logs",
)
action = models.CharField(max_length=16, choices=ACTION_CHOICES)
updated_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ["-updated_at"]
verbose_name = "لاگ آپدیت پارامتر"
verbose_name_plural = "لاگ آپدیت پارامترها"
def __str__(self):
return f"{self.parameter.code} - {self.action} - {self.updated_at}"