From 03413fb499a82d21208face13e09aeea6a65a01d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ben=C3=AD=C4=8Dek?= Date: Thu, 1 Feb 2024 08:33:05 +0100 Subject: [PATCH] bugfixing --- Components/PointsTableRTD8TMComponent.razor | 2 +- Components/RTD8TMChanelComponent.razor | 9 +- Components/RTD8TMGraphComponent.razor | 77 +++++++---- Services/RTD8TMService.cs | 141 +++++++++++++++----- 4 files changed, 164 insertions(+), 65 deletions(-) diff --git a/Components/PointsTableRTD8TMComponent.razor b/Components/PointsTableRTD8TMComponent.razor index d2ce97e..5f49d63 100644 --- a/Components/PointsTableRTD8TMComponent.razor +++ b/Components/PointsTableRTD8TMComponent.razor @@ -135,11 +135,11 @@ else await InvokeAsync(() => { TableBackground = false; - StateHasChanged(); if (TableData.Keys.Count > 1) { HalfHourTime(); } + StateHasChanged(); }); } } diff --git a/Components/RTD8TMChanelComponent.razor b/Components/RTD8TMChanelComponent.razor index a823819..6ff97a4 100644 --- a/Components/RTD8TMChanelComponent.razor +++ b/Components/RTD8TMChanelComponent.razor @@ -6,7 +6,7 @@
-
@(RecalculateValues(RTD8tmService.GetActualData(StackID, ChanelID), RTD8tmService.GetValueType(StackID, ChanelID)))
+
@(Math.Round(RecalculateValues(RTD8tmService.GetActualData(StackID, ChanelID), RTD8tmService.GetValueType(StackID, ChanelID)), 2))
@if (Edit) { @@ -70,8 +70,13 @@ RTD8tmService.SetChanelNames(StackID, ChanelID, NewName); } - private void CalibrationChanged(string NewCalibration) + private void CalibrationChanged(string? NewCalibration) { + if (NewCalibration == null || NewCalibration == "") + { + NewCalibration = "0"; + } + NewCalibration = NewCalibration.Replace(".", ","); RTD8tmService.SetCalibration(StackID, ChanelID, float.Parse(NewCalibration)); } diff --git a/Components/RTD8TMGraphComponent.razor b/Components/RTD8TMGraphComponent.razor index 5943618..6408535 100644 --- a/Components/RTD8TMGraphComponent.razor +++ b/Components/RTD8TMGraphComponent.razor @@ -1,31 +1,47 @@ @using ApexCharts +@using Newtonsoft.Json @using UI_SequentMicrosystems.Components @using UI_SequentMicrosystems.Services @using UI_SequentMicrosystems.Models @inject RTD8TMService _RTD8TMService - - @foreach (byte stack in _RTD8TMService.GetActualData().Keys) +@if (_RTD8TMService.GetActualData().Keys.Count > 0) +{ + if (false) { - for (byte chanel = 0; chanel < 8; chanel++) +
+ +
+ } + + + + @foreach (byte stack in _RTD8TMService.GetActualData().Keys) { - if (_RTD8TMService.GetChanelName(stack, chanel) != "----------") + for (byte chanel = 0; chanel < 8; chanel++) { - + if (_RTD8TMService.GetChanelName(stack, chanel) != "----------") + { + //Console.WriteLine($"Chart Data: Stack: {stack} | Chanel: {chanel} | {JsonConvert.SerializeObject(_RTD8TMService.GetChartData(stack, chanel))}"); + + + + } } } - } - +
+} + @code { private ApexChart chart; @@ -36,20 +52,20 @@ { _RTD8TMService.EventUpdateGraph += UpdateChart; - chartOptions = new ApexChartOptions - { - Theme = new Theme { - Mode = Mode.Dark, - }, - Legend = new Legend - { - Position = LegendPosition.Bottom, - FontSize = "20px", - HorizontalAlign = Align.Center - } - }; + Theme = new Theme + { + Mode = Mode.Dark, + }, + Legend = new Legend + { + Position = LegendPosition.Bottom, + FontSize = "15px", + HorizontalAlign = Align.Center + } + }; + } public async Task UpdateChart(object? o, bool b) @@ -63,7 +79,10 @@ }); } - + public void ChartPointsAdd(string points) + { + _RTD8TMService.SetChartData(JsonConvert.DeserializeObject>>>(points)); + } diff --git a/Services/RTD8TMService.cs b/Services/RTD8TMService.cs index 0a131a6..d0ba4f5 100644 --- a/Services/RTD8TMService.cs +++ b/Services/RTD8TMService.cs @@ -1,4 +1,6 @@ -using System.Threading.Channels; +using Newtonsoft.Json; +using System.Diagnostics.Metrics; +using System.Threading.Channels; using System.Timers; using UI_SequentMicrosystems.Components; using UI_SequentMicrosystems.Models; @@ -14,13 +16,14 @@ namespace UI_SequentMicrosystems.Services private SortedList Calibrations = new SortedList(); public SortedList>> GraphData = new(); public SortedList>> GraphFiltered = new(); + private int GraphFilteredCountPoint { get; set; } private byte GraphDataCounterCount = 10; private byte GraphDataCounter = 9; private string? Address { get; set; } private RTD8TM _RTD8TM = new RTD8TM(); - private System.Timers.Timer _timer = new(1000); + private System.Timers.Timer _timer = new(1000); //update timer public delegate Task AsyncEventHandler(object? sender, TEventArgs? e); public event AsyncEventHandler? EventUpdateValues; @@ -75,35 +78,37 @@ namespace UI_SequentMicrosystems.Services private async void GetActualValues() { if (Address == null) { return; } - - ActualValues = await _RTD8TM.Get(Address); - foreach (byte stack in ActualValues.Keys) + try { - if (Calibrations.ContainsKey(stack)) + ActualValues = await _RTD8TM.Get(Address); + + foreach (byte stack in ActualValues.Keys) { - for (byte chanel = 0; chanel < 8; chanel++) + if (Calibrations.ContainsKey(stack)) { - ActualValues[stack][chanel] += Calibrations[stack][chanel]; + for (byte chanel = 0; chanel < 8; chanel++) + { + ActualValues[stack][chanel] += Calibrations[stack][chanel]; + } } } - } - if (GraphDataCounter >= GraphDataCounterCount) - { - GraphDataCounter = 0; - - ReadChartData(); - - if (EventUpdateGraph != null) + if (GraphDataCounter >= GraphDataCounterCount) { - await EventUpdateGraph.Invoke(this, true); + GraphDataCounter = 0; + + ReadChartData(); + } + else + { + GraphDataCounter++; } } - else + catch (Exception ex) { - GraphDataCounter++; + Console.WriteLine($"Reading actual Data fail with message: {ex.Message}"); } } @@ -288,21 +293,38 @@ namespace UI_SequentMicrosystems.Services /// saved chart chanel Data public List GetChartData(byte StackID, byte Chanel) { + Console.WriteLine($"RTD8TMService:GetChartData({StackID}, {Chanel}) - start reading"); + if (!GraphFiltered.ContainsKey(StackID)) { + Console.WriteLine($"RTD8TMService:GetChartData({StackID}, {Chanel}) - Return new() - Unknow StackID - Stack_Keys: {JsonConvert.SerializeObject(GraphFiltered.Keys)}"); return new List(); } List RecalculatedData = new(); byte chanelRecalcTo = GetValueType(StackID, Chanel); + RTD8TMChanelComponent _RTD8TMChanelComponent = new RTD8TMChanelComponent(); + float LastValue = -1; + int CountErrors = 0; + foreach (RTD8TMPointModel point in GraphFiltered[StackID][Chanel]) { - RTD8TMPointModel recalculated = new() { Time = point.Time, Value = new RTD8TMChanelComponent().RecalculateValues(point.Value, chanelRecalcTo) }; - RecalculatedData.Add(recalculated); + RTD8TMPointModel recalculated = new() { Time = point.Time, Value = _RTD8TMChanelComponent.RecalculateValues(point.Value, chanelRecalcTo) }; + + if (LastValue == -1 || CountErrors > 1 || (recalculated.Value >= (LastValue - 10) && recalculated.Value <= (LastValue + 10))) + { + RecalculatedData.Add(recalculated); + LastValue = recalculated.Value; + CountErrors = 0; + } + else + { + CountErrors++; + } } - + Console.WriteLine($"RTD8TMService:GetChartData({StackID}, {Chanel})"); return RecalculatedData; } @@ -315,21 +337,24 @@ namespace UI_SequentMicrosystems.Services GraphData.Clear(); } - private void GetFilteredChartData() + private async void GetFilteredChartData() { - foreach ( byte stack in GraphData.Keys) + GraphFiltered = new SortedList>>(); + + foreach (byte stack in GraphData.Keys) { int pointsCount = GraphData[stack][0].Count; int counter = 0; + Console.WriteLine($"RTD8TMService:GetFilteredChartData - Stack: {stack} | PointsCount: {pointsCount} | counter: {counter} | GraphData_Keys: {JsonConvert.SerializeObject(GraphData.Keys)}"); + if (pointsCount < 100) { GraphFiltered = GraphData; } - else if (pointsCount > 100 && pointsCount < 500) // one from five + else if (pointsCount >= 100 && pointsCount < 500) // one from five { - GraphFiltered = new(); - GraphFiltered.Add(stack, new()); + GraphFiltered.Add(stack, new List>()); for (int chanel = 0; chanel < 8; chanel++) { @@ -348,9 +373,8 @@ namespace UI_SequentMicrosystems.Services } } } - else if (pointsCount > 500 && pointsCount < 1000) // one from ten + else if (pointsCount >= 500 && pointsCount < 1000) // one from ten { - GraphFiltered = new(); GraphFiltered.Add(stack, new()); for (int chanel = 0; chanel < 8; chanel++) @@ -370,9 +394,8 @@ namespace UI_SequentMicrosystems.Services } } } - else if (pointsCount > 1000 && pointsCount < 1500) // one from fifteen + else if (pointsCount >= 1000 && pointsCount < 1500) // one from fifteen { - GraphFiltered = new(); GraphFiltered.Add(stack, new()); for (int chanel = 0; chanel < 8; chanel++) @@ -392,9 +415,8 @@ namespace UI_SequentMicrosystems.Services } } } - else if (pointsCount > 1500) // one from twenty + else if (pointsCount >= 1500 && pointsCount < 2000) // one from twenty { - GraphFiltered = new(); GraphFiltered.Add(stack, new()); for (int chanel = 0; chanel < 8; chanel++) @@ -414,9 +436,61 @@ namespace UI_SequentMicrosystems.Services } } } + else if (pointsCount >= 2000 && pointsCount < 3500) // one from TwentyFive + { + GraphFiltered.Add(stack, new()); + + for (int chanel = 0; chanel < 8; chanel++) + { + GraphFiltered[stack].Add(new()); + foreach (RTD8TMPointModel GraphPoint in GraphData[stack][chanel]) + { + if (counter > 23) + { + GraphFiltered[stack][chanel].Add(GraphPoint); + counter = 0; + } + else + { + counter++; + } + } + } + } + else if (pointsCount >= 3500) // one from twenty + { + GraphFiltered.Add(stack, new()); + + for (int chanel = 0; chanel < 8; chanel++) + { + GraphFiltered[stack].Add(new()); + foreach (RTD8TMPointModel GraphPoint in GraphData[stack][chanel]) + { + if (counter > 28) + { + GraphFiltered[stack][chanel].Add(GraphPoint); + counter = 0; + } + else + { + counter++; + } + } + } + } //případně další } + + if (EventUpdateGraph != null) + { + if (CountFilteredChartData() != GraphFilteredCountPoint) + { + GraphFilteredCountPoint = CountFilteredChartData(); + await EventUpdateGraph.Invoke(this, true); + } + } + Console.WriteLine($"RTD8TMService:GetFilteredChartData - GraphFiltered_Keys: {JsonConvert.SerializeObject(GraphFiltered.Keys)}"); } public int CountFilteredChartData() @@ -453,6 +527,7 @@ namespace UI_SequentMicrosystems.Services /// public void SetChartData(SortedList>> data) { + Console.WriteLine($"RTD8TMService:SetChartData - {JsonConvert.SerializeObject(data)}"); GraphData = data; }