API_SequentMicrosystems-RPI/Services/PointsService.cs

197 lines
5.6 KiB
C#

using API_SequentMicrosystems.Models;
using Newtonsoft.Json;
using System.Timers;
namespace API_SequentMicrosystems.Services
{
public class PointsService
{
private System.Timers.Timer _SaveTimer; //timer for saving Points to file
private bool _SavePoints;
private System.Timers.Timer? _timer; //Timer for creating new points
private RTDDAService _RTDDAService;
public PointsService(IConfiguration conf, RTDDAService _RTDDAS)
{
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();
_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()));
}
/// <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>();
}
/// <summary>
/// Delete Points
/// </summary>
public void DeletePoints()
{
_points = new();
SavePoints();
}
/// <summary>
/// Delete Specified Point
/// </summary>
/// <param name="id"></param>
public void DeletePoint(int id)
{
_points.RemoveAt(id);
}
/// <summary>
/// Create actual point object
/// </summary>
/// <returns></returns>
public PointsModel GetPoint()
{
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
_SavePoints = true;
}
/// <summary>
/// Start AutoSave points with specified delay
/// </summary>
/// <param name="msec"></param>
public void AutoSaveStart(int msec)
{
if (_timer != null && _timer.Enabled)
{
AutoSaveStop();
}
_timer = new System.Timers.Timer(msec);
_timer.AutoReset = true;
_timer.Elapsed += TimerElapsed;
_timer.Start();
}
/// <summary>
/// Stop AutoSaving points
/// </summary>
/// <returns></returns>
public int AutoSaveStop()
{
if (_timer == null)
return _points.Count;
_timer.Stop();
_timer.Close();
return _points.Count;
}
/// <summary>
/// TimerElapsed and saving point
/// </summary>
/// <param name="o"></param>
/// <param name="e"></param>
private void TimerElapsed(object? o, ElapsedEventArgs? e)
{
SavePoint();
}
private void SaveTimerElapsed(object? o, ElapsedEventArgs? e)
{
if (_SavePoints)
{
_SavePoints = false;
SavePoints();
}
}
}
}