From 99ad875cb2c392b374eb23d2a433e0f87a3c3f4c Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 5 Dec 2024 18:03:52 +0200 Subject: [PATCH] Autobackups working, restore just mockup --- app/index.html | 29 ++++++-- app/js/grbl-settings.js | 77 +++++++++++++++++++++- app/wizards/calibration/calibrate-servo.js | 1 + app/wizards/calibration/calibrate-x.js | 1 + app/wizards/calibration/calibrate-y.js | 1 + app/wizards/calibration/calibrate-z.js | 1 + app/wizards/flashingtool2/flashingtool.js | 3 +- 7 files changed, 105 insertions(+), 8 deletions(-) diff --git a/app/index.html b/app/index.html index fd98a86c..b13d96a0 100644 --- a/app/index.html +++ b/app/index.html @@ -74,15 +74,36 @@ Backup
Settings
- - - + + + Grbl Settings
diff --git a/app/js/grbl-settings.js b/app/js/grbl-settings.js index 97548c5f..1ba01702 100644 --- a/app/js/grbl-settings.js +++ b/app/js/grbl-settings.js @@ -40,7 +40,49 @@ function loadGrblBackupFile(f) { } } +function populateRestoreMenu() { + // Retrieve backups from localStorage + const backups = JSON.parse(localStorage.getItem('grblParamsBackups')) || []; + + // Get the dropdown menu element + const backupMenu = document.getElementById('restoreBackupMenu'); + + // Clear existing menu items (in case you're calling this multiple times) + backupMenu.innerHTML = ''; + + // Loop through each backup and create a list item for it + backups.forEach((backup, index) => { + const backupItem = document.createElement('li'); + + // Format the timestamp (you can format it as needed) + const formattedTimestamp = new Date(backup.timestamp).toLocaleString(); // Adjust formatting as needed + + // Create the list item HTML content + backupItem.innerHTML = ` + + + Restore AutoBackup: ${formattedTimestamp} (${backup.note || 'No note'}) + + `; + + // Append the list item to the dropdown menu + backupMenu.appendChild(backupItem); + }); +} + +function restoreAutoBackup(index) { + const backups = JSON.parse(localStorage.getItem('grblParamsBackups')) || []; + const selectedBackup = backups[index]; + + // You can now access selectedBackup.grblParams and apply it as needed + console.log('Restoring backup:', selectedBackup); + // Call your function to restore the backup here, e.g., update grblParams + // Example: grblParams = selectedBackup.grblParams; +} + + function backupGrblSettings() { + autoBackup("Manual Backup") var grblBackup = "" for (key in grblParams) { var key2 = key.split('=')[0].substr(1); @@ -50,7 +92,6 @@ function backupGrblSettings() { } else { var descr = "unknown" } - grblBackup += key + "=" + grblParams[key] + " ; " + descr + "\n" } if (laststatus.machine.name.length > 0) { @@ -65,7 +106,6 @@ function backupGrblSettings() { } else { invokeSaveAsDialog(blob, 'grbl-settings-backup-' + date.yyyymmdd() + '.txt'); } - } function grblSettings(data) { @@ -409,6 +449,8 @@ function grblPopulate() { setTimeout(function() { setMachineButton(laststatus.machine.name) }, 500) + + populateRestoreMenu(); } } @@ -519,7 +561,38 @@ function checkifchanged() { } +function autoBackup(note) { + + const timestamp = new Date().toISOString(); // Generate current timestamp + const currentParams = { + machinetype: laststatus.machine.name, + note: note, + timestamp: timestamp, + grblParams: { + ...grblParams // Spread Operator copy + } + }; // Add timestamp to the current parameters + + // Retrieve existing backups from localStorage or initialize an empty array + let backups = JSON.parse(localStorage.getItem('grblParamsBackups')) || []; + + // Add the current backup to the beginning of the array + backups.unshift(currentParams); + + // Trim backups to keep only the last 20 + if (backups.length > 20) { + backups = backups.slice(0, 20); + } + + // Save the updated backups array back to localStorage + localStorage.setItem('grblParamsBackups', JSON.stringify(backups)); + + // Optionally, add your existing save functionality here + console.log('Settings saved and backup created.'); +} + function grblSaveSettings() { + autoBackup("Updated Grbl Settings") var toSaveCommands = []; var saveProgressBar = $("#grblSaveProgress").data("progress"); for (var key in grblParams) { diff --git a/app/wizards/calibration/calibrate-servo.js b/app/wizards/calibration/calibrate-servo.js index db05a50b..820939a2 100644 --- a/app/wizards/calibration/calibrate-servo.js +++ b/app/wizards/calibration/calibrate-servo.js @@ -153,6 +153,7 @@ function servocalslide3() { } function closeServoCal() { + autoBackup("Calibrated Servo"); console.log("Saving calibration: up: " + $('#penupslider').data('slider').val() + ", down: " + $('#pendownslider').data('slider').val()) servo = { up: $('#penupslider').data('slider').val(), diff --git a/app/wizards/calibration/calibrate-x.js b/app/wizards/calibration/calibrate-x.js index e76dc191..30435da2 100644 --- a/app/wizards/calibration/calibrate-x.js +++ b/app/wizards/calibration/calibrate-x.js @@ -81,6 +81,7 @@ var xcaltemplate = ` ` function applycalibrationx() { + autoBackup("Calibrated X"); var actualdist = $('#xcaltraveldist').val(); var currentstepspermm = parseFloat(grblParams['$100']); // var currentstepspermm = 199.9; diff --git a/app/wizards/calibration/calibrate-y.js b/app/wizards/calibration/calibrate-y.js index 8de30ea1..c2d25f8c 100644 --- a/app/wizards/calibration/calibrate-y.js +++ b/app/wizards/calibration/calibrate-y.js @@ -79,6 +79,7 @@ var ycaltemplate = ` ` function applycalibrationy() { + autoBackup("Calibrated Y"); var actualdist = $('#ycaltraveldist').val(); var currentstepspermm = parseFloat(grblParams['$101']); // var currentstepspermm = 199.9; diff --git a/app/wizards/calibration/calibrate-z.js b/app/wizards/calibration/calibrate-z.js index ef4084a0..8f0c8de9 100644 --- a/app/wizards/calibration/calibrate-z.js +++ b/app/wizards/calibration/calibrate-z.js @@ -79,6 +79,7 @@ var zcaltemplate = ` ` function applycalibrationz() { + autoBackup("Calibrated Z"); var actualdist = $('#zcaltraveldist').val(); var currentstepspermm = parseFloat(grblParams['$102']); // var currentstepspermm = 199.9; diff --git a/app/wizards/flashingtool2/flashingtool.js b/app/wizards/flashingtool2/flashingtool.js index b26e6fc3..51aa9a9d 100644 --- a/app/wizards/flashingtool2/flashingtool.js +++ b/app/wizards/flashingtool2/flashingtool.js @@ -244,6 +244,7 @@ function readEspFirmwareFile() { } function flashFirmwarefromWizard() { + autoBackup("Updated Firmware: " + selectedControllerType); if (selectedControllerType == "blackbox4x") { if ($("#grblAxesCount").val() == "3axes-grbl") { @@ -284,8 +285,6 @@ function flashFirmwarefromWizard() { $('#consoletab').click(); printLog("[ Firmware Upgrade ] You selected the option to use a custom firmware file, but failed to select a file to use for the operation. Please try again") } - - } else { // Precompiled Firmwares socket.emit('flashGrbl', data)