Skip to content

Commit 99ad875

Browse files
Autobackups working, restore just mockup
1 parent bee5e52 commit 99ad875

File tree

7 files changed

+105
-8
lines changed

7 files changed

+105
-8
lines changed

app/index.html

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,36 @@
7474
<span class="caption">Backup<br>Settings</span>
7575
</button>
7676

77-
78-
79-
<button class="ribbon-button btn-file">
77+
<!-- <button class="ribbon-button btn-file">
8078
<input class="btn-file" id="grblBackupFile" type="file" accept=".txt, .settings, .grbl" />
8179
<span class="icon">
8280
<i class="fas fa-upload"></i>
8381
</span>
8482
<span class="caption">Restore<br>Backup</span>
85-
</button>
83+
</button> -->
84+
<div>
85+
<button id="" class="ribbon-button dropdown-toggle">
86+
<span class="icon">
87+
<span class="fa-layers fa-fw">
88+
<i class="fas fa-upload"></i>
89+
</span>
90+
</span>
91+
<span class="caption">Restore<br>Backup</span>
92+
</button>
93+
<ul class="ribbon-dropdown" data-role="dropdown" data-duration="100">
94+
<li class="btn-file">
95+
<a href="#">
96+
<input class="btn-file" id="grblBackupFile" type="file" accept=".txt, .settings, .grbl" />
97+
<i class="fas fa-folder-open"></i>
98+
Restore Backup File
99+
</a>
100+
</li>
101+
<li class="divider fg-gray"></li>
102+
<div id="restoreBackupMenu">
103+
</div>
104+
</ul>
105+
</div>
106+
86107
<span class="title">Grbl Settings</span>
87108
</div>
88109
<div class="group">

app/js/grbl-settings.js

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,49 @@ function loadGrblBackupFile(f) {
4040
}
4141
}
4242

43+
function populateRestoreMenu() {
44+
// Retrieve backups from localStorage
45+
const backups = JSON.parse(localStorage.getItem('grblParamsBackups')) || [];
46+
47+
// Get the dropdown menu element
48+
const backupMenu = document.getElementById('restoreBackupMenu');
49+
50+
// Clear existing menu items (in case you're calling this multiple times)
51+
backupMenu.innerHTML = '';
52+
53+
// Loop through each backup and create a list item for it
54+
backups.forEach((backup, index) => {
55+
const backupItem = document.createElement('li');
56+
57+
// Format the timestamp (you can format it as needed)
58+
const formattedTimestamp = new Date(backup.timestamp).toLocaleString(); // Adjust formatting as needed
59+
60+
// Create the list item HTML content
61+
backupItem.innerHTML = `
62+
<a href="#" onclick="restoreAutoBackup(${index})">
63+
<i class="fas fa-clock fa-fw"></i>
64+
Restore AutoBackup: ${formattedTimestamp} (${backup.note || 'No note'})
65+
</a>
66+
`;
67+
68+
// Append the list item to the dropdown menu
69+
backupMenu.appendChild(backupItem);
70+
});
71+
}
72+
73+
function restoreAutoBackup(index) {
74+
const backups = JSON.parse(localStorage.getItem('grblParamsBackups')) || [];
75+
const selectedBackup = backups[index];
76+
77+
// You can now access selectedBackup.grblParams and apply it as needed
78+
console.log('Restoring backup:', selectedBackup);
79+
// Call your function to restore the backup here, e.g., update grblParams
80+
// Example: grblParams = selectedBackup.grblParams;
81+
}
82+
83+
4384
function backupGrblSettings() {
85+
autoBackup("Manual Backup")
4486
var grblBackup = ""
4587
for (key in grblParams) {
4688
var key2 = key.split('=')[0].substr(1);
@@ -50,7 +92,6 @@ function backupGrblSettings() {
5092
} else {
5193
var descr = "unknown"
5294
}
53-
5495
grblBackup += key + "=" + grblParams[key] + " ; " + descr + "\n"
5596
}
5697
if (laststatus.machine.name.length > 0) {
@@ -65,7 +106,6 @@ function backupGrblSettings() {
65106
} else {
66107
invokeSaveAsDialog(blob, 'grbl-settings-backup-' + date.yyyymmdd() + '.txt');
67108
}
68-
69109
}
70110

71111
function grblSettings(data) {
@@ -409,6 +449,8 @@ function grblPopulate() {
409449
setTimeout(function() {
410450
setMachineButton(laststatus.machine.name)
411451
}, 500)
452+
453+
populateRestoreMenu();
412454
}
413455

414456
}
@@ -519,7 +561,38 @@ function checkifchanged() {
519561
}
520562

521563

564+
function autoBackup(note) {
565+
566+
const timestamp = new Date().toISOString(); // Generate current timestamp
567+
const currentParams = {
568+
machinetype: laststatus.machine.name,
569+
note: note,
570+
timestamp: timestamp,
571+
grblParams: {
572+
...grblParams // Spread Operator copy
573+
}
574+
}; // Add timestamp to the current parameters
575+
576+
// Retrieve existing backups from localStorage or initialize an empty array
577+
let backups = JSON.parse(localStorage.getItem('grblParamsBackups')) || [];
578+
579+
// Add the current backup to the beginning of the array
580+
backups.unshift(currentParams);
581+
582+
// Trim backups to keep only the last 20
583+
if (backups.length > 20) {
584+
backups = backups.slice(0, 20);
585+
}
586+
587+
// Save the updated backups array back to localStorage
588+
localStorage.setItem('grblParamsBackups', JSON.stringify(backups));
589+
590+
// Optionally, add your existing save functionality here
591+
console.log('Settings saved and backup created.');
592+
}
593+
522594
function grblSaveSettings() {
595+
autoBackup("Updated Grbl Settings")
523596
var toSaveCommands = [];
524597
var saveProgressBar = $("#grblSaveProgress").data("progress");
525598
for (var key in grblParams) {

app/wizards/calibration/calibrate-servo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ function servocalslide3() {
153153
}
154154

155155
function closeServoCal() {
156+
autoBackup("Calibrated Servo");
156157
console.log("Saving calibration: up: " + $('#penupslider').data('slider').val() + ", down: " + $('#pendownslider').data('slider').val())
157158
servo = {
158159
up: $('#penupslider').data('slider').val(),

app/wizards/calibration/calibrate-x.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ var xcaltemplate = `
8181
`
8282

8383
function applycalibrationx() {
84+
autoBackup("Calibrated X");
8485
var actualdist = $('#xcaltraveldist').val();
8586
var currentstepspermm = parseFloat(grblParams['$100']);
8687
// var currentstepspermm = 199.9;

app/wizards/calibration/calibrate-y.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ var ycaltemplate = `
7979
`
8080

8181
function applycalibrationy() {
82+
autoBackup("Calibrated Y");
8283
var actualdist = $('#ycaltraveldist').val();
8384
var currentstepspermm = parseFloat(grblParams['$101']);
8485
// var currentstepspermm = 199.9;

app/wizards/calibration/calibrate-z.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ var zcaltemplate = `
7979
`
8080

8181
function applycalibrationz() {
82+
autoBackup("Calibrated Z");
8283
var actualdist = $('#zcaltraveldist').val();
8384
var currentstepspermm = parseFloat(grblParams['$102']);
8485
// var currentstepspermm = 199.9;

app/wizards/flashingtool2/flashingtool.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ function readEspFirmwareFile() {
244244
}
245245

246246
function flashFirmwarefromWizard() {
247+
autoBackup("Updated Firmware: " + selectedControllerType);
247248
if (selectedControllerType == "blackbox4x") {
248249

249250
if ($("#grblAxesCount").val() == "3axes-grbl") {
@@ -284,8 +285,6 @@ function flashFirmwarefromWizard() {
284285
$('#consoletab').click();
285286
printLog("<span class='fg-red'>[ Firmware Upgrade ] </span><span class='fg-red'><i class='fas fa-times fa-fw fg-red fa-fw'></i>You selected the option to use a custom firmware file, but failed to select a file to use for the operation. Please try again</span>")
286287
}
287-
288-
289288
} else {
290289
// Precompiled Firmwares
291290
socket.emit('flashGrbl', data)

0 commit comments

Comments
 (0)