Skip to content
Snippets Groups Projects
Commit 5d577645 authored by Paul-Winpenny's avatar Paul-Winpenny
Browse files

Update ConnectionPageViewModel.cs

parent 7483fcb1
No related branches found
No related tags found
No related merge requests found
......@@ -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();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment