-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfritzbox-presence.js
61 lines (53 loc) · 2.1 KB
/
fritzbox-presence.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
const fritz = require('./fritz')
module.exports = function(RED) {
function FritzBoxPresence(config) {
RED.nodes.createNode(this, config)
var credentials = RED.nodes.getNode(config.fritz)
var username = credentials.username
var password = credentials.password
var hostname = credentials.hostname
var node = this
var sessionID = ''
node.status({})
function getDevices(sid) {
if (sid === '0000000000000000') {
node.status({ fill: "red", shape: "dot", text: "connection error" })
return node.error('could not get a valid session ID. Please check credentials.')
}
sid = sid || sessionID
if (sid == '' || typeof sid != 'string') {
node.status({ fill: "green", shape: "ring", text: "connecting" })
return fritz.getSessionID(username, password, getDevices, {
host: hostname
})
}
fritz.checkSession(sid, function(isSession) {
if (!isSession) {
return getDevices()
}
sessionID = sid
node.status({ fill: "green", shape: "dot", text: "querying" })
fritz.getData(sid, function(res) {
try {
res = JSON.parse(res)
var devices = res.data.active
node.status({ fill: "green", shape: "ring", text: `${devices.length} devices detected` })
node.send({ payload: devices, full_response: res })
} catch (e) {
node.status({ fill: "red", shape: "dot", text: "data error" })
node.error(e)
}
}, {
host: hostname
})
}, {
host: hostname
})
}
this.on('input', getDevices)
this.on('close', function() {
node.status({})
})
}
RED.nodes.registerType("fritzbox-presence", FritzBoxPresence)
}