Skip to content

Commit 5b1845f

Browse files
authored
Merge pull request #2838 from haslinghuis/dynamic_timeout
Make serial connection timeout dynamic
2 parents ea6958f + f8371af commit 5b1845f

File tree

4 files changed

+23
-47
lines changed

4 files changed

+23
-47
lines changed

src/js/port_handler.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,7 @@ PortHandler.detectPort = function(currentPorts) {
190190
if (GUI.auto_connect && !GUI.connecting_to && !GUI.connected_to) {
191191
// start connect procedure. We need firmware flasher protection over here
192192
if (GUI.active_tab !== 'firmware_flasher') {
193-
let connectionTimeout = 100;
194-
ConfigStorage.get('connectionTimeout', function (result) {
195-
if (result.connectionTimeout) {
196-
connectionTimeout = result.connectionTimeout;
197-
}
198-
GUI.timeout_add('auto-connect_timeout', function () {
199-
$('div#header_btns a.connect').click();
200-
}, connectionTimeout); // timeout so bus have time to initialize after being detected by the system
201-
});
193+
$('div#header_btns a.connect').click();
202194
}
203195
}
204196
// trigger callbacks

src/js/serial_backend.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,8 @@ function update_dataflash_global() {
830830
function reinitializeConnection(originatorTab, callback) {
831831

832832
// Close connection gracefully if it still exists.
833+
const previousTimeStamp = connectionTimestamp;
834+
833835
if (serial.connectionId) {
834836
if (GUI.connected_to || GUI.connecting_to) {
835837
$('a.connect').trigger('click');
@@ -840,19 +842,25 @@ function reinitializeConnection(originatorTab, callback) {
840842

841843
GUI.log(i18n.getMessage('deviceRebooting'));
842844

843-
let connectionTimeout = 200;
844-
const result = ConfigStorage.get('connectionTimeout');
845-
846-
if (result.connectionTimeout) {
847-
connectionTimeout = result.connectionTimeout;
845+
let attempts = 0;
846+
const reconnect = setInterval(waitforSerial, 100);
847+
848+
function waitforSerial() {
849+
if (connectionTimestamp !== previousTimeStamp && CONFIGURATOR.connectionValid) {
850+
console.log(`Serial connection available after ${attempts / 10} seconds`);
851+
clearInterval(reconnect);
852+
MSP.promise(MSPCodes.MSP_STATUS).then(() => {
853+
GUI.log(i18n.getMessage('deviceReady'));
854+
originatorTab.initialize(false, $('#content').scrollTop());
855+
callback?.();
856+
});
857+
} else {
858+
attempts++;
859+
if (attempts > 100) {
860+
clearInterval(reconnect);
861+
console.log(`failed to get serial connection, gave up after 10 seconds`);
862+
GUI.log(i18n.getMessage('serialPortOpenFail'));
863+
}
864+
}
848865
}
849-
850-
setTimeout(() => {
851-
MSP.send_message(MSPCodes.MSP_STATUS, false, false, () => {
852-
GUI.log(i18n.getMessage('deviceReady'));
853-
originatorTab.initialize(false, $('#content').scrollTop());
854-
});
855-
856-
callback?.();
857-
}, connectionTimeout);
858866
}

src/js/tabs/options.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ options.initialize = function (callback) {
1414
TABS.options.initCheckForConfiguratorUnstableVersions();
1515
TABS.options.initAnalyticsOptOut();
1616
TABS.options.initCliAutoComplete();
17-
TABS.options.initAutoConnectConnectionTimeout();
1817
TABS.options.initShowAllSerialDevices();
1918
TABS.options.initShowVirtualMode();
2019
TABS.options.initCordovaForceComputerUI();
@@ -123,18 +122,6 @@ options.initCliAutoComplete = function () {
123122
}).change();
124123
};
125124

126-
options.initAutoConnectConnectionTimeout = function () {
127-
ConfigStorage.get('connectionTimeout', function (result) {
128-
if (result.connectionTimeout) {
129-
$('#connectionTimeoutSelect').val(result.connectionTimeout);
130-
}
131-
$('#connectionTimeoutSelect').on('change', function () {
132-
const value = parseInt($(this).val());
133-
ConfigStorage.set({'connectionTimeout': value});
134-
});
135-
});
136-
};
137-
138125
options.initShowAllSerialDevices = function() {
139126
const showAllSerialDevicesElement = $('div.showAllSerialDevices input');
140127
ConfigStorage.get('showAllSerialDevices', result => {

src/tabs/options.html

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@
3535
</div>
3636
<span class="freelabel" i18n="cliAutoComplete"></span>
3737
</div>
38-
<div class="connectionTimeout margin-bottom">
39-
<select id="connectionTimeoutSelect">
40-
<option value="100">100</option>
41-
<option value="500">500</option>
42-
<option value="1000">1000</option>
43-
<option value="1500">1500</option>
44-
<option value="2500">2500</option>
45-
<option value="5000">5000</option>
46-
</select>
47-
<span class="freelabel" i18n="connectionTimeout"></span>
48-
</div>
4938
<div class="showAllSerialDevices margin-bottom">
5039
<div>
5140
<input type="checkbox" class="toggle" />

0 commit comments

Comments
 (0)