forked from mapero/node-red-contrib-apcaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apcconfig.js
60 lines (51 loc) · 1.78 KB
/
apcconfig.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
module.exports = function(RED) {
"use strict";
var ApcAccess = require('apcaccess');
function ApcConfig(n) {
RED.nodes.createNode(this,n);
var node = this;
node.name = n.name;
node.host = n.host;
node.port = n.port;
node.interval = 1000;
node.attemts = 0;
node.decay = 1.5;
node.maxInterval = 30 * 60 * 1000;
node.client = new ApcAccess();
function connect() {
node.client.connect(node.host, node.port);
}
node.client.on('connect', () => {
node.attemts = 0;
node.emit('status',{fill:"green",shape:"dot",text:"online"});
node.log('Connected to '+node.host+':'+node.port);
});
node.client.on('disconnect', () => {
node.attemts++;
var timeout = node.interval * Math.pow(node.decay, node.attemts);
timeout = timeout > node.maxInterval ? node.maxInterval : timeout;
node.emit('status',{fill:"red",shape:"ring",text:"offline"});
node.log("Disconnected. Tries to reconnect in "+timeout/1000+" seconds.");
node.timeout = setTimeout(connect, timeout);
});
node.client.on('error', (error) => {
node.error(error);
});
this.on("close", function() {
if(node.timeout) {
clearTimeout(node.timeout);
}
if(node.client.isConnected) {
node.client.disconnect()
.then(function() {
node.log('Disconnected from '+node.host+':'+node.port);
}).catch( function(err) {
node.error(err);
});
}
node.client.removeAllListeners();
});
connect();
}
RED.nodes.registerType("apcconfig",ApcConfig);
};