From 5d57764517052cb81a4b4c8ad501f20278b0af04 Mon Sep 17 00:00:00 2001 From: Paul-Winpenny <92634321+Paul-Winpenny@users.noreply.github.com> Date: Mon, 28 Oct 2024 19:10:35 +0000 Subject: [PATCH] Update ConnectionPageViewModel.cs --- .../ViewModels/ConnectionPageViewModel.cs | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs b/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs index 36910486..ab467fd2 100644 --- a/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs +++ b/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs @@ -449,21 +449,22 @@ namespace RobobinApp.ViewModels catch (Exception ex) { Debug.WriteLine($"Read attempt {attempt} failed: {ex.Message}"); - if (attempt == retryCount) + + // Attempt to reconnect after a failed read + if (await TryReconnect()) { - Debug.WriteLine("Max retry attempts reached for reading. Attempting reconnection..."); - if (await TryReconnect()) - { - Debug.WriteLine("Reconnected successfully. Retrying read operation."); - return await ReadOperationAsync(retryCount); // Retry after reconnection - } - Debug.WriteLine("Reconnection failed. Returning failure."); - return "Failure"; + Debug.WriteLine("Reconnected successfully. Retrying read operation."); + continue; // Continue to the next attempt after reconnect } - await Task.Delay(1000); + + Debug.WriteLine("Reconnection failed. Returning failure."); + return "Failure"; } + + // Wait before the next attempt + await Task.Delay(1000); } - return "Failure"; + return "Failure"; // This will only be reached if all attempts failed without reconnection } private async Task<string> WriteOperationAsync(string command, string arguments = "", int retryCount = 3) @@ -476,7 +477,7 @@ namespace RobobinApp.ViewModels string joinedData = $"{command}:{arguments}"; byte[] data = Encoding.UTF8.GetBytes(joinedData); - Debug.WriteLine($"WRITE COMMAND {joinedData}, UUID: {WriteCharacteristic.Uuid}"); + Debug.WriteLine($"WRITE COMMAND {joinedData} UUID: {WriteCharacteristic.Uuid}"); for (int attempt = 1; attempt <= retryCount; attempt++) { @@ -489,21 +490,22 @@ namespace RobobinApp.ViewModels catch (Exception ex) { Debug.WriteLine($"Write attempt {attempt} failed: {ex.Message}"); - if (attempt == retryCount) + + // Attempt to reconnect after a failed write + if (await TryReconnect()) { - Debug.WriteLine("Max retry attempts reached for writing. Attempting reconnection..."); - if (await TryReconnect()) - { - Debug.WriteLine("Reconnected successfully. Retrying write operation."); - return await WriteOperationAsync(command, arguments, retryCount); // Retry after reconnection - } - Debug.WriteLine("Reconnection failed. Returning failure."); - return "Failure"; + Debug.WriteLine("Reconnected successfully. Retrying write operation."); + continue; // Continue to the next attempt after reconnect } - await Task.Delay(1000); + + Debug.WriteLine("Reconnection failed. Returning failure."); + return "Failure"; } + + // Wait before the next attempt + await Task.Delay(1000); } - return "Failure"; + return "Failure"; // This will only be reached if all attempts failed without reconnection } private async Task<bool> TryReconnect() @@ -587,6 +589,7 @@ namespace RobobinApp.ViewModels WriteCharacteristic = null; ReadWriteService = null; IsWifiNetworkSelectionVisible = false; + ScanDevices(); } -- GitLab