From 3b317535d3ee21bb284623e4b80d6a57e884759f Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Wed, 18 Apr 2018 20:53:22 +0200 Subject: [PATCH] Fixes & improvements. #8 --- lib/controller.js | 11 +++++------ lib/mqtt.js | 12 ++++++------ lib/util/settings.js | 1 + lib/zigbee.js | 11 +++-------- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/lib/controller.js b/lib/controller.js index 724117a448..d53ff06b3b 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -62,8 +62,8 @@ class Controller { const mappedModel = deviceMapping[modelID]; if (!mappedModel) { - logger.error(`Device with modelID '${modelID}' is not supported.`); - logger.error('Please create an issue on https://github.com/Koenkk/zigbee2mqtt/issues to add support for your device'); + logger.warn(`Device with modelID '${modelID}' is not supported.`); + logger.warn('Please create an issue on https://github.com/Koenkk/zigbee2mqtt/issues to add support for your device'); return; } @@ -72,22 +72,21 @@ class Controller { const converters = zigbee2mqtt.filter((c) => c.devices.includes(mappedModel.model) && c.cid === cid && c.type === message.type); if (!converters.length) { - logger.error(`No converter available for '${mappedModel.model}' with cid '${cid}' and type '${message.type}'`); - logger.error('Please create an issue on https://github.com/Koenkk/zigbee2mqtt/issues with this message.'); + logger.warn(`No converter available for '${mappedModel.model}' with cid '${cid}' and type '${message.type}'`); + logger.warn('Please create an issue on https://github.com/Koenkk/zigbee2mqtt/issues with this message.'); return; } // Convert this Zigbee message to a MQTT message. const friendlyName = settings.get().devices[device.ieeeAddr].friendly_name; const retain = settings.get().devices[device.ieeeAddr].retain; - const topic = `${settings.get().mqtt.base_topic}/${friendlyName}`; const publish = (payload) => { if (this.stateCache[device.ieeeAddr]) { payload = {...this.stateCache[device.ieeeAddr], ...payload}; } - this.mqtt.publish(topic, JSON.stringify(payload), retain); + this.mqtt.publish(friendlyName, JSON.stringify(payload), retain); } // Get payload for the message. diff --git a/lib/mqtt.js b/lib/mqtt.js index fbe164941f..23883297d0 100644 --- a/lib/mqtt.js +++ b/lib/mqtt.js @@ -41,10 +41,10 @@ class MQTT { clearTimeout(this.connectionTimer); this.connectionTimer = null; - this.publish('bridge/state', 'offline', true); - - logger.info('Disconnecting from MQTT server'); - this.client.end(); + this.publish('bridge/state', 'offline', true, () => { + logger.info('Disconnecting from MQTT server'); + this.client.end(); + }); } handleConnect() { @@ -59,7 +59,7 @@ class MQTT { } } - publish(topic, payload, retain) { + publish(topic, payload, retain, callback) { topic = `${settings.get().mqtt.base_topic}/${topic}`; if (!this.client || this.client.reconnecting) { @@ -69,7 +69,7 @@ class MQTT { } logger.info(`MQTT publish, topic: '${topic}', payload: '${payload}'`); - this.client.publish(topic, payload, {retain: retain}); + this.client.publish(topic, payload, {retain: retain}, callback); } } diff --git a/lib/util/settings.js b/lib/util/settings.js index b7c6b652cd..a405807171 100644 --- a/lib/util/settings.js +++ b/lib/util/settings.js @@ -20,4 +20,5 @@ function read() { module.exports = { get: () => settings, + write: () => write(), } diff --git a/lib/zigbee.js b/lib/zigbee.js index c79875207d..92fdff2452 100644 --- a/lib/zigbee.js +++ b/lib/zigbee.js @@ -39,12 +39,7 @@ class Zigbee { stop(callback) { this.shepherd.stop((error) => { - if (error) { - logger.error('Error while stopping zigbee-shepherd'); - } else { - logger.error('zigbee-shepherd stopped') - } - + logger.info('zigbee-shepherd stopped') callback(error); }); } @@ -97,7 +92,7 @@ class Zigbee { } if (settings.get().devices[device.ieeeAddr]) { - friendlyName = settings.devices[device.ieeeAddr].friendly_name + friendlyName = settings.get().devices[device.ieeeAddr].friendly_name } return `${friendlyName} (${device.ieeeAddr}): ${friendlyDevice.model} - ${friendlyDevice.description}`; @@ -110,7 +105,7 @@ class Zigbee { logger.error(`Zigbee cannot publish message to device because '${deviceID}' is not known by zigbee-shepherd`); } - logger.info(`Zigbee publish to '${deviceID}', ${cId} - ${cmd} - ${zclData}`); + logger.info(`Zigbee publish to '${deviceID}', ${cId} - ${cmd} - ${JSON.stringify(zclData)}`); device.functional(cId, cmd, zclData, callback); } }