Skip to content

Commit

Permalink
Fix #356: Wi-Fi connection can only be established on the second attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSomeMan committed Dec 5, 2023
1 parent 895b074 commit ea942ea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
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

0 comments on commit ea942ea

Please sign in to comment.