-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi.js
43 lines (36 loc) · 1.17 KB
/
wifi.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
const spawn = require('child_process').spawn;
const split = require('split');
const airport = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport';
const EventEmitter = require('events').EventEmitter;
const events = new EventEmitter();
const poll = () => {
let data = {};
spawn(airport, ['-I'])
.stdout
.pipe(split())
.on('data', function (line) {
const info = line.trim().split(': ');
if (info.length === 2) {
data[info[0]] = info[1]
}
})
.on('end', function () {
//console.log(`data: ${JSON.stringify(data)}`);
if (data.AirPort === 'Off') {
events.emit('off');
return
}
if (!data.SSID) {
events.emit('not-connected');
return
}
const rssi = Number(data.agrCtlRSSI);
const noise = Number(data.agrCtlNoise);
const channel = data.channel;
events.emit('data', {rssi, noise, channel, ssid: data.SSID})
})
};
setInterval(() => {
poll();
}, 20);
module.exports = events;