-
Notifications
You must be signed in to change notification settings - Fork 15
/
calllist.js
76 lines (69 loc) · 2.82 KB
/
calllist.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
var axios = require('axios')
https = require('https')
xml2js = require("xml2js");
var parser = xml2js.Parser({explicitRoot: false, explicitArray: false});
const httpclient = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});
module.exports = function(RED) {
function FritzboxList(n) {
RED.nodes.createNode(this,n);
var node = this;
node.max = n.max;
node.maxdays = n.maxdays;
node.action = n.action ? n.action : "GetCallList";
node.listurl = n.listurl ? n.listurl : "NewCallListURL";
node.phonebookId = n.id;
node.config = RED.nodes.getNode(n.device);
node.config.on('statusUpdate', node.status);
node.on('input', function(msg) {
if(node.config.state === "ready" && node.config.fritzbox) {
var args = {};
var action = node.action;
var queryParams = {};
var urlkey = node.listurl;
switch (action) {
case "GetCallList":
if(n.maxdays) {
queryParams["days"] = n.maxdays;
}
if(n.max) {
queryParams["max"] = n.max;
}
break;
case "GetPhonebook":
if (typeof msg.payload === "object" && msg.payload.NewPhonebookID) {
args = msg.payload;
} else {
args = {
NewPhonebookID: node.phonebookId ? node.phonebookId : 0
}
}
break;
}
node.config.fritzbox.services["urn:dslforum-org:service:X_AVM-DE_OnTel:1"].actions[action](args)
.then(function(response) {
var url = response[urlkey];
return httpclient.get(url, {params: queryParams})
}).then(function(result) {
return parser.parseStringPromise(result.data)
}).then(function(result) {
msg.payload = result;
node.send(msg);
}).catch(function(error) {
node.error(`Receiving Calllist / Phonebook failed. Error: ${error}`, msg);
});
} else {
node.warn("Device not ready.");
node.config.reinit();
}
});
node.on('close', function() {
node.config.removeListener('statusUpdate', node.status);
});
}
RED.nodes.registerType("fritzbox-calllist", FritzboxList);
RED.nodes.registerType("fritzbox-phonebook", FritzboxList);
}