using System.Timers; using UI_SequentMicrosystems.Models; using Wrapper_Api_SequentMicrosystems.RTD8TM; namespace UI_SequentMicrosystems.Services { public class RTD8TMService { private SortedList ChanelNames = new SortedList(); private SortedList ActualValues = new SortedList(); private List 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 /// /// Set Device Address /// /// Address [Base url] (http://1.2.3.4) public void SetAddress(string address) { Address = address; GetChanelsNames(); } /// /// Get used device Address /// /// Address or empty string public string GetAddress() { if (Address == null) { return ""; } else { return Address; } } /// /// Read Actual values from Device /// 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++; } } /// /// Read configured chanels Names from Device /// public async void GetChanelsNames() { if (Address == null) return; ChanelNames = await _RTD8TM.GetNames(Address); AutoUpdateChanelsName(); } /// /// Set new Chanels Names /// /// chanels names sorted list #pragma warning disable CS1998 // V této asynchronní metodě chybí operátory await a spustí se synchronně. public async Task SetChanelsNames(SortedList 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); } /// /// Update Chanels Names for not null loading names /// 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(); } } }