-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanel-io.js
65 lines (57 loc) · 1.83 KB
/
anel-io.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
module.exports = function(RED) {
function anelioNode(config) {
RED.nodes.createNode(this,config);
var node = this;
node.server = RED.nodes.getNode(config.server); //get connection node
node.ioNo = config.ioNo;
if (node.ioNo < 1 || node.ioNo > 8) { node.ioNo = 1; }
node.onChange = config.onChange;
node.sendFullMsg = config.fullMsg;
node.lastState = null;
//Subscribe to data updates from the connection node
node.status({fill:"yellow", shape:"ring", text:"connecting"});
if (node.server) {
node.server.subscribe(node);
node.server.updateState();
}
node.on('close', function (done) {
if (node.server) {
node.server.unsubscribe(node);
}
done();
});
node.onNewData = function(data) {
if (data.ios && data.ios[node.ioNo-1]) {
node.status({fill:"green", shape:"dot", text:data.ios[node.ioNo-1].state ? "on" : "off"});
if (!node.onChange || node.lastState !== data.ios[node.ioNo-1].state) {
if (node.sendFullMsg) {
node.send({payload: data});
} else {
node.send({payload: data.ios[node.ioNo-1]});
}
}
node.lastState = data.ios[node.ioNo-1].state;
}
};
node.onStatus = function(state) {
if (state === "error") {
node.status({fill:"red", shape:"ring", text:"connection error"});
}
}
node.on('input', function(msg, send, done) {
if (node.server) {
var onOff = false;
if (msg.payload.toString().toLowerCase() === "off" || msg.payload === "0") {
onOff = false;
} else if (msg.payload) {
onOff = true;
}
node.server.setOutletOrIo("io",node.ioNo,onOff);
}
//msg.payload = msg.payload.toLowerCase();
//node.send(msg);
done();
});
}
RED.nodes.registerType("anel-io",anelioNode);
}