-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.txt
More file actions
398 lines (354 loc) · 17 KB
/
server.txt
File metadata and controls
398 lines (354 loc) · 17 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
============= Imports and Declarations ==========================================================
*/
var argv = require('minimist')(process.argv.slice(2)); //for parsing command line arguments from testAutomator.js
var mqtt = require('mqtt');
//driver for cassandraDB
const cassandra = require('cassandra-driver');
//Wifi interface on your machine
var network_driver = 'wlp2s0';
//path to tcconfig profiles for network shaping, edit the json file for each profile to add your target IP
var tcconfigprofiles = '/home/watch1/ayesh-server/Watch1CodeServer/tcconfigprofiles/';
//path to the pcaps file location with packet captures from tshark
var pcaps = '/home/watch1/ayesh-server/Watch1CodeServer/pcaps/';
var duration = argv.duration;
//Creating new Cassandra Client
const cass_client = new cassandra.Client({
contactPoints: ['localhost'],
localDataCenter: 'datacenter1'
});
//Experiment Number, which will create a table with that name
var exp_num = 32;
// ================================== END IMPORTS SECTION ===============================================================
/*
=========== Cassandra Table Creation =====================
*/
cass_client.connect(function (err) {
// console.log(err);
cass_client.execute(`CREATE KEYSPACE IF NOT EXISTS watch_analytics WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };`)
.then(() => {
console.log('Here');
// console.log('Heree');
cass_client.execute(`CREATE TABLE IF NOT EXISTS watch_analytics.experiment${exp_num}(Network_Profile text, Exp_Name text, episodeID text, timestamp bigint primary key, period int, payload int, Device_ID text, type text, sensor_data int, device_data1 int, device_data2 text, batterylevel double,cpuload double, availablememory double, totalmemory double, roundtriptime int );`,
(err, result) => {
console.log(err, result);
});
});
});
//======================== END CDB SECTION ====================================
/*
======================= Network Shaping ================================
*/
// getting target device from command line parameters sent by testAutomator
var targetDevice = null;
console.dir(argv);
try {
if (argv.targetDevice != null) {
targetDevice = argv.targetDevice;
}
}
catch (err) { }
var isMac = /^darwin/.test(process.platform);
console.log('os is mac?' + isMac);
var d = new Date();
var n = d.getTime();
//creating unique names for pcap files with episode ID and timestamp
var episode = argv.episodeId + '-' + n;
//tconfig
const { execSync, exec, spawn } = require('child_process');
// stderr is sent to stdout of parent process
// you can set options.stdio if you want it to go elsewhere
try {
var stdout = execSync(`sudo tcdel --device ${network_driver} --all`); //reset previous shaping profiles
}
catch (err) {
console.log(err.message);
}
try {
if (argv.profile != "Phy-wifi-baseline") { // mention baseline and wifi conditions eg: AC dual wifi baseline, bitrate delay on tshark
if (isMac) {
var stdout = execSync('sudo pfctl -E');
stdout = execSync('(cat /etc/pf.conf && echo "dummynet-anchor \\"mop\\"" && echo "anchor \\"mop\\"") | sudo pfctl -f -');
stdout = execSync('echo "dummynet in quick proto tcp from any to any port 3000 pipe 1" | sudo pfctl -a mop -f -');
if (argv.profile != "2G-DevelopingRural") { // dn -> dummy network traffic shaping for mac
stdout = execSync('sudo dnctl pipe 1 config bw 20Kbit/s plr 0.02 delay 650');
}
else if (argv.profile != "2G-DevelopingUrban") {
stdout = execSync('sudo dnctl pipe 1 config bw 35Kbit/s delay 650');
}
else if (argv.profile != "3G-Average") {
stdout = execSync('sudo dnctl pipe 1 config bw 780Kbit/s delay 100');
}
else if (argv.profile != "3G-Good") {
stdout = execSync('sudo dnctl pipe 1 config bw 850Kbit/s delay 90');
}
else if (argv.profile != "Edge-Average") {
stdout = execSync('sudo dnctl pipe 1 config bw 400Kbit/s delay 240');
}
else if (argv.profile != "Edge-Good") {
stdout = execSync('sudo dnctl pipe 1 config bw 250Kbit/s delay 350');
}
else if (argv.profile != "Edge-Lossy") {
stdout = execSync('sudo dnctl pipe 1 config bw 240Kbit/s plr 0.01 delay 400');
}
}
else { //network shaping for non Mac Devices
// setting profiles from the json file based on the profile from the excel file
var stdout = execSync(`sudo tcset --import-setting ${tcconfigprofiles}` + argv.profile + '.json');
//show the network configurations on the wifi driver
stdout = execSync(`sudo tcshow --device ${network_driver}`);
}
}
console.log(stdout.toString());
}
catch (err) {
console.log(err.message);
}
//tconfig
//tshark
// http://nodejs.org/api.html#_child_processes
console.log(`tshark -i any -f "tcp port 3000" -w ${pcaps}` + episode + '.pcap');
//tshark captures packets only on websocket port 3000 and default mqtt port 1883 and writes out to a pcap file
exec(`tshark -i any -f "tcp port 3000" -f "tcp port 1883" -w ${pcaps}` + episode + '.pcap', (err, stdout, stderr) => {
if (err) {
console.log(err.message);
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
//========================= END NETWORK SHAPING ================================================
/*
========================== MQTT Section ======================================================
*/
// connect to local mqtt broker
var client = mqtt.connect('mqtt://localhost:1883');
//all automation tasks will only happen on successful connection to a broker
client.on('connect', function () {
//check what the target device is and accordingly subscribe to the topics
if(targetDevice == 'Fitbit'){
client.subscribe('watch2/watchdata');
client.subscribe('watch2/finaldata');
} else if(targetDevice == 'Huawei'){
client.subscribe('watch3/watchdata');
client.subscribe('watch3/finaldata');
client.subscribe('watch3/connect');
} else if(targetDevice == 'Samsung'){
client.subscribe('watch1/watchdata');
client.subscribe('watch1/finaldata');
client.subscribe('watch1/connect');
} else if(targetDevice == 'ESP32'){
client.subscribe('ESP32/data');
client.subscribe('ESP32/finaldata');
} else if(targetDevice == 'Apple'){
client.subscribe('watch4/watchdata');
client.subscribe('watch4/finaldata');
}
//---------------------- Automation Section (Launch Apps on Different Devices) -----------------------------------
//Samsung launch app using SDB tool
if (targetDevice == 'Samsung') {
console.log('~/tizen-studio/tools/sdb connect ' + argv.samsung);
console.log('Got here');
var child = execSync('~/tizen-studio/tools/sdb connect ' + argv.samsung, (err,stdout,stderr)=>{
if(err){
exec('~/tizen-studio/tools/sdb connect ' + argv.samsung);
}
if(stdout){
console.log(stdout);
}
if(stderr){
console.log(stderr);
exec('~/tizen-studio/tools/sdb connect ' + argv.samsung);
}
});
console.log('Connected');
execSync('~/tizen-studio/tools/sdb shell launch_app PRsDVBBVB0.HeartRateMonitor', (err,stdout,stderr)=>{
if(err){
console.log('Error in launching app!');
}
});
//Send first Mqtt message to tell Samsung to start sending data
// client.publish('watch1/start', JSON.stringify({frequency:argv.frequency, duration: duration}),{qos:2});
}
//if you want to launch app on multiple Samsung watches
else if (targetDevice == 'all') {
for (var j = 122; j < 130; j++) {
console.log('~/tizen-studio/tools/sdb connect 192.168.0.' + j);
execSync('~/tizen-studio/tools/sdb connect 192.168.0.' + j);
}
for (var j = 122; j < 130; j++) {
execSync('~/tizen-studio/tools/sdb -s 192.168.0.' + j + ':26101 shell launch_app PRsDVBBVB0.HeartRateMonitor');
}
}
// if target device is Fitbit device we use the fitbit CLI tool to build and launch the app
else if (targetDevice == 'Fitbit') {
execSync('cd ../test1; npx fitbit-build; npx fitbit', { stdio: "inherit" });
//send first MQTT message to tell Fitbit to start sending data
client.publish('watch2/start',JSON.stringify({frequency:argv.frequency,duration:duration}));
}
//if target device is Android Wear (Huawei) we use Nativescript CLI tool to launch and install the app
else if (targetDevice == 'Huawei') {
// execSync(`adb connect ${adb_huawei_addr}`, (err, stdout, stderr) => {
// if (err) {
// console.log(err);
// execSync(`adb connect ${adb_huawei_addr}`);
// var child = exec('tns run android', { cwd: '../test2huawei' }, (err, stdout, stderr) => {
// if (err) {
// console.log(err);
// }
// if (stdout) { console.log(stdout); }
// if (stderr) { console.log(stderr); }
// });
// child.stdout.on('data', (data) => {
// console.log(data);
// });
// }
// if (stdout) { console.log(stdout); }
// if (stderr) { console.log(stderr); }
// });
var child = exec('tns run android', { cwd: '../test2huawei' }, (err, stdout, stderr) => {
if (err) {
console.log('Err' + err);
}
if (stdout) { console.log(stdout); }
if (stderr) { console.log('STD ERR' + stderr); }
});
child.stdout.on('data', (data) => {
console.log(data);
});
}
//If device is ESP32 just send MQTT message to tell it to start sending data
else if(targetDevice == 'ESP32'){
client.publish('ESP32/start',JSON.stringify({frequency:argv.frequency, payload: argv.payload}));
}
else if(targetDevice == 'Apple'){
client.publish('watch4/start',"start");
}
});
//------------------------------- END AUTOMATION SECTION --------------------------------------------------
// ------------------------------- MQTT Data and Save to Cassandra DB ----------------------------------------
client.on('message', function (topic, message) {
// message is Buffer
// First data sent by all devices to which the server sends an ack to calculate roundtrip time on each device
if(topic == 'watch1/connect'){
console.log('Samsung Ready Received!');
client.publish('watch1/start', "Start");
} else if(topic == 'watch3/connect'){
console.log('Huawei Ready Received!');
//Send first MQTT message to Android Wear Nativescript App to tell it start sending data
client.publish('watch3/start',JSON.stringify({frequency:argv.frequency,duration:duration}));
}
else if (topic == 'watch1/watchdata') {
console.log('Received Samsung data... Replying');
client.publish('watch1/ack', 'Received your message');
} else if (topic == 'watch2/watchdata') {
console.log('Received Fitbit data... Replying');
client.publish('watch2/ack', 'Received your message');
} else if (topic == 'watch3/watchdata') {
console.log('Received Huawei data...Replying');
client.publish('watch3/ack', 'Received your message');
} else if(topic == 'ESP32/data'){
console.log('Received ESP32 data... Replying');
client.publish('ESP32/ack','Received your message');
} else if(topic == "watch4/watchdata"){
console.log("Received Apple data... Replying");
client.publish('watch4/ack','ack');
}
// Second data sent by all devices with round trip time field (Final Data Package) which we save to Cassandra DB
else if (topic == 'watch1/finaldata') { //Samsung Final Data
var pkg = JSON.parse(message);
// if(pkg.battery){
console.log(`TimeStamp: ${pkg.time} ; Heart Rate: ${pkg.hrm.rate} ; Battery level: ${pkg.battery.level} ; CPU Load: ${pkg.cpuLoad.load} ; Available Mem: ${pkg.av_Mem} ; Total Mem: ${pkg.totalMemory}`);
const query = `INSERT INTO watch_analytics.experiment${exp_num} (Network_Profile, episodeID, Exp_Name, timestamp, Device_ID, type, sensor_data, batterylevel,cpuload,availablememory,totalmemory, roundtriptime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
const params = [argv.profile, episode, argv.name, pkg.time, 'watch1', 'Samsung gear s3', pkg.hrm.rate, pkg.battery.level, pkg.cpuLoad.load, pkg.av_Mem, pkg.totalMemory, pkg.roundtrip_time];
cass_client.execute(query, params, { prepare: true }, function (err) {
console.log(err);
//Inserted in the cluster
});
setTimeout(function () {
//Send Kill Message to Stop the App on Samsung
client.publish('watch1/kill',"kill");
//Give a delay to allow the kill message to reach
setTimeout(function(){
//Exit this server process to read next row in the excel file from testAutomator.js
process.exit();
},5000);
}, duration); //duration is how long we want the test for a device to run (this process)
} else if (topic == 'watch2/finaldata') { //FItbit final data
var pkg = JSON.parse(message);
console.log(`TimeStamp: ${pkg.timestamp} ; Heart Rate: ${pkg.heartRate} ; Battery level: ${pkg.battery} ; Available Mem: ${pkg.av_Mem} ; Total Mem: ${pkg.totalMemory}`);
const query = `INSERT INTO watch_analytics.experiment${exp_num} (Network_Profile, episodeID, Exp_Name, timestamp, Device_ID, type, sensor_data, batterylevel,availablememory,totalmemory, roundtriptime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
const params = [argv.profile, episode, argv.name, pkg.timestamp, 'watch2', 'fitbit versa', pkg.heartRate, pkg.battery, pkg.av_Memory, pkg.totalMemory, pkg.roundtrip_time];
cass_client.execute(query, params, { prepare: true }, function (err) {
console.log(err);
//Inserted in the cluster
});
setTimeout(function () {
//Send Kill Message to Stop the App on Fitbit
client.publish('watch2/kill',"kill");
//Give a delay to allow the kill message to reach
setTimeout(function(){
//Exit this server process to read next row in the excel file from testAutomator.js
process.exit();
},5000);
}, duration); //duration is how long we want the test for a device to run (this process)
} else if (topic == 'watch3/finaldata') { //If
var pkg = JSON.parse(message);
console.log(`TimeStamp: ${pkg.timestamp} ; Heart Rate: ${pkg.heartRate} ; Battery level: ${pkg.battery} ; Available Mem: ${pkg.av_Mem} ; Total Mem: ${pkg.totalMemory}`);
const query = `INSERT INTO watch_analytics.experiment${exp_num} (Network_Profile, episodeID, Exp_Name, timestamp, Device_ID, type, sensor_data, batterylevel,availablememory,totalmemory, roundtriptime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
const params = [argv.profile, episode, argv.name, pkg.timestamp, 'watch3', 'Huawei Watch 2', pkg.heartRate, pkg.battery, pkg.av_Mem, pkg.totalMemory, pkg.roundtrip_time];
cass_client.execute(query, params, { prepare: true }, function (err) {
console.log(err);
//Inserted in the cluster
});
setTimeout(function () {
client.publish("watch3/kill","kill");
setTimeout(function(){
process.exit();
},5000);
}, duration);
} else if(topic == 'ESP32/finaldata'){
console.log('Roundtrip from ESP32:'+message);
var pkg = JSON.parse(message);
console.log(`Text: ${pkg.text} ; Rountrip Time: ${pkg.roundtrip_time}`);
const query = `INSERT INTO watch_analytics.experiment${exp_num} (Network_Profile, episodeID, Exp_Name, timestamp,sensor_data, Device_ID, type, roundtriptime, period, payload) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
const params = [argv.profile, episode, argv.name, new Date().getTime(), Number(pkg.sensor_data) ,'Device4', 'ESP32',pkg.roundtrip_time, argv.frequency, argv.payload];
cass_client.execute(query, params, { prepare: true }, function (err) {
console.log(err);
//Inserted in the cluster
});
setTimeout(function () {
console.log('Ending ESP32..');
if(client.disconnected){
console.log('Disconnected');
}
client.publish('ESP32/kill','njn',{qos:1});
setTimeout(function(){
process.exit();
},5000);
}, duration);
} else if(topic == 'watch4/finaldata'){
console.log('Received data from Apple Watch...');
var pkg = JSON.parse(message);
console.log(`X:${pkg.x} ; Y:${pkg.y} ; Z:${pkg.z} ; roundtrip_time:${pkg.roundtrip_time}`);
const query = `INSERT INTO watch_analytics.experiment${exp_num} (Network_Profile, episodeID, Exp_Name, timestamp,sensor_data,device_data1, Device_ID, type, roundtriptime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`;
const params = [argv.profile, episode, argv.name, new Date().getTime(), Number(pkg.x) , Number(pkg.y) , 'Device5', 'Apple Watch', pkg.roundtrip_time];
cass_client.execute(query, params, { prepare: true }, function (err) {
console.log(err);
// Inserted in the cluster
});
setTimeout(function () {
console.log('Ending Apple..');
if(client.disconnected){
console.log('Disconnected');
}
client.publish('watch4/kill','kill',{qos:1});
setTimeout(function(){
process.exit();
},5000);
}, duration);
}
});
// ================================= END MQTT SECTION ==========================================