-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
86 lines (79 loc) · 2.72 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
const request = require("request").defaults({jar: true})
, fs = require("fs")
, unifi = require("node-unifi");
try {
var config = JSON.parse(fs.readFileSync("config.json", "utf-8"));
}
catch (err) {
if (err.code = 'ENOENT') {
console.log(err);
console.log('config.json missing or invalid, please see README.md');
return;
} else {
throw err;
}
}
var iVar;
// Objects containing lists of all previously registered access points and clients and the times they registered
var nodes = { };
var clients = { };
var controller = new unifi.Controller(config.unifi.addr, config.unifi.port);
function getStats() {
controller.getClientDevices(config.unifi.site, function(error, data) {
if (error) {
console.log(`error: ${error}`);
clearInterval(iVar);
return;
}
var payloads = { };
var timestamp = Date.now();
data[0].forEach(function(client) {
if (!client.is_wired) {
if (payloads[client.ap_mac] == undefined) {
if (nodes[client.ap_mac] == undefined) {
console.log(`new node '${client.ap_mac}' online at ${timestamp}`);
nodes[client.ap_mac] = timestamp;
}
payloads[client.ap_mac] = {
node: client.ap_mac,
signals: [ ],
timestamp: timestamp,
group: config.findlf.group
}
}
if (clients[client.mac] == undefined) {
console.log(`new client '${client.mac}' online at ${timestamp}`);
clients[client.mac] = timestamp;
}
payloads[client.ap_mac].signals.push(
{
mac: client.mac,
rssi: client.signal
}
);
}
});
Object.keys(payloads).forEach(function(node) {
request(
{
method: 'POST',
url: config.findlf.url + '/reversefingerprint',
json: payloads[node]
}, function(error, response, body) {
if (error) {
console.log(`error: ${error}`);
clearInterval(iVar);
return;
}
});
});
});
}
controller.login(config.unifi.username, config.unifi.password, function(error) {
if (error) {
console.log(`error: ${error}`);
return;
}
getStats();
iVar = setInterval(getStats, config.other.interval*1000);
});