Skip to content

Commit

Permalink
retry connection if closed signal arrives before connected
Browse files Browse the repository at this point in the history
this hopefully fixes #162
  • Loading branch information
Novakasa committed Sep 26, 2023
1 parent fe8afb0 commit 8961c35
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

### Fixed

- Fix BLEServer connection unsuccessful on ubuntu (https://github.com/Novakasa/brickrail/issues/162)
- Fix emergency stop not triggered due to hub state when control devices is set to "switches" (https://github.com/Novakasa/brickrail/issues/156).
- Prevent scanning the same hub (or same name) twice (https://github.com/Novakasa/brickrail/issues/109).
- Fix issues when trying to assign already used port with a device of different type. User now is warned and asked whether to override or cancel (https://github.com/Novakasa/brickrail/issues/148).
Expand Down
18 changes: 13 additions & 5 deletions brickrail-gui/ble/ble_communicator.gd
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ func start_and_connect_to_process():
status = "connecting"
emit_signal("status_changed")

var err = _client.connect_to_url(websocket_url)
if err != OK:
Logger.error("[%s] Connect error: '%s'" % [logging_module])
set_process(false)
GuiApi.show_error("Unable to initialize connection to BLE Server python process!")
connect_client()

var timer = get_tree().create_timer(15.0)
var result = yield(Await.first_signal_objs([timer, self], ["timeout", "connected"]), "completed")
Expand All @@ -55,8 +51,16 @@ func start_and_connect_to_process():
busy = false
emit_signal("status_changed")
return "Err"
print("connected to ble server")
return "OK"

func connect_client():
var err = _client.connect_to_url(websocket_url)
if err != OK:
Logger.error("[%s] Connect error: '%s'" % [logging_module])
set_process(false)
GuiApi.show_error("Unable to initialize connection to BLE Server python process!")

func disconnect_and_kill_process():
status = "disconnecting"
busy = true
Expand All @@ -72,8 +76,12 @@ func disconnect_and_kill_process():
func _closed(was_clean = false):
Logger.info("[%s] Closed, clean: %s" % [logging_module, was_clean])
if not expect_close:
if busy and not connected:
connect_client()
return
var more_info = "BLE Server was disconnected for some reason.\nIt could have crashed, or the terminal window was closed.\n\nYou can try restarting the BLE Server by pressing 'Connect BLE Server'\nin the hub panel."
GuiApi.show_error("Disconnected from BLE Server python process unexpectedly!", more_info)

expect_close = false
connected=false
busy = false
Expand Down
2 changes: 1 addition & 1 deletion brickrail-gui/brickrail-layouts/ro_4trains.brl
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@
"controller": "technic-yellow",
"port": 1,
"storage": {
"0": 100,
"0": 50,
"1": 600,
"2": 0
}
Expand Down

0 comments on commit 8961c35

Please sign in to comment.