diff --git a/api.js b/api.js index e9cc936..1025c6e 100755 --- a/api.js +++ b/api.js @@ -174,7 +174,10 @@ module.exports = function(RED) { var config = req.query; var controller = RED.nodes.getNode(config.controllerID); if (controller && controller.constructor.name === "ServerNode") { - controller.refreshMap(true).then(function(response){ + + // res.json( controller.refreshMap(true)); + + controller.refreshMap(true, config.engine).then(function(response){ res.json(response); }).catch(error => { res.status(404).end(); diff --git a/lib/Zigbee2mqttHelper.js b/lib/Zigbee2mqttHelper.js index 7bb6cde..e2af261 100644 --- a/lib/Zigbee2mqttHelper.js +++ b/lib/Zigbee2mqttHelper.js @@ -232,6 +232,9 @@ class Zigbee2mqttHelper { //ContactSensor if ('contact' in payload) { msg["ContactSensor"] = { + "ContactSensorState":payload.contact?0:1 + }; + msg["ContactSensor_Inverse"] = { "ContactSensorState":payload.contact?1:0 }; } @@ -247,7 +250,8 @@ class Zigbee2mqttHelper { } //Battery - if ("powerSource" in device && "Battery" == device.powerSource && "battery" in payload && parseInt(payload.battery)>0) { + // if ("powerSource" in device && "Battery" == device.powerSource && "battery" in payload && parseInt(payload.battery)>0) { + if ('battery' in payload) { msg["Battery"] = { "BatteryLevel": parseInt(payload.battery), "StatusLowBattery": parseInt(payload.battery) <= 15 ? 1 : 0 diff --git a/nodes/bridge.html b/nodes/bridge.html index eb26036..9d1fb15 100644 --- a/nodes/bridge.html +++ b/nodes/bridge.html @@ -98,6 +98,17 @@
+ + + +  
@@ -253,7 +264,8 @@ $btn.find('span').text(RED._("node-red-contrib-zigbee2mqtt/bridge:map.loading")); $('#z2m_map_fullscreen_btn').hide(); $.getJSON('zigbee2mqtt/refreshMap', { - controllerID: node.server + controllerID: node.server, + engine: $('#node-input-engine').val() }).done(function (data, textStatus, jqXHR) { $btn.find('.fa-refresh').removeClass('fa-spin'); $btn.find('span').text(RED._("node-red-contrib-zigbee2mqtt/bridge:map.refresh")); diff --git a/nodes/bridge.js b/nodes/bridge.js index c5de1e2..d473797 100644 --- a/nodes/bridge.js +++ b/nodes/bridge.js @@ -56,8 +56,19 @@ module.exports = function(RED) { } - // if (node.server) { - // node.on('input', function (message_in) { + if (node.server) { + node.on('input', function (message_in) { + node.log('Published to mqtt topic: ' + message_in.topic + ' Payload: ' + message_in.payload); + node.server.mqtt.publish(message_in.topic, JSON.stringify(message_in.payload)); + }); + + } else { + node.status({ + fill: "red", + shape: "dot", + text: "node-red-contrib-zigbee2mqtt/bridge:status.no_server" + }); + } // clearTimeout(node.cleanTimer); // // var channels = []; diff --git a/nodes/locales/en-US/bridge.html b/nodes/locales/en-US/bridge.html index cb9af21..15e9876 100644 --- a/nodes/locales/en-US/bridge.html +++ b/nodes/locales/en-US/bridge.html @@ -6,6 +6,13 @@

Configuration

Server object
Choose the zigbee2mqtt server instance to use.
+

Inputs

+
Input
+
+
payload string
mqtt message
+
topicstring
mqtt topic
+
+

Outputs

Output
diff --git a/nodes/locales/en-US/in.html b/nodes/locales/en-US/in.html index 4c1daad..259e8d2 100644 --- a/nodes/locales/en-US/in.html +++ b/nodes/locales/en-US/in.html @@ -6,14 +6,17 @@

Configuration

Server object
Choose the zigbee2mqtt server instance to use
Device string
Select device to listen to
Payload output string
Choose option which you want to send to payload
-
Start output boolean
Output state at startup. IMPORTANT: set retain: true in zigbee2mqtt configuration.yaml for every device
+
Start output boolean
Output state at startup.
+ notice #1: set retain: true in zigbee2mqtt configuration.yaml for every device (z2m plugin sets it automatically on device add)
+ notice #2: does not output on "Deploy only modified nodes" while there are no changes in current node +

Outputs

Output
-
payload string | object
current value
-
raw object
complete payload
+
payload string | object
current value depends on output state settings formatting
+
payload_raw sobject
data as it is
deviceobject
device info
diff --git a/nodes/server.js b/nodes/server.js index c3b16a3..4a62f18 100644 --- a/nodes/server.js +++ b/nodes/server.js @@ -37,17 +37,18 @@ module.exports = function (RED) { // console.log(node.config._users); } - connectMQTT() { + connectMQTT(clientId = null) { var node = this; var options = { port: node.config.mqtt_port||1883, username: node.config.mqtt_username||null, password: node.config.mqtt_password||null, - clientId:"NodeRed-"+node.id + clientId:"NodeRed-"+node.id+(clientId?"-"+clientId:"") }; return mqtt.connect('mqtt://' + node.config.host, options); } + subscribeMQTT() { var node = this; node.mqtt.subscribe(node.topic, function (err) { @@ -78,14 +79,8 @@ module.exports = function (RED) { var timeout = null; var timeout_ms = 5000; - var options = { - port: node.config.mqtt_port || 1883, - username: node.config.mqtt_username || null, - password: node.config.mqtt_password || null, - clientId: "NodeRed-tmp-" + node.id - }; - var client = mqtt.connect('mqtt://' + node.config.host, options); + var client = node.connectMQTT('tmp'); client.on('connect', function () { //end function after timeout, if now response @@ -357,7 +352,7 @@ module.exports = function (RED) { removeDeviceFromGroup(deviceId, groupId) { var node = this; - + var device = node.getDeviceById(deviceId); if (!device) { device = {"friendly_name":deviceId}; @@ -395,22 +390,18 @@ module.exports = function (RED) { return {"success":true,"description":"command sent"}; } - refreshMap(wait = false) { + + + refreshMap(wait = false, engine = null) { var node = this; + return new Promise(function (resolve, reject) { if (wait) { var timeout = null; var timeout_ms = 60000 * 5; - var options = { - port: node.config.mqtt_port || 1883, - username: node.config.mqtt_username || null, - password: node.config.mqtt_password || null, - clientId: "NodeRed-tmp2-" + node.id - }; - var client = mqtt.connect('mqtt://' + node.config.host, options); - + var client = node.connectMQTT('tmp'); client.on('connect', function () { //end function after timeout, if now response @@ -443,7 +434,7 @@ module.exports = function (RED) { if (node.getBaseTopic() + "/bridge/networkmap/graphviz" == topic) { var messageString = message.toString(); - node.graphviz(messageString).then(function (data) { + node.graphviz(messageString, engine).then(function (data) { resolve({"success": true, "svg": node.map}); }).catch(error => { reject({'success':false, 'description':'graphviz failed'}); @@ -460,23 +451,14 @@ module.exports = function (RED) { }); } - graphviz(payload) { + async graphviz(payload, engine = null) { var node = this; - - return new Promise(function (resolve, reject) { - var options = { - format: 'svg', - engine: 'circo' - }; - var viz = new Viz({ Module, render }); - viz.renderString(payload,options).then(result => { - node.map = result; - resolve(result); - }).catch(error => { - console.error(error); - reject(error); - }); - }); + var options = { + format: 'svg', + engine: engine?engine:'circo' + }; + var viz = new Viz({ Module, render }); + return node.map = await viz.renderString(payload,options); } onMQTTConnect() { diff --git a/package.json b/package.json index df1f43b..a86a705 100755 --- a/package.json +++ b/package.json @@ -39,5 +39,5 @@ "type": "git", "url": "git+https://github.com/andreypopov/node-red-contrib-zigbee2mqtt.git" }, - "version": "0.4.0" + "version": "0.4.1" }