eregister init and finit
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
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 : BaseViewModel<SIRTBroadcasterVM>
|
||||
{
|
||||
private readonly SIRTBroadcaster broadcaster;
|
||||
private readonly ManagementEventWatcher comWatcher;
|
||||
|
||||
public SIRTBroadcasterVM()
|
||||
{
|
||||
this.broadcaster = AppHost.SIRTBroadcaster;
|
||||
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.CreateConnection(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.RemoveConnections();
|
||||
this.InvokeAsync(this.Connections.Clear);
|
||||
this.UpdatePortNames();
|
||||
});
|
||||
}
|
||||
|
||||
private void RemoveSIRTConnectionVM(SIRTConnectionVM connectionVM)
|
||||
{
|
||||
this.ComPortsEnabled = false;
|
||||
this.ClearEnabled = false;
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
this.broadcaster.RemoveConnection(connectionVM.PortName);
|
||||
})
|
||||
.ContinueWith(_ =>
|
||||
{
|
||||
this.InvokeAsync(() =>
|
||||
{
|
||||
this.Connections.Remove(connectionVM);
|
||||
});
|
||||
|
||||
this.UpdatePortNames();
|
||||
});
|
||||
}
|
||||
|
||||
private void AddSIRTs()
|
||||
{
|
||||
var portNames = Appsettings
|
||||
.Get("SIRTComPortNames")
|
||||
?.Split(new[] { ';', ',', ' ' })
|
||||
?.Select(x => x?.Trim())
|
||||
?.ToArray() ?? new string[0];
|
||||
|
||||
portNames = SerialPort
|
||||
.GetPortNames()
|
||||
.Intersect(portNames)
|
||||
.ToArray();
|
||||
|
||||
foreach (var portName in portNames)
|
||||
{
|
||||
var connection = this.broadcaster.CreateConnection(portName);
|
||||
var sirtVM = new SIRTConnectionVM(connection);
|
||||
this.Connections.Add(sirtVM);
|
||||
this.ClearEnabled = true;
|
||||
|
||||
Thread.Sleep(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;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user