Add mechanism to request a fresh snapshot upon connecting
- Added a function to request a fresh snapshot from Frigate or similar system upon connecting. - Removed client disconnection to allow continued listening for snapshot messages.
This commit is contained in:
@@ -34,6 +34,8 @@ def on_connect(client, userdata, flags, rc):
|
|||||||
publish_discovery_configurations()
|
publish_discovery_configurations()
|
||||||
# Publish initial rating of 0
|
# Publish initial rating of 0
|
||||||
publish_rating(0)
|
publish_rating(0)
|
||||||
|
# Request Frigate or the source to publish a fresh snapshot
|
||||||
|
request_fresh_snapshot()
|
||||||
else:
|
else:
|
||||||
print(f"Failed to connect, return code {rc}")
|
print(f"Failed to connect, return code {rc}")
|
||||||
|
|
||||||
@@ -53,6 +55,12 @@ def publish_rating(rating):
|
|||||||
last_rating = rating
|
last_rating = rating
|
||||||
client.publish(MQTT_TOPIC_PUBLISH, json.dumps({"rating": rating}))
|
client.publish(MQTT_TOPIC_PUBLISH, json.dumps({"rating": rating}))
|
||||||
|
|
||||||
|
def request_fresh_snapshot():
|
||||||
|
# Assuming Frigate or another system listens to this topic to generate a snapshot
|
||||||
|
SNAPSHOT_REQUEST_TOPIC = "/frigate/patiocam/request_snapshot"
|
||||||
|
print(f"Requesting fresh snapshot by publishing to topic: {SNAPSHOT_REQUEST_TOPIC}")
|
||||||
|
client.publish(SNAPSHOT_REQUEST_TOPIC, json.dumps({"request": "snapshot"}), retain=False)
|
||||||
|
|
||||||
def on_message(client, userdata, msg):
|
def on_message(client, userdata, msg):
|
||||||
global initial_snapshot_processed
|
global initial_snapshot_processed
|
||||||
if msg.topic == MQTT_SNAPSHOT_TOPIC:
|
if msg.topic == MQTT_SNAPSHOT_TOPIC:
|
||||||
@@ -64,8 +72,6 @@ def on_message(client, userdata, msg):
|
|||||||
print(f"Payload (first 100 bytes): {msg.payload[:100]}...")
|
print(f"Payload (first 100 bytes): {msg.payload[:100]}...")
|
||||||
process_snapshot(msg.payload)
|
process_snapshot(msg.payload)
|
||||||
initial_snapshot_processed = True
|
initial_snapshot_processed = True
|
||||||
# Disconnect after receiving the retained message
|
|
||||||
client.disconnect()
|
|
||||||
|
|
||||||
def process_snapshot(payload):
|
def process_snapshot(payload):
|
||||||
if not payload:
|
if not payload:
|
||||||
|
|||||||
Reference in New Issue
Block a user