new mqtt callback api
This commit is contained in:
12
src/main.py
12
src/main.py
@ -91,8 +91,8 @@ CLIENT_ID = f"{CLIENT_PREFIX}_{uuid.uuid4()}"
|
|||||||
topic_mapping = {}
|
topic_mapping = {}
|
||||||
|
|
||||||
# Callback function for successful connection to the broker
|
# Callback function for successful connection to the broker
|
||||||
def on_connect(client, userdata, flags, rc):
|
def on_connect(client, userdata, flags, reason_code, properties):
|
||||||
if rc == 0:
|
if reason_code == 0:
|
||||||
logger.info("Connected to the broker")
|
logger.info("Connected to the broker")
|
||||||
|
|
||||||
# Subscribe to dynamically generated topics for each box and create mappings
|
# Subscribe to dynamically generated topics for each box and create mappings
|
||||||
@ -125,7 +125,7 @@ def on_connect(client, userdata, flags, rc):
|
|||||||
topic_mapping[central_topic] = ("__central__", central_key)
|
topic_mapping[central_topic] = ("__central__", central_key)
|
||||||
logger.info(f"Subscribed to central topic '{central_topic}' (Key: '{central_key}')")
|
logger.info(f"Subscribed to central topic '{central_topic}' (Key: '{central_key}')")
|
||||||
else:
|
else:
|
||||||
logger.error(f"Connection error with code {rc}")
|
logger.error(f"Connection error with code {reason_code}")
|
||||||
|
|
||||||
# Callback function for received messages
|
# Callback function for received messages
|
||||||
def on_message(client, userdata, msg):
|
def on_message(client, userdata, msg):
|
||||||
@ -149,8 +149,8 @@ def on_message(client, userdata, msg):
|
|||||||
logger.error(f"Error processing message from '{msg.topic}': {e}")
|
logger.error(f"Error processing message from '{msg.topic}': {e}")
|
||||||
|
|
||||||
# Callback function for disconnection
|
# Callback function for disconnection
|
||||||
def on_disconnect(client, userdata, rc):
|
def on_disconnect(client, userdata, flags, reason_code, properties):
|
||||||
if rc != 0:
|
if reason_code != 0:
|
||||||
logger.warning("Unexpected disconnection, attempting to reconnect...")
|
logger.warning("Unexpected disconnection, attempting to reconnect...")
|
||||||
else:
|
else:
|
||||||
logger.info("Disconnected from the broker.")
|
logger.info("Disconnected from the broker.")
|
||||||
@ -161,7 +161,7 @@ def handle_exit_signal(signum, frame):
|
|||||||
client.disconnect() # Disconnects from the broker and stops loop_forever()
|
client.disconnect() # Disconnects from the broker and stops loop_forever()
|
||||||
|
|
||||||
# Initialize the MQTT client and configure callbacks
|
# Initialize the MQTT client and configure callbacks
|
||||||
client = mqtt.Client(client_id=CLIENT_ID)
|
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id=CLIENT_ID, )
|
||||||
|
|
||||||
context['client'] = client
|
context['client'] = client
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user