mirror of
https://github.com/tsightler/ring-mqtt.git
synced 2025-09-26 21:01:12 +08:00
Release 5.0.0
This commit is contained in:
18
lib/mqtt.js
18
lib/mqtt.js
@@ -47,33 +47,33 @@ class Mqtt {
|
||||
|
||||
// MQTT initialization successful, setup actions for MQTT events
|
||||
start() {
|
||||
const mqttLib = this
|
||||
const mqtt = this
|
||||
// On MQTT connect/reconnect send config/state information after delay
|
||||
this.client.on('connect', async function () {
|
||||
if (!mqttLib.connected) {
|
||||
mqttLib.connected = true
|
||||
if (!mqtt.connected) {
|
||||
mqtt.connected = true
|
||||
debug('MQTT connection established, processing locations...')
|
||||
}
|
||||
mqttLib.ring.processLocations(mqttLib)
|
||||
mqtt.ring.processLocations(mqttLib)
|
||||
})
|
||||
|
||||
this.client.on('reconnect', function () {
|
||||
if (mqttLib.connected) {
|
||||
if (mqtt.connected) {
|
||||
debug('Connection to MQTT broker lost. Attempting to reconnect...')
|
||||
} else {
|
||||
debug('Attempting to reconnect to MQTT broker...')
|
||||
}
|
||||
mqttLib.connected = false
|
||||
mqtt.connected = false
|
||||
})
|
||||
|
||||
this.client.on('error', function (error) {
|
||||
debug('Unable to connect to MQTT broker.', error.message)
|
||||
mqttLib.connected = false
|
||||
mqtt.connected = false
|
||||
})
|
||||
|
||||
// Process MQTT messages from subscribed command topics
|
||||
this.client.on('message', async function (topic, message) {
|
||||
mqttLib.processMessage(topic, message)
|
||||
mqtt.processMessage(topic, message)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ class Mqtt {
|
||||
}
|
||||
} else {
|
||||
// Parse topic to get location/device ID
|
||||
const ringTopicLevels = (this.config.data.ring_topic).split('/').length
|
||||
const ringTopicLevels = (this.config.ring_topic).split('/').length
|
||||
splitTopic = topic.split('/')
|
||||
const locationId = splitTopic[ringTopicLevels]
|
||||
const deviceId = splitTopic[ringTopicLevels + 2]
|
||||
|
@@ -49,8 +49,8 @@ class Ring {
|
||||
}
|
||||
|
||||
// Loop through each location and call publishLocation for supported/connected devices
|
||||
async processLocations(mqttLib) {
|
||||
this.mqtt = mqttLib
|
||||
async processLocations(mqtt) {
|
||||
this.mqtt = mqtt
|
||||
|
||||
// Update Ring location and device data
|
||||
await this.updateRingData()
|
||||
|
@@ -1,6 +1,5 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
// Defines
|
||||
const config = require('./lib/config')
|
||||
const state = require('./lib/state')
|
||||
const ring = require('./lib/ring')
|
||||
@@ -129,6 +128,7 @@ const main = async(generatedToken) => {
|
||||
}
|
||||
}
|
||||
|
||||
// Subscribe to token updates from token Web UI
|
||||
tokenApp.token.registerListener(function(generatedToken) {
|
||||
main(generatedToken)
|
||||
})
|
||||
|
Reference in New Issue
Block a user