laatzen/Common/OmniPlus/InspectionUI/ViewModels/ReferenceMeterVM.cs
2024-03-11 13:46:14 +01:00

249 lines
6.4 KiB
C#

namespace InspectionUI.ViewModels
{
using ExchangeHub;
using InspectionUI.Models;
using OmniPlus;
using System;
using System.Threading;
using System.Timers;
public class ReferenceMeterVM : ItemsGridRowVM
{
private readonly OmniContext omniContext;
private readonly ExchangeConnection signarR;
private int ppnr;
private bool cancel;
public ReferenceMeterVM(OmniContext omniContext) : base()
{
this.omniContext = omniContext;
this.signarR = new ExchangeConnection();
this.signarR.Connect(Appsettings.HubURL);
this.ppnr = 1;
this.LoadHeaders();
}
internal void Start(OmniPruefpunkt pp)
{
this.ppnr += 3;
this.cancel = false;
this.StartFlow(pp);
this.StartRZ(pp);
}
private void StartFlow(OmniPruefpunkt pp)
{
var interval = 1000;
var timeout = 500;
var volume = default(double);
var waitHandle = new ManualResetEventSlim();
var timer = new System.Timers.Timer
{
AutoReset = true,
Enabled = true,
Interval = interval,
};
void FlowStarted(double value, string _)
{
volume = value;
timer.Stop();
waitHandle.Set();
}
void TimerElapsed(object _, ElapsedEventArgs _1)
{
if (timeout < 0 || cancel)
{
timeout = 0;
timer.Stop();
waitHandle.Set();
}
else
{
this.Set(timeout, "... {0}", this.ppnr + 1);
}
timeout--;
}
timer.Elapsed += TimerElapsed;
this.signarR.StartFlow += FlowStarted;
timer.Start();
this.signarR.SendStartFlow(pp.FlowM3H);
waitHandle.Wait(timeout * interval);
waitHandle.Reset();
this.signarR.StartFlow -= FlowStarted;
timer.Elapsed -= TimerElapsed;
waitHandle.Wait(1000);
this.Set(volume, "{0:0.000}", this.ppnr + 1);
}
private void StartRZ(OmniPruefpunkt pp)
{
var interval = 1000;
var timeout = 60;
var seconds = default(double);
var waitHandle = new ManualResetEventSlim();
var timer = new System.Timers.Timer
{
AutoReset = true,
Enabled = true,
Interval = interval,
};
void RZStarted(int value, string _)
{
seconds = value;
timer.Stop();
waitHandle.Set();
}
void TimerElapsed(object _, ElapsedEventArgs _1)
{
if (timeout < 0 || cancel)
{
timeout = 0;
timer.Stop();
waitHandle.Set();
}
else
{
this.Set(timeout, "... {0}", this.ppnr);
}
timeout--;
}
timer.Elapsed += TimerElapsed;
this.signarR.StartReferenceMessurement += RZStarted;
timer.Start();
this.signarR.SendStartReferenceMessurement(pp.Duration);
waitHandle.Wait(timeout * interval);
waitHandle.Reset();
this.signarR.StartReferenceMessurement -= RZStarted;
timer.Elapsed -= TimerElapsed;
waitHandle.Wait(1000);
this.Set(seconds, ".../{0:0.0}", this.ppnr);
}
internal double GetFlow()
{
var interval = 1000;
var timeout = 60;
var referenceV = 0D;
var waitHandle = new ManualResetEventSlim();
var timer = new System.Timers.Timer
{
AutoReset = true,
Enabled = true,
Interval = interval,
};
void ReceiveFlow(double value, string _)
{
timeout = 0;
referenceV = value;
timer.Stop();
waitHandle.Set();
}
void TimerElapsed(object _, ElapsedEventArgs _1)
{
if (timeout < 0 || cancel)
{
timeout = 0;
timer.Stop();
waitHandle.Set();
}
else
{
this.Set(timeout, "... {0}", this.ppnr + 2);
}
timeout--;
}
timer.Elapsed += TimerElapsed;
this.signarR.StopReferenceMessurement += ReceiveFlow;
timer.Start();
this.signarR.SendStopReferenceMessurement();
waitHandle.Wait(timeout * interval);
waitHandle.Reset();
this.signarR.StopReferenceMessurement -= ReceiveFlow;
timer.Elapsed -= TimerElapsed;
waitHandle.Wait(1000);
this.Set(referenceV, "{0:0.0000000}", this.ppnr + 1);
this.Set(referenceV == 0 ? 0 : 100, "{0:0.00}", this.ppnr + 2);
return referenceV;
}
internal void Stop()
{
this.cancel = true;
this.signarR.SendStopFlow();
}
public override void Dispose()
{
try
{
this.signarR.SendStopFlow();
this.signarR.Disconnect();
}
catch (Exception)
{
}
base.Dispose();
}
private void LoadHeaders()
{
this.DispatcherInvoke(() =>
{
this.Add(new ItemsGridHeaderVM());
this.Add(new ItemsGridHeaderVM { Value = "RZ:" });
this.Add(new ItemsGridHeaderVM());
this.Add(new ItemsGridHeaderVM());
foreach (var _ in this.omniContext.Pruefpunkte)
{
this.Add(new ItemsGridHeaderVM());
this.Add(new ItemsGridHeaderVM());
this.Add(new ItemsGridHeaderVM());
}
this.Add(new ItemsGridHeaderVM());
});
}
}
}