-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
151 lines (132 loc) · 4.77 KB
/
node_helper.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var NodeHelper = require("node_helper");
var child = require('child_process');
const { exit } = require("process");
//init global state
global.state = {};
module.exports = NodeHelper.create({
start: function() {
},
socketNotificationReceived: function (notification, payload, user) {
console.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
if (notification === 'FETCH_PEOPLE') {
this.getPeople(payload);
}
},
getPeople: function(config) {
const self = this;
// run ansyc mapping of mac addresses
console.log("Mapping mac addresses...");
var macs = '';
for (var key in config.TRACK) {
macs += config.TRACK[key].mac + ' ';
}
const mapsoutput = child.exec('modules/MMM-whoshome/mapmacs.sh ' + macs, { encoding: 'utf-8', timeout: 300000 });
console.log("Mapping output:");
console.log(mapsoutput);
var mac = '';
var stateArray = new Array();
var counter = 0;
for (var key in config.TRACK) {
mac = config.TRACK[key].mac;
var state = 0;
const output = child.execSync('modules/MMM-whoshome/macping.sh ' + mac, { encoding: 'utf-8', timeout: 30000 });
var lastseen = '';
if (output.trim() == 1) {
state = 1;
} else {
state = 0;
if (output.trim() != 0) {
lastseen = output.trim();
}
}
// check if global.state.key is set and if it is different from current state
if (global.state[key] != null) {
if (global.state[key] == state) {
console.log("State not changed and is equal to global state " + key + " " + global.state[key] + " " + state);
var changed = 0;
} else {
console.log("State changed and is different from global state " + key + " " + global.state[key] + " " + state);
var changed = 1;
global.state[key] = state;
}
} else {
console.log("Global state not set, setting to current state " + key + " " + state);
global.state[key] = state;
var changed = 1;
}
if (config.push != null && changed == 1) {
if (lastseen == '') {
// use current time in mysql date time format
var currentdate = new Date();
lasts = currentdate.getFullYear() + '-' + (currentdate.getMonth() + 1) + '-' + currentdate.getDate() + '-'
+ currentdate.getHours() + ':' + currentdate.getMinutes();
} else {
// convert lastseen to mysql date time format
// if lastseen is only HH:MM, add current date
if (lastseen.indexOf('.') == -1) {
lasts = new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate() + '-' + lastseen;
}
// if lastseen is DD.MM HH:MM, add current year
if (lastseen.indexOf('.') != -1 && lastseen.indexOf(':') != -1) {
var lastseenparts = lastseen.split(' ');
var dateparts = lastseenparts[0].split('.');
var timeparts = lastseenparts[1].split(':');
lasts = new Date().getFullYear() + '-' + dateparts[1] + '-' + dateparts[0] + '-' + timeparts[0] + ':' + timeparts[1];
}
}
console.log("Pushing to server: " + key + ' ' + lastseen);
pushToServer(key, lasts, state, config);
}
console.log(key + ' ' + mac + ' ' + state + ' ' + lastseen);
stateArray[counter] = [key, state, lastseen];
counter += 1;
}
console.log("Final state:");
console.log(stateArray);
self.sendSocketNotification(
'FETCHED_PEOPLE', {
'people': stateArray,
'id': config.id,
}
);
}
});
function pushToServer(user, lastseen, state, config) {
const https = require('https');
var ret = 0;
const options = {
hostname: config.push.hostname,
path: config.push.path
+ '?username=' + encodeURIComponent(user)
+ '&lastseen=' + encodeURIComponent(lastseen)
+ '&ssid=' + encodeURIComponent(config.push.ssid)
+ '&clientname=' + encodeURIComponent(config.push.clientname)
+ '&token=' + encodeURIComponent(config.push.token)
+ '&state=' + state,
method: 'GET',
headers: {
'User-Agent': 'Node.js MagicMirror HTTPS Client'
}
};
const req = https.request(options, (res) => {
let data = '';
console.log(`Status Code: ${res.statusCode}`);
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const jsonData = JSON.parse(data);
//console.log('Response body:', jsonData);
ret = 1;
} catch (error) {
console.error('Error parsing JSON:', error.message);
}
});
});
req.on('error', (error) => {
console.error('Request error:', error.message);
});
req.end();
return ret;
}