mirror of
https://github.com/tsightler/ring-mqtt.git
synced 2025-10-05 08:47:04 +08:00
Implement MQTT options
This commit is contained in:
30
lib/mqtt.js
30
lib/mqtt.js
@@ -2,8 +2,10 @@ const mqttApi = require('mqtt')
|
||||
const debug = require('debug')('ring-mqtt')
|
||||
const colors = require('colors/safe')
|
||||
const utils = require('./utils')
|
||||
const aedes = require('aedes')()
|
||||
const mqttServer = require('net').createServer(aedes.handle)
|
||||
const fs = require('fs')
|
||||
const parseArgs = require('minimist')
|
||||
const mqttServer = require('aedes')()
|
||||
const net = require('net')
|
||||
|
||||
class Mqtt {
|
||||
constructor() {
|
||||
@@ -12,7 +14,8 @@ class Mqtt {
|
||||
this.connected = false
|
||||
|
||||
// Start internal broker, used only for inter-process communication (IPC)
|
||||
mqttServer.listen(51883, '127.0.0.1')
|
||||
net.createServer(mqttServer.handle)
|
||||
net.listen(51883, '127.0.0.1')
|
||||
|
||||
// Configure event listeners
|
||||
utils.event.on('ring_api_state', async (state) => {
|
||||
@@ -45,10 +48,29 @@ 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) {
|
||||
// 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)
|
||||
}
|
||||
} catch(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
|
||||
this.client = await mqttApi.connect(utils.config.mqtt_url, {});
|
||||
console.log(utils.config.mqtt_options)
|
||||
this.client = await mqttApi.connect(utils.config.mqtt_url, utils.config.mqtt_options);
|
||||
|
||||
// Connect to internal IPC broker
|
||||
this.ipcClient = await mqttApi.connect('mqtt://127.0.0.1:51883', {})
|
||||
|
Reference in New Issue
Block a user