Skip to content

Commit

Permalink
Merge pull request #2937 from haslinghuis/fix-auto-detect
Browse files Browse the repository at this point in the history
Fix MSP_BOARD_INFO accumulation and localStorage Quota Exceeded
  • Loading branch information
asizon committed May 31, 2022
2 parents 10645cb + b51a7b2 commit bed2b8a
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 46 deletions.
19 changes: 13 additions & 6 deletions src/js/ConfigStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ const ConfigStorage = {
if (Array.isArray(key)) {
key.forEach(function (element) {
try {
result = {...result, ...JSON.parse(window.localStorage.getItem(element))};
result = {...result, ...JSON.parse(localStorage.getItem(element))};
} catch (e) {
// is okay
console.error(e);
}
});
} else {
const keyValue = window.localStorage.getItem(key);
const keyValue = localStorage.getItem(key);
if (keyValue) {
try {
result = JSON.parse(keyValue);
} catch (e) {
// It's fine if we fail that parse
console.error(e);
}
}
}
Expand All @@ -32,10 +32,17 @@ const ConfigStorage = {
Object.keys(input).forEach(function (element) {
const tmpObj = {};
tmpObj[element] = input[element];
window.localStorage.setItem(element, JSON.stringify(tmpObj));
try {
localStorage.setItem(element, JSON.stringify(tmpObj));
} catch (e) {
console.error(e);
}
});
},
remove: function(item) {
window.localStorage.removeItem(item);
localStorage.removeItem(item);
},
clear: function() {
localStorage.clear();
},
};
16 changes: 8 additions & 8 deletions src/js/FirmwareCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ let FirmwareCache = (function () {
function persist(data) {
let obj = {};
obj[CACHEKEY] = data;
ConfigStorage.set(obj);
SessionStorage.set(obj);
}

/**
* @param {Function} callback
*/
function load(callback) {
const obj = ConfigStorage.get(CACHEKEY);
const obj = SessionStorage.get(CACHEKEY);
let entries = typeof obj === "object" && obj.hasOwnProperty(CACHEKEY)
? obj[CACHEKEY]
: [];
Expand All @@ -75,13 +75,13 @@ let FirmwareCache = (function () {
}
let key = oldest[0];
let cacheKey = withCachePrefix(key);
const obj = ConfigStorage.get(cacheKey);
const obj = SessionStorage.get(cacheKey);
/** @type {CacheItem} */
const cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey) ? obj[cacheKey] : null;
if (cached === null) {
return undefined;
}
ConfigStorage.remove(cacheKey);
SessionStorage.remove(cacheKey);
onRemoveFromCache(cached.release);
return oldest;
};
Expand Down Expand Up @@ -138,7 +138,7 @@ let FirmwareCache = (function () {
release: release,
hexdata: hexdata,
};
ConfigStorage.set(obj);
SessionStorage.set(obj);
onPutToCache(release);
}

Expand All @@ -157,7 +157,7 @@ let FirmwareCache = (function () {
return;
}
let cacheKey = withCachePrefix(key);
const obj = ConfigStorage.get(cacheKey);
const obj = SessionStorage.get(cacheKey);
const cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey) ? obj[cacheKey] : null;
callback(cached);
}
Expand All @@ -174,7 +174,7 @@ let FirmwareCache = (function () {
for (let key of journal.keys()) {
cacheKeys.push(withCachePrefix(key));
}
const obj = ConfigStorage.get(cacheKeys);
const obj = SessionStorage.get(cacheKeys);
if (typeof obj !== "object") {
return;
}
Expand All @@ -186,7 +186,7 @@ let FirmwareCache = (function () {
onRemoveFromCache(item.release);
}
}
ConfigStorage.remove(cacheKeys);
SessionStorage.remove(cacheKeys);
journal.clear();
JournalStorage.persist(journal.toJSON());
}
Expand Down
45 changes: 45 additions & 0 deletions src/js/SessionStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const SessionStorage = {
// key can be one string, or array of strings
get: function(key) {
let result = {};
if (Array.isArray(key)) {
key.forEach(function (element) {
try {
result = {...result, ...JSON.parse(sessionStorage.getItem(element))};
} catch (e) {
console.error(e);
}
});
} else {
const keyValue = sessionStorage.getItem(key);
if (keyValue) {
try {
result = JSON.parse(keyValue);
} catch (e) {
console.error(e);
}
}
}

return result;
},
set: function(input) {
Object.keys(input).forEach(function (element) {
const tmpObj = {};
tmpObj[element] = input[element];
try {
sessionStorage.setItem(element, JSON.stringify(tmpObj));
} catch (e) {
console.error(e);
}
});
},
remove: function(item) {
sessionStorage.removeItem(item);
},
clear: function() {
sessionStorage.clear();
},
};
9 changes: 4 additions & 5 deletions src/js/jenkins_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ JenkinsLoader.prototype.loadJobs = function (viewName, callback) {
callback(jobs);
};

const result = ConfigStorage.get([cacheLastUpdateTag, jobsDataTag]);
const result = SessionStorage.get([cacheLastUpdateTag, jobsDataTag]);
const jobsDataTimestamp = $.now();
const cachedJobsData = result[jobsDataTag];
const cachedJobsLastUpdate = result[cacheLastUpdateTag];
Expand Down Expand Up @@ -49,7 +49,7 @@ JenkinsLoader.prototype.loadJobs = function (viewName, callback) {
const object = {};
object[jobsDataTag] = jobs;
object[cacheLastUpdateTag] = $.now();
ConfigStorage.set(object);
SessionStorage.set(object);

wrappedCallback(jobs);
}).fail(xhr => {
Expand All @@ -68,7 +68,7 @@ JenkinsLoader.prototype.loadBuilds = function (jobName, callback) {
const buildsDataTag = `${jobUrl}BuildsData`;
const cacheLastUpdateTag = `${jobUrl}BuildsLastUpdate`;

const result = ConfigStorage.get([cacheLastUpdateTag, buildsDataTag]);
const result = SessionStorage.get([cacheLastUpdateTag, buildsDataTag]);
const buildsDataTimestamp = $.now();
const cachedBuildsData = result[buildsDataTag];
const cachedBuildsLastUpdate = result[cacheLastUpdateTag];
Expand Down Expand Up @@ -100,8 +100,7 @@ JenkinsLoader.prototype.loadBuilds = function (jobName, callback) {
const object = {};
object[buildsDataTag] = builds;
object[cacheLastUpdateTag] = $.now();
ConfigStorage.set(object);

SessionStorage.set(object);
self._parseBuilds(jobUrl, jobName, builds, callback);
}).fail(xhr => {
GUI.log(i18n.getMessage('buildServerLoadFailed', [jobName, `HTTP ${xhr.status}`]));
Expand Down
22 changes: 22 additions & 0 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,31 @@ function readConfiguratorVersionMetadata() {
CONFIGURATOR.gitRevision = manifest.gitRevision;
}

function cleanupLocalStorage() {

const cleanupLocalStorageList = [
'cache',
'firmware',
'https',
'selected_board',
'unifiedConfigLast',
'unifiedSourceCache',
];

for (const key in localStorage) {
for (const item of cleanupLocalStorageList) {
if (key.includes(item)) {
localStorage.removeItem(key);
}
}
}
}

function appReady() {
readConfiguratorVersionMetadata();

cleanupLocalStorage();

i18n.init(function() {
startProcess();

Expand Down
29 changes: 15 additions & 14 deletions src/js/msp/MSPHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,49 +843,50 @@ MspHelper.prototype.process_data = function(dataHandler) {
break;

case MSPCodes.MSP_BOARD_INFO:
let boardIdentifier = '';
FC.CONFIG.boardIdentifier = '';

for (let i = 0; i < 4; i++) {
boardIdentifier += String.fromCharCode(data.readU8());
FC.CONFIG.boardIdentifier += String.fromCharCode(data.readU8());
}
FC.CONFIG.boardIdentifier = boardIdentifier;

FC.CONFIG.boardVersion = data.readU16();
FC.CONFIG.boardType = 0;

if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_35)) {
FC.CONFIG.boardType = data.readU8();
} else {
FC.CONFIG.boardType = 0;
}

FC.CONFIG.targetCapabilities = 0;
FC.CONFIG.targetName = '';

if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_37)) {
FC.CONFIG.targetCapabilities = data.readU8();

let length = data.readU8();
const length = data.readU8();
for (let i = 0; i < length; i++) {
FC.CONFIG.targetName += String.fromCharCode(data.readU8());
}
} else {
FC.CONFIG.targetCapabilities = 0;
FC.CONFIG.targetName = "";
}

FC.CONFIG.boardName = '';
FC.CONFIG.manufacturerId = '';
FC.CONFIG.signature = [];

if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_39)) {
let length = data.readU8();

for (let i = 0; i < length; i++) {
FC.CONFIG.boardName += String.fromCharCode(data.readU8());
}

length = data.readU8();

for (let i = 0; i < length; i++) {
FC.CONFIG.manufacturerId += String.fromCharCode(data.readU8());
}

for (let i = 0; i < self.SIGNATURE_LENGTH; i++) {
FC.CONFIG.signature.push(data.readU8());
}
} else {
FC.CONFIG.boardName = "";
FC.CONFIG.manufacturerId = "";
FC.CONFIG.signature = [];
}

if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_41)) {
Expand Down
4 changes: 2 additions & 2 deletions src/js/release_checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const ReleaseChecker = function (releaseName, releaseUrl) {

ReleaseChecker.prototype.loadReleaseData = function (processFunction) {
const self = this;
const result = ConfigStorage.get([self._releaseLastUpdateTag, self._releaseDataTag]);
const result = SessionStorage.get([self._releaseLastUpdateTag, self._releaseDataTag]);
const releaseDataTimestamp = $.now();
const cacheReleaseData = result[self._releaseDataTag];
const cachedReleaseLastUpdate = result[self._releaseLastUpdateTag];
Expand All @@ -23,7 +23,7 @@ ReleaseChecker.prototype.loadReleaseData = function (processFunction) {
const data = {};
data[self._releaseDataTag] = releaseData;
data[self._releaseLastUpdateTag] = releaseDataTimestamp;
ConfigStorage.set(data);
SessionStorage.set(data);

self._processReleaseData(releaseData, processFunction);
}).fail(function (data) {
Expand Down

0 comments on commit bed2b8a

Please sign in to comment.