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(); void InvokeOnUIThread(Action action); IDictionary GetAverageTOFForeachMeter(); } 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 void GetResults(Object sender, ElapsedEventArgs args) { this.tempMeterForm.InvokeOnUIThread(() => { try { var temp = this.temperatureSource.GetTemperature(); var results = this.tempMeterForm.GetAverageTOFForeachMeter(); if (!this.hasHeaders) { File.AppendAllText(this.outputFile, $"Timestamp;°C;{string.Join(";", results.Keys)}{Environment.NewLine}"); this.hasHeaders = true; } var messurement = $"{temp:0.00};{string.Join(";", results.Values.Select(x => $"{x:0.0000000000}"))}"; File.AppendAllText(this.outputFile, $"{messurement}{Environment.NewLine}"); this.tempMeterForm.AddMessage(messurement); } 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}"); } } } }