2
0
Files
node-red-contrib-mi-devices/node-red-contrib-xiaomi-ht/xiaomi-ht.js
T

42 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-01-03 12:12:45 +01:00
const miDevicesUtils = require('../src/utils');
2017-06-25 18:48:17 +02:00
2018-01-03 12:12:45 +01:00
module.exports = (RED) => {
2018-01-05 22:21:50 +01:00
// sensor_ht, weather.v1
2017-06-25 18:48:17 +02:00
function XiaomiHtNode(config) {
RED.nodes.createNode(this, config);
this.gateway = RED.nodes.getNode(config.gateway);
this.sid = config.sid;
2018-01-03 12:12:45 +01:00
this.status({fill:"grey", shape:"ring", text:"battery - na"});
2017-06-25 18:48:17 +02:00
if (this.gateway) {
2018-01-03 12:12:45 +01:00
this.on('input', (msg) => {
let payload = msg.payload;
2017-06-25 18:48:17 +02:00
// Input from gateway
2018-01-03 12:12:45 +01:00
if (payload.sid) {
if (payload.sid == this.sid) {
miDevicesUtils.setStatus(this, payload.data);
["temperature", "humidity", "pressure"].forEach((dataType) => {
if(payload.data[dataType]) {
payload.data[dataType] = parseInt(payload.data[dataType])/100;
}
2018-01-03 12:12:45 +01:00
});
}
else {
msg = null;
2017-06-25 18:48:17 +02:00
}
}
// Prepare for request
else {
2018-01-03 12:12:45 +01:00
miDevicesUtils.prepareForGatewayRequest(this, msg);
}
2018-01-03 12:12:45 +01:00
this.send(msg);
2017-06-25 18:48:17 +02:00
});
}
}
RED.nodes.registerType("xiaomi-ht", XiaomiHtNode);
2018-01-03 12:12:45 +01:00
};