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 SortedList ValuesType = 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 delegate Task AsyncEventHandler(object? sender, TEventArgs? e); public event AsyncEventHandler? EventUpdateValues; 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(); GetValueTypes(); } /// /// Get used device Address /// /// Address or empty string public string GetAddress() { if (Address == null) { return ""; } else { return Address; } } //Actual Data /// /// Read Actual values from Device /// private 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++; } } /// /// Request for actual data /// /// public SortedList GetActualData() { return ActualValues; } /// /// Get actual data Value /// /// Stack ID /// Chanel ID /// Measure Resistance public float GetActualData(byte stack, byte chanel) { if (ActualValues.ContainsKey(stack)) { return ActualValues[stack][chanel]; } else { return 0; } } //ChanelsNames /// /// 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() #pragma warning restore CS1998 // V této asynchronní metodě chybí operátory await a spustí se synchronně. { 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) { if (!ChanelNames.ContainsKey(key)) { ChanelNames.Add(key, new string[8]); } for (int i = 0; i < 8; i++) { if (ChanelNames[key][i] == null) { ChanelNames[key][i] = "----------"; } } } } /// /// Get chanel names /// /// Chanels names object public SortedList GetChanelNames() { return ChanelNames; } /// /// Set chanel Name /// /// Stack ID /// Chanel ID /// New Name public void SetChanelNames(byte stack, byte chanel, string name) { if (!ChanelNames.ContainsKey(stack)) { ChanelNames.Add(stack, new string[8]); } ChanelNames[stack][chanel] = name; } /// /// Read chanel name /// /// Stack ID /// Chanel ID /// Chanel name public string GetChanelName(byte stack, byte chanel) { if (ChanelNames.ContainsKey(stack)) { return ChanelNames[stack][chanel]; } else { return "----------"; } } /// /// Clear chanels Names to default /// public void ClearChanelNames() { ChanelNames.Clear(); AutoUpdateChanelsName(); } //Timer private async void TimerElapsed(object? o, ElapsedEventArgs? e) { GetActualValues(); AutoUpdateChanelsName(); if (EventUpdateValues != null) { await EventUpdateValues.Invoke(this, true); } } //Graph Data /// /// Get data for graph visualize /// /// saved Graphn Data public List GetGraphData() { return GraphData; } /// /// Clear data from Graph /// public void ClearGraph() { GraphData.Clear(); } //Values Types /// /// Set Value Type identifier /// /// Stack ID /// Chanel ID /// Value Type public void SetValueType(byte StackID, byte ChanelID, byte Type) { if (!ValuesType.ContainsKey(StackID)) { ValuesType.Add(StackID, new byte[8]); } ValuesType[StackID][ChanelID] = Type; } /// /// Get Value Type /// /// Stack ID /// Chanel ID /// Value Type public byte GetValueType(byte StackID, byte ChanelID) { if (ValuesType.ContainsKey(StackID)) { return ValuesType[StackID][ChanelID]; } else { return 0; } } /// /// Get ValueTypes From API /// public async void GetValueTypes() { if (Address == null) { return; } ValuesType = await _RTD8TM.GetValueTypes(Address); } /// /// Post ValueTypes to API /// public void PostValueTypes() { if (Address == null) { return; } _RTD8TM.PostValueTypes(Address, ValuesType); } public void ClearValueTypes() { ValuesType.Clear(); } } }