using MapoSDK; using Newtonsoft.Json; using Opc.Ua; using System; using System.Collections.Generic; namespace IOB_WIN_NEXT { public class IobOpcUaEwonMonti : IobOpcUaEwon { #region Public Constructors /// /// Estende l'init della classe base, impiegando il pacchetto Nuget OPC-UA foundation con la gestione specifica per EWON (es Monti, Tenditalia) /// https://github.com/OPCFoundation/UA-.NETStandard /// /// /// public IobOpcUaEwonMonti(AdapterForm caller, IobConfiguration IOBConf) : base(caller, IOBConf) { lgInfo("Init Ewon versione Monti (Tenditalia)"); // inizializzo classe base... if (!string.IsNullOrEmpty(getOptPar("CHANGE_ODL_MODE"))) { CHANGE_ODL_MODE = getOptPar("CHANGE_ODL_MODE"); } // impostazioni specifiche Ewon di Monti forceResetInRun = true; sendKeyRichiesta = true; } #endregion Public Constructors #region Protected Methods /// /// Effettua vera scrittura parametri /// /// protected override void plcWriteParams(ref List updatedPar) { dataConf currMem = null; int byteSize = 0; string memAddrWrite = ""; string serObj = ""; if (updatedPar != null) { List nodes2Write = new List(); // controllo i parametri... ne gestisco 4... foreach (var item in updatedPar) { try { memAddrWrite = ""; int valInt = 0; double valReal = 0; // cerco in area memMapWrite... if (memMap.mMapWrite.ContainsKey(item.uid)) { // recupero! currMem = memMap.mMapWrite[item.uid]; byteSize = currMem.size; memAddrWrite = currMem.memAddr; WriteValue commWriteVal = new WriteValue(); commWriteVal.NodeId = new NodeId(currMem.memAddr); commWriteVal.AttributeId = Attributes.Value; commWriteVal.Value = new DataValue(); commWriteVal.Value.Value = item.reqValue; // faccio preliminarmente upsertKey... upsertKey(currMem.name, currMem.value); serObj = JsonConvert.SerializeObject(item); lgInfo($"Inizio processing plcWriteParams per {currMem.name} | valore richiesto {currMem.value}"); lgInfo($"---------------{Environment.NewLine}UPDATED PARAM:{Environment.NewLine}{serObj}{Environment.NewLine}---------------"); serObj = JsonConvert.SerializeObject(currMem); lgInfo($"---------------{Environment.NewLine}MEMORY CONTENT:{Environment.NewLine}{serObj}{Environment.NewLine}---------------"); switch (currMem.tipoMem) { case plcDataType.Boolean: break; case plcDataType.Int: case plcDataType.DInt: case plcDataType.Word: case plcDataType.DWord: int.TryParse(item.reqValue, out valInt); commWriteVal.Value.Value = valInt; memAddrWrite = currMem.memAddr; break; case plcDataType.Real: double.TryParse(item.reqValue, out valReal); commWriteVal.Value.Value = valReal; break; case plcDataType.String: // verifico caso speciale: se è art/comm scrivo AFFIANCATE... if (item.uid == "setArt" | item.uid == "setComm") { // accodo commessa + articolo con padding secondo lunghezza... string codArt = ""; if (currProdData.ContainsKey("setArt")) { codArt = string.IsNullOrEmpty(currProdData["setArt"]) ? "" : currProdData["setArt"]; } string codComm = ""; if (currProdData.ContainsKey("setComm")) { codComm = string.IsNullOrEmpty(currProdData["setComm"]) ? "" : currProdData["setComm"]; } // padding... codArt = codArt.PadRight(20, ' '); codComm = codComm.PadRight(20, ' '); commWriteVal.Value.Value = $"{codComm}{codArt}"; } break; default: break; } lgInfo($"---------------{Environment.NewLine}OPC-UA data:{Environment.NewLine}NodeId: {commWriteVal.NodeId}{Environment.NewLine}Value: {commWriteVal.Value.Value}{Environment.NewLine}---------------"); if (!string.IsNullOrEmpty(memAddrWrite)) { nodes2Write.Add(commWriteVal); } else { lgInfo($"Errore: memAddrWrite vuoto!"); } } else { lgInfo($"Errore uid non trovato in area write memory: {item.uid}, ci sono {memMap.mMapWrite.Count} in area write"); } } catch (Exception exc) { lgError($"Eccezione in fase di plcWriteParams per item {item.uid} con valore {item.value}{Environment.NewLine}{exc}"); } } if (nodes2Write.Count > 0) { UA_ref.WriteNodes(nodes2Write); } } } #endregion Protected Methods #region Public Methods /// /// Effettua vero processing contapezzi /// public override void processContapezzi() { if (utils.CRB("enableContapezzi")) { // check condizione validazione if (checkMultiCondition(opcUaParams.condCountEnabled) || opcUaParams.condCountEnabled.checkList.Count == 0) { // cerco parametro contapezzi... string currPzCount = getDataItemValue(opcUaParams.keyPartCount); // se ho un contapezzi... processo... if (!string.IsNullOrEmpty(currPzCount)) { int newVal = -1; bool fatto = Int32.TryParse(currPzCount, out newVal); if (fatto) { // gestione decremento contapezzi: viene "messo via" solo SE c'è un effettivo decremento contapezzi... if (newVal < contapezziPLC) { pzCountResetted = true; // incremento contatore richiesta countKeyRichiesta = countKeyRichiesta + 1; // log lgInfo("Contapezzi resettato (PLC) --> pzCountResetted = true"); } // salvo nuovo valore contapezziPLC contapezziPLC = newVal > -1 ? newVal : contapezziPLC; } else { lgError($"Errore in decodifica valore contapezzi, valore rilevato: {currPzCount}"); } } else { lgError("Errore in decodifica valore contapezzi, valore vuoto!"); } } if (CHANGE_ODL_MODE == "PZCOUNT_RESET") { // controllo comunque, se è ZERO il contapezzi, e sul server è maggiore il valore x ODL e NON abilitato il trigger reset --> abilito trigger... if (!pzCountResetted && contapezziPLC == 0 && contapezziIOB > 0) { pzCountResetted = true; // incremento contatore richiesta countKeyRichiesta = countKeyRichiesta + 1; // log lgInfo("Contapezzi resettato (PLC==0 e IOB>PLC) --> pzCountResetted = true"); } } } } #endregion Public Methods } }