Skip to content

Commit

Permalink
Fix reboot (#3986)
Browse files Browse the repository at this point in the history
* Fix reboot

* Remove runonce and add callback for cli

* Couldn't resist

* Refactoring

* Forgot callback

* deviceReady

* Prevent callback being called twice
  • Loading branch information
haslinghuis authored May 28, 2024
1 parent 0fa76eb commit a6e3761
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 51 deletions.
1 change: 0 additions & 1 deletion src/js/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const GUI_MODES = {

class GuiControl {
constructor() {
this.auto_connect = false;
this.connecting_to = false;
this.connected_to = false;
this.connect_lock = false;
Expand Down
2 changes: 1 addition & 1 deletion src/js/msp/MSPHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2801,7 +2801,7 @@ MspHelper.prototype.writeConfiguration = function(reboot, callback) {
console.log('Configuration saved to EEPROM');
if (reboot) {
GUI.tab_switch_cleanup(function() {
MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, reinitializeConnection);
return reinitializeConnection(callback);
});
}
if (callback) {
Expand Down
6 changes: 1 addition & 5 deletions src/js/port_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ PortHandler.setShowManualMode = function (showManualMode) {
PortHandler.reinitialize = function () {
this.initialPorts = false;


if (this.usbCheckLoop) {
clearTimeout(this.usbCheckLoop);
}

this.showAllSerialDevices = getConfig('showAllSerialDevices').showAllSerialDevices;

this.check(); // start listening, check after TIMEOUT_CHECK ms
Expand Down Expand Up @@ -92,6 +87,7 @@ PortHandler.check_serial_devices = function () {
self.selectActivePort();
self.initialPorts = {...self.currentPorts};
GUI.updateManualPortVisibility();
self.detectPort();
} else {
self.removePort();
self.detectPort();
Expand Down
61 changes: 17 additions & 44 deletions src/js/serial_backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
1 change: 1 addition & 0 deletions src/js/webSerial.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class WebSerial extends EventTarget {

const doCleanup = async () => {
if (this.reader) {
// this.reader.cancel();
this.reader.releaseLock();
this.reader = null;
}
Expand Down

0 comments on commit a6e3761

Please sign in to comment.