Implement MQTT options

This commit is contained in:
tsightler
2022-04-26 14:34:31 -04:00
parent 2d5cf51d8c
commit 097bca12d1
4 changed files with 29 additions and 4 deletions

View File

@@ -44,6 +44,7 @@ class Config {
} }
// If there's still no configured settings, force some defaults. // If there's still no configured settings, force some defaults.
this.data.mqtt_options = this.data.mqtt_options ? this.data.mqtt_options : {}
this.data.ring_topic = this.data.ring_topic ? this.data.ring_topic : 'ring' this.data.ring_topic = this.data.ring_topic ? this.data.ring_topic : 'ring'
this.data.hass_topic = this.data.hass_topic ? this.data.hass_topic : 'homeassistant/status' this.data.hass_topic = this.data.hass_topic ? this.data.hass_topic : 'homeassistant/status'
if (!this.data.hasOwnProperty('enable_cameras')) { this.data.enable_cameras = true } if (!this.data.hasOwnProperty('enable_cameras')) { this.data.enable_cameras = true }

View File

@@ -2,8 +2,10 @@ const mqttApi = require('mqtt')
const debug = require('debug')('ring-mqtt') const debug = require('debug')('ring-mqtt')
const colors = require('colors/safe') const colors = require('colors/safe')
const utils = require('./utils') const utils = require('./utils')
const aedes = require('aedes')() const fs = require('fs')
const mqttServer = require('net').createServer(aedes.handle) const parseArgs = require('minimist')
const mqttServer = require('aedes')()
const net = require('net')
class Mqtt { class Mqtt {
constructor() { constructor() {
@@ -12,7 +14,8 @@ class Mqtt {
this.connected = false this.connected = false
// Start internal broker, used only for inter-process communication (IPC) // 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 // Configure event listeners
utils.event.on('ring_api_state', async (state) => { utils.event.on('ring_api_state', async (state) => {
@@ -45,10 +48,29 @@ class Mqtt {
async init() { async init() {
try { 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...') debug('Attempting connection to MQTT broker...')
// Connect to client facing 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 // Connect to internal IPC broker
this.ipcClient = await mqttApi.connect('mqtt://127.0.0.1:51883', {}) this.ipcClient = await mqttApi.connect('mqtt://127.0.0.1:51883', {})

1
package-lock.json generated
View File

@@ -16,6 +16,7 @@
"got": "^11.8.3", "got": "^11.8.3",
"ip": "^1.1.5", "ip": "^1.1.5",
"is-online": "^9.0.1", "is-online": "^9.0.1",
"minimist": "^1.2.6",
"mqtt": "4.3.7", "mqtt": "4.3.7",
"ring-client-api": "10.0.0-beta.5", "ring-client-api": "10.0.0-beta.5",
"write-file-atomic": "^4.0.1" "write-file-atomic": "^4.0.1"

View File

@@ -11,6 +11,7 @@
"ip": "^1.1.5", "ip": "^1.1.5",
"is-online": "^9.0.1", "is-online": "^9.0.1",
"write-file-atomic": "^4.0.1", "write-file-atomic": "^4.0.1",
"minimist": "^1.2.6",
"mqtt": "4.3.7", "mqtt": "4.3.7",
"aedes": "0.46.3", "aedes": "0.46.3",
"ring-client-api": "10.0.0-beta.5" "ring-client-api": "10.0.0-beta.5"