-
-
Notifications
You must be signed in to change notification settings - Fork 917
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor auto-backup in flashing tab for PWA (#4005)
* Make auto-backup while flashing work for PWA * Fixes per review * Prevent caching of buttonYesCallback
- Loading branch information
1 parent
2f4337b
commit 0ada2ce
Showing
2 changed files
with
136 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import PortHandler from '../port_handler'; | ||
import FileSystem from '../FileSystem'; | ||
import { generateFilename } from './generate_filename'; | ||
import { gui_log } from '../gui_log'; | ||
import { i18n } from "../localization"; | ||
import serial from '../webSerial'; | ||
|
||
/** | ||
* | ||
* Bacup the current configuration to a file before flashing in serial mode | ||
* | ||
*/ | ||
|
||
class AutoBackup { | ||
constructor() { | ||
this.outputHistory = ''; | ||
this.callback = null; | ||
} | ||
|
||
handleConnect(openInfo) { | ||
console.log('Connected to serial port:', openInfo); | ||
if (openInfo) { | ||
serial.removeEventListener('receive', this.readSerialAdapter); | ||
serial.addEventListener('receive', this.readSerialAdapter.bind(this)); | ||
|
||
this.run(); | ||
} else { | ||
gui_log(i18n.getMessage('serialPortOpenFail')); | ||
} | ||
} | ||
|
||
handleDisconnect(event) { | ||
gui_log(i18n.getMessage(event.detail ? 'serialPortClosedOk' : 'serialPortClosedFail')); | ||
|
||
serial.removeEventListener('receive', this.readSerialAdapter); | ||
serial.removeEventListener('connect', this.handleConnect); | ||
serial.removeEventListener('disconnect', this.handleDisconnect); | ||
} | ||
|
||
readSerialAdapter(info) { | ||
const data = new Uint8Array(info.detail.buffer); | ||
|
||
for (const charCode of data) { | ||
const currentChar = String.fromCharCode(charCode); | ||
this.outputHistory += currentChar; | ||
} | ||
} | ||
|
||
onClose() { | ||
serial.addEventListener('disconnect', this.handleDisconnect.bind(this), { once: true }); | ||
serial.disconnect(); | ||
} | ||
|
||
async save() { | ||
console.log('Saving backup'); | ||
const prefix = 'cli_backup'; | ||
const suffix = 'txt'; | ||
const text = this.outputHistory; | ||
const filename = generateFilename(prefix, suffix); | ||
|
||
FileSystem.pickSaveFile(filename, i18n.getMessage('fileSystemPickerFiles', { types: suffix.toUpperCase() }), `.${suffix}`) | ||
.then((file) => { | ||
console.log("Saving config to:", file.name); | ||
FileSystem.writeFile(file, text); | ||
}) | ||
.catch((error) => { | ||
console.error("Error saving config:", error); | ||
}) | ||
.finally(() => { | ||
if (this.callback) { | ||
this.callback(); | ||
} | ||
}); | ||
} | ||
|
||
async run() { | ||
console.log('Running backup'); | ||
|
||
await this.activateCliMode(); | ||
await this.sendCommand("dump all"); | ||
|
||
setTimeout(async () => { | ||
this.sendCommand("exit", this.onClose); | ||
await this.save(this.outputHistory); | ||
}, 1500); | ||
} | ||
|
||
async activateCliMode() { | ||
return new Promise(resolve => { | ||
const bufferOut = new ArrayBuffer(1); | ||
const bufView = new Uint8Array(bufferOut); | ||
|
||
bufView[0] = 0x23; | ||
|
||
serial.send(bufferOut); | ||
|
||
setTimeout(() => { | ||
this.outputHistory = ''; | ||
resolve(); | ||
}, 500); | ||
}); | ||
} | ||
|
||
async sendSerial(line, callback) { | ||
const bufferOut = new ArrayBuffer(line.length); | ||
const bufView = new Uint8Array(bufferOut); | ||
|
||
for (let cKey = 0; cKey < line.length; cKey++) { | ||
bufView[cKey] = line.charCodeAt(cKey); | ||
} | ||
|
||
serial.send(bufferOut, callback); | ||
} | ||
|
||
async sendCommand(line, callback) { | ||
this.sendSerial(`${line}\n`, callback); | ||
} | ||
|
||
execute(callback) { | ||
this.callback = callback; | ||
|
||
const port = PortHandler.portPicker.selectedPort; | ||
const baud = PortHandler.portPicker.selectedBauds; | ||
|
||
if (port.startsWith('serial')) { | ||
serial.addEventListener('connect', this.handleConnect.bind(this), { once: true }); | ||
serial.connect(port, { baudRate: baud }); | ||
} else { | ||
gui_log(i18n.getMessage('firmwareFlasherNoPortSelected')); | ||
} | ||
} | ||
} | ||
|
||
export default new AutoBackup(); |