Fetch existing retained snapshot upon initial connection

- Added logic to request the retained snapshot message upon MQTT connection.
- Ensure initial snapshot is processed after connecting to the MQTT broker.
- Improved debugging logs for better traceability.
This commit is contained in:
2024-10-20 17:28:56 +11:00
parent 121673fde1
commit 043e56e6f7
2 changed files with 16 additions and 9 deletions
+7
View File
@@ -22,6 +22,9 @@ last_rating = 0
LOWER_COLOR = np.array([20, 100, 100])
UPPER_COLOR = np.array([40, 255, 255])
# Track if the initial snapshot has been processed
initial_snapshot_processed = False
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected successfully to MQTT broker")
@@ -30,6 +33,8 @@ def on_connect(client, userdata, flags, rc):
publish_discovery_configurations()
# Publish initial rating of 0
publish_rating(0)
# Request the retained snapshot to process the initial image
client.publish(MQTT_SNAPSHOT_TOPIC, "", qos=1)
else:
print(f"Failed to connect, return code {rc}")
@@ -50,9 +55,11 @@ def publish_rating(rating):
client.publish(MQTT_TOPIC_PUBLISH, json.dumps({"rating": rating}))
def on_message(client, userdata, msg):
global initial_snapshot_processed
if msg.topic == MQTT_SNAPSHOT_TOPIC:
print("Snapshot received")
process_snapshot(msg.payload)
initial_snapshot_processed = True
def process_snapshot(payload):
if not payload: