namespace Common.UI.ViewModels { using Common.Hardware.SIRT; using Common.UI.Infrastructure; using System; using System.Threading.Tasks; using System.Windows.Input; internal class SIRTConnectionVM : BaseViewModel { private readonly SIRTConnection connection; public SIRTConnectionVM(SIRTConnection connection) { this.connection = connection; this.connection.Activated += this.SIRTBroadcaster_Activated; this.connection.Connected += this.SIRTBroadcaster_Connected; this.connection.Disconnected += this.SIRTBroadcaster_Disconnected; this.RemoveCommand = new Command(this.RemoveCommand_Handler); this.DisconnectCommand = new Command(this.DisconnectCommand_Handler); this.ReconnectCommand = new Command(this.ReconnectCommand_Handler); this.UpdateState(this.connection); } public event Action Removed; private string portName; public string PortName { get => this.portName; set => this.Set(vm => vm.portName = value); } private string hexId; public string HexId { get => this.hexId; set => this.Set(vm => vm.hexId = value); } private string frequency; public string Frequency { get => this.frequency; set => this.Set(vm => vm.frequency = value); } private string running; public string IsRunning { get => this.running; set => this.Set(vm => vm.running = value); } private bool disconnectEnabled; public bool DisconnectEnabled { get => this.disconnectEnabled; set => this.Set(vm => vm.disconnectEnabled = value); } private bool reconnectEnabled; public bool ReconnectEnabled { get => this.reconnectEnabled; set => this.Set(vm => vm.reconnectEnabled = value); } private bool removeEnabled; public bool RemoveEnabled { get => this.removeEnabled; set => this.Set(vm => vm.removeEnabled = value); } public ICommand DisconnectCommand { get; } public ICommand ReconnectCommand { get; } public ICommand RemoveCommand { get; } private void RemoveCommand_Handler() => this.Removed?.Invoke(this); private void DisconnectCommand_Handler() { this.DisconnectEnabled = false; this.ReconnectEnabled = false; this.RemoveEnabled = false; this.IsRunning = null; Task.Run(() => { this.connection.Close(); this.UpdateState(this.connection); }); } private void ReconnectCommand_Handler() { this.DisconnectEnabled = false; this.ReconnectEnabled = false; this.RemoveEnabled = false; this.IsRunning = null; Task.Run(() => { this.connection.Close(); this.UpdateState(this.connection); this.connection.Open(); }); } private void SIRTBroadcaster_Activated() { this.DisconnectEnabled = true; this.ReconnectEnabled = false; this.RemoveEnabled = true; this.IsRunning = "Activated"; this.UpdateState(this.connection); } private void SIRTBroadcaster_Connected() { this.DisconnectEnabled = true; this.ReconnectEnabled = false; this.RemoveEnabled = true; this.IsRunning = "Connected"; this.UpdateState(this.connection); } private void SIRTBroadcaster_Disconnected() { this.DisconnectEnabled = false; this.ReconnectEnabled = true; this.RemoveEnabled = true; this.IsRunning = "Disconnected"; this.UpdateState(this.connection); } private void UpdateState(SIRTConnection connection) { this.PortName = connection.COMPort; this.HexId = connection.HexId; this.Frequency = $"{connection.Frequency}MHz"; } } }