diff --git a/scripts/ruuvi_gw_http_server.py b/scripts/ruuvi_gw_http_server.py index 0f08b4f..5f6462b 100755 --- a/scripts/ruuvi_gw_http_server.py +++ b/scripts/ruuvi_gw_http_server.py @@ -90,6 +90,7 @@ g_gw_unique_id = "00:11:22:33:44:55:66:77" g_flag_access_from_lan = False g_aes_key = None +g_flag_save_wifi_cfg_fails = False RUUVI_AUTH_REALM = 'RuuviGateway' + g_gw_mac[-5:-3] + g_gw_mac[-2:] @@ -674,6 +675,7 @@ def _do_post_connect_json(self): def _do_post_ruuvi_json(self): global g_ruuvi_dict global g_authorized_sessions + global g_flag_save_wifi_cfg_fails req_dict = self._ecdh_decrypt_request_json(g_aes_key) if req_dict is None: resp = b'' @@ -746,6 +748,12 @@ def _do_post_ruuvi_json(self): print(f'Set LAN auth (prev password): {lan_auth_type}, {lan_auth_user}, {lan_auth_pass}') g_authorized_sessions = dict() + if 'use_eth' in g_ruuvi_dict: + if not g_ruuvi_dict['use_eth']: + g_flag_save_wifi_cfg_fails = not g_flag_save_wifi_cfg_fails + if g_flag_save_wifi_cfg_fails: + return + content = '{}' self._write_json_response(content) diff --git a/src/page_wifi_connection.mjs b/src/page_wifi_connection.mjs index a234c30..d5de081 100644 --- a/src/page_wifi_connection.mjs +++ b/src/page_wifi_connection.mjs @@ -131,14 +131,27 @@ export class PageWiFiConnection { } } + async #sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); + } + async #save_network_config_and_connect_to_wifi (wifi_channel, ssid, password) { gui_loading.bodyClassLoadingAdd() let isSuccessful = false + + GwStatus.stopCheckingStatus() + GwAP.stopRefreshingAP() + await Network.waitWhileInProgress() + + this.#gwCfg.wifi_ap_cfg.setWiFiChannel(wifi_channel) + try { + await this.#gwCfg.saveNetworkConfig(this.#auth) + } catch (err) { + // First attempt to save the network config always fails because Wi-Fi channel is changed. + console.log(log_wrap(`First attempt to saveNetworkConfig failed: ${err}`)) + await this.#sleep(3000) + } try { - GwStatus.stopCheckingStatus() - GwAP.stopRefreshingAP() - await Network.waitWhileInProgress() - this.#gwCfg.wifi_ap_cfg.setWiFiChannel(wifi_channel) await this.#gwCfg.saveNetworkConfig(this.#auth) isSuccessful = await networkConnect(ssid, password, this.#auth) console.log(log_wrap(`networkConnect: ${isSuccessful}`))