add chicken counting, mortality api for daily data sync. Also adjust karung sync to sync every 30 sec after publish cutoff time to update real data without clicking sync button manually

This commit is contained in:
Alberto-Audrix committed 2026-09-04 15:32:18 +07:00
1 parent 5ae477a99d
commit 6c21af745d
17 files changed
+791 -119

No files matched your search

@@ -0,0 +1,45 @@
from django.conf import settings
from django.core.management.base import BaseCommand
from apps.farms.models import Cycle
from apps.operations.services.chicken_counting_edge import (
ChickenCountingEdgeError,
sync_chicken_counting_for_cycle,
)
class Command(BaseCommand):
help = "Sync chicken counting + mortality IoT from the edge vision API"
def add_arguments(self, parser):
parser.add_argument("--cycle-id", type=int, default=None)
def handle(self, *args, **options):
counting_on = settings.CHICKEN_COUNTING_EDGE_COUNTING_SYNC_ENABLED
mortality_on = settings.CHICKEN_COUNTING_EDGE_MORTALITY_SYNC_ENABLED
if not counting_on and not mortality_on:
self.stdout.write(
"CHICKEN_COUNTING_EDGE_*_SYNC_ENABLED=false; skipping"
)
return
cycle_id = options.get("cycle_id")
if cycle_id:
cycles = Cycle.objects.filter(pk=cycle_id).select_related("kandang")
else:
cycles = Cycle.objects.filter(status=Cycle.STATUS_ACTIVE).select_related(
"kandang"
)
for cycle in cycles:
try:
summary = sync_chicken_counting_for_cycle(cycle)
except ChickenCountingEdgeError as exc:
self.stderr.write(f"cycle={cycle.pk}: {exc}")
continue
self.stdout.write(
self.style.SUCCESS(
f"cycle={cycle.pk} upserted={summary['upserted']} "
f"dates={','.join(d.isoformat() for d in summary['dates']) or '-'}"
)
)