Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreypopov committed May 14, 2020
1 parent ff3a509 commit e052765
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 45 deletions.
5 changes: 4 additions & 1 deletion api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion lib/Zigbee2mqttHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
Expand All @@ -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
Expand Down
14 changes: 13 additions & 1 deletion nodes/bridge.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@


<div id="node-z2m-tab-map" class="node-z2m-tab-map" style="position: relative; margin-top: 30px;">


<select id="node-input-engine" style="width:100px;display:none;">
<option value="circo" selected>circo</option>
<option value="dot">dot</option>
<option value="fdp">fdp</option>
<option value="neato">neato</option>
<option value="osage">osage</option>
<option value="twopi">twopi</option>
</select>

<a href="#" id="z2m_map_refresh_btn" class="red-ui-item-add red-ui-button"><i class="fa fa-refresh"></i>&nbsp;<span data-i18n="map.refresh"></span></a>
<a href="#" id="z2m_map_fullscreen_btn" style="display:none;" class="red-ui-item-add red-ui-button"><i class="fa fa-external-link"></i>&nbsp;<span data-i18n="map.fullscreen"></span></a>
<div id="z2m_map_img"></div>
Expand Down Expand Up @@ -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"));
Expand Down
15 changes: 13 additions & 2 deletions nodes/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
7 changes: 7 additions & 0 deletions nodes/locales/en-US/bridge.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ <h3>Configuration</h3>
<dt>Server <span class="property-type">object</span></dt><dd>Choose the zigbee2mqtt server instance to use.</dd>
</dl>

<h3>Inputs</h3>
<h5>Input</h5>
<dl class="message-properties">
<dt>payload <span class="property-type">string</span></dt><dd>mqtt message</dd>
<dt>topic<span class="property-type">string</span></dt><dd>mqtt topic</dd>
</dl>

<h3>Outputs</h3>
<h5>Output</h5>
<dl class="message-properties">
Expand Down
9 changes: 6 additions & 3 deletions nodes/locales/en-US/in.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ <h3>Configuration</h3>
<dt>Server <span class="property-type">object</span></dt><dd>Choose the zigbee2mqtt server instance to use</dd>
<dt>Device <span class="property-type">string</span></dt><dd>Select device to listen to</dd>
<dt class="optional">Payload output <span class="property-type">string</span></dt><dd>Choose option which you want to send to payload</dd>
<dt class="optional">Start output <span class="property-type">boolean</span></dt><dd>Output state at startup. <b>IMPORTANT:</b> set <code>retain: true</code> in zigbee2mqtt configuration.yaml for every device</dd>
<dt class="optional">Start output <span class="property-type">boolean</span></dt><dd>Output state at startup.<br>
<b><i>notice #1:</i></b> set <code>retain: true</code> in zigbee2mqtt configuration.yaml for every device (z2m plugin sets it automatically on device add)<br>
<b><i>notice #2:</i></b> does not output on "Deploy only modified nodes" while there are no changes in current node
</dd>
</dl>

<h3>Outputs</h3>
<h5>Output</h5>
<dl class="message-properties">
<dt>payload <span class="property-type">string | object</span></dt><dd>current value</dd>
<dt>raw <span class="property-type">object</span></dt><dd>complete payload</dd>
<dt>payload <span class="property-type">string | object</span></dt><dd>current value depends on output state settings formatting</dd>
<dt>payload_raw <span class="property-type">sobject</span></dt><dd>data as it is</dd>
<dt>device<span class="property-type">object</span></dt><dd>device info</dd>
</dl>
</script>
Expand Down
54 changes: 18 additions & 36 deletions nodes/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'});
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit e052765

Please sign in to comment.