From f5dee305091380c307a6f858f92660b1c7ca2135 Mon Sep 17 00:00:00 2001 From: Marcus Brown Date: Sun, 20 Oct 2024 15:55:45 +1100 Subject: [PATCH] improve debug --- dayglo_detector/Dockerfile | 2 +- dayglo_detector/dayglo_detector.py | 6 +++++- dayglo_detector/test_mqtt.py | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 dayglo_detector/test_mqtt.py diff --git a/dayglo_detector/Dockerfile b/dayglo_detector/Dockerfile index c5ce7bb..3711e90 100644 --- a/dayglo_detector/Dockerfile +++ b/dayglo_detector/Dockerfile @@ -10,7 +10,7 @@ RUN apt-get update && apt-get install -y \ RUN pip install --no-cache-dir paho-mqtt opencv-python-headless numpy requests # Copy the dayglo detector script -COPY dayglo_detector.py /app/dayglo_detector.py +COPY dayglo_detector.py test_mqtt.py /app/ WORKDIR /app diff --git a/dayglo_detector/dayglo_detector.py b/dayglo_detector/dayglo_detector.py index a031d3b..141aeab 100644 --- a/dayglo_detector/dayglo_detector.py +++ b/dayglo_detector/dayglo_detector.py @@ -8,6 +8,8 @@ import json # Configuration MQTT_BROKER = os.environ.get('MQTT_BROKER', '10.59.221.172') MQTT_PORT = int(os.environ.get('MQTT_PORT', '1883')) +MQTT_USERNAME = os.getenv('MQTT_USERNAME', 'your_username') +MQTT_PASSWORD = os.getenv('MQTT_PASSWORD', 'your_password') MQTT_SNAPSHOT_TOPIC = "/frigate/patiocam/person/snapshot" MQTT_TOPIC_PUBLISH = "homeassistant/sensor/dayglo_rating/state" DAYGLO_THRESHOLD_TOPIC = "homeassistant/sensor/dayglo_threshold/state" @@ -71,7 +73,9 @@ def calculate_dayglo_rating(image): rating = (dayglo_pixels / total_pixels) * 100 return rating -client = mqtt.Client() +#client = mqtt.Client() +client = mqtt.Client(protocol=mqtt.MQTTv311) +client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD) client.on_connect = on_connect client.on_message = on_message client.connect(MQTT_BROKER, MQTT_PORT, 60) diff --git a/dayglo_detector/test_mqtt.py b/dayglo_detector/test_mqtt.py new file mode 100644 index 0000000..97245a7 --- /dev/null +++ b/dayglo_detector/test_mqtt.py @@ -0,0 +1,22 @@ +import os +import paho.mqtt.client as mqtt + +# Configuration +MQTT_BROKER = os.environ.get('MQTT_BROKER', '10.59.221.172') +MQTT_PORT = int(os.environ.get('MQTT_PORT', '1883')) +MQTT_USERNAME = os.getenv('MQTT_USERNAME', 'your_username') +MQTT_PASSWORD = os.getenv('MQTT_PASSWORD', 'your_password') + +client = mqtt.Client(protocol=mqtt.MQTTv311) # Ensure you are using the correct client version +client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD) # Set the username and password + +def on_connect(client, userdata, flags, rc): + if rc == 0: + print("Connected successfully.") + else: + print(f"Connected with result code {rc}") + +client.on_connect = on_connect +client.connect(MQTT_BROKER, MQTT_PORT, 60) +client.loop_forever() +