Files
laatzen/Common/OMNIPruefung/ViewModels/Content/COMContentVM.cs
T

420 lines
13 KiB
C#

namespace OMNIPruefung.ViewModels.Content
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using OmniPlus;
using OmniPlus.Features;
using OMNIPruefung.ViewModels.Base;
using OMNIPruefung.ViewModels.Features;
using OMNIPruefung.ViewModels.Options;
public class COMContentVM : ContentVM
{
private OmniPlusConnection connection;
private readonly Dictionary<OmniPlusTestPoint, OmniPlusTest> results
= new Dictionary<OmniPlusTestPoint, OmniPlusTest>();
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 int AvailableIn { get; set; }
public OMNIPlusVM Meter { get; }
internal void CancelExecution()
{
this.State = "Prüfung abgebrochen.";
this.Connected = false;
this.Running = false;
}
internal Task PrepareConnectionTask(CancellationToken cancelationToken)
{
cancelationToken.Register(this.Dispose);
void PrepareConnection()
{
this.Progress = 0;
if (!this.Connected)
{
this.Connected = this.connection.ConnectIrda();
}
this.Running = this.Connected;
if (this.Running)
{
this.State = $"Vorbereitung läuft ...";
}
else
{
this.State = $"{this.PortName} ist getrennt";
}
}
return Task.Factory.StartNew(PrepareConnection, cancelationToken);
}
internal Task ReadSystemParametersTask(CancellationToken cancelationToken)
{
cancelationToken.Register(this.Dispose);
void ReadSystemParameters()
{
if (!this.Running)
{
return;
}
this.Connected = this.connection.Connected;
if (this.Connected)
{
this.Meter.SystemParameters = this.connection.ViewSystemParameters();
this.Header = this.Meter.SystemParameters.BuildInfo;
this.Running = this.Meter.SystemParameters.IsValid;
if (this.Running)
{
this.State = "Systemeigenschaften gelesen und validiert";
}
else
{
this.State = "Systemeigenschaften konnten nicht gelesen werden";
}
this.Progress += 10;
}
else
{
this.State += $" (Verbindung unterbrochen)";
}
}
return Task.Factory.StartNew(ReadSystemParameters, cancelationToken);
}
internal Task SetInputVolumeTask(OmniPlusType defaults, CancellationToken cancelationToken)
{
cancelationToken.Register(this.Dispose);
void SetInputVolume()
{
if (!this.Running)
{
return;
}
this.Connected = this.connection.Connected;
if (this.Connected)
{
var response = this.connection.Set(defaults.InputVolume);
this.Running = response.Succeeded;
if (this.Running)
{
this.State = "Flow-Eigenschaften konfiguriert";
}
else
{
this.State = $"Flow-Eigenschaften: ({(int)response.Status} - {response.Status})";
}
this.Progress += 10;
}
else
{
this.State += $" (Verbindung unterbrochen)";
}
}
return Task.Factory.StartNew(SetInputVolume, cancelationToken);
}
internal Task InspectPPTask(OmniPlusTestPoint pp, CancellationToken cancelationToken)
{
cancelationToken.Register(this.Dispose);
void InspectPP()
{
if (!this.Running)
{
return;
}
this.Connected = this.connection.Connected;
this.results[pp] = new OmniPlusTest();
var rotations = pp.Rotations;
var duration = pp.Duration;
if (this.Connected)
{
var activationResponse = connection.StartChamberCalibration(rotations, duration);
this.Running = activationResponse.Succeeded;
if (this.Running)
{
var format = $"Prüfung für {rotations}/{duration} (rotations/seconds) Bereit in {{0}} Sekunden";
var _duration = pp.Duration;
while (_duration > 0 && !cancelationToken.IsCancellationRequested)
{
this.State = string.Format(format, _duration--);
Thread.Sleep(1200);
}
this.AvailableIn = _duration;
this.State = "Das Ergebnis wird ermittelt ...";
var calibrationResult = connection.ViewChamberCalibration();
this.Running = calibrationResult.Succeeded;
if (!this.Running)
{
this.State = $"Prüfung für {rotations}/{duration} (rotations/seconds) fehlgeschlagen: ({calibrationResult.Status})";
}
else if (calibrationResult.Control != ChamberCalibrationResponse.TEST_COMPLETED)
{
var controlNr = calibrationResult.Control;
var controlTxt = controlNr == ChamberCalibrationResponse.TEST_ACTIVATED
? nameof(ChamberCalibrationResponse.TEST_ACTIVATED)
: nameof(ChamberCalibrationResponse.TEST_FAILED);
this.Running = false;
this.State = $"Prüfung für {rotations}/{duration} (rotations/seconds) fehlgeschlagen: ({controlNr} - {controlTxt})";
}
else // process calibration
{
this.State = $"Prüfung für {rotations}/{duration} (rotations/seconds) erfolgreich";
this.results[pp].Duration = calibrationResult.Duration;
this.results[pp].Rotations = calibrationResult.Rotations;
}
}
else // not activated
{
this.State = $"Prüfung activierung fehlgeschlagen: ({(int)activationResponse.Status} - {activationResponse.Status})";
}
}
else // not connected or running
{
this.State = $"PP {rotations}/{duration} (rotations/seconds) wurde aufgrund ungültiger Eigenschaften abgebrochen";
}
this.Progress += 10;
}
return Task.Factory.StartNew(InspectPP, cancelationToken);
}
internal Task StopPPCalibrationTask(OmniPlusTestPoint pp, CancellationToken cancellationToken)
{
cancellationToken.Register(this.Dispose);
void StopPPCalibration()
{
if (this.Running && this.Connected)
{
var calibrationResult = connection.ViewChamberCalibration();
this.results[pp].Rotations = calibrationResult.Rotations;
this.results[pp].Duration = calibrationResult.Duration;
this.Running = calibrationResult.Succeeded;
if (this.Running)
{
this.State = $"PP {pp.Rotations}/{pp.Duration} (rotations/seconds) wurde durchgeführt.";
}
else
{
this.State = $"PP {pp.Rotations}/{pp.Duration} (rotations/seconds) fehlgeschlagen: {(int)calibrationResult.Status} - {calibrationResult.Status}";
}
}
this.Progress += 10;
}
return Task.Factory.StartNew(StopPPCalibration, cancellationToken);
}
internal Task StartPPCalibrationTask(OmniPlusTestPoint pp, CancellationToken cancellationToken)
{
cancellationToken.Register(this.Dispose);
void StartPPCalibration()
{
this.results[pp] = new OmniPlusTest();
if (this.Running && this.Connected)
{
var rotations = pp.Rotations;
var duration = pp.Duration;
var activationResponse = connection.StartChamberCalibration(pp.Rotations, pp.Duration);
this.Running = activationResponse.Succeeded;
if (this.Running)
{
this.State = $"PP {rotations}/{duration} (rotations/seconds) wurde gestarted.";
}
else
{
this.State = $"PP {rotations}/{duration} (rotations/seconds): {(int)activationResponse.Status} - {activationResponse.Status}";
}
}
this.Progress += 10;
}
return Task.Factory.StartNew(StartPPCalibration, cancellationToken);
}
internal void SetReferenceVolume(OmniPlusTestPoint pp, double referenceV)
{
if (this.results.ContainsKey(pp))
{
this.results[pp].VolumeMessured = referenceV;
}
}
internal double GetCorrectionFactor()
{
this.Meter.CorrectionFactor = this.results
.OrderBy(x => x.Key.GalPerMin)
.Select(x => x.Value.DifferencePercentage)
.ToArray()
.GetCorrectionFactor();
return this.Meter.CorrectionFactor;
}
public override void Dispose()
{
try
{
this.connection?.Disconnect();
}
catch (Exception e)
{
this.State = e.Message;
}
// TODO: save state
try
{
this.results.Clear();
}
catch (Exception e)
{
this.State = e.Message;
}
}
}
}