betaflight-configurator/js/backup_restore.js

415 lines
15 KiB
JavaScript
Raw Normal View History

'use strict';
// code below is highly experimental, although it runs fine on latest firmware
// the data inside nested objects needs to be verified if deep copy works properly
function configuration_backup(callback) {
var activeProfile = null,
profilesN = 3;
var profileSpecificData = [
MSP_codes.MSP_PID,
MSP_codes.MSP_RC_TUNING,
MSP_codes.MSP_ACC_TRIM,
MSP_codes.MSP_SERVO_CONF,
MSP_codes.MSP_CHANNEL_FORWARDING,
MSP_codes.MSP_MODE_RANGES,
MSP_codes.MSP_ADJUSTMENT_RANGES
];
var uniqueData = [
// Not used by cleanflight, and it's wrong anyway - AUX settings are per-profile in baseflight.
/*
MSP_codes.MSP_BOX,
*/
MSP_codes.MSP_MISC,
MSP_codes.MSP_RCMAP,
MSP_codes.MSP_BF_CONFIG,
MSP_codes.MSP_CF_SERIAL_CONFIG
];
var configuration = {
'generatedBy': chrome.runtime.getManifest().version,
'profiles': []
};
MSP.send_message(MSP_codes.MSP_STATUS, false, false, function () {
activeProfile = CONFIG.profile;
select_profile();
});
function select_profile() {
if (activeProfile > 0) {
MSP.send_message(MSP_codes.MSP_SELECT_SETTING, [0], false, fetch_specific_data);
} else {
fetch_specific_data();
}
}
2014-03-08 05:25:15 +00:00
function fetch_specific_data() {
var fetchingProfile = 0,
codeKey = 0;
function query() {
if (fetchingProfile < profilesN) {
MSP.send_message(profileSpecificData[codeKey], false, false, function () {
codeKey++;
if (codeKey < profileSpecificData.length) {
query();
} else {
configuration.profiles.push({
2014-10-12 14:55:49 +00:00
'PID': jQuery.extend(true, [], PIDs),
'RC': jQuery.extend(true, {}, RC_tuning),
2014-10-12 14:55:49 +00:00
'AccTrim': jQuery.extend(true, [], CONFIG.accelerometerTrims),
'ServoConfig': jQuery.extend(true, [], SERVO_CONFIG),
'ModeRanges': jQuery.extend(true, [], MODE_RANGES),
'AdjustmentRanges': jQuery.extend(true, [], ADJUSTMENT_RANGES)
});
codeKey = 0;
fetchingProfile++;
MSP.send_message(MSP_codes.MSP_SELECT_SETTING, [fetchingProfile], false, query);
}
});
} else {
MSP.send_message(MSP_codes.MSP_SELECT_SETTING, [activeProfile], false, fetch_unique_data);
}
}
// start fetching
query();
}
function fetch_unique_data() {
var codeKey = 0;
function query() {
if (codeKey < uniqueData.length) {
MSP.send_message(uniqueData[codeKey], false, false, function () {
codeKey++;
query();
});
} else {
// Not used by cleanflight, and it's wrong anyway - AUX settings are per-profile in baseflight.
/*
2014-10-12 14:55:49 +00:00
configuration.AUX = jQuery.extend(true, [], AUX_CONFIG_values);
*/
2014-10-12 14:55:49 +00:00
configuration.MISC = jQuery.extend(true, {}, MISC);
configuration.RCMAP = jQuery.extend(true, [], RC_MAP);
configuration.BF_CONFIG = jQuery.extend(true, {}, BF_CONFIG);
configuration.SERIAL_CONFIG = jQuery.extend(true, {}, SERIAL_CONFIG);
save();
}
}
// start fetching
query();
}
function save() {
var chosenFileEntry = null;
2014-03-08 05:25:15 +00:00
var accepts = [{
extensions: ['txt']
}];
2014-03-08 05:25:15 +00:00
// generate timestamp for the backup file
var d = new Date(),
2014-10-12 12:50:55 +00:00
now = (d.getMonth() + 1) + '.' + d.getDate() + '.' + d.getFullYear() + '.' + d.getHours() + '.' + d.getMinutes();
// create or load the file
chrome.fileSystem.chooseEntry({type: 'saveFile', suggestedName: 'cleanflight_backup_' + now, accepts: accepts}, function (fileEntry) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
return;
}
if (!fileEntry) {
console.log('No file selected, backup aborted.');
return;
}
2014-03-08 05:25:15 +00:00
chosenFileEntry = fileEntry;
// echo/console log path specified
chrome.fileSystem.getDisplayPath(chosenFileEntry, function (path) {
console.log('Backup file path: ' + path);
});
// change file entry from read only to read/write
chrome.fileSystem.getWritableEntry(chosenFileEntry, function (fileEntryWritable) {
// check if file is writable
chrome.fileSystem.isWritableEntry(fileEntryWritable, function (isWritable) {
if (isWritable) {
chosenFileEntry = fileEntryWritable;
2014-03-08 05:25:15 +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
chosenFileEntry.createWriter(function (writer) {
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;
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
console.log('Write SUCCESSFUL');
if (callback) callback();
};
2014-03-08 05:25:15 +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.');
}
});
});
});
}
}
function configuration_restore(callback) {
var chosenFileEntry = null;
2014-03-08 05:25:15 +00:00
var accepts = [{
extensions: ['txt']
}];
2014-03-08 05:25:15 +00:00
// load up the file
chrome.fileSystem.chooseEntry({type: 'openFile', accepts: accepts}, function (fileEntry) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
return;
}
if (!fileEntry) {
console.log('No file selected, restore aborted.');
return;
}
2014-03-08 05:25:15 +00:00
chosenFileEntry = fileEntry;
// echo/console log path specified
chrome.fileSystem.getDisplayPath(chosenFileEntry, function (path) {
console.log('Restore file path: ' + path);
2014-03-08 05:25:15 +00:00
});
// read contents into variable
chosenFileEntry.file(function (file) {
var reader = new FileReader();
reader.onprogress = function (e) {
if (e.total > 1048576) { // 1 MB
// dont allow reading files bigger then 1 MB
console.log('File limit (1 MB) exceeded, aborting');
reader.abort();
}
};
2014-03-08 05:25:15 +00:00
reader.onloadend = function (e) {
if (e.total != 0 && e.total == e.loaded) {
console.log('Read SUCCESSFUL');
2014-03-08 05:25:15 +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
return;
}
2014-03-08 05:25:15 +00:00
configuration_upload(deserialized_configuration_object, callback);
}
};
reader.readAsText(file);
});
});
function configuration_upload(configuration, callback) {
2014-10-12 16:00:44 +00:00
function compareVersions(generated, required) {
var a = generated.split('.'),
b = required.split('.');
2014-10-12 16:00:44 +00:00
for (var i = 0; i < a.length; ++i) {
a[i] = Number(a[i]);
}
for (var i = 0; i < b.length; ++i) {
b[i] = Number(b[i]);
}
if (a.length == 2) {
a[2] = 0;
}
2014-10-12 16:00:44 +00:00
if (a[0] > b[0]) return true;
if (a[0] < b[0]) return false;
2014-10-12 16:00:44 +00:00
if (a[1] > b[1]) return true;
if (a[1] < b[1]) return false;
2014-10-12 16:00:44 +00:00
if (a[2] > b[2]) return true;
if (a[2] < b[2]) return false;
2014-10-12 16:00:44 +00:00
return true;
}
2014-01-15 12:05:19 +00:00
2014-10-12 16:00:44 +00:00
function upload() {
var activeProfile = null,
profilesN = 3;
var profileSpecificData = [
MSP_codes.MSP_SET_PID,
MSP_codes.MSP_SET_RC_TUNING,
MSP_codes.MSP_SET_ACC_TRIM,
MSP_codes.MSP_SET_SERVO_CONF,
MSP_codes.MSP_SET_CHANNEL_FORWARDING
2014-10-12 16:00:44 +00:00
];
var uniqueData = [
// Not used by cleanflight, and it's wrong anyway - AUX settings are per-profile in baseflight.
/*
2014-10-12 16:00:44 +00:00
MSP_codes.MSP_SET_BOX,
*/
2014-10-12 16:00:44 +00:00
MSP_codes.MSP_SET_MISC,
MSP_codes.MSP_SET_RCMAP,
MSP_codes.MSP_SET_BF_CONFIG,
MSP_codes.MSP_SET_CF_SERIAL_CONFIG
2014-10-12 16:00:44 +00:00
];
MSP.send_message(MSP_codes.MSP_STATUS, false, false, function () {
activeProfile = CONFIG.profile;
select_profile();
});
2014-10-12 16:00:44 +00:00
function select_profile() {
if (activeProfile > 0) {
MSP.send_message(MSP_codes.MSP_SELECT_SETTING, [0], false, upload_specific_data);
} else {
upload_specific_data();
}
}
function upload_specific_data() {
var savingProfile = 0,
codeKey = 0;
function load_objects(profile) {
PIDs = configuration.profiles[profile].PID;
RC_tuning = configuration.profiles[profile].RC;
CONFIG.accelerometerTrims = configuration.profiles[profile].AccTrim;
SERVO_CONFIG = configuration.profiles[profile].ServoConfig;
MODE_RANGES = configuration.profiles[profile].ModeRanges;
ADJUSTMENT_RANGES = configuration.profiles[profile].AdjustmentRanges;
2014-10-12 16:00:44 +00:00
}
function upload_using_specific_commands() {
2014-10-12 16:00:44 +00:00
MSP.send_message(profileSpecificData[codeKey], MSP.crunch(profileSpecificData[codeKey]), false, function () {
codeKey++;
if (codeKey < profileSpecificData.length) {
upload_using_specific_commands();
2014-10-12 16:00:44 +00:00
} else {
codeKey = 0;
savingProfile++;
if (savingProfile < profilesN) {
load_objects(savingProfile);
MSP.send_message(MSP_codes.MSP_EEPROM_WRITE, false, false, function () {
MSP.send_message(MSP_codes.MSP_SELECT_SETTING, [savingProfile], false, upload_using_specific_commands);
2014-10-12 16:00:44 +00:00
});
} else {
MSP.send_message(MSP_codes.MSP_EEPROM_WRITE, false, false, function () {
MSP.send_message(MSP_codes.MSP_SELECT_SETTING, [activeProfile], false, upload_unique_data);
});
}
}
});
}
function upload_mode_ranges() {
MSP.sendModeRanges(upload_adjustment_ranges);
}
function upload_adjustment_ranges() {
MSP.sendAdjustmentRanges(upload_using_specific_commands);
}
2014-10-12 16:00:44 +00:00
// start uploading
load_objects(0);
upload_mode_ranges();
2014-10-12 16:00:44 +00:00
}
2014-10-12 16:00:44 +00:00
function upload_unique_data() {
var codeKey = 0;
function load_objects() {
// Disabled, cleanflight does not use MSP_BOX.
/*
2014-10-12 16:00:44 +00:00
AUX_CONFIG_values = configuration.AUX;
*/
2014-10-12 16:00:44 +00:00
MISC = configuration.MISC;
RC_MAP = configuration.RCMAP;
BF_CONFIG = configuration.BF_CONFIG;
SERIAL_CONFIG = configuration.SERIAL_CONFIG;
2014-10-12 16:00:44 +00:00
}
function query() {
if (codeKey < uniqueData.length) {
MSP.send_message(uniqueData[codeKey], MSP.crunch(uniqueData[codeKey]), false, function () {
codeKey++;
query();
});
} else {
MSP.send_message(MSP_codes.MSP_EEPROM_WRITE, false, false, reboot);
}
}
// start uploading
load_objects();
query();
}
function reboot() {
GUI.log(chrome.i18n.getMessage('eeprom_saved_ok'));
2014-10-12 16:00:44 +00:00
GUI.tab_switch_cleanup(function() {
MSP.send_message(MSP_codes.MSP_SET_REBOOT, false, false, reinitialize);
});
}
function reinitialize() {
GUI.log(chrome.i18n.getMessage('deviceRebooting'));
GUI.timeout_add('waiting_for_bootup', function waiting_for_bootup() {
MSP.send_message(MSP_codes.MSP_IDENT, false, false, function () {
GUI.log(chrome.i18n.getMessage('deviceReady'));
if (callback) callback();
});
}, 1500); // 1500 ms seems to be just the right amount of delay to prevent data request timeouts
}
}
2014-10-12 16:00:44 +00:00
// validate
if (typeof configuration.generatedBy !== 'undefined' && compareVersions(configuration.generatedBy, CONFIGURATOR.backupFileMinVersionAccepted)) {
2014-10-12 16:00:44 +00:00
upload();
} else {
GUI.log(chrome.i18n.getMessage('backupFileIncompatible'));
}
}
}