using System.Timers; using UI_SequentMicrosystems.Components; using UI_SequentMicrosystems.Constants; using UI_SequentMicrosystems.Models; namespace UI_SequentMicrosystems.Services { public class PointsService { private RTD8TMService _RTD8TMService { get; set; } private PointsModel _PointsModel = new(); private int PointsCount = 0; private System.Timers.Timer _Timer = new System.Timers.Timer(300000); public delegate Task AsyncEventHandler(object? sender, TEventArgs? e); public event AsyncEventHandler? EventUpdateTopBar; public event AsyncEventHandler? EventPointPage; public PointsService(RTD8TMService RTD8TM) { _RTD8TMService = RTD8TM; _Timer.Elapsed += SavePoint; _Timer.AutoReset = true; } /// /// Save actual data point /// public async void SavePoint() { long time = DateTime.Now.Ticks; _PointsModel.RTD8TM.Add(time, new()); PointsCount++; //Read RTD8TM actual data to point SortedList RTD8TMActual = _RTD8TMService.GetActualData(); foreach (byte stack in RTD8TMActual.Keys) { if (!_PointsModel.RTD8TM[time].ContainsKey(stack)) { _PointsModel.RTD8TM[time].Add(stack, new()); } for (byte chanel = 0; chanel < 8; chanel++) { _PointsModel.RTD8TM[time][stack].Add(RTD8TMActual[stack][chanel]); } } //other card types points saving if (EventUpdateTopBar != null) { await EventUpdateTopBar.Invoke(this, true); } if (EventPointPage != null) { await EventPointPage.Invoke(this, true); } } /// /// Read points /// /// public PointsModel GetPoints() { return _PointsModel; } /// /// Get points Count /// /// public int GetPointsCount() { return PointsCount; } //Create File Lines /// /// Create Points Lines for CSV File /// /// #pragma warning disable CS1998 // V této asynchronní metodě chybí operátory await a spustí se synchronně. public async Task CreatePointsFileLines(bool recalculate) #pragma warning restore CS1998 // V této asynchronní metodě chybí operátory await a spustí se synchronně. { List Lines = new List(); Lines.Add(CreateTitleLine(recalculate)); Lines.AddRange(CreateDataLines(recalculate)); return string.Join(Environment.NewLine, Lines); } /// /// Create Title Line of CSV File /// /// private string CreateTitleLine(bool Recalculate) { string Line = "Time"; //RTD8TM foreach (long time in _PointsModel.RTD8TM.Keys) { foreach (byte stack in _RTD8TMService.GetActualData().Keys) { for (byte chanel = 0; chanel < 8; chanel++) { if (_RTD8TMService.GetChanelName(stack, chanel) != "----------") { if (!Line.EndsWith(";")) Line += ";"; if (Recalculate && _RTD8TMService.GetValueType(stack, chanel) == RTD8TMSensorTypes.PT100) { Line += $"{_RTD8TMService.GetChanelName(stack, chanel)} °C"; } else { Line += $"{_RTD8TMService.GetChanelName(stack, chanel)} Ω"; } } } } break; } //other types return Line; } /// /// Create Data Lines of CSV File /// /// private List CreateDataLines(bool Recalculate) { Console.WriteLine("Creting Data Lines"); List Lines = new(); //RTD8TM foreach (long time in _PointsModel.RTD8TM.Keys) { Console.WriteLine($"DataLine Time: {new DateTime(time).ToString("HH:mm:ss")}"); string Line = new DateTime(time).ToString("HH:mm:ss"); foreach (byte stack in _PointsModel.RTD8TM[time].Keys) { Console.WriteLine($"DataLine Stack: {stack}"); for (byte chanel = 0; chanel < 8; chanel++) { Console.WriteLine($"DataLine Chanel: {chanel}"); if (_RTD8TMService.GetChanelName(stack, chanel) != "----------") { Console.WriteLine($"DataLine No default Name"); if (!Line.EndsWith(";")) Line += ";"; if (Recalculate) { Line += new RTD8TMChanelComponent().RecalculateValues(_PointsModel.RTD8TM[time][stack][chanel], _RTD8TMService.GetValueType(stack, chanel)).ToString(); } else { Line += _PointsModel.RTD8TM[time][stack][chanel].ToString(); } } } } //Other Types Lines.Add(Line); } return Lines; } //Timer private void SavePoint(object? o, ElapsedEventArgs? e) { SavePoint(); } /// /// Read Timer Interval /// /// Interval in seconds public int GetTimerInterval() { return (int)(_Timer.Interval / 1000); } /// /// Set Timer Interval /// /// interval in seconds public void SetTimerInterval(int interval) { _Timer.Interval = interval * 1000; } /// /// Start Timer /// public void StartTimer() { _Timer.Start(); } /// /// Stop Timer /// public void StopTimer() { _Timer.Stop(); } /// /// Get Timer status /// /// public bool GetTImerStatus() { return _Timer.Enabled; } } }