forked from bashGroup/node-red-contrib-fritz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
55 lines (49 loc) · 1.35 KB
/
config.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
var Fritzbox = require("fritzbox")
module.exports = function(RED) {
function FritzboxConfig(n) {
RED.nodes.createNode(this, n);
var node = this;
node.host = n.host;
node.timer = null;
node.state = null;
if(!node.host) return;
var config = {
host: n.host,
user: node.credentials.username,
password: node.credentials.password,
port: n.port,
ssl: n.ssl
};
node.fritzbox = new Fritzbox.Fritzbox(config);
node.reinit = function() {
Promise.all([node.fritzbox.initIGDDevice(), node.fritzbox.initTR064Device()].map(p => p.catch(error => null)))
.then(function() {
updateStatus("ready");
}).catch(function(error) {
node.error(`Initialization of device failed ${error}. Check configuration.`);
updateStatus("error");
});
};
var updateStatus = function(status) {
node.state = status;
switch(status) {
case "init":
node.emit("statusUpdate", {fill:"yellow",shape:"ring",text:"Initializing device ..."});
break;
case "ready":
node.emit("statusUpdate", {fill:"green",shape:"dot",text:"Ready"});
break;
case "error":
node.emit("statusUpdate", {fill:"red",shape:"ring",text:"Error"});
break;
}
};
node.reinit();
}
RED.nodes.registerType("fritzbox-config", FritzboxConfig, {
credentials: {
username: {type: "text"},
password: {type: "password"}
}
});
}