def start_mqtt():
global mqtt_client
try:
log.debug('Start MQTT Client')
mqtt_client = mqtt.Client(client_id=settings.MQTT_CLIENT_ID)
if settings.MQTT_LOGGING:
# Add MQTT logging to the app logs
mqtt_client.enable_logger(AppLogger.logger)
else:
mqtt_client.disable_logger()
# Assign callback functions
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
mqtt_client.on_publish = on_publish
mqtt_client.on_subscribe = on_subscribe
# Set a Will to be sent by the broker in case the client disconnects unexpectedly.
# QOS 2: The broker will deliver the message exactly once by using a four step handshake.
mqtt_client.will_set('/will/oops', payload='{} has vanished!'.format(settings.MQTT_CLIENT_ID), qos=2)
connack_code = mqtt_client.connect(settings.MQTT_SERVER, settings.MQTT_PORT)
log.info('MQTT connect reply to {}, {}: {}'.format(settings.MQTT_SERVER, settings.MQTT_PORT,
mqtt.connack_string(connack_code)))
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
mqtt_client.loop_forever()
except Exception as ex:
log.error('Exception in start_mqtt()! exception: {}'.format(ex))
raise
评论列表
文章目录