From 307f1097ee9cbe9b3df5be106925120aab642dd3 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Mon, 18 Nov 2024 10:53:40 +0100 Subject: [PATCH] Modifica Citizen x evitare disconnessione immediata in caso di problemi di rete/lettura --- IOB-UT-NEXT/ToMapo.cs | 10 ++ IOB-WIN-NEXT/DATA/CONF/L020.json | 2 + IOB-WIN-NEXT/IobRest/Base.cs | 17 ++- IOB-WIN-NEXT/IobRest/Citizen.cs | 206 ++++++++++++++++--------------- 4 files changed, 137 insertions(+), 98 deletions(-) diff --git a/IOB-UT-NEXT/ToMapo.cs b/IOB-UT-NEXT/ToMapo.cs index 818341e4..53f48f6c 100644 --- a/IOB-UT-NEXT/ToMapo.cs +++ b/IOB-UT-NEXT/ToMapo.cs @@ -738,6 +738,16 @@ namespace IOB_UT_NEXT /// public string apiUrl { get; set; } = ""; + /// + /// Periodo minimo di campionamento in ms x lettura dati frequenti stato/semafori... + /// + public int samplePeriod { get; set; } = 1000; + + /// + /// Numero di errori dopo cui fare una vera disconnesisone con report poweroff della macchina + /// + public int connErrorMax { get; set; } = 5; + /// /// Elenco delle chiamate x ID / struttura /// diff --git a/IOB-WIN-NEXT/DATA/CONF/L020.json b/IOB-WIN-NEXT/DATA/CONF/L020.json index 0f79f121..16b0469d 100644 --- a/IOB-WIN-NEXT/DATA/CONF/L020.json +++ b/IOB-WIN-NEXT/DATA/CONF/L020.json @@ -173,6 +173,8 @@ }, "timeOutSec": 60, "apiUrl": "http://192.168.80.61:8733/DRIVER_MES/rest/", + "samplePeriod": 1000, + "numErrDisconnect": 5, "CallList": { "GetConnection": { "Method": "Get", diff --git a/IOB-WIN-NEXT/IobRest/Base.cs b/IOB-WIN-NEXT/IobRest/Base.cs index 829ad7d4..e3046a50 100644 --- a/IOB-WIN-NEXT/IobRest/Base.cs +++ b/IOB-WIN-NEXT/IobRest/Base.cs @@ -192,7 +192,7 @@ namespace IOB_WIN_NEXT.IobRest // chiamo metodo connect var checkResp = ExecuteCallGet(GetUrlResource("GetConnection"), true); // forse va eliminato... - lgInfo($"GetConnection | {checkResp}"); + lgInfo($"GetConnection | {checkResp} (Base.tryConnect)"); if (checkResp != null && checkResp.ToLower().Contains("true")) { connectionOk = true; @@ -255,11 +255,23 @@ namespace IOB_WIN_NEXT.IobRest /// protected string apiUrl = "http://localhost:8733"; + /// + /// Num err connessioni corrente + /// + protected int connErrCur = 0; + + /// + /// Max num err connesisone prima di disconnettere + /// + protected int connErrMax = 5; + /// /// LookUpTable informazioni raccolte /// protected Dictionary RestDataLUT = new Dictionary(); + protected int samplePeriod = 1000; + /// /// Timeout chiamate REST /// @@ -593,6 +605,9 @@ namespace IOB_WIN_NEXT.IobRest // inizializzo dal file di conf le variabili necessarie... tOutSec = restParams.timeOutSec; apiUrl = restParams.apiUrl ?? "http://localhost:8733"; + // sistemo parametri specali come samplePeriod ed errori conn... + samplePeriod = restParams.samplePeriod; + connErrMax = restParams.connErrorMax; } } } diff --git a/IOB-WIN-NEXT/IobRest/Citizen.cs b/IOB-WIN-NEXT/IobRest/Citizen.cs index 301246b9..7fd4c8ab 100644 --- a/IOB-WIN-NEXT/IobRest/Citizen.cs +++ b/IOB-WIN-NEXT/IobRest/Citizen.cs @@ -36,6 +36,8 @@ namespace IOB_WIN_NEXT.IobRest { setupRestConf(getOptPar("REST_CONF")); } + // sistemo conf errori e samplePeriod... + lgInfo("Rest - 02"); // fixme todo levare forzatura test iniziale... if (EnableTest) @@ -48,67 +50,6 @@ namespace IOB_WIN_NEXT.IobRest #region Public Methods - /// - /// Effettua scrittura set di parametri del job e restitusice task eseguito... - /// - /// - private bool SetJobDataCitizen() - { - bool answ = false; - string varValue = ""; - CitizenJobData jobReq = new CitizenJobData(); - QtyData qtyReq = new QtyData(); - - // Token! - varValue = RestLutGet("token"); - jobReq.token = varValue; - qtyReq.token = varValue; - - // Commessa - varValue = getCurrProdData("setComm", ""); - jobReq.ordercode = varValue; - - // Articolo - varValue = getCurrProdData("setArt", ""); - jobReq.itemcode = varValue; - jobReq.itemdescription = $"ART: {varValue}"; - - // Qty - varValue = getCurrProdData("setPzComm", ""); - int pzReq = 0; - int.TryParse(varValue, out pzReq); - jobReq.piecestodo = pzReq; - qtyReq.value = pzReq; - - try - { - // preparo payload - string jobPayload = JsonConvert.SerializeObject(jobReq); - // faccio chiamata POST x invio - var rawResp = ExecuteCallPost(GetUrlResource("SetJobData"), jobPayload); - bool.TryParse(rawResp, out answ); - // se andatae se ho la chaive x invio abilitata invio anche la qty richiesta... - if (answ) - { - string urlNeedQty = GetUrlResource("SetNeededQuantity"); - if (string.IsNullOrEmpty(urlNeedQty)) - { - lgInfo($"Set NeedQty NON abilitato, non invio"); - } - else - { - jobPayload = JsonConvert.SerializeObject(qtyReq); - var rawRespQty = ExecuteCallPost(GetUrlResource("SetNeededQuantity"), jobPayload); - } - } - } - catch (Exception exc) - { - lgError($"Eccezione in SetJobDataCitizen{Environment.NewLine}{exc}"); - } - return answ; - } - /// /// Implementazione custom esecuzione task specifici /// @@ -250,7 +191,7 @@ namespace IOB_WIN_NEXT.IobRest LadderIO ladderResp = new LadderIO(); // in primis testo che sia connessa... var checkResp = ExecuteCallGet(GetUrlResource("GetConnection"), true); - lgInfo($"GetConnection | {checkResp}"); + lgInfo($"GetConnection | {checkResp} | Citizen.processContapezzi"); bool.TryParse(checkResp, out checkMachine); // proseguo SOLO SE macchina OK, altrimenti disconnetto... @@ -310,7 +251,7 @@ namespace IOB_WIN_NEXT.IobRest // chiamo metodo connect var checkResp = ExecuteCallGet(GetUrlResource("GetConnection"), true); - lgInfo($"GetConnection | {checkResp}"); + lgInfo($"GetConnection | {checkResp} | Citizen.tryConnect"); bool.TryParse(checkResp, out checkMachine); if (checkMachine) { @@ -318,12 +259,12 @@ namespace IOB_WIN_NEXT.IobRest queueInEnabCurr = true; if (adpRunning) { - lgInfo("Connessione OK"); + lgInfo("Connessione Citizen OK"); } } else { - lgError($"Errore check connessione | checkResp: {checkResp}"); + lgError($"Errore check connessione Citizen | checkResp: {checkResp}"); } } catch (Exception exc) @@ -377,6 +318,22 @@ namespace IOB_WIN_NEXT.IobRest #endregion Public Properties } + /// + /// Struttura info JobData da inviare x setup + /// + internal class CitizenJobData + { + #region Public Properties + + public string itemcode { get; set; } = ""; + public string itemdescription { get; set; } = ""; + public string ordercode { get; set; } = ""; + public int piecestodo { get; set; } = 0; + public string token { get; set; } = ""; + + #endregion Public Properties + } + /// /// Struttura info restituiti x lettura Ladder /// @@ -393,6 +350,19 @@ namespace IOB_WIN_NEXT.IobRest #endregion Public Properties } + /// + /// Struttura info QtyData da inviare x impostazioni quantità + /// + internal class QtyData + { + #region Public Properties + + public string token { get; set; } = ""; + public int value { get; set; } = 0; + + #endregion Public Properties + } + /// /// Struttura dato AlarmState (singolo, corrente) /// @@ -408,34 +378,6 @@ namespace IOB_WIN_NEXT.IobRest #endregion Public Properties } - - /// - /// Struttura info JobData da inviare x setup - /// - internal class CitizenJobData - { - #region Public Properties - - public string token { get; set; } = ""; - public string ordercode { get; set; } = ""; - public string itemcode { get; set; } = ""; - public string itemdescription { get; set; } = ""; - public int piecestodo { get; set; } = 0; - - #endregion Public Properties - } - /// - /// Struttura info QtyData da inviare x impostazioni quantità - /// - internal class QtyData - { - #region Public Properties - - public string token { get; set; } = ""; - public int value { get; set; } = 0; - - #endregion Public Properties - } /// /// Struttura risposta x scrittura parametri numerici (es qty) /// @@ -546,12 +488,14 @@ namespace IOB_WIN_NEXT.IobRest LadderIO ladderResp = new LadderIO(); // in primis testo che sia connessa... var checkResp = ExecuteCallGet(GetUrlResource("GetConnection"), true); - lgInfo($"GetConnection | {checkResp}"); + lgInfo($"GetConnection | {checkResp} | Citizen.refreshData"); bool.TryParse(checkResp, out checkMachine); // proseguo SOLO SE macchina OK, altrimenti disconnetto... if (checkMachine) { + //riporto err conn a zero + connErrCur = 0; // faccio refresh token comunque... string token = ExecuteCallGet(GetUrlResource("GetToken")); token = token.Replace("\"", ""); @@ -597,7 +541,16 @@ namespace IOB_WIN_NEXT.IobRest } else { - tryDisconnect(); + // verifico se devo disconnettere + if (connErrCur >= connErrMax) + { + connErrCur = 0; + tryDisconnect(); + } + else + { + connErrCur++; + } } } } @@ -612,8 +565,6 @@ namespace IOB_WIN_NEXT.IobRest private DateTime lastRestRefreshData = DateTime.Now; - private int samplePeriod = 1000; - #endregion Private Fields #region Private Properties @@ -700,6 +651,67 @@ namespace IOB_WIN_NEXT.IobRest } } + /// + /// Effettua scrittura set di parametri del job e restitusice task eseguito... + /// + /// + private bool SetJobDataCitizen() + { + bool answ = false; + string varValue = ""; + CitizenJobData jobReq = new CitizenJobData(); + QtyData qtyReq = new QtyData(); + + // Token! + varValue = RestLutGet("token"); + jobReq.token = varValue; + qtyReq.token = varValue; + + // Commessa + varValue = getCurrProdData("setComm", ""); + jobReq.ordercode = varValue; + + // Articolo + varValue = getCurrProdData("setArt", ""); + jobReq.itemcode = varValue; + jobReq.itemdescription = $"ART: {varValue}"; + + // Qty + varValue = getCurrProdData("setPzComm", ""); + int pzReq = 0; + int.TryParse(varValue, out pzReq); + jobReq.piecestodo = pzReq; + qtyReq.value = pzReq; + + try + { + // preparo payload + string jobPayload = JsonConvert.SerializeObject(jobReq); + // faccio chiamata POST x invio + var rawResp = ExecuteCallPost(GetUrlResource("SetJobData"), jobPayload); + bool.TryParse(rawResp, out answ); + // se andatae se ho la chaive x invio abilitata invio anche la qty richiesta... + if (answ) + { + string urlNeedQty = GetUrlResource("SetNeededQuantity"); + if (string.IsNullOrEmpty(urlNeedQty)) + { + lgInfo($"Set NeedQty NON abilitato, non invio"); + } + else + { + jobPayload = JsonConvert.SerializeObject(qtyReq); + var rawRespQty = ExecuteCallPost(GetUrlResource("SetNeededQuantity"), jobPayload); + } + } + } + catch (Exception exc) + { + lgError($"Eccezione in SetJobDataCitizen{Environment.NewLine}{exc}"); + } + return answ; + } + /// /// Test Chiamata Citizen ///