-
Notifications
You must be signed in to change notification settings - Fork 0
/
pca9536.js
130 lines (114 loc) · 3.17 KB
/
pca9536.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
128
129
130
"use strict";
const PCA9536 = require("./index.js");
module.exports = function(RED){
var sensor_pool = {};
var loaded = [];
//ensureDependencies(['node-red-contrib-aws', 'fail']);
function NcdI2cDeviceNode(config){
RED.nodes.createNode(this, config);
this.interval = config.interval;
this.addr = 0x41;
this.onchange = config.onchange;
if(typeof sensor_pool[this.id] != 'undefined'){
//Redeployment
clearTimeout(sensor_pool[this.id].timeout);
delete(sensor_pool[this.id]);
}
this.sensor = new PCA9536(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(force){
if(node.interval && !sensor_pool[node.id].polling){
sensor_pool[node.id].polling = true;
get_status(true, force);
}
}
function stop_poll(){
clearTimeout(sensor_pool[node.id].timeout);
sensor_pool[node.id].polling = false;
}
function send_payload(_status, force){
if(!force && node.onchange && JSON.stringify(_status) == status) return;
var msg = [],
dev_status = {topic: 'device_status', payload: _status};
if(config.output_all){
var old_status = JSON.parse(status);
for(var i in _status){
if(!force && node.onchange && _status[i] == old_status[i]){
msg.push(null);
}else msg.push({topic: i, payload: _status[i]})
}
msg.push(dev_status);
}else{
msg = dev_status;
}
status = JSON.stringify(_status);
node.send(msg);
}
function get_status(repeat, force){
if(repeat) clearTimeout(sensor_pool[node.id].timeout);
if(device_status(node)){
node.sensor.get().then((res) => {
send_payload(res, force);
}).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);
}
}, 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('input', (msg) => {
stop_poll();
if(msg.topic != 'get_status'){
if(typeof node.sensor.settable != 'undefined' && node.sensor.settable.indexOf(msg.topic) > -1){
node.sensor.set(msg.topic, msg.payload).then().catch().then(() => {
start_poll()
});
}
}else{
start_poll(true);
}
});
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-pca9536", NcdI2cDeviceNode)
}