UI_SequentMicrosystems-RPI/Services/RTD8TMService.cs

141 lines
3.6 KiB
C#

using System.Timers;
using UI_SequentMicrosystems.Models;
using Wrapper_Api_SequentMicrosystems.RTD8TM;
namespace UI_SequentMicrosystems.Services
{
public class RTD8TMService
{
private SortedList<byte, string[]> ChanelNames = new SortedList<byte, string[]>();
private SortedList<byte, float[]> ActualValues = new SortedList<byte, float[]>();
private List<RTD8TMGraphModel> GraphData = new();
private byte GraphDataCounterCount = 10;
private byte GraphDataCounter = 0;
private string? Address { get; set; }
private RTD8TM _RTD8TM = new RTD8TM();
private System.Timers.Timer _timer = new(1000);
public RTD8TMService()
{
_timer.AutoReset = true;
_timer.Elapsed += TimerElapsed;
_timer.Start();
}
//Main Parameters Work
/// <summary>
/// Set Device Address
/// </summary>
/// <param name="address">Address [Base url] (http://1.2.3.4)</param>
public void SetAddress(string address)
{
Address = address;
GetChanelsNames();
}
/// <summary>
/// Get used device Address
/// </summary>
/// <returns>Address or empty string</returns>
public string GetAddress()
{
if (Address == null)
{
return "";
}
else
{
return Address;
}
}
/// <summary>
/// Read Actual values from Device
/// </summary>
public async void GetActualValues()
{
if (Address == null) { return; }
ActualValues = await _RTD8TM.Get(Address);
if (GraphDataCounter >= GraphDataCounterCount)
{
GraphDataCounter = 0;
GraphData.Add(new() { Data = ActualValues, Time = DateTime.Now });
}
else
{
GraphDataCounter++;
}
}
/// <summary>
/// Read configured chanels Names from Device
/// </summary>
public async void GetChanelsNames()
{
if (Address == null) return;
ChanelNames = await _RTD8TM.GetNames(Address);
AutoUpdateChanelsName();
}
/// <summary>
/// Set new Chanels Names
/// </summary>
/// <param name="names">chanels names sorted list</param>
#pragma warning disable CS1998 // V této asynchronní metodě chybí operátory await a spustí se synchronně.
public async Task SetChanelsNames(SortedList<byte, string[]> names)
#pragma warning restore CS1998 // V této asynchronní metodě chybí operátory await a spustí se synchronně.
{
ChanelNames = names;
if (Address == null)
return;
_RTD8TM.PostNames(Address, ChanelNames);
}
/// <summary>
/// Update Chanels Names for not null loading names
/// </summary>
private void AutoUpdateChanelsName()
{
if (ActualValues.Count <= 0)
return;
foreach (byte key in ActualValues.Keys)
{
for (int i = 0; i < 8; i++)
{
if (ChanelNames[key][i] == null)
{
ChanelNames[key][i] = "----------";
}
}
}
}
private void TimerElapsed(object? o, ElapsedEventArgs? e)
{
GetActualValues();
AutoUpdateChanelsName();
}
}
}