From 5ace8f2fac3003d61eda95c9c3d0883446fa2371 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Fri, 9 Sep 2022 17:04:17 +0200 Subject: [PATCH] Gestione allarmi: - spostato da ModBus/Siemens a IobGeneric metodo hasAlarms - implementaizoni in override di base --- IOB-WIN-NEXT/IobGeneric.cs | 194 +++++++++++++++++++++++- IOB-WIN-NEXT/IobModbusTCP.cs | 140 ++++++++++------- IOB-WIN-NEXT/IobModbusTCPCedax.cs | 2 +- IOB-WIN-NEXT/IobModbusTCPCenterfrigo.cs | 2 +- IOB-WIN-NEXT/IobModbusTCPHam.cs | 2 +- IOB-WIN-NEXT/IobModbusTCPHelpi.cs | 4 +- IOB-WIN-NEXT/IobSiemens.cs | 62 ++++---- IOB-WIN-NEXT/IobSiemensComeca.cs | 2 +- IOB-WIN-NEXT/IobSiemensNWSE.cs | 2 +- 9 files changed, 317 insertions(+), 93 deletions(-) diff --git a/IOB-WIN-NEXT/IobGeneric.cs b/IOB-WIN-NEXT/IobGeneric.cs index ef08ecc8..45f1496b 100644 --- a/IOB-WIN-NEXT/IobGeneric.cs +++ b/IOB-WIN-NEXT/IobGeneric.cs @@ -1958,6 +1958,185 @@ namespace IOB_WIN_NEXT return outVal; } + /// + /// Implementazione di riferimento della verifica stato allarmi + /// + /// + /// + protected virtual int getAlarmStatus(BaseAlarmConf item) + { + int answ = 0; + return answ; + } + /// + /// Implementazione di riferimento della verifica stato allarmi formato UInt + /// + /// + /// + protected virtual uint getAlarmStatusUInt(BaseAlarmConf item) + { + uint answ = 0; + return answ; + } + + /// + /// Verifica presenza eventuali allarmi + /// + /// + protected virtual bool hasAlarms() + { + bool answ = false; + int numErrors = 0; + int currStatus = 0; + int[] listInt = new int[2]; + if (alarmMaps != null) + { + // leggo a ciclo le aree degli allarmi CONFIGURATI, se ne trovo --> segnalo allarme... + foreach (var item in alarmMaps) + { + // banchi in array Int16 --> scompongo + for (int i = 0; i < item.size / 2; i++) + { +#if false + // verifico se usare LUT + bool useLUT = memSetR != null && memSetR.Count > 0; + if (useLUT) + { + int lutAddress = 40000 + item.index; + if (HoldingRegisterLUT.ContainsKey(lutAddress)) + { + try + { + listInt = HoldingRegisterLUT[lutAddress]; + } + catch (Exception exc) + { + lgError($"Errore in lettura da HoldingRegisterLUT per indirizzo {lutAddress} | item {item.memAddr}{Environment.NewLine}{exc}"); + } + } + } + else + { + listInt = readInputReg(item.index, item.size); + } + currStatus = ModbusClient.ConvertRegistersToInt(listInt); +#endif + currStatus = getAlarmStatus(item); + + // aggiornamento blink counters dato nuovo valore + item.checkBlinkCounter(i, (uint)currStatus); + + // calcolo indice errori + if ((uint)(item.alarmsMask[i] & currStatus) > 0) + { + numErrors++; + } + // verifico SE sia variato... confronto allarmi filtrato stile blink per + // bit status: + // - allarmi che iniziano per # IGNORATI + // - altri allarmi con un countdown da MAX_COUNTER_BLINK a 0 per il + // fronte di discesa + if (item.isChanged(i, (uint)currStatus)) + { + // registro gli allarmi attivi e trasmetto... + if (sendAlarmVariations(item.memAddr, i, item.alarmsState[i], (uint)(item.alarmsMask[i] & currStatus), item.messages)) + { + // se inviato --> salvo stato da current... + item.updStatusVal(i, (uint)(item.alarmsMask[i] & currStatus)); + } + } + } + } + } + answ = numErrors > 0; + return answ; + } + + /// + /// Processing degli allarmi (se presenti e gestiti) + /// + /// + public virtual Dictionary getAlarmData() + { + Dictionary outVal = new Dictionary(); + // ora aggiungo (se ci fossero) gli allarmi... + if (alarmMaps != null && alarmMaps.Count > 0) + { + if (hasAlarms()) + { + string bankVal = ""; + foreach (var item in alarmMaps) + { + bankVal = ""; + var currState = currAlarmsState(item); + // calcolo vettore stringa degli allarmi... + bankVal = getAlarmState(item, currState); + bankVal = string.IsNullOrEmpty(bankVal) ? "OK" : bankVal; + saveAlarmString(ref outVal, bankVal, item.description); + } + } + } + + if (periodicLog || outVal.Count > 0) + { + lgDebug($"Esito getAlarmData: {outVal.Count} allarmi attivi/validi in outVal"); + } + return outVal; + } + + /// + /// Restituisce stato allarmi in formato byte[] + /// + /// + /// + protected byte[] currAlarmsState(BaseAlarmConf item) + { + byte[] answ = new byte[item.size]; + int currStatus = 0; + int[] listInt = new int[2]; + // banchi in array Int16 --> scompongo + for (int i = 0; i < item.size / 2; i++) + { +#if false + // verifico se usare LUT + bool useLUT = memSetR != null && memSetR.Count > 0; + if (useLUT) + { + int lutAddress = 40000 + item.index; + if (HoldingRegisterLUT.ContainsKey(lutAddress)) + { + try + { + listInt = HoldingRegisterLUT[lutAddress]; + } + catch (Exception exc) + { + lgError($"Errore in lettura da HoldingRegisterLUT per indirizzo {lutAddress} | item {item.memAddr}{Environment.NewLine}{exc}"); + } + } + } + else + { + listInt = readInputReg(item.index, item.size); + } + currStatus = ModbusClient.ConvertRegistersToInt(listInt); +#endif + currStatus = getAlarmStatus(item); + + // aggiornamento blink counters dato nuovo valore + item.checkBlinkCounter(i, (uint)currStatus); + + // calcolo indice errori + if ((uint)(item.alarmsMask[i] & currStatus) > 0) + { + var currByte = BitConverter.GetBytes(currStatus); + // salvo nell'array di byte + Buffer.BlockCopy(currByte, 0, answ, i * 2, 2); + } + } + return answ; + } + /// /// Cerca se esiste il parametro opzionale e lo restituisce /// @@ -2568,6 +2747,19 @@ namespace IOB_WIN_NEXT if (connectionOk) { currDynData = getDynData(); + var currAlarmData = getAlarmData(); + // se ho allarmi + if (currAlarmData != null && currAlarmData.Count > 0) + { + foreach (var item in currAlarmData) + { + if (!currDynData.ContainsKey(item.Key)) + { + // aggiungo! + currDynData.Add(item.Key, item.Value); + } + } + } try { string sVal = ""; @@ -4637,6 +4829,7 @@ namespace IOB_WIN_NEXT lgInfoStartup($"Apertura file {jsonFileName}"); StreamReader reader = new StreamReader(jsonFileName); string jsonData = reader.ReadToEnd(); + reader.Dispose(); if (!string.IsNullOrEmpty(jsonData)) { lgInfoStartup($"File json ALLARMI composto da {jsonData.Length} caratteri"); @@ -4654,7 +4847,6 @@ namespace IOB_WIN_NEXT { lgError("Errore in loadMemConf: file json vuoto!"); } - reader.Dispose(); } else { diff --git a/IOB-WIN-NEXT/IobModbusTCP.cs b/IOB-WIN-NEXT/IobModbusTCP.cs index 38a37bb4..71148a98 100644 --- a/IOB-WIN-NEXT/IobModbusTCP.cs +++ b/IOB-WIN-NEXT/IobModbusTCP.cs @@ -243,6 +243,8 @@ namespace IOB_WIN_NEXT outVal = new Dictionary(); tryConnect(); } + // refuso, gestito da getAlarmData di IobGeneric +#if false // ora aggiungo (se ci fossero) gli allarmi... if (alarmMaps != null && alarmMaps.Count > 0) { @@ -259,7 +261,8 @@ namespace IOB_WIN_NEXT saveAlarmString(ref outVal, bankVal, item.description); } } - } + } +#endif } } else @@ -599,74 +602,103 @@ namespace IOB_WIN_NEXT #region Protected Properties - protected bool hasAlarms + protected override int getAlarmStatus(BaseAlarmConf item) { - get + int answ = 0; + int[] listInt = new int[2]; + // verifico se usare LUT + bool useLUT = memSetR != null && memSetR.Count > 0; + if (useLUT) { - bool answ = false; - int numErrors = 0; - int currStatus = 0; - int[] listInt = new int[2]; - if (alarmMaps != null) + int lutAddress = 40000 + item.index; + if (HoldingRegisterLUT.ContainsKey(lutAddress)) { - // leggo a ciclo le aree degli allarmi CONFIGURATI, se ne trovo --> segnalo allarme... - foreach (var item in alarmMaps) + try { - // banchi in array Int16 --> scompongo - for (int i = 0; i < item.size / 2; i++) + listInt = HoldingRegisterLUT[lutAddress]; + } + catch (Exception exc) + { + lgError($"Errore in lettura da HoldingRegisterLUT per indirizzo {lutAddress} | item {item.memAddr}{Environment.NewLine}{exc}"); + } + } + } + else + { + listInt = readInputReg(item.index, item.size); + } + answ = ModbusClient.ConvertRegistersToInt(listInt); + return answ; + } + +#if false + // refuso: usato metodo base IobGeneric + protected override bool hasAlarms() + { + bool answ = false; + int numErrors = 0; + int currStatus = 0; + int[] listInt = new int[2]; + if (alarmMaps != null) + { + // leggo a ciclo le aree degli allarmi CONFIGURATI, se ne trovo --> segnalo allarme... + foreach (var item in alarmMaps) + { + // banchi in array Int16 --> scompongo + for (int i = 0; i < item.size / 2; i++) + { + // verifico se usare LUT + bool useLUT = memSetR != null && memSetR.Count > 0; + if (useLUT) { - // verifico se usare LUT - bool useLUT = memSetR != null && memSetR.Count > 0; - if (useLUT) + int lutAddress = 40000 + item.index; + if (HoldingRegisterLUT.ContainsKey(lutAddress)) { - int lutAddress = 40000 + item.index; - if (HoldingRegisterLUT.ContainsKey(lutAddress)) + try { - try - { - listInt = HoldingRegisterLUT[lutAddress]; - } - catch (Exception exc) - { - lgError($"Errore in lettura da HoldingRegisterLUT per indirizzo {lutAddress} | item {item.memAddr}{Environment.NewLine}{exc}"); - } + listInt = HoldingRegisterLUT[lutAddress]; + } + catch (Exception exc) + { + lgError($"Errore in lettura da HoldingRegisterLUT per indirizzo {lutAddress} | item {item.memAddr}{Environment.NewLine}{exc}"); } } - else - { - listInt = readInputReg(item.index, item.size); - } - currStatus = ModbusClient.ConvertRegistersToInt(listInt); + } + else + { + listInt = readInputReg(item.index, item.size); + } + currStatus = ModbusClient.ConvertRegistersToInt(listInt); - // aggiornamento blink counters dato nuovo valore - item.checkBlinkCounter(i, (uint)currStatus); + // aggiornamento blink counters dato nuovo valore + item.checkBlinkCounter(i, (uint)currStatus); - // calcolo indice errori - if ((uint)(item.alarmsMask[i] & currStatus) > 0) + // calcolo indice errori + if ((uint)(item.alarmsMask[i] & currStatus) > 0) + { + numErrors++; + } + // verifico SE sia variato... confronto allarmi filtrato stile blink per + // bit status: + // - allarmi che iniziano per # IGNORATI + // - altri allarmi con un countdown da MAX_COUNTER_BLINK a 0 per il + // fronte di discesa + if (item.isChanged(i, (uint)currStatus)) + { + // registro gli allarmi attivi e trasmetto... + if (sendAlarmVariations(item.memAddr, i, item.alarmsState[i], (uint)(item.alarmsMask[i] & currStatus), item.messages)) { - numErrors++; - } - // verifico SE sia variato... confronto allarmi filtrato stile blink per - // bit status: - // - allarmi che iniziano per # IGNORATI - // - altri allarmi con un countdown da MAX_COUNTER_BLINK a 0 per il - // fronte di discesa - if (item.isChanged(i, (uint)currStatus)) - { - // registro gli allarmi attivi e trasmetto... - if (sendAlarmVariations(item.memAddr, i, item.alarmsState[i], (uint)(item.alarmsMask[i] & currStatus), item.messages)) - { - // se inviato --> salvo stato da current... - item.updStatusVal(i, (uint)(item.alarmsMask[i] & currStatus)); - } + // se inviato --> salvo stato da current... + item.updStatusVal(i, (uint)(item.alarmsMask[i] & currStatus)); } } } } - answ = numErrors > 0; - return answ; } - } + answ = numErrors > 0; + return answ; + } +#endif /// /// Dizionario delle ultime operazioni di scrittura per OGNI memoria (in modo che fa log @@ -678,6 +710,7 @@ namespace IOB_WIN_NEXT #region Protected Methods +#if false /// /// Restituisce stato allarmi in formato byte[] /// @@ -726,7 +759,8 @@ namespace IOB_WIN_NEXT } } return answ; - } + } +#endif /// /// Decodifica il resto dell'area x i dati accessori (allarmi, ...) diff --git a/IOB-WIN-NEXT/IobModbusTCPCedax.cs b/IOB-WIN-NEXT/IobModbusTCPCedax.cs index 66501e2e..d953287b 100644 --- a/IOB-WIN-NEXT/IobModbusTCPCedax.cs +++ b/IOB-WIN-NEXT/IobModbusTCPCedax.cs @@ -79,7 +79,7 @@ namespace IOB_WIN_NEXT } // processo dagli stati + gravi... - if (hasAlarms) + if (hasAlarms()) { byteSignals += (1 << 3); } diff --git a/IOB-WIN-NEXT/IobModbusTCPCenterfrigo.cs b/IOB-WIN-NEXT/IobModbusTCPCenterfrigo.cs index 52adbcd3..d5d774a9 100644 --- a/IOB-WIN-NEXT/IobModbusTCPCenterfrigo.cs +++ b/IOB-WIN-NEXT/IobModbusTCPCenterfrigo.cs @@ -82,7 +82,7 @@ namespace IOB_WIN_NEXT } // processo dagli stati + gravi... - if (hasAlarms) + if (hasAlarms()) { byteSignals += (1 << 3); } diff --git a/IOB-WIN-NEXT/IobModbusTCPHam.cs b/IOB-WIN-NEXT/IobModbusTCPHam.cs index 627374b1..6bb223cb 100644 --- a/IOB-WIN-NEXT/IobModbusTCPHam.cs +++ b/IOB-WIN-NEXT/IobModbusTCPHam.cs @@ -73,7 +73,7 @@ namespace IOB_WIN_NEXT } // processo dagli stati + gravi... - if (hasAlarms) + if (hasAlarms()) { byteSignals += (1 << 3); } diff --git a/IOB-WIN-NEXT/IobModbusTCPHelpi.cs b/IOB-WIN-NEXT/IobModbusTCPHelpi.cs index 35ec1d1c..f9efb7c3 100644 --- a/IOB-WIN-NEXT/IobModbusTCPHelpi.cs +++ b/IOB-WIN-NEXT/IobModbusTCPHelpi.cs @@ -79,7 +79,7 @@ namespace IOB_WIN_NEXT // hard coded int statusReg = 41094; // deve avere allarmi (รจ un allarme EStop) - if (hasAlarms) + if (hasAlarms()) { int[] listInt = new int[2]; listInt = HoldingRegisterLUT[statusReg]; @@ -150,7 +150,7 @@ namespace IOB_WIN_NEXT { byteSignals += (1 << 7); } - else if (hasAlarms) + else if (hasAlarms()) { byteSignals += (1 << 3); } diff --git a/IOB-WIN-NEXT/IobSiemens.cs b/IOB-WIN-NEXT/IobSiemens.cs index 3a6ac177..99591367 100644 --- a/IOB-WIN-NEXT/IobSiemens.cs +++ b/IOB-WIN-NEXT/IobSiemens.cs @@ -1146,52 +1146,50 @@ namespace IOB_WIN_NEXT #region Protected Properties + /// /// Verifica presenza allarmi (tra quelli configurati) /// - protected bool hasAlarms + protected override bool hasAlarms() { - get + bool answ = false; + int numErrors = 0; + uint currStatus = 0; + if (alarmMaps != null) { - bool answ = false; - int numErrors = 0; - uint currStatus = 0; - if (alarmMaps != null) + // leggo a ciclo le aree degli allarmi CONFIGURATI, se ne trovo --> segnalo allarme... + foreach (var item in alarmMaps) { - // leggo a ciclo le aree degli allarmi CONFIGURATI, se ne trovo --> segnalo allarme... - foreach (var item in alarmMaps) + // banchi in WORD (2 byte) --> scompongo + for (int i = 0; i < item.size / 2; i++) { - // banchi in WORD (2 byte) --> scompongo - for (int i = 0; i < item.size / 2; i++) - { - currStatus = S7.Net.Types.Counter.FromByteArray(RawInput.Skip(item.index + 2 * i).Take(2).ToArray()); - // verifica e decremento blink... - item.checkBlinkCounter(i, (uint)currStatus); + currStatus = S7.Net.Types.Counter.FromByteArray(RawInput.Skip(item.index + 2 * i).Take(2).ToArray()); + // verifica e decremento blink... + item.checkBlinkCounter(i, (uint)currStatus); - // verifico SE sia variato... confronto allarmi filtrato stile blink per - // bit status: - // - allarmi che iniziano per # IGNORATI - // - altri allarmi con un countdown da MAX_COUNTER_BLINK a 0 per il - // fronte di discesa - if (item.isChanged(i, currStatus)) + // verifico SE sia variato... confronto allarmi filtrato stile blink per + // bit status: + // - allarmi che iniziano per # IGNORATI + // - altri allarmi con un countdown da MAX_COUNTER_BLINK a 0 per il + // fronte di discesa + if (item.isChanged(i, currStatus)) + { + if (currStatus > 0) { - if (currStatus > 0) - { - numErrors++; - } - // registro gli allarmi attivi e trasmetto... - if (sendAlarmVariations(item.memAddr, i, item.alarmsState[i], (uint)(item.alarmsMask[i] & currStatus), item.messages)) - { - // se inviato --> salvo stato da current... - item.updStatusVal(i, (uint)(item.alarmsMask[i] & currStatus)); - } + numErrors++; + } + // registro gli allarmi attivi e trasmetto... + if (sendAlarmVariations(item.memAddr, i, item.alarmsState[i], (uint)(item.alarmsMask[i] & currStatus), item.messages)) + { + // se inviato --> salvo stato da current... + item.updStatusVal(i, (uint)(item.alarmsMask[i] & currStatus)); } } } } - answ = numErrors > 0; - return answ; } + answ = numErrors > 0; + return answ; } /// diff --git a/IOB-WIN-NEXT/IobSiemensComeca.cs b/IOB-WIN-NEXT/IobSiemensComeca.cs index 00b902af..40da6375 100644 --- a/IOB-WIN-NEXT/IobSiemensComeca.cs +++ b/IOB-WIN-NEXT/IobSiemensComeca.cs @@ -228,7 +228,7 @@ namespace IOB_WIN_NEXT } // processo dagli stati + gravi... - if (hasAlarms) + if (hasAlarms()) { byteSignals += (1 << 3); } diff --git a/IOB-WIN-NEXT/IobSiemensNWSE.cs b/IOB-WIN-NEXT/IobSiemensNWSE.cs index 45ece468..0979474a 100644 --- a/IOB-WIN-NEXT/IobSiemensNWSE.cs +++ b/IOB-WIN-NEXT/IobSiemensNWSE.cs @@ -195,7 +195,7 @@ namespace IOB_WIN_NEXT } // processo dagli stati + gravi... - if (hasAlarms) + if (hasAlarms()) { byteSignals += (1 << 3); }