-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (105 loc) · 2.95 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
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
var TotalCount = require('./lib/totalCount.js')
, VendorCount = require('./lib/vendorCount.js')
, util = require('util')
, stream = require('stream')
, fs = require('fs')
, exec = require('child_process').exec;
// Give our module a stream interface
util.inherits(scanner,stream);
var DEBUG = false;
function scanner(opts,app) {
this.app = app;
app.on('client::up',function() {
this.setupVendorDevices.call(this);
this.startScanner.call(this)
}.bind(this));
};
scanner.prototype.startScanner = function(cb) {
var MINUTES = 5;
var command = 'cat '+__dirname+'/launch.json';
var self = this;
this.app.log.info('Beginning Wifi Scan');
var spawnScanner = function(err,json) {
if (err) {
return;
}
this.app.log.info('Finished Wifi Scan');
this.parseJSON(json);
setTimeout(function() {
child = exec(command,spawnScanner);
},MINUTES*60*1000);
}.bind(this);
var child = exec(command,spawnScanner);
};
var totalDevice;
scanner.prototype.parseJSON = function(json) {
var self = this;
try {
var dataObj = JSON.parse(json);
} catch (err) {
this.app.log.error(err);
return;
}
// Archive it
var d = new Date();
fs.writeFile(__dirname+'/results/result-'+d.toString(),json);
if (!DEBUG && !totalDevice) {
totalDevice = new TotalCount();
this.emit('register', totalDevice);
}
totalDevice.emit('data',dataObj.length);
// We send in sliced arrays because that will clone them
// and we can then modify them without worrying about anybody
// else having ill effects
this.mapVendors(dataObj.slice(0));
this.mapAssociated(dataObj.slice(0));
};
var vendors = [
'apple'
, 'intel'
, 'htc'
, 'samsung'
, 'motorola'
, 'lg'
, 'asustek'
];
var vendorDevices = {};
scanner.prototype.setupVendorDevices = function() {
for (var i=0;i<vendors.length;i++) {
vendorDevices[vendors[i]] = new VendorCount(vendors[i]);
this.emit('register',vendorDevices[vendors[i]]);
}
vendorDevices['other'] = new VendorCount('other');
this.emit('register',vendorDevices['other']);
};
scanner.prototype.mapVendors = function(obj) {
var out = {};
// Iterate over the big array
for (var i=0; i<obj.length; i++) {
if (!obj[i].vendor) continue;
var radioVendor = obj[i].vendor;
var matched = false;
for (var j=0;j<vendors.length;j++) {
var normalisedVendor = vendors[j];
var regex = new RegExp(normalisedVendor,'i');
if (regex.test(radioVendor)) {
out[normalisedVendor] = out[normalisedVendor] || 0;
out[normalisedVendor]++;
matched = true;
}
}
// We couldn't find this vendor, so we put it as other
if (!matched) {
out['other'] = out['other'] || 0;
out['other']++;
}
}
// Emit the counts that we calculated previously
for (var v in out) {
vendorDevices[v].emit('data',out[v]);
}
};
scanner.prototype.mapAssociated = function(obj) {
}
// Export it
module.exports = scanner;