-
Notifications
You must be signed in to change notification settings - Fork 146
/
replay.js
119 lines (99 loc) · 3.39 KB
/
replay.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
require('env2')('config.env');
var debug = require('debug')('replay');
var fs = require('fs');
var util = require('util');
var utils = require('./lib/utils')
var path=require('path');
var events = require('events');
var getopt = require('node-getopt');
var async = require('async');
var options = new getopt([
['i' , 'input=FILE' , 'input file'],
['s' , 'services=FILE' , 'services json input file'],
['p' , 'peripheral=MAC' , 'target peripheral MAC'],
['h' , 'help' , 'display this help'],
]);
options.setHelp("Usage: node replay -i <FILE> -p <MAC> [ -s <FILE> ]\n[[OPTIONS]]" )
opt=options.parseSystem();
if ( !opt.options.input || !opt.options.peripheral) {
console.info(options.getHelp());
process.exit(0);
}
var peripheralId = opt.options.peripheral;
if (opt.options.services) {
if (opt.options.services.indexOf('.srv.json') > -1 ) {
servicesFile=opt.options.services;
} else {
servicesFile=opt.options.services + '.srv.json';
}
} else {
var devicesPath=process.env.DEVICES_PATH;
servicesFile = devicesPath+ '/'+peripheralId+'.srv.json';
}
var services = JSON.parse(fs.readFileSync(servicesFile, 'utf8'));
var waiting = false;
var wsclient = require('./lib/ws-client');
var inputData = fs.createReadStream(opt.options.input,'utf8');
function readLines(input, func) {
var remaining = '';
input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
var last = 0;
while (index > -1) {
var line = remaining.substring(last, index);
last = index + 1;
func(line);
index = remaining.indexOf('\n', last);
}
remaining = remaining.substring(last);
});
input.on('end', function() {
if (remaining.length > 0) {
func(remaining);
}
});
}
function parse(line) {
if(waiting) {//we want it to match
console.log('LINE: ' + line);
setTimeout(parse(line), 500);//wait 50 millisecnds then recheck
return;
}
var arr=line.split('|');
var operator = arr[1].trim();
var serviceUuid = arr[2].trim().split(' ')[0]; //split(' ') to remove optional description
var uuid = arr[3].trim().split(' ')[0];
var data = arr[4].trim().split(' ')[0];
switch(operator) {
case '< W' : console.log('WRITE REQ: '.blue + data );
wsclient.write(peripheralId, serviceUuid, uuid, new Buffer(data,'hex'), true , function(error) {
if (error){
console.log('------ Write error: '.red);
throw(error);
}
}); break;
case '< C' : console.log('WRITE CMD: '.blue + data );
wsclient.write(peripheralId, serviceUuid, uuid, new Buffer(data,'hex'), false , function(error) {
if (error){
console.log('------ Write error: '.red);
throw(error);
}
}); break;
case '> R' : console.log('READ: '.grey + data + ' --- skip');
break
case '> N' : console.log('NOTIFICATION: '.grey + data + ' --- skip');
break
}
}
//wait for the ws connection
wsclient.on('ws_open', function(){
wsclient.getMac(function(address){
myAddress = address;
console.log('Noble MAC address : ' + myAddress);
})
wsclient.initialize(peripheralId, services, true, function(){
console.log('initialized !');
readLines(inputData,parse);
})
});