Files
Mapo-IOB-WIN/IOB-WIN-NEXT/IobOpc/OpcUaCMS.cs
T
Samuele Locatelli 8bb0f158b5 SPLIT PROGETTO!!!
- proj di base con le 2 form da ereditare
- progetto globale che contiene TUTTI gli adapter (pronto a venire spezzettato
- gettate le basi x "portare fuori" i vari componenti oppure fare compilazione condizonale
2024-12-20 10:16:32 +01:00

375 lines
15 KiB
C#

using IOB_UT_NEXT;
using MapoSDK;
using Newtonsoft.Json;
using Opc.Ua;
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
namespace IOB_WIN_NEXT.IobOpc
{
public class OpcUaCMS : OpcUa
{
#region Public Constructors
/// <summary>
/// Estende l'init della classe base, impiegando il pacchetto Nuget OPC-UA foundation con la
/// gestione specifica per CMS/SCM (es. Eidos termoformatrice)
/// https://github.com/OPCFoundation/UA-.NETStandard
/// </summary>
/// <param name="caller"></param>
/// <param name="IOBConf"></param>
public OpcUaCMS(AdapterFormNext caller, IobConfiguration IOBConf) : base(caller, IOBConf)
{
// inizializzo classe base...
if (!string.IsNullOrEmpty(getOptPar("CHANGE_ODL_MODE")))
{
CHANGE_ODL_MODE = getOptPar("CHANGE_ODL_MODE");
}
// impostazioni specifiche OpcUA SCM
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Processo i task richiesti e li elimino dalla coda 1:1
/// </summary>
/// <param name="task2exe"></param>
public override Dictionary<string, string> executeTasks(Dictionary<string, string> task2exe)
{
// uso metodo base x ora
return base.executeTasks(task2exe);
}
/// <summary>
/// Effettua vero processing contapezzi
/// </summary>
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 da {opcUaParams.keyPartCount} | 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");
}
}
}
}
/// <summary>
/// Effettua reset del contapezzi, NON POSSIBILE in questa versione
/// </summary>
/// <returns></returns>
public override bool resetContapezziPLC()
{
bool answ = false;
return answ;
}
/// <summary>
/// Effettua IMPOSTAZIONE FORZATA del contapezzi, NON POSSIBILE in questa versione
/// </summary>
/// <returns></returns>
public override bool setcontapezziPLC(int newPzCount)
{
bool answ = false;
return answ;
}
#endregion Public Methods
#region Protected Fields
protected bool testDone = false;
#endregion Protected Fields
#region Protected Methods
/// <summary>
/// Effettua decodifica aree memoria alla bitmap usata x MAPO
/// </summary>
protected override void decodeToBaseBitmap()
{
DateTime adesso = DateTime.Now;
// init a zero...
B_input = 0;
/* -----------------------------------------------------
* STATE MACHINE 60 STD / SIMULA
*------------------------------------------------------
* bitmap MAPO
* B0: POWER_ON
* B1: RUN
* B2: pzCount
* B3: allarme
* B4: manuale
* B5: SlowTC (NON gestito qui)
* B6: warm-up / cool-down (NON GESTITO ?!?)
* B7: emergenza
---------------------------------------------------- */
// se valido il check ping lo eseguo... altrimenti lo do x buono
bool checkPing = !opcUaParams.pingAsPowerOn;
string currRun = "";
if (!checkPing)
{
checkPing = (testPingMachine == IPStatus.Success);
}
// bit 0 (poweron) imposto a 1 SE pingo + PowerOn=="ON"...
bool powerOnOk = checkPing && hasPowerOn;
// procedo SOLO SE mi da ping OK...
if (checkPing)
{
B_input = powerOnOk ? 1 : 0;
// variabili RUN...
currRun = getDataItemValue(opcUaParams.keyRunMode);
// variabili RUN... se richiesto invio runMode
if (opcUaParams.runModeSend)
{
if (!string.IsNullOrEmpty(currRun))
{
// se ho valore --> invio
string sVal = "";
string descr = $"RunModeVal";
DateTime locTStamp = DateTime.Now;
sVal = $"Change: {locTStamp.ToString()} | descr: {descr} | Id: {opcUaParams.keyRunMode} | Val: {currRun}";
accodaFLog(sVal, qEncodeFLog(descr, currRun));
// se richiesto provo a tradurre
if (opcUaParams.runModeTrad)
{
string RunModeDescr = itemTranslation("RunMode", currRun);
descr = $"RunMode";
accodaFLog(sVal, qEncodeFLog(descr, RunModeDescr));
}
}
}
// salvo running come = working...
isRunning = isWorking;
// se ho emergenza premuta --> emergenza!
if (hasEStopArmed)
{
B_input += (1 << 7);
}
//// se ho emergenza premuta --> emergenza!
//if (isWarmUpCoolDown)
//{
// B_input += (1 << 6);
//}
// se ho almeno 1 allarme E NON SONO IN AUTO --> ALARM!
if (hasError)
{
B_input += (1 << 3);
}
if (isWorking)
{
// RUN = LAVORA!
B_input += (1 << 1);
}
else if (powerOnOk && (!isReady || isManual))
{
// se NON ready --> manual
B_input += (1 << 4);
}
}
// controllo se sono poweroff e se non ho dati buoni da > 2 minuti --> disconnetto
if (adesso.Subtract(lastCurrent).TotalMinutes > 5)
{
tryDisconnect();
}
// solo se non ho veto check
int vFactor = 2;
if (vetoCheckStatus < adesso)
{
lgDebug($"Stato variabili checkPing: {testPingMachine}");
// imposto veto per vetoSeconds...
vetoCheckStatus = adesso.AddSeconds(vetoSeconds * vFactor);
}
// log opzionale!
if (verboseLog)
{
lgDebug($"Trasformazione checkPing: {checkPing} | hasPowerOn: {hasPowerOn} | B_input: {B_input} | currRun = {currRun}");
}
}
/// <summary>
/// Effettua vera scrittura parametri
/// </summary>
/// <param name="updatedPar"></param>
protected override void plcWriteParams(ref List<objItem> updatedPar)
{
lg.Info($"plcWriteParams: {updatedPar.Count} params to write");
dataConf currMem = null;
int byteSize = 0;
string memAddrWrite = "";
string serObj = "";
if (updatedPar != null)
{
List<WriteValue> nodes2Write = new List<WriteValue>();
// 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);
lgDebug($"Inizio processing plcWriteParams per {currMem.name} | valore richiesto {currMem.value}");
lgDebug($"---------------{Environment.NewLine}UPDATED PARAM:{Environment.NewLine}{serObj}{Environment.NewLine}---------------");
serObj = JsonConvert.SerializeObject(currMem);
lgDebug($"---------------{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);
// 2023.09.19 resetto richiesta
item.reqValue = "";
}
else
{
lgError($"Errore: memAddrWrite vuoto!");
}
}
else
{
lgError($"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
}
}