feat(sensors): add battery level
Also refactored some common parts for sensors. Refactored to parse data in payload and set it as object
This commit is contained in:
@@ -56,7 +56,12 @@ module.exports = function(RED) {
|
|||||||
server.on('message', function (message, remote) {
|
server.on('message', function (message, remote) {
|
||||||
var msg;
|
var msg;
|
||||||
if(remote.address == node.addr) {
|
if(remote.address == node.addr) {
|
||||||
msg = { payload: JSON.parse(message.toString('utf8')) };
|
var msg = message.toString('utf8');
|
||||||
|
var jsonMsg = JSON.parse(msg);
|
||||||
|
if(jsonMsg.data) {
|
||||||
|
jsonMsg.data = JSON.parse(jsonMsg.data) || jsonMsg.data;
|
||||||
|
}
|
||||||
|
msg = { payload: jsonMsg };
|
||||||
node.send(msg);
|
node.send(msg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ module.exports = function(RED) {
|
|||||||
"use strict";
|
"use strict";
|
||||||
var mustache = require("mustache");
|
var mustache = require("mustache");
|
||||||
var dgram = require('dgram');
|
var dgram = require('dgram');
|
||||||
|
var miDevicesUtils = require('../utils');
|
||||||
|
|
||||||
function XiaomiHtNode(config) {
|
function XiaomiHtNode(config) {
|
||||||
RED.nodes.createNode(this, config);
|
RED.nodes.createNode(this, config);
|
||||||
@@ -13,8 +14,7 @@ module.exports = function(RED) {
|
|||||||
this.divide = config.divide;
|
this.divide = config.divide;
|
||||||
|
|
||||||
var node = this;
|
var node = this;
|
||||||
|
node.status({fill:"grey", shape:"ring", text:"battery - na"});
|
||||||
node.status({fill:"grey",shape:"ring",text:"battery"});
|
|
||||||
|
|
||||||
if (this.gateway) {
|
if (this.gateway) {
|
||||||
node.on('input', function(msg) {
|
node.on('input', function(msg) {
|
||||||
@@ -23,20 +23,11 @@ module.exports = function(RED) {
|
|||||||
node.log("Received message from: " + payload.model + " sid: " + payload.sid + " payload: " + payload.data);
|
node.log("Received message from: " + payload.model + " sid: " + payload.sid + " payload: " + payload.data);
|
||||||
|
|
||||||
if (payload.sid == node.sid && ["sensor_ht", "weather.v1"].indexOf(payload.model) >= 0) {
|
if (payload.sid == node.sid && ["sensor_ht", "weather.v1"].indexOf(payload.model) >= 0) {
|
||||||
var data = JSON.parse(payload.data)
|
var data = payload.data;
|
||||||
|
miDevicesUtils.setStatus(node, data);
|
||||||
if (data.voltage) {
|
|
||||||
if (data.voltage < 2500) {
|
|
||||||
node.status({fill:"red",shape:"dot",text:"battery"});
|
|
||||||
} else if (data.voltage < 2900) {
|
|
||||||
node.status({fill:"yellow",shape:"dot",text:"battery"});
|
|
||||||
} else {
|
|
||||||
node.status({fill:"green",shape:"dot",text:"battery"});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node.output == "0") {
|
if (node.output == "0") {
|
||||||
msg.payload = payload;
|
miDevicesUtils.prepareFullDataOutput(payload);
|
||||||
node.send([msg]);
|
node.send([msg]);
|
||||||
} else if (node.output == "1") {
|
} else if (node.output == "1") {
|
||||||
var temp = null;
|
var temp = null;
|
||||||
@@ -76,7 +67,7 @@ module.exports = function(RED) {
|
|||||||
|
|
||||||
if (data.pressure) {
|
if (data.pressure) {
|
||||||
if (this.divide) {
|
if (this.divide) {
|
||||||
data.pressure = String(data.pressure / 1000);
|
data.pressure = String(data.pressure / 100);
|
||||||
}
|
}
|
||||||
pressure = {"payload": mustache.render(node.pressure, data)}
|
pressure = {"payload": mustache.render(node.pressure, data)}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
module.exports = function(RED) {
|
module.exports = function(RED) {
|
||||||
"use strict";
|
"use strict";
|
||||||
var mustache = require("mustache");
|
var mustache = require("mustache");
|
||||||
|
var miDevicesUtils = require('../utils');
|
||||||
|
|
||||||
function XiaomiMagnetNode(config) {
|
function XiaomiMagnetNode(config) {
|
||||||
RED.nodes.createNode(this, config);
|
RED.nodes.createNode(this, config);
|
||||||
@@ -14,7 +15,7 @@ module.exports = function(RED) {
|
|||||||
var state = "";
|
var state = "";
|
||||||
|
|
||||||
// node.status({fill:"yellow", shape:"dot", text:"unknown state"});
|
// node.status({fill:"yellow", shape:"dot", text:"unknown state"});
|
||||||
node.status({fill:"grey",shape:"ring",text:"battery"});
|
node.status({fill:"grey", shape:"ring", text:"battery - na"});
|
||||||
|
|
||||||
if (this.gateway) {
|
if (this.gateway) {
|
||||||
node.on('input', function(msg) {
|
node.on('input', function(msg) {
|
||||||
@@ -22,7 +23,7 @@ module.exports = function(RED) {
|
|||||||
var payload = msg.payload;
|
var payload = msg.payload;
|
||||||
|
|
||||||
if (payload.sid == node.sid && ["magnet", "sensor_magnet.aq2"].indexOf(payload.model) >= 0) {
|
if (payload.sid == node.sid && ["magnet", "sensor_magnet.aq2"].indexOf(payload.model) >= 0) {
|
||||||
var data = JSON.parse(payload.data)
|
var data = payload.data;
|
||||||
|
|
||||||
// if (data.status && data.status == "open") {
|
// if (data.status && data.status == "open") {
|
||||||
// node.status({fill:"green", shape:"dot", text:"open"});
|
// node.status({fill:"green", shape:"dot", text:"open"});
|
||||||
@@ -31,20 +32,11 @@ module.exports = function(RED) {
|
|||||||
// node.status({fill:"red", shape:"dot", text:"closed"});
|
// node.status({fill:"red", shape:"dot", text:"closed"});
|
||||||
// state = "closed";
|
// state = "closed";
|
||||||
// }
|
// }
|
||||||
|
miDevicesUtils.setStatus(node, data);
|
||||||
if (data.voltage) {
|
|
||||||
if (data.voltage < 2500) {
|
|
||||||
node.status({fill:"red",shape:"dot",text:"battery"});
|
|
||||||
} else if (data.voltage < 2900) {
|
|
||||||
node.status({fill:"yellow",shape:"dot",text:"battery"});
|
|
||||||
} else {
|
|
||||||
node.status({fill:"green",shape:"dot",text:"battery"});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (node.output == "0") {
|
if (node.output == "0") {
|
||||||
msg.payload = payload;
|
miDevicesUtils.prepareFullDataOutput(payload);
|
||||||
node.send([msg]);
|
node.send([msg]);
|
||||||
} else if (node.output == "1") {
|
} else if (node.output == "1") {
|
||||||
var status = null;
|
var status = null;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
module.exports = function(RED) {
|
module.exports = function(RED) {
|
||||||
"use strict";
|
"use strict";
|
||||||
var mustache = require("mustache");
|
var mustache = require("mustache");
|
||||||
|
var miDevicesUtils = require('../utils');
|
||||||
|
|
||||||
function XiaomiMotionNode(config) {
|
function XiaomiMotionNode(config) {
|
||||||
RED.nodes.createNode(this, config);
|
RED.nodes.createNode(this, config);
|
||||||
@@ -14,7 +15,7 @@ module.exports = function(RED) {
|
|||||||
var state = "";
|
var state = "";
|
||||||
|
|
||||||
// node.status({fill:"yellow", shape:"dot", text:"unknown state"});
|
// node.status({fill:"yellow", shape:"dot", text:"unknown state"});
|
||||||
node.status({fill:"grey",shape:"ring",text:"battery"});
|
node.status({fill:"grey", shape:"ring", text:"battery - na"});
|
||||||
|
|
||||||
if (this.gateway) {
|
if (this.gateway) {
|
||||||
node.on('input', function(msg) {
|
node.on('input', function(msg) {
|
||||||
@@ -22,7 +23,7 @@ module.exports = function(RED) {
|
|||||||
var payload = msg.payload;
|
var payload = msg.payload;
|
||||||
|
|
||||||
if (payload.sid == node.sid && payload.model == "motion") {
|
if (payload.sid == node.sid && payload.model == "motion") {
|
||||||
var data = JSON.parse(payload.data)
|
var data = payload.data;
|
||||||
|
|
||||||
// if (data.status && data.status == "open") {
|
// if (data.status && data.status == "open") {
|
||||||
// node.status({fill:"green", shape:"dot", text:"open"});
|
// node.status({fill:"green", shape:"dot", text:"open"});
|
||||||
@@ -31,20 +32,11 @@ module.exports = function(RED) {
|
|||||||
// node.status({fill:"red", shape:"dot", text:"closed"});
|
// node.status({fill:"red", shape:"dot", text:"closed"});
|
||||||
// state = "closed";
|
// state = "closed";
|
||||||
// }
|
// }
|
||||||
|
miDevicesUtils.setStatus(node, data);
|
||||||
if (data.voltage) {
|
|
||||||
if (data.voltage < 2500) {
|
|
||||||
node.status({fill:"red",shape:"dot",text:"battery"});
|
|
||||||
} else if (data.voltage < 2900) {
|
|
||||||
node.status({fill:"yellow",shape:"dot",text:"battery"});
|
|
||||||
} else {
|
|
||||||
node.status({fill:"green",shape:"dot",text:"battery"});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (node.output == "0") {
|
if (node.output == "0") {
|
||||||
msg.payload = payload;
|
miDevicesUtils.prepareFullDataOutput(payload);
|
||||||
node.send([msg]);
|
node.send([msg]);
|
||||||
} else if (node.output == "1") {
|
} else if (node.output == "1") {
|
||||||
var status = null;
|
var status = null;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
module.exports = function(RED) {
|
module.exports = function(RED) {
|
||||||
"use strict";
|
"use strict";
|
||||||
var mustache = require("mustache");
|
var mustache = require("mustache");
|
||||||
|
var miDevicesUtils = require('../utils');
|
||||||
|
|
||||||
function XiaomiSwitchNode(config) {
|
function XiaomiSwitchNode(config) {
|
||||||
RED.nodes.createNode(this, config);
|
RED.nodes.createNode(this, config);
|
||||||
@@ -12,7 +13,7 @@ module.exports = function(RED) {
|
|||||||
|
|
||||||
var node = this;
|
var node = this;
|
||||||
|
|
||||||
node.status({fill:"grey",shape:"ring",text:"battery"});
|
node.status({fill:"grey", shape:"ring", text:"battery - na"});
|
||||||
|
|
||||||
if (this.gateway) {
|
if (this.gateway) {
|
||||||
node.on('input', function(msg) {
|
node.on('input', function(msg) {
|
||||||
@@ -20,20 +21,11 @@ module.exports = function(RED) {
|
|||||||
var payload = msg.payload;
|
var payload = msg.payload;
|
||||||
|
|
||||||
if (payload.sid == node.sid && ["switch", "sensor_switch.aq2"].indexOf(payload.model) >= 0) {
|
if (payload.sid == node.sid && ["switch", "sensor_switch.aq2"].indexOf(payload.model) >= 0) {
|
||||||
var data = JSON.parse(payload.data)
|
var data = payload.data;
|
||||||
|
miDevicesUtils.setStatus(node, data);
|
||||||
if (data.voltage) {
|
|
||||||
if (data.voltage < 2500) {
|
|
||||||
node.status({fill:"red",shape:"dot",text:"battery"});
|
|
||||||
} else if (data.voltage < 2900) {
|
|
||||||
node.status({fill:"yellow",shape:"dot",text:"battery"});
|
|
||||||
} else {
|
|
||||||
node.status({fill:"green",shape:"dot",text:"battery"});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node.output == "0") {
|
if (node.output == "0") {
|
||||||
msg.payload = payload;
|
miDevicesUtils.prepareFullDataOutput(payload);
|
||||||
node.send([msg]);
|
node.send([msg]);
|
||||||
} else if (node.output == "1") {
|
} else if (node.output == "1") {
|
||||||
var status = null;
|
var status = null;
|
||||||
|
|||||||
33
utils.js
Normal file
33
utils.js
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
computeBatteryLevel: function(voltage) {
|
||||||
|
/*
|
||||||
|
When full, CR2032 batteries are between 3 and 3.4V
|
||||||
|
http://farnell.com/datasheets/1496885.pdf
|
||||||
|
*/
|
||||||
|
return Math.min(Math.round((voltage - 2200) / 14), 100);
|
||||||
|
},
|
||||||
|
setStatus: function(node, data) {
|
||||||
|
if (data.voltage) {
|
||||||
|
var batteryPercent = Math.min(Math.round((data.voltage - 2200) / 14), 100);
|
||||||
|
var status = {
|
||||||
|
fill: "green", shape: "dot",
|
||||||
|
text: "battery - " + batteryPercent + "%"
|
||||||
|
};
|
||||||
|
|
||||||
|
if (data.voltage < 2500) {
|
||||||
|
status.color = "red";
|
||||||
|
} else if (data.voltage < 2900) {
|
||||||
|
status.color = "yellow";
|
||||||
|
}
|
||||||
|
node.status(status);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
prepareFullDataOutput: function(payload) {
|
||||||
|
if(payload.data.voltage) {
|
||||||
|
payload.data.batteryLevel = this.computeBatteryLevel(payload.data.voltage);
|
||||||
|
}
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user