Files
laatzen/Common/Ui/GenesisToolBox/Infrastructure/TempMeterModels.cs
T
2023-09-25 12:44:12 +02:00

189 lines
5.2 KiB
C#

namespace Xylem.Common.Ui.GenesisToolBox.Infrastructure
{
using System;
using System.Globalization;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Timers;
using static System.Globalization.NumberStyles;
public class CordonelTempLut
{
public int Id { get; set; }
public int PcbId { get; set; }
public double RefTempC { get; set; }
public double AvgTof { get; set; }
public bool Active { get; set; }
public DateTime? Timestamp { get; set; }
}
public class RefTempRange
{
public Double Min { get; set; }
public Double Max { get; set; }
}
class TemperatureSource : IDisposable
{
private readonly NamedPipeClientStream pipeStream;
private readonly StreamReader pipeReader;
private readonly CultureInfo de;
private readonly CultureInfo us;
public TemperatureSource()
{
try
{
this.pipeStream = new NamedPipeClientStream(".", "TempPipe", PipeDirection.In);
this.pipeStream.Connect(3000);
}
catch (Exception)
{
}
if (this.pipeStream.IsConnected)
{
this.pipeReader = new StreamReader(this.pipeStream);
}
this.de = new CultureInfo("de-DE");
this.us = new CultureInfo("en-US");
}
public double GetTemperature()
{
var temperature = default(double);
try
{
lock (this.pipeReader)
{
var value = $"{this.pipeReader.ReadLine()}";
if (double.TryParse(value, Any, this.us, out temperature) || double.TryParse(value, Any, this.de, out temperature))
{
}
}
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
}
return temperature;
}
public void Dispose()
{
this.pipeReader?.Dispose();
this.pipeStream?.Dispose();
}
public void Flush()
{
this.pipeStream.Flush();
this.pipeReader.BaseStream.Flush();
}
}
partial class AutomaticTemperatureCalibration
{
private readonly string pcbId;
private readonly string messurementsFile;
private readonly Func<double> sourceTOF;
private readonly TemperatureSource temperature;
private Timer longTimer;
private Timer shortTimer;
private bool messuring;
public AutomaticTemperatureCalibration(string pcbId, Func<double> sourceTOF)
{
this.pcbId = pcbId;
this.sourceTOF = sourceTOF;
this.messurementsFile = this.GetNextFile();
this.temperature = new TemperatureSource();
this.shortTimer = new Timer();
this.longTimer = new Timer();
}
public void StartMessuring()
{
this.shortTimer.Enabled = true;
this.shortTimer.AutoReset = true;
this.shortTimer.Interval = 1_000;
this.shortTimer.Elapsed += this.ShortTimerElapsed;
// this.shortTimer.Start();
this.shortTimer.Stop();
this.longTimer.Enabled = true;
this.longTimer.AutoReset = true;
this.longTimer.Interval = 300_000;
this.longTimer.Elapsed += this.LongTimerElapsed;
this.longTimer.Start();
}
private void LongTimerElapsed(Object sender, ElapsedEventArgs args)
{
this.messuring = !this.messuring;
if (this.messuring)
{
System.Threading.Thread.Sleep(1_000);
this.shortTimer.Start();
}
else
{
this.shortTimer.Stop();
System.Threading.Thread.Sleep(1_000);
File.AppendAllText(this.messurementsFile, Environment.NewLine);
}
}
private void ShortTimerElapsed(Object sender, ElapsedEventArgs args)
{
var currentTOF = this.sourceTOF();
var currentT = this.temperature.GetTemperature();
File.AppendAllText(
path: this.messurementsFile,
contents: $"[{args.SignalTime:yyyy-MM-dd hh:mm:ss}]\t{this.pcbId,9}\t{currentT,5:00.00}\t{currentTOF,14:0.000000000000}{Environment.NewLine}");
}
public void StopMessuring()
{
this.shortTimer.Stop();
this.shortTimer.Dispose();
this.longTimer.Stop();
this.longTimer.Dispose();
}
private String GetNextFile()
{
var filesCount = Directory
.CreateDirectory(@".\temperature_calibration")
.GetFiles("*_messurements.txt")
.Count();
var nextFilePath = $@".\temperature_calibration\{filesCount + 1}_messurements.txt";
File.WriteAllText(nextFilePath, string.Empty);
return nextFilePath;
}
}
}