common/Ui/Common.UI/ViewModels/SIRTBroadcastersVM.cs
2026-04-23 17:50:07 +02:00

64 lines
1.9 KiB
C#

namespace Common.UI.ViewModels
{
using Common.UI.Infrastructure;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
public class SIRTBroadcastersVM : VM<SIRTBroadcastersVM>
{
public SIRTBroadcastersVM()
{
this.Connections = new ObservableCollection<SIRTConnectionVM>();
this.AddSIRTsCommand = new Command(this.AddSIRTs);
this.AddSIRTs();
}
public ICommand AddSIRTsCommand { get; }
public ObservableCollection<SIRTConnectionVM> Connections { get; }
private void AddSIRTs()
{
_ = Task.Run(() =>
{
this.InvokeAsync(this.Connections.Clear);
var portNames = Appsettings
.GetSetting("SIRTComPorts")
?.Split(new[] { ';', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
?.Select(x => x?.Trim())
?.ToArray() ?? new string[0];
foreach (var portName in portNames)
{
AppHost.SIRTHub.Close(portName);
if (AppHost.SIRTHub.TryOpen(portName, out var hexId, out var frequency))
{
this.InvokeAsync(() => this.Connections.Add(new SIRTConnectionVM(portName, hexId, frequency.ToString())));
}
}
});
}
protected override void ViewModel_Dispose()
{
var portNames = Appsettings
.GetSetting("SIRTComPorts")
?.Split(new[] { ';', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
?.Select(x => x?.Trim())
?.ToArray() ?? new string[0];
foreach (var portName in portNames)
{
AppHost.SIRTHub.Close(portName);
}
}
}
}