Přidejte soubory projektu.
parent
fbb5b3e699
commit
77441ec126
|
@ -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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get data with GET method
|
||||
/// </summary>
|
||||
/// <typeparam name="Type"> Waited type of data</typeparam>
|
||||
/// <param name="Endpoint">String with API endpoint</param>
|
||||
/// <returns>Requested Data</returns>
|
||||
/// <exception cref="Exception">Error message</exception>
|
||||
public async Task<Type?> GetData<Type>(string Endpoint)
|
||||
{
|
||||
HttpResponseMessage response = await _httpClient.GetAsync(Endpoint);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<Type>(await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception(response.EnsureSuccessStatusCode().Content.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send data to api with POST method
|
||||
/// </summary>
|
||||
/// <typeparam name="Type"></typeparam>
|
||||
/// <param name="Endpoint">Api Enpoint</param>
|
||||
/// <param name="data">Data for send to API</param>
|
||||
/// <exception cref="Exception">Error Message</exception>
|
||||
public async void PostData<Type>(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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete data from Api with DELETE method
|
||||
/// </summary>
|
||||
/// <param name="Endpoint">Api Endpoint</param>
|
||||
/// <exception cref="Exception">Error message</exception>
|
||||
public async void DeleteData(string Endpoint)
|
||||
{
|
||||
HttpResponseMessage response = await _httpClient.DeleteAsync(Endpoint);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception(response.EnsureSuccessStatusCode().Content.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
||||
/// <summary>
|
||||
/// Get data from configured cards
|
||||
/// </summary>
|
||||
/// <param name="address">Base device address (http://1.2.3.4)</param>
|
||||
/// <returns>Deserialized data from API</returns>
|
||||
public async Task<SortedList<byte, float[]>> Get(string address)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _client.GetData<SortedList<byte, float[]>>($"{address}api/RTDDA");
|
||||
}
|
||||
catch //(Exception ex)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get data from all cards
|
||||
/// </summary>
|
||||
/// <param name="address">Base device address (http://1.2.3.4)</param>
|
||||
/// <returns>Deserialized data from API</returns>
|
||||
public async Task<SortedList<byte, float[]>> GetAll(string address)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _client.GetData<SortedList<byte, float[]>>($"{address}api/RTDDA/All");
|
||||
}
|
||||
catch //(Exception ex)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get data from Selected card
|
||||
/// </summary>
|
||||
/// <param name="address">Base device address (http://1.2.3.4)</param>
|
||||
/// <param name="stack">Stack ID</param>
|
||||
/// <returns>Deserialized data from API</returns>
|
||||
public async Task<float[]> GetStack(string address, byte stack)
|
||||
{
|
||||
if (stack > 7)
|
||||
return new float[0];
|
||||
if (stack < 0)
|
||||
return new float[0];
|
||||
|
||||
try
|
||||
{
|
||||
return await _client.GetData<float[]>($"{address}api/RTDDA/{stack}");
|
||||
}
|
||||
catch //(Exception ex)
|
||||
{
|
||||
return new float[0];
|
||||
}
|
||||
}
|
||||
|
||||
//Names
|
||||
|
||||
/// <summary>
|
||||
/// Get configured chanels names
|
||||
/// </summary>
|
||||
/// <param name="address">Base device address (http://1.2.3.4)</param>
|
||||
/// <returns>Deserialized data from API</returns>
|
||||
public async Task<SortedList<byte, string[]>> GetNames(string address)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _client.GetData<SortedList<byte, string[]>>($"{address}api/RTDDA/Names");
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Post chanels names configuration
|
||||
/// </summary>
|
||||
/// <param name="address">Base device address (http://1.2.3.4)</param>
|
||||
/// <param name="data">Data object with data for Post to API</param>
|
||||
public async void PostNames(string address, SortedList<byte, string[]> data)
|
||||
{
|
||||
await Task.Run(() => _client.PostData<SortedList<byte, string[]>>($"{address}api/RTDDA/Names", data));
|
||||
}
|
||||
|
||||
//Preconfigured Names
|
||||
|
||||
/// <summary>
|
||||
/// Get prepared chanels names list
|
||||
/// </summary>
|
||||
/// <param name="address">Base device address (http://1.2.3.4)</param>
|
||||
/// <returns>Deserialized object from API</returns>
|
||||
public async Task<List<string>> GetPreconfNames(string address)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _client.GetData<List<string>>($"{address}api/RTDDA/Names/Preconfigured");
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Post preconfigured chanels names to API
|
||||
/// </summary>
|
||||
/// <param name="address">Base device address (http://1.2.3.4)</param>
|
||||
/// <param name="data">Data object to Post to API</param>
|
||||
public async void PostPreconfNames(string address, List<string> data)
|
||||
{
|
||||
await Task.Run(() => _client.PostData<List<string>>($"{address}api/RTDDA/Names/Preconfigured", data));
|
||||
}
|
||||
|
||||
// Calibration
|
||||
|
||||
/// <summary>
|
||||
/// Get Calibration settings
|
||||
/// </summary>
|
||||
/// <param name="address">Base device address (http://1.2.3.4)</param>
|
||||
/// <returns>Deserialized Calibration data from API</returns>
|
||||
public async Task<SortedList<byte, float[]>> GetCalibration(string address)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _client.GetData<SortedList<byte, float[]>>($"{address}api/RTDDA/Calibration");
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Post calibration data to API
|
||||
/// </summary>
|
||||
/// <param name="address">Base device address (http://1.2.3.4)</param>
|
||||
/// <param name="data">Data object for Post to API</param>
|
||||
public async void PostCalibration(string address, SortedList<byte, float[]> data)
|
||||
{
|
||||
await Task.Run(() => _client.PostData<SortedList<byte, float[]>>($"{address}api/RTDDA/Calibration", data));
|
||||
}
|
||||
#pragma warning restore CS8603 // Může jít o vrácený odkaz null.
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Create data objects list from sortedlists with datas and names
|
||||
/// </summary>
|
||||
/// <param name="data">Data</param>
|
||||
/// <param name="names">Names</param>
|
||||
/// <param name="address">Device address</param>
|
||||
/// <returns>List with data objects</returns>
|
||||
public List<RTD8TMDataModel> GetDataList(SortedList<byte, float[]> data, SortedList<byte, string[]> names, string address)
|
||||
{
|
||||
List<RTD8TMDataModel> objects = new List<RTD8TMDataModel>();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get data objects list by device address
|
||||
/// </summary>
|
||||
/// <param name="address">Base device address (http://1.2.3.4)</param>
|
||||
/// <param name="all">true -> read from all cards | false -> read from configured cards</param>
|
||||
/// <returns>List with data objects</returns>
|
||||
public async Task<List<RTD8TMDataModel>> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Loading…
Reference in New Issue