Skip to content

Commit

Permalink
feat: publish app version to mqtt #53
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Dec 7, 2020
1 parent ee462b7 commit 2f051a2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ Fully configurable Zwave to MQTT **Gateway** and **Control Panel**.
- [Environment variables](#environment-variables)
- [:question: FAQ](#question-faq)
- [:pray: Thanks](#pray-thanks)
- [:pencil: TODOs](#pencil-todos)
- [:bowtie: Author](#bowtie-author)

## :electric_plug: Installation
Expand Down Expand Up @@ -365,6 +364,18 @@ Settings, scenes and Zwave configuration are stored in `JSON` files under projec
#### Special topics
- **App version**:
`<mqtt_prefix>/_CLIENTS/ZWAVE_GATEWAY-<mqtt_name>/version`
The payload will be in the time-value json format and the value will contain the app string version.
- **Mqtt status**:
`<mqtt_prefix>/_CLIENTS/ZWAVE_GATEWAY-<mqtt_name>/status`
The payload will be in the time-value json format and the value will be `true` when mqtt is connected, `false` otherwise.
- **Node status**:
`<mqtt_prefix>/<?node_location>/<node_name>/status`
Expand Down
47 changes: 25 additions & 22 deletions lib/MqttClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const reqlib = require('app-root-path').require,
url = require('native-url'),
inherits = require('util').inherits

const appVersion = reqlib('package.json').version

debug.color = 5

const CLIENTS_PREFIX = '_CLIENTS'
const EVENTS_PREFIX = '_EVENTS'

const DEVICES_PREFIX = '$devices'

const BROADCAST_PREFIX = '_BROADCAST'

const NAME_PREFIX = 'ZWAVE_GATEWAY-'
Expand All @@ -26,6 +26,9 @@ const ACTIONS = ['broadcast', 'api']

const HASS_WILL = 'homeassistant/status'

const STATUS_TOPIC = 'status'
const VERSION_TOPIC = 'version'

/**
* The constructor
*/
Expand Down Expand Up @@ -61,7 +64,7 @@ function init (config) {
clean: config.clean,
rejectUnauthorized: !config.allowSelfsigned,
will: {
topic: this.getClientTopic(),
topic: this.getClientTopic(STATUS_TOPIC),
payload: JSON.stringify({ value: false }),
qos: this.config.qos,
retain: this.config.retain
Expand Down Expand Up @@ -143,6 +146,8 @@ function onConnect () {

this.emit('brokerStatus', true)

this.publishVersion()

// Update client status
this.updateClientStatus(true)

Expand Down Expand Up @@ -237,23 +242,8 @@ function onMessageReceived (topic, payload) {
* Returns the topic used to send client and devices status updateStates
* if name is null the client is the gateway itself
*/
MqttClient.prototype.getClientTopic = function (...devices) {
let subTopic = ''

for (let i = 0; i < devices.length; i++) {
const name = this.cleanName(devices[i])
subTopic += '/' + DEVICES_PREFIX + '/' + name
}

return (
this.config.prefix +
'/' +
CLIENTS_PREFIX +
'/' +
this.clientID +
subTopic +
'/status'
)
MqttClient.prototype.getClientTopic = function (suffix) {
return `${this.config.prefix}/${CLIENTS_PREFIX}/${this.clientID}/${suffix}`
}

MqttClient.prototype.cleanName = function (name) {
Expand Down Expand Up @@ -303,16 +293,29 @@ MqttClient.prototype.getStatus = function () {
/**
* Method used to update client connection status
*/
MqttClient.prototype.updateClientStatus = function (connected, ...devices) {
MqttClient.prototype.updateClientStatus = function (connected) {
if (this.client) {
this.client.publish(
this.getClientTopic(...devices),
this.getClientTopic(STATUS_TOPIC),
JSON.stringify({ value: connected, time: Date.now() }),
{ retain: this.config.retain, qos: this.config.qos }
)
}
}

/**
* Method used to publish app version to mqtt
*/
MqttClient.prototype.publishVersion = function () {
if (this.client) {
this.client.publish(
this.getClientTopic(VERSION_TOPIC),
JSON.stringify({ value: appVersion, time: Date.now() }),
{ retain: this.config.retain, qos: this.config.qos }
)
}
}

/**
* Method used to update client
*/
Expand Down

0 comments on commit 2f051a2

Please sign in to comment.