From f1ce54ccaf916b6cb9f54d4e19a80bd27efb572f Mon Sep 17 00:00:00 2001 From: KarstenSiedentopp Date: Wed, 19 Dec 2018 18:43:42 +0100 Subject: [PATCH] new dps topics type error fixed --- README.md | 2 +- tuya-mqtt.js | 44 +++++++++++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 49923e9..f5c46c1 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ Color for lightbulb: tuya/lightbulb////color // Color as Payload as hexColor Read data from device: - tuya/////dps // returns JSON.stringify(dps) values + tuya/////dps // returns JSON.stringify(dps) values, use with care, does not always contain all dps values tuya/////dps/ // return single dps data value ``` diff --git a/tuya-mqtt.js b/tuya-mqtt.js index 3403252..f2340a8 100644 --- a/tuya-mqtt.js +++ b/tuya-mqtt.js @@ -2,12 +2,18 @@ const mqtt = require('mqtt'); const TuyaDevice = require('./tuya-device'); const debug = require('debug')('tuya-mqtt'); const debugMqtt = require('debug')('mqtt'); +const debugTuya = require('debug')('tuyAPI-Events'); +const debugError = require('debug')('error'); var cleanup = require('./cleanup').Cleanup(onExit); function bmap(istate) { return istate ? 'ON' : "OFF"; } +function boolToString(istate) { + return istate ? 'true' : "false"; +} + var connected = undefined; const CONFIG = require("./config"); @@ -69,7 +75,7 @@ mqtt_client.on('message', function (topic, message) { } } } catch (e) { - debug(e); + debugError(e); } }); @@ -92,12 +98,12 @@ function publishStatus(device, status) { retain: true, qos: 2 }); - debug("mqtt status updated to:" + topic + " -> " + status); + debugTuya("mqtt status updated to:" + topic + " -> " + status); } else { - debug("mqtt status not updated"); + debugTuya("mqtt status not updated"); } } catch (e) { - debug(e); + debugError(e); } } } @@ -117,23 +123,27 @@ function publishDPS(device, dps) { if (tuyaID != undefined && tuyaKey != undefined && tuyaIP != undefined) { var topic = CONFIG.topic + type + "/" + tuyaID + "/" + tuyaKey + "/" + tuyaIP + "/dps"; - mqtt_client.publish(topic, JSON.stringify(dps), { + var data = JSON.stringify(dps); + debugTuya("mqtt dps updated to:" + topic + " -> ", data); + mqtt_client.publish(topic, data, { retain: true, qos: 2 }); + Object.keys(dps).forEach(function (key) { var topic = CONFIG.topic + type + "/" + tuyaID + "/" + tuyaKey + "/" + tuyaIP + "/dps/" + key; - mqtt_client.publish(topic, dps[key], { + var data = JSON.stringify(dps[key]); + debugTuya("mqtt dps updated to:" + topic + " -> dps[" + key + "]", data); + mqtt_client.publish(topic, data, { retain: true, qos: 2 }); }); - debug("mqtt dps updated to:" + topic + " -> " + dps); } else { - debug("mqtt dps not updated"); + debugTuya("mqtt dps not updated"); } } catch (e) { - debug(e); + debugError(e); } } } @@ -143,13 +153,17 @@ function publishDPS(device, dps) { * @see TuyAPI (https://github.com/codetheweb/tuyapi) */ TuyaDevice.onAll('data', function (data) { - debug('Data from device ' + this.type + ' :', data); - var status = data.dps['1']; - if (this.type == "lightbulb" && status == undefined) { - status = true; + try { + debugTuya('Data from device ' + this.type + ' :', data); + var status = data.dps['1']; + if (this.type == "lightbulb" && status == undefined) { + status = true; + } + publishStatus(this, bmap(status)); + publishDPS(this, data.dps); + } catch (e) { + debugError(e); } - publishStatus(this, bmap(status)); - publishDPS(this, data.dps); }); /**