From 90f682195e0106b8bc476a6055b25bc32c4dcc3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ben=C3=AD=C4=8Dek?= Date: Sat, 25 Nov 2023 19:00:12 +0100 Subject: [PATCH] Add support for RTD card and saving points with values for save --- .config/dotnet-tools.json | 12 +++ API_SequentMicrosystems.csproj | 2 + Controllers/PointsController.cs | 65 ++++++++----- Controllers/RTDDataAcquisitionController.cs | 17 ++++ Program.cs | 6 +- Services/PointsService.cs | 101 ++++++++++++++++++-- Services/RTDDataAcquisitionService.cs | 92 ++++++++++++++++-- appsettings.json | 1 + 8 files changed, 253 insertions(+), 43 deletions(-) create mode 100644 .config/dotnet-tools.json diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json new file mode 100644 index 0000000..e3cadb9 --- /dev/null +++ b/.config/dotnet-tools.json @@ -0,0 +1,12 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-ef": { + "version": "8.0.0", + "commands": [ + "dotnet-ef" + ] + } + } +} \ No newline at end of file diff --git a/API_SequentMicrosystems.csproj b/API_SequentMicrosystems.csproj index 47b1d5a..4cac697 100644 --- a/API_SequentMicrosystems.csproj +++ b/API_SequentMicrosystems.csproj @@ -8,6 +8,8 @@ + + diff --git a/Controllers/PointsController.cs b/Controllers/PointsController.cs index e01a421..bb0432b 100644 --- a/Controllers/PointsController.cs +++ b/Controllers/PointsController.cs @@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Mvc; namespace API_SequentMicrosystems.Controllers { - [Route("points")] + [Route("Points")] [ApiController] public class PointsController : ControllerBase { @@ -25,7 +25,39 @@ namespace API_SequentMicrosystems.Controllers [HttpGet] public List Get() { - return _PointsService.GetPoints(); + return _PointsService.GetPoints().Take(1000).ToList(); ; + } + + // GET api//5/5 + /// + /// Read points from and to specified points positions + /// + /// limit of returned points (Maximum 1000) + /// id of start element + /// + [HttpGet("{start}/{limit}")] + public List Get(int limit, int start) + { + if (limit > 1000) + limit = 1000; + + return _PointsService.GetPoints(start, limit); + } + + // POST api//5 + /// + /// Read points from and to specified points positions + /// + /// limit of returned points (Maximum 1000) + /// id of start element + /// + [HttpPost("{limit}")] + public List Post(int limit, [FromBody] PointsModel pm) + { + if (limit > 1000) + limit = 1000; + + return _PointsService.GetPoints(limit, pm); } // GET api//5 @@ -34,23 +66,10 @@ namespace API_SequentMicrosystems.Controllers /// /// max readed points /// - [HttpGet("{max}")] - public List Get(int max) + [HttpGet("Count")] + public int GetCount() { - return _PointsService.GetPoints().Take(max).ToList(); - } - - // GET api//5/5 - /// - /// Read points from and to specified points positions - /// - /// - /// - /// - [HttpGet("{max}/{start}")] - public List Get(int max, int start) - { - return _PointsService.GetPoints().Skip(start).Take(max).ToList(); + return _PointsService.GetPoints().Count; } //DELETE api/points @@ -78,20 +97,20 @@ namespace API_SequentMicrosystems.Controllers /// Start timer to automatic saving points in specified interval /// /// - [HttpGet("save/{sec}")] - public void GetSaveSec(int sec) + [HttpGet("AutoSave/{msec}")] + public void GetSaveSec(int msec) { - //start autosave timer + _PointsService.AutoSaveStart(msec); } //GET api/points/save /// /// Stop timer for automatic save points /// - [HttpDelete("save")] + [HttpDelete("AutoSave")] public void DeleteSaveSec() { - //stop autosave timer + _PointsService.AutoSaveStop(); } diff --git a/Controllers/RTDDataAcquisitionController.cs b/Controllers/RTDDataAcquisitionController.cs index dd2721b..28fc507 100644 --- a/Controllers/RTDDataAcquisitionController.cs +++ b/Controllers/RTDDataAcquisitionController.cs @@ -93,6 +93,23 @@ namespace API_SequentMicrosystems.Controllers _RTDDAservice.SetPreconfiguratedChanelsNames(data); } + //GET api/RTDDA/Calibration + /// + /// Get Calibration data + /// + /// + [HttpGet("Calibration")] + public SortedList GetCalibration() + { + return _RTDDAservice.GetCalibrationData(); + } + + //POST api/RTDDA/Calibration + [HttpPost("Calibration")] + public void PostCalibration([FromBody] SortedList data) + { + _RTDDAservice.SetCalibrationData(data); + } } } diff --git a/Program.cs b/Program.cs index 0ff13ef..c32d6d6 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,6 @@ using API_SequentMicrosystems.Services; +using System.Text.Json; +using System.Text.Json.Serialization; namespace API_SequentMicrosystems { @@ -10,9 +12,10 @@ namespace API_SequentMicrosystems // Add services to the container. - builder.Services.AddControllers(); + builder.Services.AddControllers().AddNewtonsoftJson(); builder.Services.AddSingleton(); + builder.Services.AddSingleton(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); @@ -30,6 +33,7 @@ namespace API_SequentMicrosystems app.UseAuthorization(); app.Services.GetService(); + app.Services.GetService(); app.MapControllers(); diff --git a/Services/PointsService.cs b/Services/PointsService.cs index ac7715c..51859ea 100644 --- a/Services/PointsService.cs +++ b/Services/PointsService.cs @@ -1,21 +1,30 @@ using API_SequentMicrosystems.Models; using Newtonsoft.Json; +using System.Timers; namespace API_SequentMicrosystems.Services { public class PointsService { - - + private System.Timers.Timer _SaveTimer; + private bool _SavePoints; + private System.Timers.Timer? _timer; private RTDDAService _RTDDAService; - public PointsService(RTDDAService _RTDDAS) + 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("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(); } @@ -44,7 +53,7 @@ namespace API_SequentMicrosystems.Services /// private void SavePoints() { - File.WriteAllText("Points/SavedData.json", JsonConvert.SerializeObject(_points)); + File.WriteAllText("Points/SavedData.json", JsonConvert.SerializeObject(_points.ToList())); } /// @@ -53,7 +62,39 @@ namespace API_SequentMicrosystems.Services /// public List GetPoints() { - return _points; + return _points.ToList(); + } + + /// + /// Get points from start with limit + /// + /// Limit of returned points + /// + public List GetPoints(int limit) + { + return _points.Take(limit).ToList(); + } + + /// + /// Get Points with start and count limit + /// + /// Start at this list position + /// Limit of returned points + /// + public List GetPoints(int start, int limit) + { + return _points.Skip(start).Take(limit).ToList(); + } + + /// + /// Get Points with start element and limit + /// + /// Limit of returned points + /// Point from start reading + /// + public List GetPoints(int limit, PointsModel pm) + { + return _points.Skip(_points.IndexOf(pm) + 1).Take(limit).ToList(); } /// @@ -75,18 +116,60 @@ namespace API_SequentMicrosystems.Services pm.RTDDA = _RTDDAService.ReadAllCard(); //save RTD data to point _points.Add(pm); //add point to list - SavePoints(); //Save updated points + + _SavePoints = true; } + /// + /// Start AutoSave points with specified delay + /// + /// + 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(); + } + /// + /// Stop AutoSaving points + /// + /// + public int AutoSaveStop() + { + if (_timer == null) + return _points.Count; + _timer.Stop(); + _timer.Close(); + return _points.Count; + } + /// + /// TimerElapsed and saving point + /// + /// + /// + private void TimerElapsed(object? o, ElapsedEventArgs? e) + { + SavePoint(); + } - - - + private void SaveTimerElapsed(object? o, ElapsedEventArgs? e) + { + if (_SavePoints) + { + _SavePoints = false; + SavePoints(); + } + } diff --git a/Services/RTDDataAcquisitionService.cs b/Services/RTDDataAcquisitionService.cs index ed68205..75c95d6 100644 --- a/Services/RTDDataAcquisitionService.cs +++ b/Services/RTDDataAcquisitionService.cs @@ -32,6 +32,7 @@ namespace API_SequentMicrosystems.Services ChanelsNames = LoadChanelsNames(); //ChanelsNames PreconfiguredChanelsNames = LoadPreconfiguredChanelsNames(); //ChanelsPreconfiguredNames + CalibrationData = LoadCalibratioData(); } /// @@ -44,15 +45,10 @@ namespace API_SequentMicrosystems.Services for (byte i = 0; i < 8; i++) //loop for read all stack levels { - try - { - data.Add(i, ReadCard(i)); //read stack level - } - catch - { - Console.WriteLine($"RTD stack {i} is not available"); - } + float[] RD = ReadCard(i); //read stack level + if (RD.Length > 3) + data.Add(i, RD); //Add readed data to SortedList } return data; //return data @@ -93,7 +89,14 @@ namespace API_SequentMicrosystems.Services { try { - return _stackLevelReader.GetStack(stack); //return data from specified card + float[] data = _stackLevelReader.GetStack(stack); //return data from specified card + + for (byte i = 0;i < 8; i++) + { + data[i] += ReadCpecifiedChanelCalibration(stack, i); + } + + return data; } catch { @@ -101,7 +104,6 @@ namespace API_SequentMicrosystems.Services } } - #region ChanelsName private SortedList ChanelsNames; @@ -153,6 +155,7 @@ namespace API_SequentMicrosystems.Services #endregion #region PreconfiguredChanelsNames + private List PreconfiguredChanelsNames; /// @@ -202,5 +205,74 @@ namespace API_SequentMicrosystems.Services #endregion + #region Calibration + + private SortedList CalibrationData; + + /// + /// Load Calibration data from File + /// + /// + private SortedList LoadCalibratioData() + { + try + { + #pragma warning disable CS8603 // Může jít o vrácený odkaz null. + return JsonConvert.DeserializeObject>(File.ReadAllText("RTDDA/Calibration.json")); + #pragma warning restore CS8603 // Může jít o vrácený odkaz null. + } + catch + { + return new(); + } + } + + /// + /// Save Calibration data to File + /// + private void SaveCalibrationData() + { + File.WriteAllText("RTDDA/Calibration.json", JsonConvert.SerializeObject(CalibrationData)); + } + + /// + /// Get Calibration Data + /// + /// + public SortedList GetCalibrationData() + { + return CalibrationData; + } + + /// + /// Set new calibration Data + /// + /// + public void SetCalibrationData(SortedList CD) + { + CalibrationData = CD; + SaveCalibrationData(); + } + + /// + /// Get Calibration data of specific card chanel + /// + /// Card stack position ID + /// Chanel ID + /// Calibration data of specific chanel + private float ReadCpecifiedChanelCalibration(byte stack, byte chanel) + { + if (!CalibrationData.ContainsKey(stack)) + return 0; + + if (CalibrationData[stack].Length < chanel) + return 0; + + return CalibrationData[stack][chanel]; + } + + + #endregion + } } diff --git a/appsettings.json b/appsettings.json index f18bdf9..dcee792 100644 --- a/appsettings.json +++ b/appsettings.json @@ -6,6 +6,7 @@ } }, "AllowedHosts": "*", + "PointsSaveToFileMinimumSec": "10", "Cards": [ { "ID": "0",