-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncd-humichip.js
127 lines (116 loc) · 3.39 KB
/
ncd-humichip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"use strict";
process.on('unhandledRejection', r => console.log(r));
const HumiChip = require("./index.js");
module.exports = function(RED){
var sensor_pool = {};
function NcdI2cDeviceNode(config){
RED.nodes.createNode(this, config);
this.interval = parseInt(config.interval);
this.addr = 0x28;
this.onchangeT = config.onchangeT;
this.onchangeH = config.onchangeH;
if(typeof sensor_pool[this.id] != 'undefined'){
//Redeployment
clearTimeout(sensor_pool[this.id].timeout);
delete(sensor_pool[this.id]);
}
this.sensor = new HumiChip(this.addr, config, RED.nodes.getNode(config.connection).i2c);
sensor_pool[this.id] = {
sensor: this.sensor,
polling: false,
timeout: 0,
node: this
};
var node = this;
var status = "{}";
function device_status(){
if(!node.sensor.initialized){
node.status({fill:"red",shape:"ring",text:"disconnected"});
return false;
}
node.status({fill:"green",shape:"dot",text:"connected"});
return true;
}
function start_poll(){
if(node.interval && !sensor_pool[node.id].polling){
sensor_pool[node.id].polling = true;
get_status(true);
}
}
function stop_poll(){
clearTimeout(sensor_pool[node.id].timeout);
sensor_pool[node.id].polling = false;
}
function send_payload(_status){
if(node.onchange && JSON.stringify(_status) == status) return;
var msg = [],
dev_status = {topic: 'device_status', payload: _status},
changed = false;
if(config.output_all){
var old_status = JSON.parse(status);
if(node.onchangeH && Math.abs(_status.humidity - old_status.humidity) < node.onchangeH){
msg.push(null);
}else{
changed = true;
msg.push({topic: 'humidity', payload: _status.humidity});
}
if(node.onchangeT && Math.abs(_status.temperature - old_status.temperature) < node.onchangeT){
msg.push(null);
}else{
changed = true;
msg.push({topic: 'temperature', payload: _status.temperature});
}
msg.push(dev_status);
}else{
if((!node.onchangeT || Math.abs(_status.temperature - old_status.temperature) >= node.onchangeT) ||
(!node.onchangH || Math.abs(_status.humidity - old_status.humidity) >= node.onchangeH)){
changed = true;
}
msg = dev_status;
}
if(status == "{}" || changed){
status = JSON.stringify(_status);
node.send(msg);
}
}
function get_status(repeat){
if(repeat) clearTimeout(sensor_pool[node.id].timeout);
if(device_status(node)){
node.sensor.get().then((res) => {
send_payload(res);
}).catch((err) => {
node.send({error: err});
}).then(() => {
if(repeat && node.interval){
clearTimeout(sensor_pool[node.id].timeout);
sensor_pool[node.id].timeout = setTimeout(() => {
if(typeof sensor_pool[node.id] != 'undefined'){
get_status(true);
}
}, sensor_pool[node.id].node.interval);
}else{
sensor_pool[node.id].polling = false;
}
});
}else{
node.sensor.init();
clearTimeout(sensor_pool[node.id].timeout);
sensor_pool[node.id].timeout = setTimeout(() => {
if(typeof sensor_pool[node.id] != 'undefined'){
get_status(true);
}
}, 3000);
}
}
node.on('close', (removed, done) => {
if(removed){
clearTimeout(sensor_pool[node.id].timeout);
delete(sensor_pool[node.id]);
}
done();
});
start_poll();
device_status(node);
}
RED.nodes.registerType("ncd-humichip", NcdI2cDeviceNode);
};