common/Ui/GenesisToolBox/Infrastructure/TempMeterModels.cs
2026-04-23 17:50:07 +02:00

170 lines
5.3 KiB
C#

namespace Xylem.Common.Ui.GenesisToolBox.Infrastructure
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Timers;
internal interface ITempMeterForm
{
void AddMessage(string message);
void StartRecording();
bool StartLED(int Slot);
bool StopLED(int Slot);
void InvokeOnUIThread(Action action);
IDictionary<string, double> GetAverageTOFForeachMeter(int Slot);
}
internal partial class AutomaticTemperatureCalibration
{
private readonly ITempMeterForm tempMeterForm;
private TemperatureSource temperatureSource;
private readonly string outputFile;
private Timer timer;
private bool hasHeaders;
public AutomaticTemperatureCalibration(ITempMeterForm tempMeterForm)
{
this.tempMeterForm = tempMeterForm;
this.outputFile = Path.Combine(Appsettings.WorkingDirectory, $"{DateTime.Now:yyyy_MM_dd}_mesurements.txt");
Directory.CreateDirectory(Path.GetDirectoryName(this.outputFile));
File.AppendAllText(this.outputFile, default(string));
}
public void StartMessuring()
{
this.temperatureSource = new TemperatureSource();
this.tempMeterForm.StartRecording();
//this.GetResults(null, null);
this.timer = new Timer()
{
AutoReset = true,
Interval = Appsettings.MessurementStep,
};
this.timer.Elapsed += this.GetResults;
this.timer.Start();
}
private double currentCount = 0.5;
private void GetResults(Object sender, ElapsedEventArgs args)
{
this.tempMeterForm.InvokeOnUIThread(() =>
{
try
{
if (currentCount > -1)
{
var tmpCurrent = currentCount;
currentCount = -1;
try
{
if (tmpCurrent % 1 == 0)
{
// sleep
this.tempMeterForm.StopLED((int)tmpCurrent);
this.tempMeterForm.AddMessage($"Stop LED {((int)tmpCurrent).ToString()}");
if (((int)tmpCurrent) + 1 == 3)
{
this.tempMeterForm.StartLED(1);
this.tempMeterForm.AddMessage($"Start LED 1");
}
else
{
this.tempMeterForm.StartLED((int)tmpCurrent +1);
this.tempMeterForm.AddMessage($"Stop LED {((int)tmpCurrent +1).ToString()}");
}
}
else
{
var temp = this.temperatureSource.GetTemperature();
var results = this.tempMeterForm.GetAverageTOFForeachMeter((int)tmpCurrent);
//if (!this.hasHeaders)
//{
// File.AppendAllText(this.outputFile, $"Timestamp;°C;{string.Join(";", results.Keys)}{Environment.NewLine}");
// this.hasHeaders = true;
var messurement = $"{DateTime.Now};{temp:0.00};{string.Join(";", results.Keys.Select(x => $"{x}"))}; {string.Join(";", results.Values.Select(x => $"{x:0.0000000000}"))}";
File.AppendAllText(this.outputFile, $"{messurement}{Environment.NewLine}");
this.tempMeterForm.AddMessage(messurement);
}
}
catch (Exception)
{
}
finally
{
currentCount = tmpCurrent +0.5;
}
if (currentCount >= 3)
{
currentCount = 0.5;
}
}
else
{
this.tempMeterForm.AddMessage($"Blocked{Environment.NewLine}");
}
}
catch (Exception e)
{
this.tempMeterForm.AddMessage(e.ToString());
}
});
}
public void StopMessuring()
{
try
{
this.timer.Stop();
this.timer.Dispose();
this.timer = null;
}
catch (Exception e)
{
this.tempMeterForm.AddMessage($"{e}");
}
try
{
this.temperatureSource.Dispose();
}
catch (Exception e)
{
this.tempMeterForm.AddMessage($"{e}");
}
}
}
}