Files
laatzen/Common/OMNIPruefung/ViewModels/Content/COMContentVM.cs
T
2023-12-14 16:18:51 +01:00

508 lines
16 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 void InitializeAsync()
{
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)
{
Thread.Sleep(2000);
this.Connected = this.connection.ConnectIrda();
}
if (this.Connected)
{
this.State = "Mit IRDA verbunden";
this.Meter.SystemParameters = this.connection.ViewSystemParameters();
this.Header = this.Meter.SystemParameters.BuildInfo;
this.Running = this.Meter.SystemParameters.IsValid;
if (this.Running)
{
this.State = this.Meter.SystemParameters.BuildInfo;
}
else
{
this.State = $"({this.PortName} - fehler!)";
}
}
else
{
this.connection.Disconnect();
this.Header = $"({this.PortName} - nicht verbunden!)";
this.State = this.Header;
}
}
catch (Exception e)
{
this.connection.Disconnect();
this.Meter.Error = e.Message;
this.Header = $"({this.PortName} - nicht vorhanden!)";
this.State = this.Header;
}
}
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 void SaveInspection()
{
if (!this.Running)
{
return;
}
var resultsBuilder = new StringBuilder();
var correctionFactor = this.GetCorrectionFactor();
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"
+ $"{correctionFactor:0.00}%\t"
+ $"{pruefpunkt.MinDiff:0.00}\t"
+ $"{pruefpunkt.MaxDiff:0.00}\t");
}
this.TestResults = resultsBuilder.ToString();
// TODO: Log for instance in databse table.
try
{
File.WriteAllText($"{this.Meter.SystemParameters.BuildInfo}_{DateTime.Now:yyyyMMddHHmmss}.csv", this.TestResults);
}
catch
{
}
}
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.results[pp].VolumeMessured = 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 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 && 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 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();
this.State = $"Korekturfaktor: {this.Meter.CorrectionFactor:0.00} %";
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;
}
}
}
}