AI UPDATE
This commit is contained in:
@@ -23,3 +23,33 @@ class IrrigationMethodSerializer(serializers.ModelSerializer):
|
||||
"updated_at",
|
||||
]
|
||||
read_only_fields = ["id", "created_at", "updated_at"]
|
||||
|
||||
|
||||
class IrrigationRecommendRequestSerializer(serializers.Serializer):
|
||||
"""سریالایزر ورودی برای درخواست توصیه آبیاری."""
|
||||
|
||||
sensor_uuid = serializers.CharField(help_text="شناسه یکتای سنسور (اجباری)")
|
||||
plant_name = serializers.CharField(required=False, allow_blank=True, help_text="نام گیاه")
|
||||
growth_stage = serializers.CharField(required=False, allow_blank=True, help_text="مرحله رشد گیاه")
|
||||
irrigation_method_name = serializers.CharField(
|
||||
required=False, allow_blank=True, help_text="نام روش آبیاری"
|
||||
)
|
||||
query = serializers.CharField(required=False, allow_blank=True, help_text="سوال اختیاری")
|
||||
|
||||
|
||||
class IrrigationPlanSerializer(serializers.Serializer):
|
||||
"""سریالایزر خروجی برای پلن توصیه آبیاری."""
|
||||
|
||||
frequencyPerWeek = serializers.IntegerField(help_text="تعداد دفعات آبیاری در هفته")
|
||||
durationMinutes = serializers.IntegerField(help_text="مدت هر بار آبیاری به دقیقه")
|
||||
bestTimeOfDay = serializers.CharField(help_text="بهترین زمان آبیاری")
|
||||
moistureLevel = serializers.IntegerField(help_text="سطح رطوبت مطلوب خاک به درصد")
|
||||
warning = serializers.CharField(help_text="هشدار یا توصیه مهم", allow_blank=True)
|
||||
|
||||
|
||||
class IrrigationRecommendResponseSerializer(serializers.Serializer):
|
||||
"""سریالایزر خروجی برای ریسپانس توصیه آبیاری."""
|
||||
|
||||
code = serializers.IntegerField()
|
||||
msg = serializers.CharField()
|
||||
data = serializers.DictField(child=serializers.JSONField())
|
||||
|
||||
+8
-1
@@ -1,8 +1,15 @@
|
||||
from django.urls import path
|
||||
|
||||
from .views import IrrigationMethodDetailView, IrrigationMethodListCreateView
|
||||
from .views import (
|
||||
IrrigationMethodDetailView,
|
||||
IrrigationMethodListCreateView,
|
||||
IrrigationRecommendView,
|
||||
IrrigationRecommendStatusView,
|
||||
)
|
||||
|
||||
urlpatterns = [
|
||||
path("", IrrigationMethodListCreateView.as_view(), name="irrigation-list-create"),
|
||||
path("<int:pk>/", IrrigationMethodDetailView.as_view(), name="irrigation-detail"),
|
||||
path("recommend/", IrrigationRecommendView.as_view(), name="irrigation-recommend"),
|
||||
path("recommend/<str:task_id>/status/", IrrigationRecommendStatusView.as_view(), name="irrigation-recommend-status"),
|
||||
]
|
||||
|
||||
+104
-1
@@ -8,7 +8,10 @@ from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from .models import IrrigationMethod
|
||||
from .serializers import IrrigationMethodSerializer
|
||||
from .serializers import (
|
||||
IrrigationMethodSerializer,
|
||||
IrrigationRecommendRequestSerializer,
|
||||
)
|
||||
|
||||
|
||||
class IrrigationMethodListCreateView(APIView):
|
||||
@@ -28,6 +31,106 @@ class IrrigationMethodListCreateView(APIView):
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
class IrrigationRecommendView(APIView):
|
||||
"""
|
||||
توصیه آبیاری با Celery.
|
||||
POST با sensor_uuid، plant_name، growth_stage، irrigation_method_name.
|
||||
اطلاعات گیاه از plant app و روش آبیاری از irrigation app دریافت میشود.
|
||||
"""
|
||||
|
||||
@extend_schema(
|
||||
tags=["Irrigation Recommendation"],
|
||||
summary="درخواست توصیه آبیاری",
|
||||
description=(
|
||||
"دادههای سنسور، گیاه و روش آبیاری را دریافت کرده و یک تسک Celery "
|
||||
"برای تولید توصیه آبیاری در صف قرار میدهد. "
|
||||
"اطلاعات گیاه از جدول Plant و روش آبیاری از جدول IrrigationMethod بارگذاری میشود."
|
||||
),
|
||||
request=IrrigationRecommendRequestSerializer,
|
||||
responses={
|
||||
202: OpenApiResponse(description="تسک در صف قرار گرفت"),
|
||||
400: OpenApiResponse(description="پارامتر ورودی نامعتبر"),
|
||||
},
|
||||
examples=[
|
||||
OpenApiExample(
|
||||
"نمونه درخواست",
|
||||
value={
|
||||
"sensor_uuid": "550e8400-e29b-41d4-a716-446655440000",
|
||||
"plant_name": "گوجهفرنگی",
|
||||
"growth_stage": "گلدهی",
|
||||
"irrigation_method_name": "آبیاری قطرهای",
|
||||
},
|
||||
request_only=True,
|
||||
),
|
||||
],
|
||||
)
|
||||
def post(self, request):
|
||||
from rag.tasks import irrigation_recommendation_task
|
||||
|
||||
serializer = IrrigationRecommendRequestSerializer(data=request.data)
|
||||
if not serializer.is_valid():
|
||||
return Response(
|
||||
{"code": 400, "msg": "داده نامعتبر.", "data": serializer.errors},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
validated = serializer.validated_data
|
||||
sensor_uuid = validated["sensor_uuid"]
|
||||
plant_name = validated.get("plant_name")
|
||||
growth_stage = validated.get("growth_stage")
|
||||
irrigation_method_name = validated.get("irrigation_method_name")
|
||||
query = validated.get("query")
|
||||
|
||||
task = irrigation_recommendation_task.delay(
|
||||
sensor_uuid=sensor_uuid,
|
||||
plant_name=plant_name,
|
||||
growth_stage=growth_stage,
|
||||
irrigation_method_name=irrigation_method_name,
|
||||
query=query,
|
||||
)
|
||||
return Response(
|
||||
{
|
||||
"code": 202,
|
||||
"msg": "تسک توصیه آبیاری در صف قرار گرفت.",
|
||||
"data": {
|
||||
"task_id": task.id,
|
||||
"status_url": f"/api/irrigation/recommend/{task.id}/status/",
|
||||
},
|
||||
},
|
||||
status=status.HTTP_202_ACCEPTED,
|
||||
)
|
||||
|
||||
|
||||
class IrrigationRecommendStatusView(APIView):
|
||||
"""وضعیت تسک توصیه آبیاری."""
|
||||
|
||||
@extend_schema(
|
||||
tags=["Irrigation Recommendation"],
|
||||
summary="وضعیت تسک توصیه آبیاری",
|
||||
description="وضعیت تسک Celery توصیه آبیاری را برمیگرداند.",
|
||||
responses={
|
||||
200: OpenApiResponse(description="وضعیت تسک"),
|
||||
},
|
||||
)
|
||||
def get(self, request, task_id):
|
||||
from celery.result import AsyncResult
|
||||
|
||||
result = AsyncResult(task_id)
|
||||
data = {"task_id": task_id, "status": result.state}
|
||||
if result.state == "PENDING":
|
||||
data["message"] = "تسک در صف یا یافت نشد."
|
||||
elif result.state == "PROGRESS":
|
||||
data["progress"] = result.info
|
||||
elif result.state == "SUCCESS":
|
||||
data["result"] = result.result
|
||||
elif result.state == "FAILURE":
|
||||
data["error"] = str(result.result)
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": data},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
@extend_schema(
|
||||
tags=["Irrigation"],
|
||||
summary="ایجاد روش آبیاری جدید",
|
||||
|
||||
Reference in New Issue
Block a user