UI_SequentMicrosystems-RPI/Services/PointsService.cs

252 lines
7.2 KiB
C#

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<TEventArgs>(object? sender, TEventArgs? e);
public event AsyncEventHandler<bool>? EventUpdateTopBar;
public event AsyncEventHandler<bool>? EventPointPage;
public PointsService(RTD8TMService RTD8TM)
{
_RTD8TMService = RTD8TM;
_Timer.Elapsed += SavePoint;
_Timer.AutoReset = true;
}
/// <summary>
/// Save actual data point
/// </summary>
public async void SavePoint()
{
long time = DateTime.Now.Ticks;
_PointsModel.RTD8TM.Add(time, new());
PointsCount++;
//Read RTD8TM actual data to point
SortedList<byte, float[]> 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);
}
}
/// <summary>
/// Read points
/// </summary>
/// <returns></returns>
public PointsModel GetPoints()
{
return _PointsModel;
}
/// <summary>
/// Get points Count
/// </summary>
/// <returns></returns>
public int GetPointsCount()
{
return PointsCount;
}
//Create File Lines
/// <summary>
/// Create Points Lines for CSV File
/// </summary>
/// <returns></returns>
#pragma warning disable CS1998 // V této asynchronní metodě chybí operátory await a spustí se synchronně.
public async Task<string> CreatePointsFileLines(bool recalculate)
#pragma warning restore CS1998 // V této asynchronní metodě chybí operátory await a spustí se synchronně.
{
List<string> Lines = new List<string>();
Lines.Add(CreateTitleLine(recalculate));
Lines.AddRange(CreateDataLines(recalculate));
return string.Join(Environment.NewLine, Lines);
}
/// <summary>
/// Create Title Line of CSV File
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// Create Data Lines of CSV File
/// </summary>
/// <returns></returns>
private List<string> CreateDataLines(bool Recalculate)
{
Console.WriteLine("Creting Data Lines");
List<string> 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();
}
/// <summary>
/// Read Timer Interval
/// </summary>
/// <returns>Interval in seconds</returns>
public int GetTimerInterval()
{
return (int)(_Timer.Interval / 1000);
}
/// <summary>
/// Set Timer Interval
/// </summary>
/// <param name="interval">interval in seconds</param>
public void SetTimerInterval(int interval)
{
_Timer.Interval = interval * 1000;
}
/// <summary>
/// Start Timer
/// </summary>
public void StartTimer()
{
_Timer.Start();
}
/// <summary>
/// Stop Timer
/// </summary>
public void StopTimer()
{
_Timer.Stop();
}
/// <summary>
/// Get Timer status
/// </summary>
/// <returns></returns>
public bool GetTImerStatus()
{
return _Timer.Enabled;
}
}
}