diff --git a/src/butler.js b/src/butler.js index 910cc6a2..eeb6f688 100644 --- a/src/butler.js +++ b/src/butler.js @@ -117,39 +117,20 @@ const start = async () => { // ------------------------------------ // Create MQTT client object and connect to MQTT broker, if MQTT is enabled - try { - await mqttInitHandlers(); - - // if ( - // globals.config.has('Butler.mqttConfig.enable') && - // globals.config.has('Butler.mqttConfig.brokerHost') && - // globals.config.has('Butler.mqttConfig.brokerPort') && - // globals.config.get('Butler.mqttConfig.enable') - // ) { - // const mqttOptions = { - // host: globals.config.get('Butler.mqttConfig.brokerHost'), - // port: globals.config.get('Butler.mqttConfig.brokerPort'), - // }; - - // globals.mqttClient = mqtt.connect(mqttOptions); - // /* - // Following might be needed for conecting to older Mosquitto versions - // var mqttClient = mqtt.connect('mqtt://', { - // protocolId: 'MQIsdp', - // protocolVersion: 3 - // }); - // */ - // if (!globals.mqttClient.connected) { - // globals.logger.verbose( - // `CONFIG: Created (but not yet connected) MQTT object for ${mqttOptions.host}:${mqttOptions.port}, protocol version ${mqttOptions.protocolVersion}` - // ); - // } - // } else { - // globals.logger.info('CONFIG: MQTT disabled, not connecting to MQTT broker'); - // } - } catch (err) { - globals.logger.error(`CONFIG: Could not set up MQTT: ${JSON.stringify(err, null, 2)}`); - } + mqttInitHandlers(); + + // Sleep 5 seconds to allow MQTT to connect + globals.logger.info('MAIN: Sleeping 5 seconds to allow MQTT to connect.'); + globals.logger.info('5...'); + await globals.sleep(1000); + globals.logger.info('4...'); + await globals.sleep(1000); + globals.logger.info('3...'); + await globals.sleep(1000); + globals.logger.info('2...'); + await globals.sleep(1000); + globals.logger.info('1...'); + await globals.sleep(1000); // Set up service monitoring, if enabled in the config file if (globals.config.has('Butler.serviceMonitor.enable') && globals.config.get('Butler.serviceMonitor.enable') === true) { diff --git a/src/lib/mqtt_handlers.js b/src/lib/mqtt_handlers.js index 1cba97a3..71385725 100644 --- a/src/lib/mqtt_handlers.js +++ b/src/lib/mqtt_handlers.js @@ -8,7 +8,7 @@ const { config, logger, execPath } = require('../globals'); const globals = require('../globals'); const qrsUtil = require('../qrs_util'); -async function mqttInitHandlers() { +function mqttInitHandlers() { try { let mqttClient; let mqttOptions; @@ -24,6 +24,8 @@ async function mqttInitHandlers() { mqttClient = mqtt.connect(mqttOptions); + globals.mqttClient = mqttClient; + /* Following might be needed for conecting to older Mosquitto versions var mqttClient = mqtt.connect('mqtt://', { @@ -52,11 +54,11 @@ async function mqttInitHandlers() { }; // Connect to MQTT broker - // mqttClient = mqtt.connect(mqttOptions); mqttClient = mqtt.connect( `mqtts://${config.get('Butler.mqttConfig.brokerHost')}:${config.get('Butler.mqttConfig.brokerPort')}`, mqttOptions ); + globals.mqttClient = mqttClient; } else { logger.error('MQTT INIT HANDLERS: MQTT configuration error'); diff --git a/src/udp/udp_handlers.js b/src/udp/udp_handlers.js index ac1da77d..fe0d658a 100644 --- a/src/udp/udp_handlers.js +++ b/src/udp/udp_handlers.js @@ -461,7 +461,7 @@ const schedulerFailed = async (msg, legacyFlag) => { } else { globals.logger.warn( `MQTT: MQTT client not connected. Unable to publish message to topic ${globals.config.get( - 'Butler.mqttConfig.taskAbortedTopic' + 'Butler.mqttConfig.taskFailureTopic' )}` ); } @@ -730,7 +730,7 @@ const schedulerFailed = async (msg, legacyFlag) => { } else { globals.logger.warn( `MQTT: MQTT client not connected. Unable to publish message to topic ${globals.config.get( - 'Butler.mqttConfig.taskAbortedTopic' + 'Butler.mqttConfig.taskFailureTopic' )}` ); } @@ -931,8 +931,8 @@ module.exports.udpInitTaskErrorServer = () => { globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.taskFailureServerStatusTopic'), 'start'); } else { globals.logger.warn( - `MQTT: MQTT client not connected. Unable to publish message to topic ${globals.config.get( - 'Butler.mqttConfig.taskAbortedTopic' + `UDP SERVER INIT: MQTT client not connected. Unable to publish message to topic ${globals.config.get( + 'Butler.mqttConfig.taskFailureServerStatusTopic' )}` ); } @@ -952,8 +952,8 @@ module.exports.udpInitTaskErrorServer = () => { globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.taskFailureServerStatusTopic'), 'error'); } else { globals.logger.warn( - `MQTT: MQTT client not connected. Unable to publish message to topic ${globals.config.get( - 'Butler.mqttConfig.taskAbortedTopic' + `UDP SERVER ERROR: MQTT client not connected. Unable to publish message to topic ${globals.config.get( + 'Butler.mqttConfig.taskFailureServerStatusTopic' )}` ); }