Merge branch 'temperature_calibration' of https://slm.sms-esaap.com/la_operations/laa_production into temperature_calibration
This commit is contained in:
+1
-1
@@ -1 +1 @@
|
||||
1a3d4919c9c702245832d925951f2b7cb72fc333
|
||||
2060865c94a192d7a90bef853b7068b66f669cb3
|
||||
|
||||
@@ -3,7 +3,9 @@ using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.IO.Pipes;
|
||||
using System.Linq;
|
||||
@@ -16,6 +18,7 @@ using Xylem.Common.Hardware.WaterMeter.Genesis.DataPackages;
|
||||
using Xylem.Common.Hardware.WaterMeter.Genesis.DataPackages.MeasurementRecords;
|
||||
using Xylem.Common.Hardware.WaterMeter.Genesis.Registers;
|
||||
using Xylem.Common.Hardware.WaterMeter.WaterMeterCore;
|
||||
using Xylem.Common.Logic.ServiceCore;
|
||||
using Xylem.Common.Logic.SoftwareAccessHelper;
|
||||
using Xylem.Common.Ui.GenesisToolBox.Infrastructure;
|
||||
using Watcher = System.Timers.Timer;
|
||||
@@ -51,12 +54,6 @@ namespace Xylem.Common.Ui.GenesisToolBox
|
||||
{
|
||||
InitializeComponent();
|
||||
SetEnabled(ProgrammState.Start);
|
||||
|
||||
|
||||
this.automaticCalibration = new AutomaticTemperaturCalibration(() => 10.0/*this.OutputAvgTof()*/);
|
||||
this.automaticCalibration.TemperatureMesured += this.SaveMessurement;
|
||||
|
||||
this.automaticCalibration.StartMessuring();
|
||||
}
|
||||
|
||||
public ZeroFlowGenesisMeter _currentGenesis { get; private set; }
|
||||
@@ -716,8 +713,13 @@ namespace Xylem.Common.Ui.GenesisToolBox
|
||||
this.SetEnabled(ProgrammState.RecordStoped);
|
||||
}
|
||||
|
||||
private void SaveMessurement(double meterT, double referenceT, DateTime timestamp)
|
||||
=> File.AppendAllText($"{DateTime.Now:yyyy_MM_dd}.messurements", $"{meterT};{referenceT};{timestamp.ToLongDateString()}");
|
||||
private void SaveMessurement(double meterTOF, double referenceT, DateTime timestamp)
|
||||
{
|
||||
var pcbid = this._currentGenesis.PcbId;
|
||||
|
||||
File.AppendAllLines($"{timestamp:yyyy_MM_dd}.messurements", new[] { $"{pcbid};{referenceT};{meterTOF};{timestamp.ToLongTimeString()}" });
|
||||
LocalWebRequest.PostRequestAsync($"http://10.49.40.25/MeterProcessState/api/TempFlange/AddLut?PcbId={pcbid}&RefTempC={referenceT}&AvgTof={meterTOF}");
|
||||
}
|
||||
}
|
||||
|
||||
internal class AutomaticTemperaturCalibration
|
||||
@@ -726,23 +728,28 @@ namespace Xylem.Common.Ui.GenesisToolBox
|
||||
private readonly Watcher temperatureWatcher;
|
||||
private readonly Watcher messurementWatcher;
|
||||
private readonly Func<double> meterTCallback;
|
||||
private readonly ICollection<KeyValuePair<double, double>> messurements;
|
||||
|
||||
private double referenceT;
|
||||
private int messurmentN;
|
||||
private int checksCount;
|
||||
private StreamReader streamReader;
|
||||
private NamedPipeClientStream pipeStream;
|
||||
|
||||
public AutomaticTemperaturCalibration(Func<double> meterTCallback)
|
||||
{
|
||||
this.meterTCallback = meterTCallback;
|
||||
this.messurementsCount = Appsettings.MessurementDauer / Appsettings.MessurementStep;
|
||||
|
||||
if (this.meterTCallback != null)
|
||||
{
|
||||
this.meterTCallback = meterTCallback;
|
||||
this.messurementsCount = Appsettings.MessurementDauer / Appsettings.MessurementStep;
|
||||
|
||||
this.temperatureWatcher = new Watcher(Appsettings.TemperatureCheckInMs);
|
||||
this.temperatureWatcher.Elapsed += this.CheckTemperature;
|
||||
|
||||
this.messurementWatcher = new Watcher(Appsettings.MessurementStep);
|
||||
this.messurementWatcher.Elapsed += this.Messure;
|
||||
|
||||
this.messurements = new List<KeyValuePair<double, double>>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -750,14 +757,23 @@ namespace Xylem.Common.Ui.GenesisToolBox
|
||||
|
||||
public void StartMessuring()
|
||||
{
|
||||
this.StopMessurementWatcher();
|
||||
this.StartTemperatureWatcher();
|
||||
this.pipeStream = new NamedPipeClientStream(".", "TempPipe", PipeDirection.In);
|
||||
this.pipeStream.Connect(3000);
|
||||
|
||||
if (this.pipeStream.IsConnected)
|
||||
{
|
||||
this.streamReader = new StreamReader(this.pipeStream);
|
||||
this.StopMessurementWatcher();
|
||||
this.StartTemperatureWatcher();
|
||||
}
|
||||
}
|
||||
|
||||
public void StopMessuring()
|
||||
{
|
||||
this.StopMessurementWatcher();
|
||||
this.StopTemperatureWatcher();
|
||||
this.streamReader.Dispose();
|
||||
this.pipeStream.Close();
|
||||
}
|
||||
|
||||
private void CheckTemperature(object sender, ElapsedEventArgs e)
|
||||
@@ -769,7 +785,7 @@ namespace Xylem.Common.Ui.GenesisToolBox
|
||||
var differenceT = Math.Abs(this.referenceT - referenceT);
|
||||
|
||||
// If difference is greater as configured then reset counter and start counting.
|
||||
if (differenceT > Appsettings.TemperatureStepInC)
|
||||
if (differenceT >= Appsettings.TemperatureStepInC)
|
||||
{
|
||||
this.checksCount = 1;
|
||||
}
|
||||
@@ -781,40 +797,51 @@ namespace Xylem.Common.Ui.GenesisToolBox
|
||||
|
||||
this.referenceT = referenceT;
|
||||
|
||||
// if count of same temperature events reaches 6 then start messurement for configured time.
|
||||
// if count of same temperature events reaches 10 then start messurement for configured time.
|
||||
if (this.checksCount == 10)
|
||||
{
|
||||
this.checksCount = 1;
|
||||
|
||||
// next call breaks current method execution.
|
||||
this.StopTemperatureWatcher();
|
||||
// next call starts messurements again.
|
||||
this.StartMessurementWatcher();
|
||||
}
|
||||
}
|
||||
// TODO: Break hole process?
|
||||
else
|
||||
{
|
||||
this.checksCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void Messure(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
// If reading reference temperature succeeds and is in the expected range.
|
||||
if (this.TryGetReferenceT(out var referenceT) && Math.Abs(this.referenceT - referenceT) > Appsettings.TemperaturToleranceInC)
|
||||
if (this.TryGetReferenceT(out var referenceT))
|
||||
{
|
||||
this.referenceT = referenceT;
|
||||
var meterT = this.meterTCallback();
|
||||
if (Math.Abs(this.referenceT - referenceT) <= Appsettings.TemperaturToleranceInC)
|
||||
{
|
||||
this.referenceT = referenceT;
|
||||
var meterTOF = this.meterTCallback();
|
||||
|
||||
this.TemperatureMesured?.Invoke(meterT, this.referenceT, e.SignalTime);
|
||||
}
|
||||
// TODO: May be break hole process?
|
||||
else
|
||||
{
|
||||
this.messurmentN = this.messurementsCount;
|
||||
}
|
||||
this.messurements.Add(new KeyValuePair<double, double>(this.referenceT, meterTOF));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.messurmentN = this.messurementsCount;
|
||||
}
|
||||
|
||||
if (this.messurmentN++ >= this.messurementsCount)
|
||||
{
|
||||
this.StopMessurementWatcher();
|
||||
this.StartTemperatureWatcher();
|
||||
this.messurmentN++;
|
||||
|
||||
if (this.messurmentN >= this.messurementsCount)
|
||||
{
|
||||
var avgT = this.messurements.Average(x => x.Key);
|
||||
var avgTOF = this.messurements.Average(x => x.Value);
|
||||
|
||||
this.messurements.Clear();
|
||||
this.TemperatureMesured?.Invoke(avgTOF, avgTOF, e.SignalTime);
|
||||
|
||||
// next call breaks current method execution.
|
||||
this.StopMessurementWatcher();
|
||||
// next call starts seeking for new temperature change.
|
||||
this.StartTemperatureWatcher();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -853,36 +880,23 @@ namespace Xylem.Common.Ui.GenesisToolBox
|
||||
referenceT = 0;
|
||||
|
||||
var succeeded = false;
|
||||
var readAttempts = 1000;
|
||||
|
||||
using (var pipeStream = new NamedPipeClientStream(".", "TempPipe", PipeDirection.In))
|
||||
do
|
||||
{
|
||||
pipeStream.Connect(3000);
|
||||
var referenceTemperatureLine = this.streamReader.ReadLine();
|
||||
|
||||
if (pipeStream.IsConnected)
|
||||
if (double.TryParse(referenceTemperatureLine, NumberStyles.Any, new CultureInfo("us"), out referenceT)
|
||||
|| double.TryParse(referenceTemperatureLine, NumberStyles.Any, new CultureInfo("de"), out referenceT))
|
||||
{
|
||||
pipeStream.Flush();
|
||||
succeeded = true;
|
||||
|
||||
using (var pipeStreamReader = new StreamReader(pipeStream))
|
||||
{
|
||||
var readAttempts = 1000;
|
||||
|
||||
do
|
||||
{
|
||||
var referenceTemperatureLine = pipeStreamReader.ReadLine();
|
||||
|
||||
if (double.TryParse(referenceTemperatureLine, out referenceT))
|
||||
{
|
||||
succeeded = true;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
Thread.Sleep(10);
|
||||
|
||||
} while (readAttempts-- > 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Thread.Sleep(10);
|
||||
|
||||
} while (readAttempts-- > 0);
|
||||
|
||||
return succeeded;
|
||||
}
|
||||
|
||||
@@ -46,7 +46,9 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Describes the tolerance in whitch the messurement is valid. If the tolerance is oversteped, the messurement is canceled.
|
||||
/// Describes the temperature tolerance in whitch the messurement is valid.
|
||||
/// If the tolerance is oversteped, the messurement is canceled.
|
||||
/// By default: 0.05.
|
||||
/// </summary>
|
||||
public static double TemperaturToleranceInC
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user