105 lines
3.0 KiB
C#
105 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Timers;
|
|
|
|
namespace Xylem.Common.Utils.UserAccessCtrl
|
|
{
|
|
public interface ITempMeterForm
|
|
{
|
|
void AddMessage(String message);
|
|
|
|
void StartRecording();
|
|
|
|
void InvokeOnUIThread(Action action);
|
|
|
|
IDictionary<String, Double> GetAverageTOFForeachMeter();
|
|
}
|
|
|
|
public partial class AutomaticTemperatureCalibration
|
|
{
|
|
private readonly ITempMeterForm tempMeterForm;
|
|
private TemperatureSource temperatureSource;
|
|
private readonly String outputFile;
|
|
|
|
private Timer timer;
|
|
private Boolean hasHeaders;
|
|
|
|
public AutomaticTemperatureCalibration(ITempMeterForm tempMeterForm)
|
|
{
|
|
this.tempMeterForm = tempMeterForm;
|
|
outputFile = Path.Combine(Appsettings.WorkingDirectory, $"{DateTime.Now:yyyy_MM_dd}_mesurements.txt");
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(outputFile));
|
|
File.AppendAllText(outputFile, default(String));
|
|
}
|
|
|
|
public void StartMessuring()
|
|
{
|
|
temperatureSource = new TemperatureSource();
|
|
tempMeterForm.StartRecording();
|
|
GetResults(null, null);
|
|
|
|
timer = new Timer()
|
|
{
|
|
AutoReset = true,
|
|
Interval = Appsettings.MessurementStep,
|
|
};
|
|
timer.Elapsed += GetResults;
|
|
timer.Start();
|
|
}
|
|
|
|
private void GetResults(Object sender, ElapsedEventArgs args)
|
|
{
|
|
tempMeterForm.InvokeOnUIThread(() =>
|
|
{
|
|
try
|
|
{
|
|
var temp = temperatureSource.GetTemperature();
|
|
var results = tempMeterForm.GetAverageTOFForeachMeter();
|
|
|
|
if (!hasHeaders)
|
|
{
|
|
File.AppendAllText(outputFile, $"Timestamp;°C;{string.Join(";", results.Keys)}{Environment.NewLine}");
|
|
|
|
hasHeaders = true;
|
|
}
|
|
|
|
var messurement = $"{temp:0.00};{string.Join(";", results.Values.Select(x => $"{x:0.0000000000}"))}";
|
|
|
|
File.AppendAllText(outputFile, $"{messurement}{Environment.NewLine}");
|
|
|
|
tempMeterForm.AddMessage(messurement);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
tempMeterForm.AddMessage(e.ToString());
|
|
}
|
|
});
|
|
}
|
|
|
|
public void StopMessuring()
|
|
{
|
|
try
|
|
{
|
|
timer.Stop();
|
|
timer.Dispose();
|
|
timer = null;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
tempMeterForm.AddMessage($"{e}");
|
|
}
|
|
|
|
try
|
|
{
|
|
temperatureSource.Dispose();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
tempMeterForm.AddMessage($"{e}");
|
|
}
|
|
}
|
|
}
|
|
} |