Skip to content

Commit

Permalink
add pulsing connect button based on ping status
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecarr committed Mar 3, 2025
1 parent 2b56035 commit 5e57ded
Show file tree
Hide file tree
Showing 9 changed files with 545 additions and 180 deletions.
21 changes: 18 additions & 3 deletions OpenIPC_Config/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ public class App : Application

public static string OSType { get; private set; }



#if DEBUG
private bool _ShouldCheckForUpdates = false;
#else
private bool _ShouldCheckForUpdates = true;
#endif

private void DetectOsType()
{
Expand Down Expand Up @@ -62,6 +68,7 @@ private IConfigurationRoot LoadConfiguration()
Log.Information($"Default appsettings.json created at {configPath}");
//}

Console.WriteLine($"Loading configuration from: {configPath}");
// Build configuration
var configuration = new ConfigurationBuilder()
.AddJsonFile(configPath, false, true)
Expand Down Expand Up @@ -192,6 +199,8 @@ private async Task CheckForUpdatesAsync()
var configPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
Assembly.GetExecutingAssembly().GetName().Name, "appsettings.json");

Console.WriteLine($"Loading configuration from: {configPath}");

// Create an IConfiguration instance
var configuration = new ConfigurationBuilder()
.AddJsonFile(configPath, false, true)
Expand Down Expand Up @@ -269,7 +278,7 @@ private void ConfigureServices(IServiceCollection services, IConfiguration confi
private static void RegisterViewModels(IServiceCollection services)
{
// Register ViewModels
services.AddTransient<MainViewModel>();
services.AddSingleton<MainViewModel>();

// Register tab ViewModels as singletons
services.AddSingleton<GlobalSettingsViewModel>();
Expand Down Expand Up @@ -327,7 +336,12 @@ private JObject createDefaultAppSettings()
new JProperty("WriteTo",
new JArray(
new JObject(
new JProperty("Name", "Console")
new JProperty("Name", "Console"),
new JProperty("Args", // Add Args here for Console
new JObject(
new JProperty("outputTemplate", "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext}: {Message:lj}{NewLine}{Exception}")
)
)
),
new JObject(
new JProperty("Name", "File"),
Expand All @@ -337,7 +351,8 @@ private JObject createDefaultAppSettings()
new JProperty("rollingInterval",
"Day"),
new JProperty("retainedFileCountLimit",
"5")
"5"),
new JProperty("outputTemplate", "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext}: {Message:lj}{NewLine}{Exception}")
)
)
)
Expand Down
1 change: 1 addition & 0 deletions OpenIPC_Config/Models/OpenIPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum FileType
public const string OpenIPCBuilderGitHubApiUrl = "https://api.github.com/repos/OpenIPC/builder/releases/latest";
public const string MajesticFileLoc = "/etc/majestic.yaml";
public const string WfbConfFileLoc = "/etc/wfb.conf";
public const string WfbYamlFileLoc = "/etc/wfb.yaml";
public const string TelemetryConfFileLoc = "/etc/telemetry.conf";

// Radxa files
Expand Down
82 changes: 82 additions & 0 deletions OpenIPC_Config/Services/PingService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Concurrent;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;
using Serilog;

namespace OpenIPC_Config.Services;

public class PingService
{
private static PingService _instance;
private static readonly object _lock = new object();

private readonly SemaphoreSlim _pingSemaphore = new SemaphoreSlim(1, 1);
private readonly TimeSpan _defaultTimeout = TimeSpan.FromMilliseconds(500);
private readonly ILogger _logger;

// Private constructor for singleton pattern
private PingService(ILogger logger)
{
_logger = logger;
}

// Singleton instance getter
public static PingService Instance(ILogger logger)
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = new PingService(logger);
}
}
}
return _instance;
}

// Ping method with default timeout
public async Task<PingReply> SendPingAsync(string ipAddress)
{
return await SendPingAsync(ipAddress, (int)_defaultTimeout.TotalMilliseconds);
}

// Ping method with custom timeout
public async Task<PingReply> SendPingAsync(string ipAddress, int timeout)
{
// Log which IP is being pinged
_logger.Debug($"Attempting to ping IP: {ipAddress}");

if (await _pingSemaphore.WaitAsync(timeout))
{
try
{
using (var ping = new Ping())
{
return await ping.SendPingAsync(ipAddress, timeout);
}
}
finally
{
// Release the semaphore when done
_pingSemaphore.Release();
}
}
else
{
_logger.Warning("Timeout waiting to acquire ping semaphore for {IpAddress}", ipAddress);

// Since we can't create a PingReply directly, throw a meaningful exception instead
throw new TimeoutException($"Ping operation to {ipAddress} was delayed due to concurrent requests");
}
}

// Dispose method to clean up all resources
public void Dispose()
{
_pingSemaphore.Dispose();
}
}
2 changes: 2 additions & 0 deletions OpenIPC_Config/ViewModels/FirmwareTabViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,10 @@ private void UpdateCanExecuteCommands()
OnPropertyChanged(nameof(CanUseDropdownsBySoc));
OnPropertyChanged(nameof(CanUseSelectFirmware));


(DownloadFirmwareAsyncCommand as RelayCommand)?.NotifyCanExecuteChanged();
(PerformFirmwareUpgradeAsyncCommand as RelayCommand)?.NotifyCanExecuteChanged();
(SelectFirmwareCommand as RelayCommand)?.NotifyCanExecuteChanged();
}

private async Task<FirmwareData> FetchFirmwareListAsync()
Expand Down
4 changes: 3 additions & 1 deletion OpenIPC_Config/ViewModels/GlobalSettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ private async Task CheckWfbYamlSupport(CancellationToken cancellationToken)
try
{
var cmdResult = await GetIsWfbYamlSupported(cancellationToken);

IsWfbYamlEnabled = bool.TryParse(Utilities.RemoveLastChar(cmdResult?.Result), out var result) && result;

// TODO: check if wfb.yaml exists when all parameters are supported
// https://github.com/svpcom/wfb-ng/wiki/Drone-auto-provisioning
IsWfbYamlEnabled = false;
//IsWfbYamlEnabled = false;

Logger.Debug($"WFB YAML support status: {IsWfbYamlEnabled}");
}
Expand Down
Loading

0 comments on commit 5e57ded

Please sign in to comment.