betaflight-configurator/tabs/logging.js

313 lines
11 KiB
JavaScript
Raw Normal View History

2014-08-09 18:51:06 +00:00
'use strict';
TABS.logging = {};
2014-08-12 14:20:26 +00:00
TABS.logging.initialize = function (callback) {
var self = this;
2014-05-22 10:13:16 +00:00
if (GUI.active_tab != 'logging') {
GUI.active_tab = 'logging';
}
2014-05-22 10:13:16 +00:00
2014-09-08 15:52:07 +00:00
var requested_properties = [],
samples = 0,
requests = 0,
log_buffer = [];
if (CONFIGURATOR.connectionValid) {
2014-08-09 18:51:06 +00:00
var get_motor_data = function () {
MSP.send_message(MSPCodes.MSP_MOTOR, false, false, load_html);
}
2014-08-09 18:51:06 +00:00
var load_html = function () {
$('#content').load("./tabs/logging.html", process_html);
}
MSP.send_message(MSPCodes.MSP_RC, false, false, get_motor_data);
}
2014-05-22 10:13:16 +00:00
function process_html() {
// translate to user-selected language
localize();
// UI hooks
$('a.log_file').click(prepare_file);
2014-08-12 14:20:26 +00:00
$('a.logging').click(function () {
if (GUI.connected_to) {
if (fileEntry != null) {
var clicks = $(this).data('clicks');
if (!clicks) {
// reset some variables before start
samples = 0;
2014-06-25 07:28:44 +00:00
requests = 0;
log_buffer = [];
requested_properties = [];
2014-08-12 14:20:26 +00:00
$('.properties input:checked').each(function () {
requested_properties.push($(this).prop('name'));
});
if (requested_properties.length) {
// print header for the csv file
print_head();
2014-08-09 18:51:06 +00:00
var log_data_poll = function () {
2014-06-25 07:28:44 +00:00
if (requests) {
// save current data (only after everything is initialized)
crunch_data();
}
// request new
2014-09-08 15:52:07 +00:00
for (var i = 0; i < requested_properties.length; i++, requests++) {
MSP.send_message(MSPCodes[requested_properties[i]]);
}
}
2014-07-10 12:48:33 +00:00
GUI.interval_add('log_data_poll', log_data_poll, parseInt($('select.speed').val()), true); // refresh rate goes here
GUI.interval_add('write_data', function write_data() {
if (log_buffer.length) { // only execute when there is actual data to write
if (fileWriter.readyState == 0 || fileWriter.readyState == 2) {
append_to_file(log_buffer.join('\n'));
$('.samples').text(samples += log_buffer.length);
log_buffer = [];
} else {
console.log('IO having trouble keeping up with the data flow');
}
}
}, 1000);
$('.speed').prop('disabled', true);
$(this).text(chrome.i18n.getMessage('loggingStop'));
$(this).data("clicks", !clicks);
} else {
GUI.log(chrome.i18n.getMessage('loggingErrorOneProperty'));
}
} else {
2014-07-10 12:48:33 +00:00
GUI.interval_kill_all();
$('.speed').prop('disabled', false);
$(this).text(chrome.i18n.getMessage('loggingStart'));
$(this).data("clicks", !clicks);
}
} else {
GUI.log(chrome.i18n.getMessage('loggingErrorLogFile'));
}
} else {
GUI.log(chrome.i18n.getMessage('loggingErrorNotConnected'));
2014-05-22 10:13:16 +00:00
}
});
2014-08-12 14:20:26 +00:00
chrome.storage.local.get('logging_file_entry', function (result) {
if (result.logging_file_entry) {
2014-08-12 14:20:26 +00:00
chrome.fileSystem.restoreEntry(result.logging_file_entry, function (entry) {
fileEntry = entry;
prepare_writer(true);
});
}
});
2014-07-10 16:36:10 +00:00
GUI.content_ready(callback);
2014-05-22 10:13:16 +00:00
}
function print_head() {
var head = "timestamp";
for (var i = 0; i < requested_properties.length; i++) {
switch (requested_properties[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;
2014-05-23 15:37:57 +00:00
case 'MSP_RAW_GPS':
head += ',' + 'gpsFix';
head += ',' + 'gpsNumSat';
head += ',' + 'gpsLat';
head += ',' + 'gpsLon';
head += ',' + 'gpsAlt';
head += ',' + 'gpsSpeed';
head += ',' + 'gpsGroundCourse';
break;
2014-06-20 09:55:14 +00:00
case 'MSP_ANALOG':
head += ',' + 'voltage';
head += ',' + 'amperage';
head += ',' + 'mAhdrawn';
head += ',' + 'rssi';
break;
case 'MSP_RC':
for (var chan = 0; chan < RC.active_channels; chan++) {
head += ',' + 'RC' + chan;
}
break;
case 'MSP_MOTOR':
for (var motor = 0; motor < MOTOR_DATA.length; motor++) {
head += ',' + 'Motor' + motor;
}
break;
case 'MSP_DEBUG':
for (var debug = 0; debug < SENSOR_DATA.debug.length; debug++) {
head += ',' + 'Debug' + debug;
}
break;
}
}
2014-06-25 07:18:01 +00:00
append_to_file(head);
}
function crunch_data() {
var sample = millitime();
for (var i = 0; i < requested_properties.length; i++) {
switch (requested_properties[i]) {
case 'MSP_RAW_IMU':
sample += ',' + SENSOR_DATA.gyroscope;
sample += ',' + SENSOR_DATA.accelerometer;
sample += ',' + SENSOR_DATA.magnetometer;
break;
case 'MSP_ATTITUDE':
sample += ',' + SENSOR_DATA.kinematics[0];
sample += ',' + SENSOR_DATA.kinematics[1];
sample += ',' + SENSOR_DATA.kinematics[2];
break;
case 'MSP_ALTITUDE':
sample += ',' + SENSOR_DATA.altitude;
break;
case 'MSP_RAW_GPS':
sample += ',' + GPS_DATA.fix;
sample += ',' + GPS_DATA.numSat;
sample += ',' + (GPS_DATA.lat / 10000000);
sample += ',' + (GPS_DATA.lon / 10000000);
sample += ',' + GPS_DATA.alt;
sample += ',' + GPS_DATA.speed;
sample += ',' + GPS_DATA.ground_course;
break;
case 'MSP_ANALOG':
sample += ',' + ANALOG.voltage;
sample += ',' + ANALOG.amperage;
sample += ',' + ANALOG.mAhdrawn;
sample += ',' + ANALOG.rssi;
break;
case 'MSP_RC':
for (var chan = 0; chan < RC.active_channels; chan++) {
sample += ',' + RC.channels[chan];
}
break;
case 'MSP_MOTOR':
sample += ',' + MOTOR_DATA;
break;
case 'MSP_DEBUG':
sample += ',' + SENSOR_DATA.debug;
break;
}
}
log_buffer.push(sample);
}
2014-05-22 10:13:16 +00:00
// IO related methods
2014-08-12 14:20:26 +00:00
var fileEntry = null,
fileWriter = null;
2014-05-22 10:13:16 +00:00
function prepare_file() {
// create or load the file
chrome.fileSystem.chooseEntry({type: 'saveFile', suggestedName: 'betaflight_data_log', accepts: [{extensions: ['csv']}]}, function(entry) {
2014-05-22 10:13:16 +00:00
if (!entry) {
console.log('No file selected');
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
chrome.storage.local.set({'logging_file_entry': chrome.fileSystem.retainEntry(fileEntry)});
2014-06-25 07:18:01 +00:00
// reset sample counter in UI
$('.samples').text(0);
2014-05-22 10:13:16 +00:00
prepare_writer();
} else {
console.log('File appears to be read only, sorry.');
}
});
});
});
}
function prepare_writer(retaining) {
2014-08-12 14:20:26 +00:00
fileEntry.createWriter(function (writer) {
2014-05-22 10:13:16 +00:00
fileWriter = writer;
2014-08-12 14:20:26 +00:00
fileWriter.onerror = function (e) {
2014-05-22 10:13:16 +00:00
console.error(e);
2014-05-22 15:00:35 +00:00
// stop logging if the procedure was/is still running
if ($('a.logging').data('clicks')) $('a.logging').click();
2014-05-22 10:13:16 +00:00
};
2014-08-12 14:20:26 +00:00
fileWriter.onwriteend = function () {
2014-06-25 07:18:01 +00:00
$('.size').text(bytesToSize(fileWriter.length));
2014-05-22 10:13:16 +00:00
};
if (retaining) {
2014-08-12 14:20:26 +00:00
chrome.fileSystem.getDisplayPath(fileEntry, function (path) {
GUI.log(chrome.i18n.getMessage('loggingAutomaticallyRetained', [path]));
});
}
2014-06-25 07:18:01 +00:00
// update log size in UI on fileWriter creation
$('.size').text(bytesToSize(fileWriter.length));
2014-08-12 14:20:26 +00:00
}, function (e) {
// File is not readable or does not exist!
2014-05-22 10:13:16 +00:00
console.error(e);
if (retaining) {
fileEntry = null;
}
2014-05-22 10:13:16 +00:00
});
}
function append_to_file(data) {
if (fileWriter.position < fileWriter.length) {
fileWriter.seek(fileWriter.length);
}
fileWriter.write(new Blob([data + '\n'], {type: 'text/plain'}));
}
2014-07-10 16:36:10 +00:00
};
2014-08-12 14:20:26 +00:00
TABS.logging.cleanup = function (callback) {
2014-07-10 16:36:10 +00:00
if (callback) callback();
2016-06-16 05:25:11 +00:00
};