2
0

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:
Pierre CLEMENT
2018-01-01 01:06:33 +01:00
parent 8608f4eed4
commit 8948caf0d1
6 changed files with 60 additions and 55 deletions

View File

@@ -1,6 +1,7 @@
module.exports = function(RED) {
"use strict";
var mustache = require("mustache");
var miDevicesUtils = require('../utils');
function XiaomiMotionNode(config) {
RED.nodes.createNode(this, config);
@@ -14,7 +15,7 @@ module.exports = function(RED) {
var 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) {
node.on('input', function(msg) {
@@ -22,7 +23,7 @@ module.exports = function(RED) {
var payload = msg.payload;
if (payload.sid == node.sid && payload.model == "motion") {
var data = JSON.parse(payload.data)
var data = payload.data;
// if (data.status && data.status == "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"});
// state = "closed";
// }
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"});
}
}
miDevicesUtils.setStatus(node, data);
if (node.output == "0") {
msg.payload = payload;
miDevicesUtils.prepareFullDataOutput(payload);
node.send([msg]);
} else if (node.output == "1") {
var status = null;