-
Notifications
You must be signed in to change notification settings - Fork 146
/
scan.js
141 lines (113 loc) · 3.98 KB
/
scan.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
require('env2')('config.env');
var wsclient = require('./lib/ws-client');
var fs = require('fs');
var utils = require('./lib/utils')
var events = require('events')
var util = require('util');
var async = require('async');
var fs = require('fs');
var debug = require('debug')('explore');
var getopt = require('node-getopt');
var options = new getopt([
['a' , 'no-advertisements' , 'do not save advertisements (default: save)'],
['r' , 'no-read' , 'do not read characteristics values (default: read), helpful when device requires auth and service exploring stalls'],
['o' , 'overwrite' , 'overwrite peripheral services file'],
['f' , 'funmode' , 'have fun!'],
['' , 'jk' , 'see http://xkcd.com/1692'],
['h' , 'help' , 'display this help'],
]);
options.setHelp('Usage: node scan [ -a ] [ -r ] [ peripheral ]\n' +
'peripheral - optional peripheral device do explore services (MAC address e.g. ec:fe:7e:12:34:56 or id e.g. ecfe7e123456). If not provided, broadcast advertisement scan.' +
'Command-line options:\n[[OPTIONS]]' )
opt=options.parseSystem();
if (opt.options.help) {
options.showHelp()
process.exit(0)
}
if (opt.options.funmode) {
console.log('>>>>>>>>>>>>>>>>> MAY THE FUN BE WITH YOU! <<<<<<<<<<<<<<<<<<'.rainbow.inverse)
}
if (opt.argv.length > 0) {
var specifiedPeripheral = opt.argv[0].replace(/:/g,'').toLowerCase();
}
var devicesPath=process.env.DEVICES_PATH;
var scanOrExplore = '';
var peripherals=[];
var saveAdvertisements=true;
var readValues=true;
var overWriteServices=false;
if (opt.options["no-advertisements"]) {
console.log('Not saving advertisement discovery.');
saveAdvertisements=false;
}
if (opt.options["no-read"]) {
console.log('Not reading characteristic values.')
readValues=false;
}
if (opt.options.overwrite) {
console.log('Overwrite services file if exists.');
overWriteServices=true;
}
function exploreSpecified(peripheralId) {
checkFile(peripheralId, function(exists){
if (!exists) {
console.log('Start to explore ' + peripheralId)
wsclient.explore(peripheralId, readValues);
} else {
console.log('Services file for ' + peripheralId + ' already saved, skipping. Use -o option to overwrite.');
}
})
}
//check if the services file exists
function checkFile(peripheralId, callback) {
if (overWriteServices) {
callback(false);
} else {
fs.stat(devicesPath + '/' + peripheralId + '.srv.json', function(err, stat) {
if(err == null) {
callback(true);
// console.log('File exists');
} else {
callback(false)
}
});
}
}
wsclient.on('stateChange', function(state) {
if (state === 'poweredOn') {
if (specifiedPeripheral) {
console.log('Start exploring ' + specifiedPeripheral);
exploreSpecified(specifiedPeripheral)
} else {
console.log('Start scanning.');
wsclient.startScanning();
}
} else if (state === 'unknown') {
console.log('state unknown - waiting...')
} else {
wsclient.stopScanning();
}
});
wsclient.on('discover', function(peripheralId, address, addressType, connectable, advertisement, rssi) {
if (saveAdvertisements) {
utils.saveAdvertisement(peripheralId, address, addressType, connectable, advertisement, rssi);
}
if (!peripherals[peripheralId]) {
peripherals[peripheralId] = { explored: false, triedToExplore: false};
}
})
wsclient.on('explore', function(peripheralId, state, servicesJson){
console.log('explore state: ' + peripheralId + ' : ' + state);
if(state === 'finished') {
var filename = devicesPath+'/'+peripheralId+'.srv.json';
fs.writeFile(filename, JSON.stringify(servicesJson, null, 4), function(err) {
if(err) {
return console.log(err);
}
console.log("Services file "+ filename +" saved!");
if (peripheralId === specifiedPeripheral) {
process.exit(0);
}
});
}
})