diff --git a/README.md b/README.md index 23c465d41e1..f93f9e8ba95 100644 --- a/README.md +++ b/README.md @@ -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 @@ -365,6 +364,18 @@ Settings, scenes and Zwave configuration are stored in `JSON` files under projec #### Special topics +- **App version**: + +`/_CLIENTS/ZWAVE_GATEWAY-/version` + +The payload will be in the time-value json format and the value will contain the app string version. + +- **Mqtt status**: + +`/_CLIENTS/ZWAVE_GATEWAY-/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**: `///status` diff --git a/lib/MqttClient.js b/lib/MqttClient.js index ef80c4642f9..492bb8b2fea 100644 --- a/lib/MqttClient.js +++ b/lib/MqttClient.js @@ -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-' @@ -26,6 +26,9 @@ const ACTIONS = ['broadcast', 'api'] const HASS_WILL = 'homeassistant/status' +const STATUS_TOPIC = 'status' +const VERSION_TOPIC = 'version' + /** * The constructor */ @@ -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 @@ -143,6 +146,8 @@ function onConnect () { this.emit('brokerStatus', true) + this.publishVersion() + // Update client status this.updateClientStatus(true) @@ -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) { @@ -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 */