-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotion_data.js
84 lines (77 loc) · 2.46 KB
/
motion_data.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
module.exports = {
// web_socket_gps_data: function(callback) {
// var oauth = require('./oauth.js');
// var https = require('https');
// var nbi_label = "nbi";
// var nbi_host = process.env[nbi_label+"_IP_ADDRESS"];
// var nbi_port = process.env[nbi_label+"_TCP_9999_PORT"];
// var ws = new Websocket('wss://' + nbi_host + ':' + nbi_port + '/api/v1/mw/topics/gps');
// ws.onopen = function() {
// console.log('================Successfully connect WebSocket');
// }
// ws.onmessage = function(message) {
// console.log('Receive message: ' + message.data);
// try {
// callback(JSON.parse(message.data));
// } catch (e) {
// console.error(e);
// }
// }
// },
get_motion_data: function(callback) {
var oauth = require('./oauth.js');
var https = require('https');
var nbi_label = "nbi";
var nbi_host = process.env[nbi_label+"_IP_ADDRESS"];
var nbi_port = process.env[nbi_label+"_TCP_9999_PORT"];
var product_id = process.env.CAF_SYSTEM_PRODUCT_ID;
var serial_id = process.env.CAF_SYSTEM_SERIAL_ID;
var device_id = product_id+":"+serial_id;
oauth.get_oauth(function(oauth_token) {
console.log('oauth in getdata:');
console.log(oauth_token);
var header = {
"Authorization": "Bearer "+oauth_token
};
var motion_data_type = ["gyroscope", "accelerometer"];
for(var i=0; i<2; i++) {
var options = {
host: nbi_host,
port: nbi_port,
path: '/api/v1/mw/motion/' + motion_data_type[i],
method: 'GET',
rejectUnauthorized: false,
header
};
var req = https.request(options, (res) => {
console.log('Get data statusCode:', res.statusCode);
// console.log('headers:', res.headers);
res.on('data', (d) => {
// console.log('response: ');
data = JSON.parse(d);
if(data['G-X']) {
data['G-X'] = parseFloat(data['G-X']) * 8.75 / 1000.0;
data['G-Y'] = parseFloat(data['G-Y']) * 8.75 / 1000.0;
data['G-Z'] = parseFloat(data['G-Z']) * 8.75 / 1000.0;
}
else if(data['XL-X']) {
data['XL-X'] = parseFloat(data['XL-X']) / 100.0;
data['XL-Y'] = parseFloat(data['XL-Y']) / 100.0;
data['XL-Z'] = parseFloat(data['XL-Z']) / 100.0;
}
data['device_id'] = device_id;
// console.log(data);
});
res.on('end', () => {
console.log('No more data in getdata-api response.');
callback(data);
});
});
req.on('error', (e) => {
console.error(e);
});
req.end();
}
});
}
}