-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
113 lines (85 loc) · 3.47 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
const async = require('async');
const mqtt = require('mqtt');
const ms = require('ms');
const argv = require('yargs')
.usage('Usage: $0 --config config')
.argv;
const ip = require('ip');
const logger = require('./lib/logger');
const Unifi = require('./lib/Collectors/Unifi');
const Nmap = require('./lib/Collectors/Nmap');
const CollectorRunner = require('./lib/CollectorRunner');
const MySQL = require('./lib/Database/MySQL');
const AliveHosts = require('./lib/Database/AliveHosts');
const configurations = require('./lib/configurations');
const config = configurations.getConfig(argv.config);
const mqttClient = mqtt.connect('mqtt://' + config.mqtt.hostname);
const mysql = new MySQL(config.mysql);
const aliveHosts = new AliveHosts(mysql);
const collector = new CollectorRunner();
collector.add(new Unifi(config.unifi));
collector.add(new Nmap(config.nmap));
let lastDeviceCount = false;
let lastMemberCount = false;
let lastMemberNames = false;
function update() {
async
.waterfall([
(callback) => collector.update(callback),
(hosts, callback) => {
if (config.ip) {
hosts = hosts.filter(host => (ip.toLong(host.ip) >= config.ip.from && ip.toLong(host.ip) <= config.ip.to));
}
if (lastDeviceCount !== hosts.length) {
mqttClient.publish(config.mqtt.topics.deviceCount, '' + hosts.length, config.mqtt.options);
}
aliveHosts.insertHosts(hosts, (error, result) => callback(error));
lastDeviceCount = hosts.length;
},
(callback) => aliveHosts.getMembersSince(config.lookbackIntervalMs, callback),
(members, callback) => {
const memberNames = members.map(member => {
if (member.privacy == 2) {
return 'anonymous';
}
return member.nickname;
}).join(', ');
if (lastMemberNames !== memberNames) {
mqttClient.publish(config.mqtt.topics.memberNames, memberNames, config.mqtt.options);
}
if (lastMemberCount !== members.length) {
mqttClient.publish(config.mqtt.topics.memberPresent, '' + members.length, config.mqtt.options);
if (members.length > 0 && lastMemberCount === 0) {
mqttClient.publish(config.mqtt.topics.spaceStatus, 'open', config.mqtt.options);
} else if (members.length === 0 && lastMemberCount > 0) {
mqttClient.publish(config.mqtt.topics.spaceStatus, 'closed', config.mqtt.options);
}
}
lastMemberCount = members.length;
lastMemberNames = memberNames;
return callback(null);
},
(callback) => {
aliveHosts.cleanup(config.cleanupIntervalMs, callback);
return callback(null);
}
], (error) => {
if (error) {
logger.error({
module: 'main',
event: 'finished',
error: error
});
} else {
logger.info({
module: 'main',
event: 'finished'
});
}
setTimeout(update, config.intervalMs);
});
}
mysql.connect(() => {
update();
});