172 lines
4.9 KiB
C#
172 lines
4.9 KiB
C#
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 : ViewModel<SIRTConnectionVM>
|
|
{
|
|
private static int count;
|
|
|
|
private readonly SIRTBroadcaster connection;
|
|
|
|
public SIRTConnectionVM(SIRTBroadcaster connection)
|
|
{
|
|
this.Id = ++count;
|
|
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<SIRTConnectionVM> Removed;
|
|
|
|
public int Id { get; }
|
|
|
|
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.InvokeAsync(() =>
|
|
{
|
|
this.DisconnectEnabled = false;
|
|
this.ReconnectEnabled = false;
|
|
this.RemoveEnabled = false;
|
|
this.IsRunning = null;
|
|
});
|
|
|
|
Task.Run((Action)this.connection.Close);
|
|
}
|
|
|
|
private void ReconnectCommand_Handler()
|
|
{
|
|
this.InvokeAsync(() =>
|
|
{
|
|
this.DisconnectEnabled = false;
|
|
this.ReconnectEnabled = false;
|
|
this.RemoveEnabled = false;
|
|
this.IsRunning = null;
|
|
});
|
|
|
|
Task.Run((Action)this.connection.Open);
|
|
}
|
|
|
|
private void SIRTBroadcaster_Activated()
|
|
{
|
|
this.InvokeAsync(() =>
|
|
{
|
|
this.DisconnectEnabled = true;
|
|
this.ReconnectEnabled = false;
|
|
this.RemoveEnabled = true;
|
|
this.IsRunning = "Activated";
|
|
});
|
|
|
|
this.UpdateState(this.connection);
|
|
}
|
|
|
|
private void SIRTBroadcaster_Connected()
|
|
{
|
|
this.InvokeAsync(() =>
|
|
{
|
|
this.DisconnectEnabled = true;
|
|
this.ReconnectEnabled = false;
|
|
this.RemoveEnabled = true;
|
|
this.IsRunning = "Connected";
|
|
});
|
|
|
|
this.UpdateState(this.connection);
|
|
}
|
|
|
|
private void SIRTBroadcaster_Disconnected()
|
|
{
|
|
this.InvokeAsync(() =>
|
|
{
|
|
this.DisconnectEnabled = false;
|
|
this.ReconnectEnabled = true;
|
|
this.RemoveEnabled = true;
|
|
this.IsRunning = "Disconnected";
|
|
});
|
|
|
|
this.UpdateState(this.connection);
|
|
}
|
|
|
|
private void UpdateState(SIRTBroadcaster connection)
|
|
{
|
|
this.InvokeAsync(() =>
|
|
{
|
|
this.PortName = connection.COMPort;
|
|
this.HexId = connection.HexId;
|
|
this.Frequency = $"{connection.Frequency}MHz";
|
|
this.DisconnectEnabled = this.disconnectEnabled;
|
|
this.ReconnectEnabled = this.reconnectEnabled;
|
|
this.RemoveEnabled = this.removeEnabled;
|
|
this.IsRunning = this.running;
|
|
});
|
|
}
|
|
}
|
|
}
|