-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (67 loc) · 2.21 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
"use strict";
const fs = require("fs");
const checkEnvironmentFunction = require("./libs/envChecker");
const mqttFunctions = require("./libs/mqttFunctions");
const puppeteerFunctions = require("./libs/puppeteerFunctions");
async function main() {
// getting data array's & macs
let newDataRaw = JSON.stringify(
await puppeteerFunctions.retrieveRawTableData()
);
let newDataArray = JSON.parse(newDataRaw);
// looping trough array to extract macs and push them into own array
let newMacs = [];
newDataArray.forEach((element) => {
newMacs.push(element[1][0]);
});
// getting old data array & macs
let oldDataRaw = fs.readFileSync("./storage/devices.json", "utf8");
let oldDataArray = JSON.parse(oldDataRaw);
// looping trough array to extract macs and push them into own array
let oldMacs = [];
oldDataArray.forEach((element) => {
oldMacs.push(element[1][0]);
});
// check 2 mac lists to see which device has gone online or offline
let GoneSinceLastScan = oldMacs.filter((x) => !newMacs.includes(x));
let AppearedSinceLastScan = newMacs.filter((x) => !oldMacs.includes(x));
GoneSinceLastScan.forEach((e) => {
// getting info of object
oldDataArray.forEach((element) => {
if (element[1] == e) {
mqttFunctions.updateAllMqttTopics(
element[0][0],
e.replaceAll(":", ""),
"not_home"
); // replace all to cleanup mac-format for mqtt support
}
});
});
AppearedSinceLastScan.forEach((e) => {
// getting info of object
newDataArray.forEach((element) => {
if (element[1] == e) {
mqttFunctions.updateAllMqttTopics(
element[0][0],
e.replaceAll(":", ""),
"home"
); // replace all to cleanup mac-format for mqtt support
}
});
});
// writing back new list of devices to json file
fs.writeFileSync("./storage/devices.json", JSON.stringify(newDataArray));
}
checkEnvironmentFunction.checkEnvironmentVariables();
console.log(`Starting to track ${process.env.router_url}.`);
(function loop() {
setTimeout(function () {
try {
main();
} catch (error) {
console.log("Error! ", error);
} finally {
loop();
}
}, 90000); // timeout in ms
})();