Files
laatzen/Common/OMNIPruefung/ViewModels/Content/COMContentVM.cs
T
2023-12-05 10:46:06 +01:00

179 lines
4.6 KiB
C#

namespace OMNIPruefung.ViewModels.Content
{
using System.Threading;
using System.Threading.Tasks;
using OmniPlus;
using OMNIPruefung.ViewModels.Base;
using OMNIPruefung.ViewModels.Features;
using OMNIPruefung.ViewModels.Options;
public class COMContentVM : ContentVM
{
private OmniPlusConnection connection;
private CancellationTokenSource cancellationTokenSource;
private Task currentTask;
public COMContentVM() : base()
{
this.Meter = new OMNIPlusVM();
this.Option = new COMOptionVM(this);
this.Option.OptionSelected += this.NotifyOptionSelected;
}
private int progress;
public int Progress
{
get => this.progress;
set
{
this.progress = value;
this.NotifyPropertyChanged(nameof(this.Progress));
}
}
private bool running;
public bool Running
{
get => this.running;
set
{
this.running = value;
this.NotifyPropertyChanged(nameof(this.Running));
}
}
private bool connected;
public bool Connected
{
get => this.connected;
set
{
this.connected = value;
this.NotifyPropertyChanged(nameof(this.Connected));
}
}
private bool visible;
public bool Visible
{
get => this.visible;
set
{
this.visible = value;
this.NotifyPropertyChanged(nameof(this.Visible));
}
}
private string state;
public string State
{
get => this.state;
set
{
this.state = value;
this.NotifyPropertyChanged(nameof(this.State));
}
}
private int nr;
public int Nr
{
get => this.nr;
set
{
this.nr = value;
this.NotifyPropertyChanged(nameof(this.Nr));
}
}
private string portName;
public string PortName
{
get => this.portName;
set
{
this.portName = value;
this.connection = new OmniPlusConnection(value);
this.NotifyPropertyChanged(nameof(this.Nr));
}
}
private bool exists;
public bool Exists
{
get => this.exists;
set
{
this.exists = value;
this.NotifyPropertyChanged(nameof(this.Exists));
}
}
public OMNIPlusVM Meter { get; }
internal void CancelExecution()
{
this.cancellationTokenSource?.Cancel();
this.State = this.Header;
}
internal Task PrepareConnection()
{
this.cancellationTokenSource?.Cancel();
this.cancellationTokenSource = new CancellationTokenSource();
this.currentTask = Task.Factory
.StartNew(() =>
{
this.connection = new OmniPlusConnection(this.PortName);
this.Connected = this.connection.ConnectIrda();
this.State = $"Mit IRDA verbunden ...";
this.Running = this.Connected;
}, this.cancellationTokenSource.Token);
return this.currentTask;
}
internal Task ReadSystemParameters()
{
this.cancellationTokenSource?.Cancel();
this.cancellationTokenSource = new CancellationTokenSource();
this.currentTask = Task.Factory
.StartNew(() =>
{
if (this.Connected)
{
this.Meter.SysParameters = this.connection.ViewSystemParameters();
this.Header = this.Meter.SysParameters.BuildInfo;
this.State = "Systemeigenschaften eingelesen";
this.Progress += 10;
}
else
{
this.State = $"Lesen der Systemeigenschaften fehlgeschlagen";
}
}, this.cancellationTokenSource.Token);
return this.currentTask;
}
public override void Dispose()
{
this.cancellationTokenSource?.Cancel();
this.connection?.Disconnect();
}
}
}