Added reading and sending Value Type data

master
Jan Beníček 2023-12-17 02:25:43 +01:00
parent a7cf60f6a9
commit 95749a5f00
2 changed files with 141 additions and 26 deletions

View File

@ -1,4 +1,5 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Net;
using System.Text; using System.Text;
namespace Wrapper_Api_SequentMicrosystems.Main namespace Wrapper_Api_SequentMicrosystems.Main
@ -28,11 +29,43 @@ namespace Wrapper_Api_SequentMicrosystems.Main
{ {
return JsonConvert.DeserializeObject<Type>(response);//await response.Content.ReadAsStringAsync()); return JsonConvert.DeserializeObject<Type>(response);//await response.Content.ReadAsStringAsync());
} }
catch (Exception ex) { } catch (WebException exc)
{ {
throw new Exception("GetData deserialize error"); Console.WriteLine("Network Error: " + exc.Message + "\nStatus code: " + exc.Status);
} }
catch (ProtocolViolationException exc)
{
Console.WriteLine("Protocol Error: " + exc.Message);
}
catch (UriFormatException exc)
{
Console.WriteLine("URI Format Error: " + exc.Message);
}
catch (NotSupportedException exc)
{
Console.WriteLine("Unknown Protocol: " + exc.Message);
}
catch (IOException exc)
{
Console.WriteLine("I/O Error: " + exc.Message);
}
catch (System.Security.SecurityException exc)
{
Console.WriteLine("Security Exception: " + exc.Message);
}
catch (InvalidOperationException exc)
{
Console.WriteLine("Invalid Operation: " + exc.Message);
}
catch (HttpRequestException exc)
{
Console.WriteLine("Invalid Operation: " + exc.Message);
}
catch (Exception exc)
{
Console.WriteLine("Invalid Operation: " + exc.Message);
}
return default(Type);
} }
/// <summary> /// <summary>
@ -46,17 +79,46 @@ namespace Wrapper_Api_SequentMicrosystems.Main
{ {
StringContent sc = new(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"); StringContent sc = new(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
try
{
HttpResponseMessage response = await _httpClient.PostAsync(new Uri(Endpoint), sc); HttpResponseMessage response = await _httpClient.PostAsync(new Uri(Endpoint), sc);
if (response.IsSuccessStatusCode)
{
return;
} }
else catch (WebException exc)
{ {
throw new Exception(response.EnsureSuccessStatusCode().Content.ToString()); Console.WriteLine("Network Error: " + exc.Message + "\nStatus code: " + exc.Status);
}
catch (ProtocolViolationException exc)
{
Console.WriteLine("Protocol Error: " + exc.Message);
}
catch (UriFormatException exc)
{
Console.WriteLine("URI Format Error: " + exc.Message);
}
catch (NotSupportedException exc)
{
Console.WriteLine("Unknown Protocol: " + exc.Message);
}
catch (IOException exc)
{
Console.WriteLine("I/O Error: " + exc.Message);
}
catch (System.Security.SecurityException exc)
{
Console.WriteLine("Security Exception: " + exc.Message);
}
catch (InvalidOperationException exc)
{
Console.WriteLine("Invalid Operation: " + exc.Message);
}
catch (HttpRequestException exc)
{
Console.WriteLine("Invalid Operation: " + exc.Message);
}
catch (Exception exc)
{
Console.WriteLine("Invalid Operation: " + exc.Message);
} }
} }
/// <summary> /// <summary>
@ -66,15 +128,45 @@ namespace Wrapper_Api_SequentMicrosystems.Main
/// <exception cref="Exception">Error message</exception> /// <exception cref="Exception">Error message</exception>
public async void DeleteData(string Endpoint) public async void DeleteData(string Endpoint)
{ {
HttpResponseMessage response = await _httpClient.DeleteAsync(new Uri(Endpoint)); try
if (response.IsSuccessStatusCode)
{ {
return; await _httpClient.DeleteAsync(new Uri(Endpoint));
} }
else catch (WebException exc)
{ {
throw new Exception(response.EnsureSuccessStatusCode().Content.ToString()); Console.WriteLine("Network Error: " + exc.Message + "\nStatus code: " + exc.Status);
}
catch (ProtocolViolationException exc)
{
Console.WriteLine("Protocol Error: " + exc.Message);
}
catch (UriFormatException exc)
{
Console.WriteLine("URI Format Error: " + exc.Message);
}
catch (NotSupportedException exc)
{
Console.WriteLine("Unknown Protocol: " + exc.Message);
}
catch (IOException exc)
{
Console.WriteLine("I/O Error: " + exc.Message);
}
catch (System.Security.SecurityException exc)
{
Console.WriteLine("Security Exception: " + exc.Message);
}
catch (InvalidOperationException exc)
{
Console.WriteLine("Invalid Operation: " + exc.Message);
}
catch (HttpRequestException exc)
{
Console.WriteLine("Invalid Operation: " + exc.Message);
}
catch (Exception exc)
{
Console.WriteLine("Invalid Operation: " + exc.Message);
} }
} }

View File

@ -31,14 +31,7 @@ namespace Wrapper_Api_SequentMicrosystems.RTD8TM
/// <returns>Deserialized data from API</returns> /// <returns>Deserialized data from API</returns>
public async Task<SortedList<byte, float[]>> Get(string address) public async Task<SortedList<byte, float[]>> Get(string address)
{ {
//try
//{
return await _client.GetData<SortedList<byte, float[]>>($"{address}api/RTDDA"); return await _client.GetData<SortedList<byte, float[]>>($"{address}api/RTDDA");
//}
//catch (Exception ex)
//{
// throw new Exception(ex.Message);
//}
} }
/// <summary> /// <summary>
@ -105,9 +98,9 @@ namespace Wrapper_Api_SequentMicrosystems.RTD8TM
/// </summary> /// </summary>
/// <param name="address">Base device address (http://1.2.3.4)</param> /// <param name="address">Base device address (http://1.2.3.4)</param>
/// <param name="data">Data object with data for Post to API</param> /// <param name="data">Data object with data for Post to API</param>
public async void PostNames(string address, SortedList<byte, string[]> data) public void PostNames(string address, SortedList<byte, string[]> data)
{ {
await Task.Run(() => _client.PostData<SortedList<byte, string[]>>($"{address}api/RTDDA/Names", data)); _client.PostData<SortedList<byte, string[]>>($"{address}api/RTDDA/Names", data);
} }
//Preconfigured Names //Preconfigured Names
@ -167,7 +160,37 @@ namespace Wrapper_Api_SequentMicrosystems.RTD8TM
{ {
await Task.Run(() => _client.PostData<SortedList<byte, float[]>>($"{address}api/RTDDA/Calibration", 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.
//Value Types
/// <summary>
/// Get Value Types settings
/// </summary>
/// <param name="address">Base device address (http://1.2.3.4)</param>
/// <returns>Deserialized Value Types from API</returns>
public async Task<SortedList<byte, byte[]>> GetValueTypes(string address)
{
try
{
return await _client.GetData<SortedList<byte, byte[]>>($"{address}api/RTDDA/ValueTypes");
}
catch
{
return new();
}
}
/// <summary>
/// Post Value Types 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 PostValueTypes(string address, SortedList<byte, byte[]> data)
{
await Task.Run(() => _client.PostData<SortedList<byte, byte[]>>($"{address}api/RTDDA/ValueTypes", data));
}
#pragma warning restore CS8603 // Může jít o vrácený odkaz null.
#endregion #endregion