update iot panel will now listen to push from panel

This commit is contained in:
Alberto-Audrix committed 2026-09-21 14:38:17 +07:00
1 parent 20d04acb3e
commit 520c4bcaad
10 files changed
+158 -754

No files matched your search

+23 -26
View File
@@ -33,7 +33,7 @@ from apps.operations.services.chicken_counting_edge import (
sync_chicken_counting_for_cycle,
sync_chicken_weight_for_cycle,
)
from apps.operations.services.iot_api import IotApiError, sync_iot_panels
from apps.operations.services.iot_api import IotPushError, handle_iot_push
from apps.operations.services.visibility import dashboard_publish_time, visible_through_date
READ_ACTIONS = frozenset({"list", "retrieve", "latest_average", "latest", "dates", "cached"})
@@ -251,38 +251,35 @@ class IotPanelViewSet(viewsets.ModelViewSet):
)
return Response(dates)
@action(detail=False, methods=["post"], url_path="sync")
def sync_from_api(self, request):
flock_id = request.data.get("flock_id")
if not flock_id:
class IotPanelPushView(APIView):
"""Webhook endpoint for IoT panels to push sensor data."""
permission_classes = [AllowAny]
authentication_classes = []
def post(self, request, flock_id: int):
try:
flock = Flock.objects.get(pk=flock_id)
except Flock.DoesNotExist:
return Response(
{"detail": "flock_id is required."},
status=status.HTTP_400_BAD_REQUEST,
{"detail": "Flock not found."},
status=status.HTTP_404_NOT_FOUND,
)
try:
flock = Flock.objects.get(pk=int(flock_id))
except (Flock.DoesNotExist, TypeError, ValueError):
return Response({"detail": "Flock not found."}, status=status.HTTP_404_NOT_FOUND)
if not (flock.iot_api_url or "").strip():
return Response(
{"detail": "Flock has no IoT API URL configured."},
status=status.HTTP_400_BAD_REQUEST,
)
try:
summary = sync_iot_panels(flock_id=flock.pk)
except IotApiError as exc:
panel = handle_iot_push(flock, request.data)
except IotPushError as exc:
code = getattr(exc, "status_code", None)
return Response(
{"detail": str(exc)},
status=code if code and code >= 400 else status.HTTP_502_BAD_GATEWAY,
status=code if code and code >= 400 else status.HTTP_400_BAD_REQUEST,
)
return Response(
{
"upserted": summary.upserted,
"skipped": summary.skipped,
"pages": summary.pages,
}
)
except Exception as exc:
return Response(
{"detail": f"Push failed: {exc}"},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
return Response({"ok": True, "panel_id": panel.pk})
class AIInsightViewSet(CycleScopedViewSet):