Skip to content
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

Fix #356: Wi-Fi connection can only be established on the second attempt #357

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions scripts/ruuvi_gw_http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]

Expand Down Expand Up @@ -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''
Expand Down Expand Up @@ -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)

Expand Down
21 changes: 17 additions & 4 deletions src/page_wifi_connection.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`))
Expand Down
Loading