Implement MQTT advanced options

This commit is contained in:
tsightler
2022-04-26 20:12:30 -04:00
parent 32e323602e
commit e84bda4ff7

View File

@@ -48,29 +48,34 @@ class Mqtt {
async init() {
try {
let mqttOptions = utils.config.mqtt_options ? parseArgs(utils.config.mqtt_options) : {}
console.log(mqttOptions)
if (Object.keys(mqttOptions).length > 0) {
let mqttOptions = {}
if (utils.config.mqtt_options) {
// If any of the cerficiate keys are in mqtt_options, read the data from the file
try {
if (utils.config.mqtt_options.hasOwnProperty('key')) {
utils.config.mqtt_options.key = fs.readFileSync(utils.config.mqtt_options.key)
}
if (utils.config.mqtt_options.hasOwnProperty('cert')) {
utils.config.mqtt_options.cert = fs.readFileSync(utils.config.mqtt_options.cert)
}
if (utils.config.mqtt_options.hasOwnProperty('ca')) {
utils.config.mqtt_options.ca = fs.readFileSync(utils.config.mqtt_options.ca)
}
const mqttConfigOptions = parseArgs(utils.config.mqtt_options.split(','))
Object.keys(mqttConfigOptions).forEach(key => {
switch (key) {
// For any of the file based options read the file into the option property
case 'key':
case 'cert':
case 'ca':
mqttConfigOptions[key] = fs.readFileSync(mqttConfigOptions[key])
break;
case '_':
delete mqttConfigOptions[key]
}
})
mqttOptions = mqttConfigOptions
} catch(err) {
debug(err)
debug(colors.yellow('Could not parse MQTT advanced options, continuing with default settings'))
}
}
debug('Attempting connection to MQTT broker...')
// Connect to client facing MQTT broker
console.log(utils.config.mqtt_options)
this.client = await mqttApi.connect(utils.config.mqtt_url, utils.config.mqtt_options);
console.log(mqttOptions)
this.client = await mqttApi.connect(utils.config.mqtt_url, mqttOptions);
// Connect to internal IPC broker
this.ipcClient = await mqttApi.connect('mqtt://127.0.0.1:51883', {})