2014-08-09 17:38:46 +00:00
|
|
|
'use strict';
|
|
|
|
|
2014-10-06 14:07:47 +00:00
|
|
|
function configuration_backup(callback) {
|
2013-12-16 10:01:00 +00:00
|
|
|
// request configuration data (one by one)
|
2014-03-20 21:19:27 +00:00
|
|
|
function get_ident_data() {
|
2014-06-01 12:10:18 +00:00
|
|
|
MSP.send_message(MSP_codes.MSP_IDENT, false, false, get_status_data);
|
2014-03-20 21:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_status_data() {
|
2014-06-01 12:10:18 +00:00
|
|
|
MSP.send_message(MSP_codes.MSP_STATUS, false, false, get_pid_data);
|
2014-03-20 21:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_pid_data() {
|
2014-06-01 12:10:18 +00:00
|
|
|
MSP.send_message(MSP_codes.MSP_PID, false, false, get_rc_tuning_data);
|
2014-03-20 21:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_rc_tuning_data() {
|
2014-06-01 12:10:18 +00:00
|
|
|
MSP.send_message(MSP_codes.MSP_RC_TUNING, false, false, get_box_names_data);
|
2014-03-20 21:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_box_names_data() {
|
2014-06-01 12:10:18 +00:00
|
|
|
MSP.send_message(MSP_codes.MSP_BOXNAMES, false, false, get_box_data);
|
2014-03-20 21:19:27 +00:00
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-03-20 21:19:27 +00:00
|
|
|
function get_box_data() {
|
2014-06-01 12:10:18 +00:00
|
|
|
MSP.send_message(MSP_codes.MSP_BOX, false, false, get_acc_trim_data);
|
2014-03-20 21:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_acc_trim_data() {
|
2014-06-01 12:10:18 +00:00
|
|
|
MSP.send_message(MSP_codes.MSP_ACC_TRIM, false, false, get_misc_data);
|
2014-03-20 21:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_misc_data() {
|
2014-10-06 13:24:34 +00:00
|
|
|
MSP.send_message(MSP_codes.MSP_MISC, false, false, get_servo_config_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_servo_config_data() {
|
|
|
|
MSP.send_message(MSP_codes.MSP_SERVO_CONF, false, false, backup);
|
2014-03-20 21:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function backup() {
|
2013-06-17 21:36:19 +00:00
|
|
|
var chosenFileEntry = null;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-06-17 21:36:19 +00:00
|
|
|
var accepts = [{
|
|
|
|
extensions: ['txt']
|
|
|
|
}];
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-06-17 21:36:19 +00:00
|
|
|
// generate timestamp for the backup file
|
2014-09-17 07:27:54 +00:00
|
|
|
var d = new Date(),
|
2014-10-12 12:50:55 +00:00
|
|
|
now = (d.getMonth() + 1) + '.' + d.getDate() + '.' + d.getFullYear() + '.' + d.getHours() + '.' + d.getMinutes();
|
2013-06-17 21:36:19 +00:00
|
|
|
|
|
|
|
// create or load the file
|
2014-09-17 07:27:54 +00:00
|
|
|
chrome.fileSystem.chooseEntry({type: 'saveFile', suggestedName: 'baseflight_backup_' + now, accepts: accepts}, function (fileEntry) {
|
2014-09-22 19:16:52 +00:00
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
console.error(chrome.runtime.lastError.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-17 21:36:19 +00:00
|
|
|
if (!fileEntry) {
|
2013-06-17 22:14:49 +00:00
|
|
|
console.log('No file selected, backup aborted.');
|
2013-06-17 21:36:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
|
|
|
chosenFileEntry = fileEntry;
|
2013-06-17 21:36:19 +00:00
|
|
|
|
|
|
|
// echo/console log path specified
|
2014-08-10 04:01:44 +00:00
|
|
|
chrome.fileSystem.getDisplayPath(chosenFileEntry, function (path) {
|
2013-06-17 21:36:19 +00:00
|
|
|
console.log('Backup file path: ' + path);
|
|
|
|
});
|
|
|
|
|
|
|
|
// change file entry from read only to read/write
|
2014-08-10 04:01:44 +00:00
|
|
|
chrome.fileSystem.getWritableEntry(chosenFileEntry, function (fileEntryWritable) {
|
2013-06-17 21:36:19 +00:00
|
|
|
// check if file is writable
|
2014-08-10 04:01:44 +00:00
|
|
|
chrome.fileSystem.isWritableEntry(fileEntryWritable, function (isWritable) {
|
2013-06-17 21:36:19 +00:00
|
|
|
if (isWritable) {
|
|
|
|
chosenFileEntry = fileEntryWritable;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-06-17 21:36:19 +00:00
|
|
|
// create config object that will be used to store all downloaded data
|
|
|
|
var configuration = {
|
2014-03-07 16:43:53 +00:00
|
|
|
'firmware_version': CONFIG.version,
|
|
|
|
'configurator_version': chrome.runtime.getManifest().version,
|
|
|
|
'PID': PIDs,
|
|
|
|
'AUX_val': AUX_CONFIG_values,
|
|
|
|
'RC': RC_tuning,
|
|
|
|
'AccelTrim': CONFIG.accelerometerTrims,
|
2014-10-06 13:24:34 +00:00
|
|
|
'MISC': MISC,
|
|
|
|
'SERVO_CONFIG': SERVO_CONFIG
|
2013-12-12 16:10:10 +00:00
|
|
|
};
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-06-17 21:36:19 +00:00
|
|
|
// crunch the config object
|
|
|
|
var serialized_config_object = JSON.stringify(configuration);
|
|
|
|
var blob = new Blob([serialized_config_object], {type: 'text/plain'}); // first parameter for Blob needs to be an array
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-08-10 04:01:44 +00:00
|
|
|
chosenFileEntry.createWriter(function (writer) {
|
2013-06-17 21:36:19 +00:00
|
|
|
writer.onerror = function (e) {
|
|
|
|
console.error(e);
|
|
|
|
};
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-22 20:23:06 +00:00
|
|
|
var truncated = false;
|
2014-08-10 04:01:44 +00:00
|
|
|
writer.onwriteend = function () {
|
2014-02-22 20:23:06 +00:00
|
|
|
if (!truncated) {
|
|
|
|
// onwriteend will be fired again when truncation is finished
|
|
|
|
truncated = true;
|
2014-02-22 20:43:17 +00:00
|
|
|
writer.truncate(blob.size);
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-02-22 20:23:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-06-17 21:36:19 +00:00
|
|
|
console.log('Write SUCCESSFUL');
|
2014-10-06 14:07:47 +00:00
|
|
|
if (callback) callback();
|
2013-06-17 21:36:19 +00:00
|
|
|
};
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-06-17 21:36:19 +00:00
|
|
|
writer.write(blob);
|
|
|
|
}, function (e) {
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Something went wrong or file is set to read only and cannot be changed
|
|
|
|
console.log('File appears to be read only, sorry.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-03-20 21:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// begin fetching latest data
|
|
|
|
get_ident_data();
|
2013-06-17 21:36:19 +00:00
|
|
|
}
|
|
|
|
|
2014-10-06 14:07:47 +00:00
|
|
|
function configuration_restore(callback) {
|
2013-06-17 21:36:19 +00:00
|
|
|
var chosenFileEntry = null;
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-06-17 21:36:19 +00:00
|
|
|
var accepts = [{
|
|
|
|
extensions: ['txt']
|
|
|
|
}];
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2013-06-17 21:36:19 +00:00
|
|
|
// load up the file
|
2014-08-10 04:01:44 +00:00
|
|
|
chrome.fileSystem.chooseEntry({type: 'openFile', accepts: accepts}, function (fileEntry) {
|
2014-09-22 19:16:52 +00:00
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
console.error(chrome.runtime.lastError.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-17 21:36:19 +00:00
|
|
|
if (!fileEntry) {
|
2013-06-17 22:14:49 +00:00
|
|
|
console.log('No file selected, restore aborted.');
|
2013-06-17 21:36:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
|
|
|
chosenFileEntry = fileEntry;
|
|
|
|
|
2013-06-17 21:36:19 +00:00
|
|
|
// echo/console log path specified
|
2014-08-10 04:01:44 +00:00
|
|
|
chrome.fileSystem.getDisplayPath(chosenFileEntry, function (path) {
|
2013-06-17 21:36:19 +00:00
|
|
|
console.log('Restore file path: ' + path);
|
2014-03-08 05:25:15 +00:00
|
|
|
});
|
2013-06-17 21:36:19 +00:00
|
|
|
|
|
|
|
// read contents into variable
|
2014-08-10 04:01:44 +00:00
|
|
|
chosenFileEntry.file(function (file) {
|
2013-06-17 21:36:19 +00:00
|
|
|
var reader = new FileReader();
|
|
|
|
|
2014-08-10 04:01:44 +00:00
|
|
|
reader.onprogress = function (e) {
|
2014-01-15 12:07:51 +00:00
|
|
|
if (e.total > 1048576) { // 1 MB
|
|
|
|
// dont allow reading files bigger then 1 MB
|
|
|
|
console.log('File limit (1 MB) exceeded, aborting');
|
|
|
|
reader.abort();
|
|
|
|
}
|
2013-06-17 21:36:19 +00:00
|
|
|
};
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-08-10 04:01:44 +00:00
|
|
|
reader.onloadend = function (e) {
|
2014-01-15 12:07:51 +00:00
|
|
|
if (e.total != 0 && e.total == e.loaded) {
|
|
|
|
console.log('Read SUCCESSFUL');
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-15 12:07:51 +00:00
|
|
|
try { // check if string provided is a valid JSON
|
|
|
|
var deserialized_configuration_object = JSON.parse(e.target.result);
|
|
|
|
} catch (e) {
|
|
|
|
// data provided != valid json object
|
|
|
|
console.log('Data provided != valid JSON string, restore aborted.');
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-01-15 12:07:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-03-08 05:25:15 +00:00
|
|
|
|
2014-10-06 14:07:47 +00:00
|
|
|
configuration_upload(deserialized_configuration_object, callback);
|
2013-06-17 21:36:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
reader.readAsText(file);
|
|
|
|
});
|
|
|
|
});
|
2013-06-17 22:14:49 +00:00
|
|
|
}
|
|
|
|
|
2014-10-06 14:07:47 +00:00
|
|
|
function configuration_upload(configuration, callback) {
|
2014-10-06 13:24:34 +00:00
|
|
|
// check if all attributes that we will be saving exist inside the configuration object
|
|
|
|
var validate = [
|
|
|
|
'PID',
|
|
|
|
'AUX_val',
|
|
|
|
'RC',
|
|
|
|
'AccelTrim',
|
|
|
|
'MISC',
|
|
|
|
'SERVO_CONFIG'
|
|
|
|
];
|
|
|
|
|
|
|
|
for (var i = 0; i < validate.length; i++) {
|
2014-10-06 14:07:47 +00:00
|
|
|
if (typeof (configuration[validate[i]]) === 'undefined') {
|
2014-10-06 13:24:34 +00:00
|
|
|
GUI.log(chrome.i18n.getMessage('backupFileIncompatible'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace data
|
|
|
|
PIDs = configuration.PID;
|
|
|
|
AUX_CONFIG_values = configuration.AUX_val;
|
|
|
|
RC_tuning = configuration.RC;
|
|
|
|
CONFIG.accelerometerTrims = configuration.AccelTrim;
|
|
|
|
MISC = configuration.MISC;
|
|
|
|
SERVO_CONFIG = configuration.SERVO_CONFIG;
|
|
|
|
|
2014-09-16 21:07:52 +00:00
|
|
|
function rc_tuning() { // Send over the RC_tuning changes
|
2014-09-17 07:27:54 +00:00
|
|
|
MSP.send_message(MSP_codes.MSP_SET_RC_TUNING, MSP.crunch(MSP_codes.MSP_SET_RC_TUNING), false, aux);
|
2014-03-20 21:19:27 +00:00
|
|
|
}
|
2013-06-17 22:14:49 +00:00
|
|
|
|
2014-09-16 21:07:52 +00:00
|
|
|
function aux() { // Send over the AUX changes
|
2014-09-17 07:27:54 +00:00
|
|
|
MSP.send_message(MSP_codes.MSP_SET_BOX, MSP.crunch(MSP_codes.MSP_SET_BOX), false, trim);
|
2014-09-16 21:07:52 +00:00
|
|
|
}
|
2013-06-17 22:14:49 +00:00
|
|
|
|
2014-09-16 21:07:52 +00:00
|
|
|
function trim() { // Send over the new trims
|
2014-09-17 07:27:54 +00:00
|
|
|
MSP.send_message(MSP_codes.MSP_SET_ACC_TRIM, MSP.crunch(MSP_codes.MSP_SET_ACC_TRIM), false, misc);
|
2014-03-20 21:19:27 +00:00
|
|
|
}
|
2014-01-15 12:05:19 +00:00
|
|
|
|
2014-09-17 07:27:54 +00:00
|
|
|
function misc() { // Send ove the new MISC
|
2014-10-06 13:24:34 +00:00
|
|
|
MSP.send_message(MSP_codes.MSP_SET_MISC, MSP.crunch(MSP_codes.MSP_SET_MISC), false, servo_conf);
|
|
|
|
}
|
|
|
|
|
|
|
|
function servo_conf() { // send over the new SERVO_CONF
|
|
|
|
MSP.send_message(MSP_codes.MSP_SET_SERVO_CONF, MSP.crunch(MSP_codes.MSP_SET_SERVO_CONF), false, save_eeprom);
|
2014-09-17 07:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function save_eeprom() {
|
|
|
|
MSP.send_message(MSP_codes.MSP_EEPROM_WRITE, false, false, function () {
|
|
|
|
GUI.log(chrome.i18n.getMessage('eeprom_saved_ok'));
|
2014-10-06 14:07:47 +00:00
|
|
|
if (callback) callback();
|
2014-01-15 13:31:03 +00:00
|
|
|
});
|
2014-03-20 21:19:27 +00:00
|
|
|
}
|
2014-09-17 07:27:54 +00:00
|
|
|
|
|
|
|
// Send over the PID changes
|
|
|
|
MSP.send_message(MSP_codes.MSP_SET_PID, MSP.crunch(MSP_codes.MSP_SET_PID), false, rc_tuning);
|
2013-06-17 21:36:19 +00:00
|
|
|
}
|