175 lines
5.2 KiB
C#
175 lines
5.2 KiB
C#
namespace Common.UI.ViewModels
|
|
{
|
|
using Common.Hardware.Ports;
|
|
using Common.Hardware.SIRT;
|
|
using Common.UI.Infrastructure;
|
|
|
|
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Management;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Input;
|
|
|
|
internal class SIRTBroadcasterVM : ViewModel<SIRTBroadcasterVM>
|
|
{
|
|
private readonly SIRTFactory broadcaster;
|
|
private readonly ManagementEventWatcher comWatcher;
|
|
|
|
public SIRTBroadcasterVM()
|
|
{
|
|
this.broadcaster = AppHost.GetService<SIRTFactory>();
|
|
this.Connections = new ObservableCollection<SIRTConnectionVM>();
|
|
this.PortNames = new ObservableCollection<string>();
|
|
this.AddSIRTCommand = new Command(this.AddSIRTConnectionCommandHandler);
|
|
this.ClearAllCommand = new Command(this.ClearAllCommandHandler);
|
|
this.comWatcher = new ManagementEventWatcher("SELECT * FROM Win32_DeviceChangeEvent");
|
|
this.comWatcher.EventArrived += (_, args) => this.UpdatePortNames();
|
|
this.comWatcher.Start();
|
|
|
|
this.AddSIRTs();
|
|
this.UpdatePortNames();
|
|
}
|
|
|
|
private bool comPortsEnabled;
|
|
public bool ComPortsEnabled
|
|
{
|
|
get => this.comPortsEnabled;
|
|
set => this.Set(vm => vm.comPortsEnabled = value);
|
|
}
|
|
|
|
private bool addEnabled;
|
|
public bool AddEnabled
|
|
{
|
|
get => this.addEnabled;
|
|
set => this.Set(vm => vm.addEnabled = value);
|
|
}
|
|
|
|
private bool cleanEnabled;
|
|
public bool ClearEnabled
|
|
{
|
|
get => this.cleanEnabled;
|
|
set => this.Set(vm => vm.cleanEnabled = value);
|
|
}
|
|
|
|
private string portName;
|
|
public string PortName
|
|
{
|
|
get => this.portName;
|
|
set
|
|
{
|
|
this.Set(vm => vm.portName = value);
|
|
|
|
this.AddEnabled = !string.IsNullOrWhiteSpace(value);
|
|
}
|
|
}
|
|
|
|
public Action InvokeWhenReady { get; set; }
|
|
|
|
public ICommand AddSIRTCommand { get; }
|
|
|
|
public ICommand ClearAllCommand { get; }
|
|
|
|
public ObservableCollection<string> PortNames { get; }
|
|
|
|
public ObservableCollection<SIRTConnectionVM> Connections { get; }
|
|
|
|
private void AddSIRTConnectionCommandHandler()
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(this.PortName))
|
|
{
|
|
var connection = this.broadcaster.ConnectSIRT(this.PortName);
|
|
var connectionVM = new SIRTConnectionVM(connection);
|
|
connectionVM.Removed += this.RemoveSIRTConnectionVM;
|
|
this.Connections.Add(connectionVM);
|
|
|
|
this.ClearEnabled = true;
|
|
this.PortName = default(string);
|
|
this.UpdatePortNames();
|
|
}
|
|
}
|
|
|
|
private void ClearAllCommandHandler()
|
|
{
|
|
this.ComPortsEnabled = false;
|
|
this.ClearEnabled = false;
|
|
|
|
Task.Run(() =>
|
|
{
|
|
this.broadcaster.CloseConnections();
|
|
this.InvokeAsync(this.Connections.Clear);
|
|
this.UpdatePortNames();
|
|
});
|
|
}
|
|
|
|
private void RemoveSIRTConnectionVM(SIRTConnectionVM connectionVM)
|
|
{
|
|
this.ComPortsEnabled = false;
|
|
this.ClearEnabled = false;
|
|
|
|
Task.Run(() =>
|
|
{
|
|
this.broadcaster.CloseConnection(connectionVM.PortName);
|
|
})
|
|
.ContinueWith(_ =>
|
|
{
|
|
this.InvokeAsync(() =>
|
|
{
|
|
this.Connections.Remove(connectionVM);
|
|
});
|
|
|
|
this.UpdatePortNames();
|
|
});
|
|
}
|
|
|
|
private void AddSIRTs()
|
|
{
|
|
var portNames = Appsettings
|
|
.Get("SIRTComPorts")
|
|
?.Split(new[] { ';', ',', ' ' })
|
|
?.Select(x => x?.Trim())
|
|
?.ToArray() ?? new string[0];
|
|
|
|
portNames = SerialPort
|
|
.GetPortNames()
|
|
.Intersect(portNames)
|
|
.ToArray();
|
|
|
|
Task.Run(async () =>
|
|
{
|
|
foreach (var portName in portNames)
|
|
{
|
|
this.InvokeAsync(() =>
|
|
{
|
|
var connection = this.broadcaster.ConnectSIRT(portName);
|
|
var sirtVM = new SIRTConnectionVM(connection);
|
|
this.Connections.Add(sirtVM);
|
|
this.ClearEnabled = true;
|
|
});
|
|
|
|
await Task.Delay(500);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void UpdatePortNames()
|
|
=> this.InvokeAsync(() =>
|
|
{
|
|
this.PortNames.Clear();
|
|
|
|
var portNames = SerialPort
|
|
.GetPortNames()
|
|
.Except(this.broadcaster.GetPortNames())
|
|
.OrderBy(x => x);
|
|
|
|
foreach (var portName in portNames)
|
|
{
|
|
this.PortNames.Add(portName);
|
|
}
|
|
|
|
this.ComPortsEnabled = true;
|
|
});
|
|
}
|
|
}
|