From 77441ec12630d91cd1b5ab480848ccaf606185d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ben=C3=AD=C4=8Dek?= Date: Sat, 16 Dec 2023 09:07:42 +0100 Subject: [PATCH] =?UTF-8?q?P=C5=99idejte=20soubory=20projektu.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Main/ApiClient.cs | 87 +++++++++ RTD8TM/RTD8TM.cs | 250 +++++++++++++++++++++++++ RTD8TM/RTD8TMDataModel.cs | 23 +++ Wrapper_Api_SequentMicrosystems.csproj | 13 ++ 4 files changed, 373 insertions(+) create mode 100644 Main/ApiClient.cs create mode 100644 RTD8TM/RTD8TM.cs create mode 100644 RTD8TM/RTD8TMDataModel.cs create mode 100644 Wrapper_Api_SequentMicrosystems.csproj diff --git a/Main/ApiClient.cs b/Main/ApiClient.cs new file mode 100644 index 0000000..8c48cc5 --- /dev/null +++ b/Main/ApiClient.cs @@ -0,0 +1,87 @@ +using Newtonsoft.Json; +using System.Text; + +namespace Wrapper_Api_SequentMicrosystems.Main +{ + internal class ApiClient + { + private HttpClient _httpClient; + + + public ApiClient() + { + _httpClient = new HttpClient(); + } + + /// + /// Get data with GET method + /// + /// Waited type of data + /// String with API endpoint + /// Requested Data + /// Error message + public async Task GetData(string Endpoint) + { + HttpResponseMessage response = await _httpClient.GetAsync(Endpoint); + + if (response.IsSuccessStatusCode) + { + return JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + } + else + { + throw new Exception(response.EnsureSuccessStatusCode().Content.ToString()); + } + + } + + /// + /// Send data to api with POST method + /// + /// + /// Api Enpoint + /// Data for send to API + /// Error Message + public async void PostData(string Endpoint, Type data) + { + StringContent sc = new(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"); + + HttpResponseMessage response = await _httpClient.PostAsync(Endpoint, sc); + + if (response.IsSuccessStatusCode) + { + return; + } + else + { + throw new Exception(response.EnsureSuccessStatusCode().Content.ToString()); + } + + } + + /// + /// Delete data from Api with DELETE method + /// + /// Api Endpoint + /// Error message + public async void DeleteData(string Endpoint) + { + HttpResponseMessage response = await _httpClient.DeleteAsync(Endpoint); + + if (response.IsSuccessStatusCode) + { + return; + } + else + { + throw new Exception(response.EnsureSuccessStatusCode().Content.ToString()); + } + + } + + + + + + } +} diff --git a/RTD8TM/RTD8TM.cs b/RTD8TM/RTD8TM.cs new file mode 100644 index 0000000..b1cb7f8 --- /dev/null +++ b/RTD8TM/RTD8TM.cs @@ -0,0 +1,250 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Wrapper_Api_SequentMicrosystems.Main; + +namespace Wrapper_Api_SequentMicrosystems.RTD8TM +{ + public class RTD8TM + { + + private ApiClient _client; + + + public RTD8TM() + { + _client = new ApiClient(); + } + + #region Communication + + #pragma warning disable CS8603 // Může jít o vrácený odkaz null. + + //RTD + + /// + /// Get data from configured cards + /// + /// Base device address (http://1.2.3.4) + /// Deserialized data from API + public async Task> Get(string address) + { + try + { + return await _client.GetData>($"{address}api/RTDDA"); + } + catch //(Exception ex) + { + return new(); + } + } + + /// + /// Get data from all cards + /// + /// Base device address (http://1.2.3.4) + /// Deserialized data from API + public async Task> GetAll(string address) + { + try + { + return await _client.GetData>($"{address}api/RTDDA/All"); + } + catch //(Exception ex) + { + return new(); + } + } + + /// + /// Get data from Selected card + /// + /// Base device address (http://1.2.3.4) + /// Stack ID + /// Deserialized data from API + public async Task GetStack(string address, byte stack) + { + if (stack > 7) + return new float[0]; + if (stack < 0) + return new float[0]; + + try + { + return await _client.GetData($"{address}api/RTDDA/{stack}"); + } + catch //(Exception ex) + { + return new float[0]; + } + } + + //Names + + /// + /// Get configured chanels names + /// + /// Base device address (http://1.2.3.4) + /// Deserialized data from API + public async Task> GetNames(string address) + { + try + { + return await _client.GetData>($"{address}api/RTDDA/Names"); + } + catch + { + return new(); + } + } + + /// + /// Post chanels names configuration + /// + /// Base device address (http://1.2.3.4) + /// Data object with data for Post to API + public async void PostNames(string address, SortedList data) + { + await Task.Run(() => _client.PostData>($"{address}api/RTDDA/Names", data)); + } + + //Preconfigured Names + + /// + /// Get prepared chanels names list + /// + /// Base device address (http://1.2.3.4) + /// Deserialized object from API + public async Task> GetPreconfNames(string address) + { + try + { + return await _client.GetData>($"{address}api/RTDDA/Names/Preconfigured"); + } + catch + { + return new(); + } + } + + /// + /// Post preconfigured chanels names to API + /// + /// Base device address (http://1.2.3.4) + /// Data object to Post to API + public async void PostPreconfNames(string address, List data) + { + await Task.Run(() => _client.PostData>($"{address}api/RTDDA/Names/Preconfigured", data)); + } + + // Calibration + + /// + /// Get Calibration settings + /// + /// Base device address (http://1.2.3.4) + /// Deserialized Calibration data from API + public async Task> GetCalibration(string address) + { + try + { + return await _client.GetData>($"{address}api/RTDDA/Calibration"); + } + catch + { + return new(); + } + } + + /// + /// Post calibration data to API + /// + /// Base device address (http://1.2.3.4) + /// Data object for Post to API + public async void PostCalibration(string address, SortedList data) + { + await Task.Run(() => _client.PostData>($"{address}api/RTDDA/Calibration", data)); + } + #pragma warning restore CS8603 // Může jít o vrácený odkaz null. + + #endregion + + /// + /// Create data objects list from sortedlists with datas and names + /// + /// Data + /// Names + /// Device address + /// List with data objects + public List GetDataList(SortedList data, SortedList names, string address) + { + List objects = new List(); + + foreach (byte stack in data.Keys) + { + for (byte rtd = 0; rtd < 8; rtd++) + { + if (!names.ContainsKey(stack) || names[stack].Length < rtd) + { + RTD8TMDataModel model = new(data[stack][rtd], "", (byte)(rtd + 1), stack, address); + objects.Add(model); + } + else + { + RTD8TMDataModel model = new(data[stack][rtd], names[stack][rtd], (byte)(rtd + 1), stack, address); + objects.Add(model); + } + } + } + + return objects; + } + + /// + /// Get data objects list by device address + /// + /// Base device address (http://1.2.3.4) + /// true -> read from all cards | false -> read from configured cards + /// List with data objects + public async Task> GetDataList(string address, bool all) + { + if (all) + { + return GetDataList(await GetAll(address), await GetNames(address), address); + } + else + { + return GetDataList(await Get(address), await GetNames(address), address); + } + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + } +} diff --git a/RTD8TM/RTD8TMDataModel.cs b/RTD8TM/RTD8TMDataModel.cs new file mode 100644 index 0000000..d8faccd --- /dev/null +++ b/RTD8TM/RTD8TMDataModel.cs @@ -0,0 +1,23 @@ +namespace Wrapper_Api_SequentMicrosystems.RTD8TM +{ + public class RTD8TMDataModel + { + public float Resistance { get; set; } + public string Name { get; set; } + public byte ID { get; set; } + public byte Stack { get; set; } + public string DeviceAddress { get; set; } + + + public RTD8TMDataModel(float r, string name, byte id, byte stack, string da) + { + Resistance = r; + Name = name; + ID = id; + Stack = stack; + DeviceAddress = da; + } + + + } +} diff --git a/Wrapper_Api_SequentMicrosystems.csproj b/Wrapper_Api_SequentMicrosystems.csproj new file mode 100644 index 0000000..3b5b170 --- /dev/null +++ b/Wrapper_Api_SequentMicrosystems.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + +