1
0
Fork 0
mirror of https://github.com/betaflight/betaflight-configurator.git synced 2025-07-24 00:35:26 +03:00

Fix reboot (#3986)

* Fix reboot

* Remove runonce and add callback for cli

* Couldn't resist

* Refactoring

* Forgot callback

* deviceReady

* Prevent callback being called twice
This commit is contained in:
Mark Haslinghuis 2024-05-28 17:36:13 +02:00 committed by GitHub
parent 0fa76eb63e
commit a6e3761c26
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 20 additions and 51 deletions

View file

@ -694,9 +694,11 @@ function onClosed(result) {
MSP.clearListeners();
serial.removeEventListener('receive', read_serial_adapter);
serial.removeEventListener('connect', connectHandler);
serial.removeEventListener('disconnect', disconnectHandler);
if (PortHandler.portPicker.selectedPort !== 'virtual') {
serial.removeEventListener('receive', read_serial_adapter);
serial.removeEventListener('connect', connectHandler);
serial.removeEventListener('disconnect', disconnectHandler);
}
CONFIGURATOR.connectionValid = false;
CONFIGURATOR.cliValid = false;
@ -782,53 +784,24 @@ function startLiveDataRefreshTimer() {
}
export function reinitializeConnection(callback) {
const isVirtual = CONFIGURATOR.virtualMode && GUI.connected_to == 'virtual' && CONFIGURATOR.connectionValid && serial.connectionId === 'virtual';
gui_log(i18n.getMessage('deviceRebooting'));
// Close connection gracefully if it still exists.
const previousTimeStamp = connectionTimestamp;
if (serial.connectionId) {
if (GUI.connected_to || GUI.connecting_to) {
$('a.connect').trigger('click');
} else {
serial.disconnect();
}
}
// In virtual mode reconnect when autoconnect is enabled
if (isVirtual) {
return setTimeout(() => {
if (PortHandler.portPicker.autoConnect) {
$('a.connect').trigger('click');
}
if (typeof callback === 'function') {
callback();
}
if (PortHandler.portPicker.selectedPort === 'virtual' && PortHandler.portPicker.autoConnect) {
return setTimeout(function() {
$('a.connect').trigger('click');
}, 500);
}
// Wait for serial or tcp connection to be available
let attempts = 0;
const reconnect = setInterval(waitforSerial, 100);
MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false);
function waitforSerial() {
if ((connectionTimestamp !== previousTimeStamp && CONFIGURATOR.connectionValid) || GUI.active_tab === 'firmware_flasher') {
console.log(`Serial connection available after ${attempts / 10} seconds`);
clearInterval(reconnect);
gui_log(i18n.getMessage('deviceReady'));
gui_log(i18n.getMessage('deviceRebooting'));
if (typeof callback === 'function') {
callback();
}
} else {
attempts++;
if (attempts > 100) {
clearInterval(reconnect);
console.log(`failed to get serial connection, gave up after 10 seconds`);
gui_log(i18n.getMessage('serialPortOpenFail'));
}
}
// wait for the device to reboot
setTimeout(function() {
gui_log(i18n.getMessage('deviceReady'));
}, 2000);
if (callback) {
callback();
}
}