API_SequentMicrosystems-RPI/Services/PointsService.cs

197 lines
5.6 KiB
C#
Raw Normal View History

2023-11-25 09:24:04 +01:00
using API_SequentMicrosystems.Models;
using Newtonsoft.Json;
using System.Timers;
2023-11-25 09:24:04 +01:00
namespace API_SequentMicrosystems.Services
{
public class PointsService
{
2024-01-15 10:33:10 +01:00
private System.Timers.Timer _SaveTimer; //timer for saving Points to file
private bool _SavePoints;
2024-01-15 10:33:10 +01:00
private System.Timers.Timer? _timer; //Timer for creating new points
2023-11-25 09:24:04 +01:00
private RTDDAService _RTDDAService;
public PointsService(IConfiguration conf, RTDDAService _RTDDAS)
2023-11-25 09:24:04 +01:00
{
if (!Directory.Exists("Points"))
Directory.CreateDirectory("Points");
_RTDDAService = _RTDDAS;
#pragma warning disable CS8604 // Může jít o argument s odkazem null.
_SaveTimer = new System.Timers.Timer(conf.GetValue<int>("PointsSaveToFileMinimumSec") * 1000);
#pragma warning restore CS8604 // Může jít o argument s odkazem null.
_SaveTimer.Elapsed += SaveTimerElapsed;
_SaveTimer.AutoReset = true;
_SaveTimer.Start();
2023-11-25 09:24:04 +01:00
_points = LoadPoints();
}
private List<PointsModel> _points;
/// <summary>
/// Load points from File
/// </summary>
/// <returns></returns>
private List<PointsModel> LoadPoints()
{
try
{
#pragma warning disable CS8603 // Může jít o vrácený odkaz null.
return JsonConvert.DeserializeObject<List<PointsModel>>(File.ReadAllText("Points/SavedData.json"));
#pragma warning restore CS8603 // Může jít o vrácený odkaz null.
}
catch
{
return new();
}
}
/// <summary>
/// Save points to File
/// </summary>
private void SavePoints()
{
File.WriteAllText("Points/SavedData.json", JsonConvert.SerializeObject(_points.ToList()));
2023-11-25 09:24:04 +01:00
}
/// <summary>
/// Get saved Points
/// </summary>
/// <returns></returns>
public List<PointsModel> GetPoints()
{
return _points.ToList();
}
/// <summary>
/// Get points from start with limit
/// </summary>
/// <param name="limit">Limit of returned points</param>
/// <returns></returns>
public List<PointsModel> GetPoints(int limit)
{
return _points.Take(limit).ToList<PointsModel>();
}
/// <summary>
/// Get Points with start and count limit
/// </summary>
/// <param name="start">Start at this list position</param>
/// <param name="limit">Limit of returned points</param>
/// <returns></returns>
public List<PointsModel> GetPoints(int start, int limit)
{
return _points.Skip(start).Take(limit).ToList<PointsModel>();
}
/// <summary>
/// Get Points with start element and limit
/// </summary>
/// <param name="limit">Limit of returned points</param>
/// <param name="pm">Point from start reading</param>
/// <returns></returns>
public List<PointsModel> GetPoints(int limit, PointsModel pm)
{
return _points.Skip(_points.IndexOf(pm) + 1).Take(limit).ToList<PointsModel>();
2023-11-25 09:24:04 +01:00
}
/// <summary>
/// Delete Points
/// </summary>
public void DeletePoints()
{
_points = new();
SavePoints();
}
2023-11-30 16:46:33 +01:00
/// <summary>
/// Delete Specified Point
/// </summary>
/// <param name="id"></param>
public void DeletePoint(int id)
{
_points.RemoveAt(id);
}
2023-11-25 09:24:04 +01:00
/// <summary>
/// Create actual point object
2023-11-25 09:24:04 +01:00
/// </summary>
/// <returns></returns>
public PointsModel GetPoint()
2023-11-25 09:24:04 +01:00
{
PointsModel pm = new PointsModel(); //initialize point object
pm.Time = DateTime.Now; //set time of point created
pm.RTDDA = _RTDDAService.ReadAllCard(); //save RTD data to point
return pm;
}
/// <summary>
/// Save data/status point
/// </summary>
public void SavePoint()
{
_points.Add(GetPoint()); //add point to list
2023-11-25 09:24:04 +01:00
_SavePoints = true;
}
2023-11-25 09:24:04 +01:00
/// <summary>
/// Start AutoSave points with specified delay
/// </summary>
/// <param name="msec"></param>
public void AutoSaveStart(int msec)
{
if (_timer != null && _timer.Enabled)
{
AutoSaveStop();
}
2023-11-25 09:24:04 +01:00
_timer = new System.Timers.Timer(msec);
_timer.AutoReset = true;
_timer.Elapsed += TimerElapsed;
_timer.Start();
}
2023-11-25 09:24:04 +01:00
/// <summary>
/// Stop AutoSaving points
/// </summary>
/// <returns></returns>
public int AutoSaveStop()
{
if (_timer == null)
return _points.Count;
2023-11-25 09:24:04 +01:00
_timer.Stop();
_timer.Close();
2023-11-25 09:24:04 +01:00
return _points.Count;
}
2023-11-25 09:24:04 +01:00
/// <summary>
/// TimerElapsed and saving point
/// </summary>
/// <param name="o"></param>
/// <param name="e"></param>
private void TimerElapsed(object? o, ElapsedEventArgs? e)
{
SavePoint();
}
2023-11-25 09:24:04 +01:00
private void SaveTimerElapsed(object? o, ElapsedEventArgs? e)
{
if (_SavePoints)
{
_SavePoints = false;
SavePoints();
}
}
2023-11-25 09:24:04 +01:00
}
}