2
0
Files
node-red-contrib-mi-devices/utils.js
Pierre CLEMENT 8948caf0d1 feat(sensors): add battery level
Also refactored some common parts for sensors. Refactored to parse data in payload and set it as object
2018-01-01 01:06:33 +01:00

34 lines
1013 B
JavaScript

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;
}
}