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

437 lines
13 KiB
C#

namespace OMNIPruefung.ViewModels.Content
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
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 readonly Dictionary<OmniPlusTestPoint, OmniPlusTest> results
= new Dictionary<OmniPlusTestPoint, OmniPlusTest>();
private OmniPlusConnection connection;
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.PortName));
}
}
private string testResults;
public string TestResults
{
get => this.testResults;
set
{
this.testResults = value;
this.NotifyPropertyChanged(nameof(this.TestResults));
}
}
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 InitializeAsync(CancellationToken cancellationToken)
{
cancellationToken.Register(this.Dispose);
void Initialize()
{
this.results.Clear();
this.Header = $"{this.PortName}";
this.State = this.Header;
this.State = $"{this.Header} - wird ermittelt ...";
try
{
this.Connected = this.connection.Connected;
if (!this.Connected)
{
this.Connected = this.connection.ConnectIrda();
this.Running = this.Connected;
}
if (this.IsRunning())
{
this.State = "Mit IRDA verbunden";
}
else
{
this.connection.Disconnect();
this.Header = $"({this.PortName} - nicht verbunden!)";
this.State = this.Header;
}
}
catch (Exception e)
{
this.connection.Disconnect();
this.Header = $"({this.PortName} - nicht vorhanden!)";
this.State = this.Header;
}
}
return Task.Factory.StartNew(Initialize, cancellationToken);
}
internal Task ReadSystemParametersAsync(CancellationToken cancelationToken)
{
cancelationToken.Register(this.Dispose);
void ReadSystemParameters()
{
if (this.IsRunning())
{
var response = this.connection.ViewSystemParameters();
this.Meter.Add(response);
this.Header = response.BuildInfo.Value;
this.Running = response.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 SetInputVolumeAsync(OmniPlusType defaults, CancellationToken cancelationToken)
{
cancelationToken.Register(this.Dispose);
void SetInputVolume()
{
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 SetReferenceVolumeAsync(OmniPlusTestPoint pp, double referenceV, CancellationToken cancellationToken)
{
cancellationToken.Register(this.Dispose);
void SetReferenceVolume()
{
if (this.results.ContainsKey(pp))
{
this.results[pp].VolumeMessured = referenceV;
}
}
return Task.Factory.StartNew(SetReferenceVolume, cancellationToken);
}
internal Task SaveInspectionAsync(CancellationToken cancellationToken)
{
cancellationToken.Register(this.Dispose);
void SaveInspection()
{
if (!this.IsRunning())
{
return;
}
var resultsBuilder = new StringBuilder();
resultsBuilder.AppendLine($"GPM\t"
+ $"m³/h\t"
+ $"ml/R\t"
+ $"R(soll)\t"
+ $"R(ist)\t"
+ $"T(soll)\t"
+ $"T(ist)\t"
+ $"m³(ist)\t"
+ $"Diff %\t"
+ $"Factor %\t"
+ $"Min\t"
+ $"Max\t");
foreach (var kvp in this.results)
{
var pruefpunkt = kvp.Key;
var ergebnis = kvp.Value;
resultsBuilder.AppendLine($"{pruefpunkt.GalPerMin:0.00}\t"
+ $"{pruefpunkt.VolumeM3H:0.00}\t"
+ $"{pruefpunkt.MlPerRotation:0.00}\t"
+ $"{pruefpunkt.Rotations}\t"
+ $"{ergebnis.Rotations}\t"
+ $"{pruefpunkt.Duration}\t"
+ $"{ergebnis.Duration}\t"
+ $"{ergebnis.VolumeMessured}\t"
+ $"{ergebnis.DifferencePercentage:0.00}%\t"
+ $"{this.GetCorrectionFactor():0.00}%\t"
+ $"{pruefpunkt.MinDiff:0.00}\t"
+ $"{pruefpunkt.MaxDiff:0.00}\t");
}
this.TestResults = resultsBuilder.ToString();
try
{
File.WriteAllText($"{this.Header}_{DateTime.Now:yyyyMMddHHmmss}.csv", this.TestResults);
}
catch (Exception e)
{
this.State = e.ToString();
}
}
return Task.Factory.StartNew(SaveInspection, cancellationToken);
}
internal Task StartPPCalibrationAsync(OmniPlusTestPoint pp, CancellationToken cancellationToken)
{
cancellationToken.Register(this.Dispose);
void StartPPCalibration()
{
this.results[pp] = new OmniPlusTest();
//if (this.IsRunning())
//{
// var rotations = pp.Rotations;
// var duration = pp.Duration;
// var activationResponse = connection.StartChamberCalibration(pp.Rotations, pp.Duration);
// this.Running = activationResponse.Succeeded && activationResponse.Control == ChamberCalibrationResponse.TEST_ACTIVATED;
// 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 Task StopPPCalibrationAsync(OmniPlusTestPoint pp, byte[] response, CancellationToken cancellationToken)
{
cancellationToken.Register(this.Dispose);
void StopPPCalibration()
{
if (this.IsRunning())
{
// var calibrationResult = connection.ViewChamberCalibration();
var calibrationResult = new ChamberCalibrationResponse(response);
this.results[pp].Rotations = calibrationResult.Rotations;
this.results[pp].Duration = calibrationResult.Duration;
this.results[pp].MeterVolume = pp.VolumeM3(this.results[pp]);
this.Running = calibrationResult.Succeeded && calibrationResult.Control == ChamberCalibrationResponse.TEST_COMPLETED;
if (this.Running)
{
this.State = $"PP {pp.Rotations}/{pp.Duration} (rotations/seconds) wurde durchgeführt.";
}
else
{
this.State = $"PP {pp.Rotations}/{pp.Duration} fehlgeschlagen: {(int)calibrationResult.Control} - {calibrationResult.Control}";
}
}
this.Progress += 10;
}
return Task.Factory.StartNew(StopPPCalibration, cancellationToken);
}
internal double GetCorrectionFactor()
{
var correction = this.results
.OrderBy(x => x.Key.GalPerMin)
.Select(x => x.Value.DifferencePercentage)
.ToArray()
.GetCorrectionFactor();
this.State = $"Korekturfaktor: {correction:0.00} %";
return correction;
}
public override void Dispose()
{
try
{
this.SaveInspectionAsync(CancellationToken.None).Wait();
this.connection?.Disconnect();
}
catch (Exception e)
{
this.State = e.Message;
}
try
{
this.results.Clear();
}
catch (Exception e)
{
this.State = e.Message;
}
}
private bool IsRunning()
{
if (!this.Running)
{
return false;
}
this.Connected = this.connection.Connected;
return this.Connected;
}
}
}