From 8bb0eb2031d776bbada1158141bed078229c0b22 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Mon, 24 Jul 2017 15:12:53 +0200 Subject: [PATCH] modifica oggetto gestione contatori istantanei con mediana (POI da testare se va bene va esteso a altri counters...) --- MTC_Adapter/MTC/baseUtils.cs | 117 ++++++++++++++++++++++ MTC_Adapter/MTC/packages.config | 2 + MTC_Adapter/MTC_Adapter/AdapterGeneric.cs | 43 ++++---- MTC_Adapter/MTC_Adapter/App.config | 4 +- 4 files changed, 145 insertions(+), 21 deletions(-) diff --git a/MTC_Adapter/MTC/baseUtils.cs b/MTC_Adapter/MTC/baseUtils.cs index 24d5085..1c397b9 100644 --- a/MTC_Adapter/MTC/baseUtils.cs +++ b/MTC_Adapter/MTC/baseUtils.cs @@ -6,6 +6,7 @@ using System.Data; using System.IO; using System.Linq; using NLog; +using AForge.Math; namespace MTC { @@ -357,7 +358,123 @@ namespace MTC } } + /// + /// Classe gestione valori campionati su periodo + /// + public class sampleVect + { + /// + /// Dimensione finestra di campionamento (secondi) + /// + protected int windSize; + /// + /// vettore valori temporali della serie + /// + protected List lTime; + /// + /// vettore valoti puntuali della serie + /// + protected List lVal; + /// + /// Inizializzo l'oggetto + /// + public sampleVect() + { + // init valori default... + windSize = baseUtils.CRI("countWindSize") > 0 ? baseUtils.CRI("countWindSize") : 60; + } + /// + /// Conteggio elementi + /// + protected int numElem + { + get + { + return lTime.Count; + } + } + /// + /// Verifica ampiezza finestra valori First-Last + /// + protected double flWindSize + { + get + { + double answ = 0; + if (numElem > 1) + { + answ = lTime.Last().Subtract(lTime[0]).TotalSeconds; + } + return answ; + } + } + /// + /// Verifica ampiezza finestra valori Second-Last + /// + protected double slWindSize + { + get + { + double answ = 0; + if (numElem > 2) // altrimenti SE non ne ho almeno 3 NON posso avere secondo/ultimo... + { + answ = lTime.Last().Subtract(lTime[1]).TotalSeconds; + } + return answ; + } + } + /// + /// Aggiunge un valore alla serie ed eventualmente elimina i valori superflui a garantirne una finestra temporale valida + /// + /// + /// + public void addValue(DateTime tempo, int valore) + { + lTime.Add(tempo); + lVal.Add(valore); + // verifico se siano da accorciare le serie... ovvero i 2 intervalli ENTRAMBI sono superiori al periodo minimo (in tal caso riduco.. + while (flWindSize > windSize && slWindSize > windSize) + { + // elimino i 2 valori + vecchi + lTime.RemoveAt(0); + lVal.RemoveAt(0); + // ora ricontrollo... + } + } + /// + /// Calcola il valore mediano... + /// + public double vcMedian + { + get + { + double answ = 0; + // restituisce la mediana SE valida, altrimenti null... + if (numElem > 1 && flWindSize > windSize) + { + try + { + // calcolo mediana! + answ = Statistics.Median(lVal.ToArray()); + } + catch + { } + } + return answ; + } + } + /// + /// Verifica se la vc sia valida (ovvero almeno 2 valori e intervallo > window richiesta) + /// + public bool vcValid + { + get + { + return (flWindSize > windSize && numElem > 1); + } + } + } public static class TimingData { diff --git a/MTC_Adapter/MTC/packages.config b/MTC_Adapter/MTC/packages.config index f5a50e5..321df3b 100644 --- a/MTC_Adapter/MTC/packages.config +++ b/MTC_Adapter/MTC/packages.config @@ -1,5 +1,7 @@  + + diff --git a/MTC_Adapter/MTC_Adapter/AdapterGeneric.cs b/MTC_Adapter/MTC_Adapter/AdapterGeneric.cs index 6afcffe..f2a4360 100644 --- a/MTC_Adapter/MTC_Adapter/AdapterGeneric.cs +++ b/MTC_Adapter/MTC_Adapter/AdapterGeneric.cs @@ -788,7 +788,7 @@ namespace MTC_Adapter /// /// Vettore ISTANTANEO dei contatori generici /// - public uint[] istCounters; + public sampleVect[] istCounters; /// /// Vettore ISTANTANEO dei contatori del numero movimenti Slitta Magazzino /// @@ -1445,7 +1445,7 @@ namespace MTC_Adapter // ora inizializzo l'insieme degli events dei COUNTERS generici... mCounters = new Event[numCounters]; currCounters = new uint[numCounters]; - istCounters = new uint[numCounters]; + istCounters = new sampleVect[numCounters]; int idx = 0; foreach (otherData item in maintData) { @@ -3353,7 +3353,8 @@ namespace MTC_Adapter } if (numero > 0) { - istCounters[numero - 1] = tabDatiMtz[i]; + //istCounters[numero - 1] = tabDatiMtz[i]; + istCounters[numero - 1].addValue(DateTime.Now, Convert.ToInt32(tabDatiMtz[i])); } } else if (maintData[i].varName.StartsWith("Path_")) @@ -4553,27 +4554,31 @@ namespace MTC_Adapter StringBuilder sb = new StringBuilder(); for (int i = 0; i < mCounters.Length; i++) { - delta = istCounters[i] - currCounters[i]; - // controllo delta < 50% max... - if (delta < uint.MaxValue / 2) + // procedo solo SE HO FINESTRA VALIDA... + if (istCounters[i].vcValid) { - //processo comunque sempre... - uint contTot = updateValUIntByIncr(i, delta, "Counter_{0:000}"); - // passo valore totale all'adapter - mCounters[i].Value = contTot; - // controllo valore riferimento... - if (delta > 0) + delta = Convert.ToUInt32(istCounters[i].vcMedian) - currCounters[i]; + // controllo delta < 50% max... + if (delta < uint.MaxValue / 2) { - // segnalo necessità salvataggio! - needSave = true; + //processo comunque sempre... + uint contTot = updateValUIntByIncr(i, delta, "Counter_{0:000}"); + // passo valore totale all'adapter + mCounters[i].Value = contTot; + // controllo valore riferimento... + if (delta > 0) + { + // segnalo necessità salvataggio! + needSave = true; + } + // ...aggiorno valore riferimento... + currCounters[i] = Convert.ToUInt32(istCounters[i].vcMedian); } - // ...aggiorno valore riferimento... - currCounters[i] = istCounters[i]; + sb.AppendLine(mCounters[i].ToString().Replace("|", " | ")); } - sb.AppendLine(mCounters[i].ToString().Replace("|", " | ")); + // salvo su maschera... + parentForm.dataMonitor_2 += sb.ToString(); } - // salvo su maschera... - parentForm.dataMonitor_2 += sb.ToString(); return needSave; } /// diff --git a/MTC_Adapter/MTC_Adapter/App.config b/MTC_Adapter/MTC_Adapter/App.config index f8c8429..7bb1ae2 100644 --- a/MTC_Adapter/MTC_Adapter/App.config +++ b/MTC_Adapter/MTC_Adapter/App.config @@ -15,8 +15,8 @@ - - + +