-
-
Notifications
You must be signed in to change notification settings - Fork 983
Refactor auto-backup in flashing tab for PWA #4005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,137 @@ | ||
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) { | ||
if (event.detail) { | ||
gui_log(i18n.getMessage('serialPortClosedOk')); | ||
} else { | ||
gui_log(i18n.getMessage('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; | ||
|
||
if (port.startsWith('serial')) { | ||
const baud = parseInt($('#flash_manual_baud_rate').val()) || 115200; | ||
McGiverGim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
serial.addEventListener('connect', this.handleConnect.bind(this), { once: true }); | ||
serial.connect(port, { baudRate: baud }); | ||
} else { | ||
gui_log(i18n.getMessage('firmwareFlasherNoPortSelected')); | ||
} | ||
} | ||
} | ||
|
||
export default new AutoBackup(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.