This commit is contained in:
2026-04-14 17:17:51 +07:00
parent a7ea4aa575
commit 70c61f2ede
5 changed files with 768 additions and 6 deletions
+44 -4
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import json
import threading
import requests
import paho.mqtt.client as mqtt
import os
import sqlite3
@@ -41,6 +42,11 @@ DATA_FILE = "/etc/frigate-counter/karung-tuang/karung_tuang.json"
# =====================
DB_FILE = "/etc/frigate-counter/karung-tuang/karung_tuang.db"
DETIK_ANTAR_KARUNG = 30
RELAY_KIRI_ON = "http://192.168.192.26:5001/relay_on"
RELAY_KIRI_OFF = "http://192.168.192.26:5001/relay_off"
def init_database():
"""Initialize the SQLite database with the required table"""
@@ -138,13 +144,34 @@ def republish_counter():
f"{SITE_TOPIC}/{camera}/{label}", value, qos=1, retain=True
)
# =====================
# RELAY FUNCTION
# =====================
def relay_kiri_off():
global RELAY_KIRI_ON, RELAY_KIRI_OFF
try:
requests.get(RELAY_KIRI_OFF, timeout=1)
print(f"Relay Kiri OFF")
except requests.exceptions.RequestException as e:
print(e)
def relay_kiri_on():
global DETIK_ANTAR_KARUNG
try:
requests.get(RELAY_KIRI_ON, timeout=1)
threading.Timer(DETIK_ANTAR_KARUNG, relay_kiri_off).start()
print(f"Relay Kiri ON -> Start Counter Kiri: {DETIK_ANTAR_KARUNG} seconds")
except requests.exceptions.RequestException as e:
print(e)
# =====================
# RESET HARIAN
# =====================
def reset_counter():
global counter, seen_objects
print(f"[{datetime.now()}] Reset counter otomatis")
print(f"Reset counter otomatis")
# Save current counter values to database before reset
save_counter_to_database(camera_feeder)
# reset semua
@@ -166,6 +193,7 @@ def reset_counter():
republish_counter()
"""
def reset_counter_masuk():
global counter, seen_objects
print(f"[{datetime.now()}] Reset counter otomatis")
@@ -178,12 +206,13 @@ def reset_counter_masuk():
for camera_name in camera_masuk:
counter[camera_name]["karung"] = 0
counter["kandang_1_karung_masuk"]["karung"] = 0
#counter["kandang_1_karung_masuk"]["karung"] = 0
seen_objects = {}
save_counter()
schedule_reset_masuk() # jadwalkan besok
republish_counter()
"""
def schedule_reset():
@@ -196,6 +225,7 @@ def schedule_reset():
print(f"Reset counter dijadwalkan pada: {reset_time}")
"""
def schedule_reset_masuk():
now = datetime.now()
reset_time = now.replace(hour=23, minute=59, second=55, microsecond=0)
@@ -204,6 +234,7 @@ def schedule_reset_masuk():
delay = (reset_time - now).total_seconds()
threading.Timer(delay, reset_counter_masuk).start()
print(f"Reset counter dijadwalkan pada: {reset_time}")
"""
# =====================
@@ -221,6 +252,7 @@ def on_connect_publish(client, userdata, flags, rc):
def on_message(client, userdata, msg):
global DETIK_ANTAR_KARUNG
try:
payload = json.loads(msg.payload.decode())
if "after" not in payload:
@@ -271,7 +303,9 @@ def on_message(client, userdata, msg):
):
"""
if camera in camera_feeder:
DETIK_ANTAR_KARUNG = 35
#DETIK_ANTAR_KARUNG = 35
# Update 20260411 to 30s
#DETIK_ANTAR_KARUNG = 30
current_time = datetime.now()
for ts in seen_objects[camera].values():
delta = current_time - ts
@@ -283,7 +317,7 @@ def on_message(client, userdata, msg):
seen_objects[camera][track_id] = datetime.now()
print(
f"[{datetime.now()}] Kamera {camera}: Object {label} masuk zona {new_zones}"
f"Kamera {camera}: Object {label} masuk zona {new_zones}"
)
print(f"Total {label} di {camera}: {counter[camera][label]}")
@@ -297,6 +331,10 @@ def on_message(client, userdata, msg):
f"{SITE_TOPIC}/{camera}/{label}", counter[camera][label], qos=1, retain=True
)
# Start Relay
if camera == "kandang_atas_feeder_kiri":
relay_kiri_on()
except Exception as e:
print("Error parsing message:", e)
@@ -333,6 +371,8 @@ client_publish.connect(PUBLISH_BROKER, PUBLISH_PORT, 60)
# jadwalkan reset pertama
schedule_reset()
#schedule_reset_masuk()
# Pastikan Relay OFF
relay_kiri_off()
client_publish.loop_start()
print(f"MQTT Publish Counter berjalan di {PUBLISH_BROKER}")