forked from betaflight/betaflight-configurator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.js
345 lines (290 loc) · 12.4 KB
/
logging.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
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
import { millitime, bytesToSize, checkChromeRuntimeError } from '../utils/common.js';
import GUI, { TABS } from '../gui';
import { generateFilename } from '../utils/generate_filename.js';
import { i18n } from '../localization';
import { get as getConfig, set as setConfig } from '../ConfigStorage';
import FC from '../fc.js';
import MSP from '../msp.js';
import MSPCodes from '../msp/MSPCodes.js';
import CONFIGURATOR from '../data_storage.js';
import { gui_log } from '../gui_log.js';
import $ from 'jquery';
const logging = {};
logging.initialize = function (callback) {
if (GUI.active_tab != 'logging') {
GUI.active_tab = 'logging';
}
let requestedProperties = [];
let samples = 0;
let requests = 0;
let logBuffer = [];
if (CONFIGURATOR.connectionValid) {
const getMotorData = function () {
MSP.send_message(MSPCodes.MSP_MOTOR, false, false, loadHtml);
};
const loadHtml = function () {
$('#content').load("./tabs/logging.html", process_html);
};
MSP.send_message(MSPCodes.MSP_RC, false, false, getMotorData);
}
function process_html() {
// translate to user-selected language
i18n.localizePage();
// UI hooks
$('a.log_file').click(prepare_file);
$('a.logging').click(function () {
if (GUI.connected_to) {
if (fileEntry != null) {
const clicks = $(this).data('clicks');
if (!clicks) {
// reset some variables before start
samples = 0;
requests = 0;
logBuffer = [];
requestedProperties = [];
$('.properties input:checked').each(function () {
requestedProperties.push($(this).prop('name'));
});
if (requestedProperties.length) {
// print header for the csv file
print_head();
const logDataPoll = function () {
if (requests) {
// save current data (only after everything is initialized)
crunch_data();
}
// request new
for (let i = 0; i < requestedProperties.length; i++, requests++) {
MSP.send_message(MSPCodes[requestedProperties[i]]);
}
};
GUI.interval_add('log_data_poll', logDataPoll, parseInt($('select.speed').val()), true); // refresh rate goes here
GUI.interval_add('write_data', function write_data() {
if (logBuffer.length) { // only execute when there is actual data to write
if (fileWriter.readyState == 0 || fileWriter.readyState == 2) {
append_to_file(logBuffer.join('\n'));
$('.samples').text(samples += logBuffer.length);
logBuffer = [];
} else {
console.log('IO having trouble keeping up with the data flow');
}
}
}, 1000);
$('.speed').prop('disabled', true);
$(this).text(i18n.getMessage('loggingStop'));
$(this).data("clicks", clicks !== true);
} else {
gui_log(i18n.getMessage('loggingErrorOneProperty'));
}
} else {
GUI.interval_kill_all();
$('.speed').prop('disabled', false);
$(this).text(i18n.getMessage('loggingStart'));
$(this).data("clicks", !clicks);
}
} else {
gui_log(i18n.getMessage('loggingErrorLogFile'));
}
} else {
gui_log(i18n.getMessage('loggingErrorNotConnected'));
}
});
const result = getConfig('logging_file_entry');
if (result.logging_file_entry) {
chrome.fileSystem.restoreEntry(result.logging_file_entry, function (entry) {
if (checkChromeRuntimeError()) {
return;
}
fileEntry = entry;
prepare_writer(true);
});
}
GUI.content_ready(callback);
}
function print_head() {
let head = "timestamp";
for (let i = 0; i < requestedProperties.length; i++) {
switch (requestedProperties[i]) {
case 'MSP_RAW_IMU':
head += ',' + 'gyroscopeX';
head += ',' + 'gyroscopeY';
head += ',' + 'gyroscopeZ';
head += ',' + 'accelerometerX';
head += ',' + 'accelerometerY';
head += ',' + 'accelerometerZ';
head += ',' + 'magnetometerX';
head += ',' + 'magnetometerY';
head += ',' + 'magnetometerZ';
break;
case 'MSP_ATTITUDE':
head += ',' + 'kinematicsX';
head += ',' + 'kinematicsY';
head += ',' + 'kinematicsZ';
break;
case 'MSP_ALTITUDE':
head += ',' + 'altitude';
break;
case 'MSP_RAW_GPS':
head += ',' + 'gpsFix';
head += ',' + 'gpsNumSat';
head += ',' + 'gpsLat';
head += ',' + 'gpsLon';
head += ',' + 'gpsAlt';
head += ',' + 'gpsSpeed';
head += ',' + 'gpsGroundCourse';
break;
case 'MSP_ANALOG':
head += ',' + 'voltage';
head += ',' + 'amperage';
head += ',' + 'mAhdrawn';
head += ',' + 'rssi';
break;
case 'MSP_RC':
for (let chan = 0; chan < FC.RC.active_channels; chan++) {
head += `${',' + 'RC'}${chan}`;
}
break;
case 'MSP_MOTOR':
for (let motor = 0; motor < FC.MOTOR_DATA.length; motor++) {
head += `${',' + 'Motor'}${motor}`;
}
break;
case 'MSP_DEBUG':
for (let debug = 0; debug < FC.SENSOR_DATA.debug.length; debug++) {
head += `${',' + 'Debug'}${debug}`;
}
break;
}
}
append_to_file(head);
}
function crunch_data() {
let sample = millitime();
for (let i = 0; i < requestedProperties.length; i++) {
switch (requestedProperties[i]) {
case 'MSP_RAW_IMU':
sample += `,${FC.SENSOR_DATA.gyroscope}`;
sample += `,${FC.SENSOR_DATA.accelerometer}`;
sample += `,${FC.SENSOR_DATA.magnetometer}`;
break;
case 'MSP_ATTITUDE':
sample += `,${FC.SENSOR_DATA.kinematics[0]}`;
sample += `,${FC.SENSOR_DATA.kinematics[1]}`;
sample += `,${FC.SENSOR_DATA.kinematics[2]}`;
break;
case 'MSP_ALTITUDE':
sample += `,${FC.SENSOR_DATA.altitude}`;
break;
case 'MSP_RAW_GPS':
sample += `,${FC.GPS_DATA.fix}`;
sample += `,${FC.GPS_DATA.numSat}`;
sample += `,${FC.GPS_DATA.lat / 10000000}`;
sample += `,${FC.GPS_DATA.lon / 10000000}`;
sample += `,${FC.GPS_DATA.alt}`;
sample += `,${FC.GPS_DATA.speed}`;
sample += `,${FC.GPS_DATA.ground_course}`;
break;
case 'MSP_ANALOG':
sample += `,${FC.ANALOG.voltage}`;
sample += `,${FC.ANALOG.amperage}`;
sample += `,${FC.ANALOG.mAhdrawn}`;
sample += `,${FC.ANALOG.rssi}`;
break;
case 'MSP_RC':
for (let chan = 0; chan < FC.RC.active_channels; chan++) {
sample += `,${FC.RC.channels[chan]}`;
}
break;
case 'MSP_MOTOR':
sample += `,${FC.MOTOR_DATA}`;
break;
case 'MSP_DEBUG':
sample += `,${FC.SENSOR_DATA.debug}`;
break;
}
}
logBuffer.push(sample);
}
// IO related methods
let fileEntry = null;
let fileWriter = null;
function prepare_file() {
const prefix = 'log';
const suffix = 'csv';
const filename = generateFilename(prefix, suffix);
const accepts = [{
description: `${suffix.toUpperCase()} files`, extensions: [suffix],
}];
// create or load the file
chrome.fileSystem.chooseEntry({type: 'saveFile', suggestedName: filename, accepts: accepts}, function(entry) {
if (checkChromeRuntimeError()) {
return;
}
fileEntry = entry;
// echo/console log path specified
chrome.fileSystem.getDisplayPath(fileEntry, function(path) {
console.log(`Log file path: ${path}`);
});
// change file entry from read only to read/write
chrome.fileSystem.getWritableEntry(fileEntry, function(fileEntryWritable) {
// check if file is writable
chrome.fileSystem.isWritableEntry(fileEntryWritable, function(isWritable) {
if (isWritable) {
fileEntry = fileEntryWritable;
// save entry for next use
setConfig({'logging_file_entry': chrome.fileSystem.retainEntry(fileEntry)});
// reset sample counter in UI
$('.samples').text(0);
prepare_writer();
} else {
console.log('File appears to be read only, sorry.');
}
});
});
});
}
function prepare_writer(retaining) {
fileEntry.createWriter(function (writer) {
fileWriter = writer;
fileWriter.onerror = function (e) {
console.error(e);
// stop logging if the procedure was/is still running
if ($('a.logging').data('clicks')) $('a.logging').click();
};
fileWriter.onwriteend = function () {
$('.size').text(bytesToSize(fileWriter.length));
};
if (retaining) {
chrome.fileSystem.getDisplayPath(fileEntry, function (path) {
gui_log(i18n.getMessage('loggingAutomaticallyRetained', [path]));
});
}
// update log size in UI on fileWriter creation
$('.size').text(bytesToSize(fileWriter.length));
// update log name in UI
chrome.fileSystem.getDisplayPath(fileEntry, function (path) {
$('.name').text(path);
});
}, function (e) {
// File is not readable or does not exist!
console.error(e);
if (retaining) {
fileEntry = null;
}
});
}
function append_to_file(data) {
if (fileWriter.position < fileWriter.length) {
fileWriter.seek(fileWriter.length);
}
fileWriter.write(new Blob([`${data}\n`], {type: 'text/plain'}));
}
};
logging.cleanup = function (callback) {
if (callback) callback();
};
// TODO: only for transition to modules, drop this eventually
TABS.logging = logging;
export {
logging,
};