-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfreebox.js
75 lines (53 loc) · 1.84 KB
/
freebox.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
/**
* Node-Red-Contrib-Freebox v0.1 https://github.com/ltoinel/node-red-contrib-freebox
*
* Copyright 2019 DomoGeeek Released under the Apache License 2.0 (Apache-2.0)
*
* @author: [email protected]
*/
module.exports = function(RED) {
// Local require
var freebox = require('./lib/freebox-api.js');
var fs = require('fs');
function FreeboxDevicesNode(config) {
var node = this;
RED.nodes.createNode(node,config);
node.on('input', function(msg) {
// We check the Freebox Token
var app_token = node.context().get("app_token");
if (app_token == undefined) {
// Registration
freebox.connect();
freebox.on('ready', function(box) {
freebox.register();
node.status({fill:"yellow",shape:"ring",text:"Trying to register this node-red module"});
freebox.on('registered', function(receivedToken) {
// We persist the token for a futur use
node.context().set("app_token",receivedToken.app_token);
node.context().set("track_id",receivedToken.track_id);
node.status({fill:"yellow",shape:"ring",text:"Please accept token on the Freebox to continue"});
});
});
} else {
console.info("Freebox authorisation token found !");
freebox.connect({
'app_token' : node.context().get("app_token"),
'track_id' : node.context().get("track_id")
});
freebox.on(
'ready',
function(box) {
node.status({fill:"yellow",shape:"ring",text:"Listing connected devices"});
// Removing the listener
freebox.removeAllListeners('ready');
// Retrieving wifi devices
freebox.browserPub(function(devices) {
node.send({payload:devices});
node.status({fill:"green",shape:"dot",text:"Connected"});
});
});
}
});
}
RED.nodes.registerType("freebox",FreeboxDevicesNode);
}