294 lines
8.4 KiB
C#
294 lines
8.4 KiB
C#
namespace InspectionUI.ViewModels
|
|
{
|
|
using InspectionUI.Models;
|
|
|
|
using OmniPlus;
|
|
|
|
using SignalR;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
public class ReferenceMeterVM : ItemsGridRowVM
|
|
{
|
|
private readonly ICollection<double> flows;
|
|
private readonly OmniContext omniContext;
|
|
private readonly DataHub signarR;
|
|
|
|
private int ppnr;
|
|
private int column;
|
|
private Task flowWatcher;
|
|
private CancellationTokenSource cancellationTokenSource;
|
|
|
|
public ReferenceMeterVM(OmniContext omniContext) : base()
|
|
{
|
|
this.flows = new List<double>();
|
|
this.omniContext = omniContext;
|
|
this.signarR = new DataHub();
|
|
this.ppnr = 0;
|
|
|
|
Task.Factory
|
|
.StartNew(state =>
|
|
{
|
|
if (state is DataHub dataHub)
|
|
{
|
|
dataHub.Connect(Appsettings.HubURL);
|
|
}
|
|
|
|
}, this.signarR, CancellationToken.None)
|
|
.ContinueWith(task =>
|
|
{
|
|
|
|
});
|
|
|
|
this.Add(new ItemsGridHeaderVM { Width = ItemsGridHeaderVM.Auto });
|
|
this.Add(new ItemsGridHeaderVM { Value = "Referenzzähler", Width = ItemsGridHeaderVM.Star });
|
|
this.Add(new ItemsGridHeaderVM { Value = "Ø m³/h", Width = ItemsGridHeaderVM.Star });
|
|
|
|
this.column = 2;
|
|
|
|
foreach (var _ in this.omniContext.Pruefpunkte)
|
|
{
|
|
this.Add(new ItemsGridHeaderVM { Width = ItemsGridHeaderVM.Star });
|
|
}
|
|
|
|
this.Add(new ItemsGridHeaderVM { Value = "(±) %", Width = ItemsGridHeaderVM.Star });
|
|
}
|
|
|
|
internal void Start(OmniPruefpunkt pp)
|
|
{
|
|
this.ppnr += 3;
|
|
|
|
this.StartFlow(pp);
|
|
this.StartRZ(pp);
|
|
this.StartFlowWatcher(pp);
|
|
}
|
|
|
|
private void StartFlow(OmniPruefpunkt pp)
|
|
{
|
|
this.column++;
|
|
|
|
const string Job = nameof(Job);
|
|
const string GetFlow = nameof(GetFlow);
|
|
const string StartFlow = nameof(StartFlow);
|
|
const string m3h = nameof(m3h);
|
|
|
|
var currentM3H = default(double);
|
|
|
|
void getCurrentFlow(DataDictionary _data)
|
|
{
|
|
var job = _data.GetString(Job);
|
|
|
|
if (job == GetFlow)
|
|
{
|
|
currentM3H = _data.GetDouble(m3h);
|
|
}
|
|
}
|
|
|
|
this.signarR.DataReceived += getCurrentFlow;
|
|
|
|
var data = new DataDictionary();
|
|
data.AddString(Job, StartFlow);
|
|
data.AddDouble(m3h, pp.FlowM3H);
|
|
this.signarR.Send(data);
|
|
|
|
var waitHandle = new ManualResetEventSlim();
|
|
var secondsIn = default(int);
|
|
var qmin = pp.FlowM3H * 0.95;
|
|
var qmax = pp.FlowM3H * 1.05;
|
|
var timeout = 500;
|
|
|
|
void checkCurrentFlow(object _)
|
|
{
|
|
var _data = new DataDictionary();
|
|
data.AddString(Job, GetFlow);
|
|
this.signarR.Send(data);
|
|
|
|
timeout--;
|
|
|
|
if (qmin <= currentM3H && currentM3H <= qmax)
|
|
{
|
|
secondsIn++;
|
|
}
|
|
else
|
|
{
|
|
secondsIn = 0;
|
|
}
|
|
|
|
this.Set(timeout, "... {0}", this.column);
|
|
|
|
if (secondsIn >= 20 || timeout <= 0)
|
|
{
|
|
waitHandle.Set();
|
|
}
|
|
}
|
|
|
|
var timer = new Timer(checkCurrentFlow, null, 0, 1000);
|
|
|
|
waitHandle.Wait(timeout * 1000);
|
|
timer.Dispose();
|
|
|
|
this.signarR.DataReceived -= getCurrentFlow;
|
|
this.Set(string.Empty, null, this.column);
|
|
this.flows.Add(currentM3H);
|
|
}
|
|
|
|
private void StartRZ(OmniPruefpunkt pp)
|
|
{
|
|
const string Job = nameof(Job);
|
|
const string StartRZ = nameof(StartRZ);
|
|
const string m3h = nameof(m3h);
|
|
const string ss = nameof(ss);
|
|
|
|
var waitHandle = new ManualResetEventSlim();
|
|
|
|
void waitRZStart(DataDictionary _data)
|
|
{
|
|
if (_data.GetString(Job) == StartRZ)
|
|
{
|
|
waitHandle.Set();
|
|
}
|
|
}
|
|
|
|
this.signarR.DataReceived += waitRZStart;
|
|
|
|
var data = new DataDictionary();
|
|
data.AddDouble(m3h, pp.FlowM3H);
|
|
data.AddDouble(ss, pp.Duration);
|
|
data.AddString(Job, StartRZ);
|
|
this.signarR.Send(data);
|
|
|
|
var timeout = 60;
|
|
|
|
void updateTimeLeft(object _)
|
|
{
|
|
this.Set(timeout--, "... {0}", this.column);
|
|
|
|
if (timeout <= 0)
|
|
{
|
|
waitHandle.Set();
|
|
}
|
|
}
|
|
|
|
var timer = new Timer(updateTimeLeft, null, 0, 1000);
|
|
|
|
waitHandle.Wait(timeout * 1000);
|
|
timer.Dispose();
|
|
|
|
this.signarR.DataReceived -= waitRZStart;
|
|
|
|
this.Set(this.flows.Average(), "{0:0.000}", this.column);
|
|
}
|
|
|
|
private void StartFlowWatcher(OmniPruefpunkt pp)
|
|
{
|
|
this.cancellationTokenSource = new CancellationTokenSource();
|
|
var cancellationToken = this.cancellationTokenSource.Token;
|
|
|
|
this.flowWatcher = Task.Factory
|
|
.StartNew(() =>
|
|
{
|
|
const string Job = nameof(Job);
|
|
const string GetFlow = nameof(GetFlow);
|
|
|
|
void dataReceived(DataDictionary _data)
|
|
{
|
|
var job = _data.GetString(Job);
|
|
var m3h = default(double);
|
|
|
|
if (job == GetFlow)
|
|
{
|
|
m3h = _data.GetDouble(nameof(m3h));
|
|
|
|
this.flows.Add(m3h);
|
|
this.Set(this.flows.Average(), "{0:0.000}", this.column);
|
|
}
|
|
}
|
|
|
|
this.signarR.DataReceived += dataReceived;
|
|
|
|
void requestFlow(object _)
|
|
{
|
|
var data = new DataDictionary();
|
|
data.AddString(Job, GetFlow);
|
|
this.signarR.Send(data);
|
|
}
|
|
|
|
var waitHandle = new ManualResetEventSlim();
|
|
cancellationToken.Register(waitHandle.Set);
|
|
|
|
var timer = new Timer(requestFlow, null, 0, 1000);
|
|
waitHandle.Wait(pp.Duration * 1000);
|
|
timer.Dispose();
|
|
|
|
this.signarR.DataReceived += dataReceived;
|
|
}, cancellationToken);
|
|
}
|
|
|
|
internal double GetM3S(OmniPruefpunkt pp)
|
|
{
|
|
this.cancellationTokenSource.Cancel();
|
|
|
|
const string Job = nameof(Job);
|
|
const string StopRZ = nameof(StopRZ);
|
|
const string ss = nameof(ss);
|
|
|
|
var waitHandle = new ManualResetEventSlim();
|
|
var m3s = default(double);
|
|
|
|
void waitM3S(DataDictionary _data)
|
|
{
|
|
if (_data.GetString(Job) == StopRZ)
|
|
{
|
|
m3s = _data.GetDouble(nameof(m3s));
|
|
|
|
waitHandle.Set();
|
|
}
|
|
}
|
|
|
|
this.signarR.DataReceived += waitM3S;
|
|
|
|
var data = new DataDictionary();
|
|
data.AddString(Job, StopRZ);
|
|
data.AddDouble(nameof(m3s), pp.FlowM3H);
|
|
data.AddInt(ss, pp.Duration);
|
|
this.signarR.Send(data);
|
|
|
|
waitHandle.Wait(60_000);
|
|
this.signarR.DataReceived -= waitM3S;
|
|
|
|
return m3s;
|
|
}
|
|
|
|
internal void Stop()
|
|
{
|
|
this.cancellationTokenSource.Cancel();
|
|
|
|
const string Job = nameof(Job);
|
|
const string StopFlow = nameof(StopFlow);
|
|
|
|
var data = new DataDictionary();
|
|
data.AddString(Job, StopFlow);
|
|
this.signarR.Send(data);
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
try
|
|
{
|
|
this.Stop();
|
|
this.signarR.Disconnect();
|
|
}
|
|
catch (Exception _ )
|
|
{
|
|
|
|
}
|
|
|
|
base.Dispose();
|
|
}
|
|
}
|
|
}
|