From 9151413d450ed510da32b8af8a5b7f88dcecb39f Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 26 May 2026 10:46:12 +0200 Subject: [PATCH 1/2] Update gestione UI x Adapter --- IOB-WIN-FORM/AdapterForm.cs | 1075 ++++++++++++++----------------- IOB-WIN-FORM/MainForm.cs | 16 +- IOB-WIN-PING/AdapterFormNext.cs | 2 +- 3 files changed, 509 insertions(+), 584 deletions(-) diff --git a/IOB-WIN-FORM/AdapterForm.cs b/IOB-WIN-FORM/AdapterForm.cs index 8871edb9..7d67b2d8 100644 --- a/IOB-WIN-FORM/AdapterForm.cs +++ b/IOB-WIN-FORM/AdapterForm.cs @@ -1,6 +1,5 @@ using IOB_UT_NEXT; using IOB_UT_NEXT.Config; -using IOB_UT_NEXT.Config.Base; using IOB_UT_NEXT.Objects; using IOB_UT_NEXT.Services.Core; using IOB_UT_NEXT.Services.Files; @@ -13,7 +12,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; -using System.Diagnostics.Eventing.Reader; using System.Drawing; using System.IO; using System.Linq; @@ -29,16 +27,6 @@ namespace IOB_WIN_FORM { #region Public Fields - /// - /// oggetto logging - /// - public static Logger lg; - - /// - /// Configurazione gerarchica completa (v 4.x.x.x) - /// - public IobConfTree IOBConfFull; - /// /// Oggetto x gestione dell'adapter GENERICO (x poter usare metodi di ognuno...) /// @@ -79,55 +67,6 @@ namespace IOB_WIN_FORM #region Public Properties - public long alQueueLen - { - set - { - qAlLen = value; - lblQueueAlarmLen.Text = qAlLen.ToString(); - showQueueData(); - // se supero max precedente, ed è > 10... loggo! - if (qAlLen > maxAlQueue && qAlLen > 10) - { - maxAlQueue = qAlLen; - lgInfo($"[WARN] Coda FLog di {value} record"); - } - else - { - maxAlQueue--; - maxAlQueue = maxAlQueue < qAlLen ? qAlLen : maxAlQueue; - } - } - get - { - return qAlLen; - } - } - - /// - /// Button restart encapsulato x gestione da classi derivate - /// - public Button btnRestart - { - get => restart; - } - - /// - /// Button start encapsulato x gestione da classi derivate - /// - public Button btnStart - { - get => start; - } - - /// - /// Button stop encapsulato x gestione da classi derivate - /// - public Button btnStop - { - get => stop; - } - /// /// Visualizzazione stato di comunicazione attiva con PLC /// @@ -150,8 +89,7 @@ namespace IOB_WIN_FORM this.UIThread(delegate { lblCNC.ForeColor = value ? Color.SeaGreen : Color.Black; - //statusStrip1.Refresh(); - statusStrip1.Invalidate(); + //statusStrip1.Invalidate(); }); } } @@ -191,7 +129,7 @@ namespace IOB_WIN_FORM default: break; } - statusStrip1.Refresh(); + //statusStrip1.Invalidate(); }); } } @@ -204,7 +142,10 @@ namespace IOB_WIN_FORM } set { - lblPzCountIob.Text = value; + this.UIThread(delegate + { + lblPzCountIob.Text = value; + }); } } @@ -216,131 +157,10 @@ namespace IOB_WIN_FORM } set { - lblPzCountMac.Text = value; - } - } - - /// - /// Modello macchina - /// - public string curModel - { - get => IOBConfFull != null ? IOBConfFull.Device.Model : ""; - } - - /// - /// Vendor macchina - /// - public string curVendor - { - get => IOBConfFull != null ? IOBConfFull.Device.Vendor : ""; - } - - /// - /// Stringa dati monitoraggio mostrata (1 SX)... - /// - public string dataMonitor_0 - { - set - { - DateTime adesso = DateTime.Now; - // salvo in cima alla lista... - dMonValues[0].Insert(0, value); - // se supero limite --> trim! - if (dMonValues[0].Count > nLine2show) + this.UIThread(delegate { - dMonValues[0] = dMonValues[0].Take(nLine2show).ToList(); - } - if (dMonDisplVetoVeto[0] < adesso) - { - // update veto! - dMonDisplVetoVeto[0] = adesso.AddMilliseconds(delayShowLogMs); - - if (!ShouldUpdateUI()) return; - // se scaduto veto --> display! - lblRawData.Text = String.Join(Environment.NewLine, dMonValues[0]); - } - } - } - - /// - /// Stringa dati monitoraggio mostrata (1 SX)... - /// - public string dataMonitor_1 - { - set - { - DateTime adesso = DateTime.Now; - // salvo nell'array... - dMonValues[1].Insert(0, value); - // se supero limite --> trim! - if (dMonValues[1].Count > nLine2show) - { - dMonValues[1] = dMonValues[1].Take(nLine2show).ToList(); - } - if (dMonDisplVetoVeto[1] < adesso) - { - // update veto! - dMonDisplVetoVeto[1] = adesso.AddMilliseconds(delayShowLogMs); - - if (!ShouldUpdateUI()) return; - // se scaduto veto --> display! - lblOutMessage.Text = String.Join(Environment.NewLine, dMonValues[1]); - } - } - } - - /// - /// Stringa dati monitoraggio mostrata (2 centro)... - /// - public string dataMonitor_2 - { - set - { - DateTime adesso = DateTime.Now; - // salvo nell'array... - dMonValues[2].Insert(0, value); - // se supero limite --> trim! - if (dMonValues[2].Count > nLine2show) - { - dMonValues[2] = dMonValues[2].Take(nLine2show).ToList(); - } - if (dMonDisplVetoVeto[2] < adesso) - { - // update veto! - dMonDisplVetoVeto[2] = adesso.AddMilliseconds(delayShowLogMs); - - if (!ShouldUpdateUI()) return; - // se scaduto veto --> display! - lblOutMessage2.Text = String.Join(Environment.NewLine, dMonValues[2]); - } - } - } - - /// - /// Stringa dati monitoraggio mostrata (3 dx)... - /// - public string dataMonitor_3 - { - set - { - DateTime adesso = DateTime.Now; - // salvo nell'array... - dMonValues[3].Insert(0, value); - // se supero limite --> trim! - if (dMonValues[3].Count > nLine2show) - { - dMonValues[3] = dMonValues[3].Take(nLine2show).ToList(); - } - if (dMonDisplVetoVeto[3] < adesso) - { - // update veto! - dMonDisplVetoVeto[3] = adesso.AddMilliseconds(delayShowLogMs); - - if (!ShouldUpdateUI()) return; - // se scaduto veto --> display! - lblOutMessage3.Text = String.Join(Environment.NewLine, dMonValues[3]); - } + lblPzCountMac.Text = value; + }); } } @@ -355,92 +175,10 @@ namespace IOB_WIN_FORM } set { - lblDataProc.Text = value; - } - } - - /// - /// File configurazione default - /// - public string defConfFilePath - { - get - { - return Path.Combine(utils.confDir, $"{CurrIOB}.ini"); - } - } - - /// - /// File configurazione json (completo) se presente - /// - public string defConfFilePathJson - { - get - { - return Path.Combine(utils.confDir, $"{CurrIOB}.json"); - } - } - - /// - /// File configurazione yaml (completo) se presente - /// - public string defConfFilePathYaml - { - get - { - return Path.Combine(utils.confDir, $"{CurrIOB}.yaml"); - } - } - - public bool enableEditMes2Plc { get; set; } - - public long evQueueLen - { - set - { - qEvLen = value; - lblQueueLen.Text = qEvLen.ToString(); - showQueueData(); - // se supero max precedente, ed è > 10... loggo! - if (qEvLen > maxEvQueue && qEvLen > 10) + this.UIThread(delegate { - maxEvQueue = qEvLen; - lgInfo($"[WARN] Coda EV di {value} record"); - } - else - { - maxEvQueue--; - maxEvQueue = maxEvQueue < qEvLen ? qEvLen : maxEvQueue; - } - } - get - { - return qEvLen; - } - } - - public long flQueueLen - { - set - { - qFlLen = value; - lblQueueFLogLen.Text = qFlLen.ToString(); - showQueueData(); - // se supero max precedente, ed è > 10... loggo! - if (qFlLen > maxFlQueue && qFlLen > 10) - { - maxFlQueue = qFlLen; - lgInfo($"[WARN] Coda FLog di {value} record"); - } - else - { - maxFlQueue--; - maxFlQueue = maxFlQueue < qFlLen ? qFlLen : maxFlQueue; - } - } - get - { - return qFlLen; + lblDataProc.Text = value; + }); } } @@ -449,89 +187,6 @@ namespace IOB_WIN_FORM /// public bool isVerboseLog { get; set; } = utils.CRB("verbose"); - public string lblCncText - { - get => lblCNC.Text; - set => lblCNC.Text = value; - } - - public string lblSrvUrlText - { - get => lblSrvUrl.Text; - set => lblSrvUrl.Text = value; - } - - /// - /// Logwatcher (in modalità "accodamento in testa" ultimi messaggi...) - /// - public string logWatcher - { - get - { - return lblLogfile.Text; - } - set - { - try - { - // aggiungo in testa la NUOVA stringa (eventualmente multiline) - logWatchString.Enqueue(value); - // se supero limite --> trim! - string sTemp = ""; - while (logWatchString.Count > nLine2show) - { - logWatchString.TryDequeue(out sTemp); - //logWatchString = logWatchString.Take(nLine2show).ToList(); - } - DateTime adesso = DateTime.Now; - if (logWatchWriteVeto < adesso) - { - if (!ShouldUpdateUI()) return; - this.UIThread(delegate - { - lblLogfile.Text = string.Join(Environment.NewLine, logWatchString); - lblLogfile.Refresh(); - }); - - logWatchWriteVeto = adesso.AddMilliseconds(delayShowLogMs); - } - } - catch (Exception exc) - { - lgError($"Errore in esecuzione logWatcher{Environment.NewLine}--> {value}"); - if (isVerboseLog) - { - lgError($"{exc}"); - } - } - } - } - - public long msQueueLen - { - set - { - qMsLen = value; - lblQueueMessLen.Text = qMsLen.ToString(); - showQueueData(); - // se supero max precedente, ed è > 10... loggo! - if (qMsLen > maxMsQueue && qMsLen > 10) - { - maxMsQueue = qMsLen; - lgInfo($"[WARN] Coda Ms di {value} record"); - } - else - { - maxMsQueue--; - maxMsQueue = maxMsQueue < qMsLen ? qMsLen : maxMsQueue; - } - } - get - { - return qMsLen; - } - } - /// /// Numero max linee da mostrare (da controllo)... /// @@ -554,31 +209,6 @@ namespace IOB_WIN_FORM } } - public long rtrQueueLen - { - set - { - qRTrLen = value; - lblQueueRwTrLog.Text = qRTrLen.ToString(); - showQueueData(); - // se supero max precedente, ed è > 10... loggo! - if (qRTrLen > maxRwTrQueue && qRTrLen > 10) - { - maxRwTrQueue = qRTrLen; - lgInfo($"[WARN] Coda RawTransf di {value} record"); - } - else - { - maxRwTrQueue--; - maxRwTrQueue = maxRwTrQueue < qRTrLen ? qRTrLen : maxRwTrQueue; - } - } - get - { - return qRTrLen; - } - } - /// /// Imposta COLORE SFONDO x Semaforo IN /// @@ -598,7 +228,7 @@ namespace IOB_WIN_FORM this.UIThread(delegate { bIN.BackColor = newColor; - bIN.Refresh(); + //bIN.Invalidate(); }); } } @@ -623,7 +253,7 @@ namespace IOB_WIN_FORM this.UIThread(delegate { bOUT.BackColor = newColor; - bOUT.Refresh(); + //bOUT.Invalidate(); }); } } @@ -652,7 +282,7 @@ namespace IOB_WIN_FORM this.UIThread(delegate { lblTaskLog.Text = string.Join(Environment.NewLine, logTaskString); - lblTaskLog.Refresh(); + //lblTaskLog.Invalidate(); }); } catch (Exception exc) @@ -674,127 +304,10 @@ namespace IOB_WIN_FORM get => IOBConfFull != null ? IOBConfFull.General.IobType : tipoAdapter.ND; } - public long ulQueueLen - { - set - { - qUlLen = value; - lblQueueULog.Text = qUlLen.ToString(); - showQueueData(); - // se supero max precedente, ed è > 10... loggo! - if (qUlLen > maxUlQueue && qUlLen > 10) - { - maxUlQueue = qUlLen; - lgInfo($"[WARN] Coda UL di {value} record"); - } - else - { - maxUlQueue--; - maxUlQueue = maxUlQueue < qUlLen ? qUlLen : maxUlQueue; - } - } - get - { - return qUlLen; - } - } - #endregion Public Properties #region Public Methods - /// - /// Decodifica colore da valore semaforico - /// - /// - /// - public static Color decSemaforo(Semaforo valore) - { - Color colore = Color.LightGray; - switch (valore) - { - case Semaforo.SV: - colore = Color.LightGreen; - break; - - case Semaforo.SG: - colore = Color.LightGoldenrodYellow; - break; - - case Semaforo.SR: - colore = Color.Red; - break; - - case Semaforo.SS: - colore = Color.DarkGray; - break; - - default: - colore = Color.LightGray; - break; - } - return colore; - } - - /// - /// Avvio l'adapter - /// - /// indica se sia richiesto di SVUOTARE le code delle info - public void AvviaAdapter(bool resetQueue) - { - displayTaskAndLog("Adapter starting", true); - // se NON sta girando... - if (!iobObj.adpRunning) - { - iobObj.startAdapter(resetQueue); - displayTaskAndLog("Adapter started!"); - - // fix buttons start/stop/dump - start.Enabled = false; - stop.Enabled = true; - restart.Enabled = true; - // 2026.01.02 fix timers al restart - gather.Enabled = true; - - // gestione worker - StartWorker(); - displayTaskAndLog("Start Timers", true); - - // inizializzo le scadenze dei timers... - DateTime adesso = DateTime.Now; - - foreach (gatherCycle item in Enum.GetValues(typeof(gatherCycle))) - { - // setup iniziale deterministico dei 2 obj gestione - TimerMachine.Veto.Add(item, adesso.AddMilliseconds(iobObj.IOBConfFull.TimerMs(item, 0))); - TimerMachine.CycleCount.Add(item, 0); - TimerMachine.CycleElaps.Add(item, 0); - } - sendStartFLog = utils.CRB("sendStartFLog"); - displayTaskAndLog("Adapter Running...", true); - // init max queue - maxEvQueue = 1; - maxFlQueue = 1; - - try - { - if (sendStartFLog) - { - // segnalo reboot (programma)... - iobObj.QHelp.QueueFLog.Enqueue(iobObj.qEncodeFLog("IOB-STATUS", "IOB Adapter Started")); - } - } - catch (Exception exc) - { - lgError(string.Format("EXCEPTION in fase di chiamata URL di segnalazione AVVIO IOB:{0}{1}", Environment.NewLine, exc)); - } - } - else - { - displayTaskAndLog("Adapter STILL Running...", true); - } - } - /// /// Registra variazione error delay in caso di errori o successi /// @@ -837,7 +350,6 @@ namespace IOB_WIN_FORM } if (!ShouldUpdateUI()) return; lblStatus.Text = txt2show; - lblStatus.Invalidate(); } /// @@ -881,19 +393,19 @@ namespace IOB_WIN_FORM // RealTime display if (!string.IsNullOrWhiteSpace(currDispData.newInData)) { - dataMonitor_0 = currDispData.newInData; + AddLog(0, currDispData.newInData); } if (!string.IsNullOrWhiteSpace(currDispData.newSignalData)) { - dataMonitor_1 = currDispData.newSignalData; - } - if (!string.IsNullOrWhiteSpace(currDispData.newFLogData)) - { - dataMonitor_3 = currDispData.newFLogData; + AddLog(1, currDispData.newSignalData); } if (!string.IsNullOrWhiteSpace(currDispData.newUrlCallData)) { - dataMonitor_2 = currDispData.newUrlCallData; + AddLog(2, currDispData.newUrlCallData); + } + if (!string.IsNullOrWhiteSpace(currDispData.newFLogData)) + { + AddLog(3, currDispData.newFLogData); } // Bitmap lettura attuale @@ -902,7 +414,7 @@ namespace IOB_WIN_FORM if (logCounterVeto < adesso || lblCounter.Text != $"{currDispData.counter}") { lblCounter.Text = $"{currDispData.counter}"; - lblCounter.Refresh(); + //lblCounter.Invalidate(); logCounterVeto = adesso.AddMilliseconds(delayShowLogMs); } } @@ -910,12 +422,12 @@ namespace IOB_WIN_FORM if (!string.IsNullOrWhiteSpace(currDispData.currBitmap)) { lblBitmap.Text = currDispData.currBitmap; - lblBitmap.Refresh(); + //lblBitmap.Invalidate(); } // LiveLog if (!string.IsNullOrWhiteSpace(currDispData.newLiveLogData)) { - logWatcher = currDispData.newLiveLogData; + AddLogWatcher(currDispData.newLiveLogData); } // semafori if (currDispData.semOut != Semaforo.ND) @@ -944,7 +456,7 @@ namespace IOB_WIN_FORM this.UIThread(delegate { // accoda messaggio in gestione - dataMonitor_3 = text; + AddLog(3, text); }); } @@ -959,6 +471,11 @@ namespace IOB_WIN_FORM /// protected DateTime firstStart; + /// + /// Configurazione gerarchica completa (v 4.x.x.x) + /// + protected IobConfTree IOBConfFull; + /// /// Oggetto ultimo inviato stato IOB x REDIS /// @@ -1018,11 +535,79 @@ namespace IOB_WIN_FORM /// protected Semaforo _sOUT { get; set; } = Semaforo.ND; + protected long alQueueLen + { + set + { + qAlLen = value; + lblQueueAlarmLen.Text = qAlLen.ToString(); + showQueueData(); + // se supero max precedente, ed è > 10... loggo! + if (qAlLen > maxAlQueue && qAlLen > 10) + { + maxAlQueue = qAlLen; + lgInfo($"[WARN] Coda FLog di {value} record"); + } + else + { + maxAlQueue--; + maxAlQueue = maxAlQueue < qAlLen ? qAlLen : maxAlQueue; + } + } + get + { + return qAlLen; + } + } + + /// + /// Button restart encapsulato x gestione da classi derivate + /// + protected Button btnRestart + { + get => restart; + } + + /// + /// Button start encapsulato x gestione da classi derivate + /// + public Button btnStart + { + get => start; + set => start = value; + } + + /// + /// Button stop encapsulato x gestione da classi derivate + /// + public Button btnStop + { + get => stop; + set => stop = value; + } + + + /// + /// Modello macchina + /// + protected string curModel + { + get => IOBConfFull != null ? IOBConfFull.Device.Model : ""; + } + /// /// Codice IOB della macchina cui connettersi (x scegliere corretto file di conf...) /// protected string CurrIOB { get; set; } + /// + /// Vendor macchina + /// + protected string curVendor + { + get => IOBConfFull != null ? IOBConfFull.Device.Vendor : ""; + } + protected int delayShowLogMs { get; set; } = utils.CRI("delayShowLogMs"); /// @@ -1031,9 +616,9 @@ namespace IOB_WIN_FORM protected Dictionary dMonDisplVetoVeto { get; set; } = new Dictionary(); /// - /// array degli oggetti datamonitor da mostrare + /// Dizionario degli oggetti datamonitor da mostrare /// - protected Dictionary> dMonValues { get; set; } = new Dictionary>(); + protected Dictionary> dMonValues { get; set; } = new Dictionary>(); /// /// Veto a NUOVE scritture in COUNTER... @@ -1227,7 +812,8 @@ namespace IOB_WIN_FORM case tipoAdapter.ND: default: iobObj = new Iob.Simula(this, IOBConfFull); - start.Enabled = false; + btnStart.Enabled = false; + btnStop.Enabled = true; break; } if (!await iobInitAsync()) @@ -1270,7 +856,7 @@ namespace IOB_WIN_FORM /// protected void setDefaults() { - stop.Enabled = false; + //btnStop.Enabled = false; alQueueLen = 0; flQueueLen = 0; @@ -1281,7 +867,7 @@ namespace IOB_WIN_FORM } /// - /// MOstra info su coda complessiva + /// Mostra info su coda complessiva /// protected void showQueueData() { @@ -1320,6 +906,11 @@ namespace IOB_WIN_FORM /// private static readonly SingleThreadTaskScheduler _singleScheduler = new SingleThreadTaskScheduler("IOB-SINGLE"); + /// + /// oggetto logging + /// + private static Logger lg; + private readonly object _lock = new object(); /// @@ -1355,9 +946,13 @@ namespace IOB_WIN_FORM private FormWindowState _lastState = FormWindowState.Minimized; private Thread _machineThread; + private Task _redisTask; + private Task _workerTask; + private int currentCount = 0; + /// /// Counter esecuzione WorkerLoopMachine /// @@ -1398,8 +993,217 @@ namespace IOB_WIN_FORM #endregion Private Delegates + #region Private Properties + + /// + /// File configurazione default + /// + private string defConfFilePath + { + get + { + return Path.Combine(utils.confDir, $"{CurrIOB}.ini"); + } + } + + /// + /// File configurazione json (completo) se presente + /// + private string defConfFilePathJson + { + get + { + return Path.Combine(utils.confDir, $"{CurrIOB}.json"); + } + } + + /// + /// File configurazione yaml (completo) se presente + /// + private string defConfFilePathYaml + { + get + { + return Path.Combine(utils.confDir, $"{CurrIOB}.yaml"); + } + } + + private bool enableEditMes2Plc { get; set; } + + private long evQueueLen + { + set + { + qEvLen = value; + lblQueueLen.Text = qEvLen.ToString(); + showQueueData(); + // se supero max precedente, ed è > 10... loggo! + if (qEvLen > maxEvQueue && qEvLen > 10) + { + maxEvQueue = qEvLen; + lgInfo($"[WARN] Coda EV di {value} record"); + } + else + { + maxEvQueue--; + maxEvQueue = maxEvQueue < qEvLen ? qEvLen : maxEvQueue; + } + } + get + { + return qEvLen; + } + } + + private long flQueueLen + { + set + { + qFlLen = value; + lblQueueFLogLen.Text = qFlLen.ToString(); + showQueueData(); + // se supero max precedente, ed è > 10... loggo! + if (qFlLen > maxFlQueue && qFlLen > 10) + { + maxFlQueue = qFlLen; + lgInfo($"[WARN] Coda FLog di {value} record"); + } + else + { + maxFlQueue--; + maxFlQueue = maxFlQueue < qFlLen ? qFlLen : maxFlQueue; + } + } + get + { + return qFlLen; + } + } + + private string lblCncText + { + get => lblCNC.Text; + set => lblCNC.Text = value; + } + + private string lblSrvUrlText + { + get => lblSrvUrl.Text; + set => lblSrvUrl.Text = value; + } + + private long msQueueLen + { + set + { + qMsLen = value; + lblQueueMessLen.Text = qMsLen.ToString(); + showQueueData(); + // se supero max precedente, ed è > 10... loggo! + if (qMsLen > maxMsQueue && qMsLen > 10) + { + maxMsQueue = qMsLen; + lgInfo($"[WARN] Coda Ms di {value} record"); + } + else + { + maxMsQueue--; + maxMsQueue = maxMsQueue < qMsLen ? qMsLen : maxMsQueue; + } + } + get + { + return qMsLen; + } + } + + private long rtrQueueLen + { + set + { + qRTrLen = value; + lblQueueRwTrLog.Text = qRTrLen.ToString(); + showQueueData(); + // se supero max precedente, ed è > 10... loggo! + if (qRTrLen > maxRwTrQueue && qRTrLen > 10) + { + maxRwTrQueue = qRTrLen; + lgInfo($"[WARN] Coda RawTransf di {value} record"); + } + else + { + maxRwTrQueue--; + maxRwTrQueue = maxRwTrQueue < qRTrLen ? qRTrLen : maxRwTrQueue; + } + } + get + { + return qRTrLen; + } + } + + private long ulQueueLen + { + set + { + qUlLen = value; + lblQueueULog.Text = qUlLen.ToString(); + showQueueData(); + // se supero max precedente, ed è > 10... loggo! + if (qUlLen > maxUlQueue && qUlLen > 10) + { + maxUlQueue = qUlLen; + lgInfo($"[WARN] Coda UL di {value} record"); + } + else + { + maxUlQueue--; + maxUlQueue = maxUlQueue < qUlLen ? qUlLen : maxUlQueue; + } + } + get + { + return qUlLen; + } + } + + #endregion Private Properties + #region Private Methods + /// + /// Decodifica colore da valore semaforico + /// + /// + /// + private static Color decSemaforo(Semaforo valore) + { + Color colore = Color.LightGray; + switch (valore) + { + case Semaforo.SV: + colore = Color.LightGreen; + break; + + case Semaforo.SG: + colore = Color.LightGoldenrodYellow; + break; + + case Semaforo.SR: + colore = Color.Red; + break; + + case Semaforo.SS: + colore = Color.DarkGray; + break; + + default: + colore = Color.LightGray; + break; + } + return colore; + } + /// /// Esecuzione dei metodi in modalità async dopo il load della Form /// @@ -1480,6 +1284,163 @@ namespace IOB_WIN_FORM displayTaskAndLog("Adapter Form OK", true); } + /// + /// Accodamento log messaggi con eventuale update + /// + /// + /// + private void AddLog(int id, string value) + { + DateTime now = DateTime.Now; + + var queue = dMonValues[id]; + + if (queue.Count >= nLine2show) + queue.Dequeue(); // rimuove il più vecchio + + queue.Enqueue(value); // aggiunge in coda + + if (dMonDisplVetoVeto[id] < now) + { + dMonDisplVetoVeto[id] = now.AddMilliseconds(delayShowLogMs); + + if (!ShouldUpdateUI()) return; + + // inverti per mostrare newest-first + string newText = string.Join( + Environment.NewLine, + queue.Reverse() + ); + + switch (id) + { + case 0: + lblRawData.Text = newText; + break; + case 1: + lblOutMessage.Text = newText; + break; + case 2: + lblOutMessage2.Text = newText; + break; + case 3: + lblOutMessage3.Text = newText; + break; + default: + break; + } + } + } + + /// + /// Logwatcher (in modalità "accodamento in testa" ultimi messaggi...) + /// + private void AddLogWatcher(string value) + { + try + { + logWatchString.Enqueue(value); + + Interlocked.Increment(ref currentCount); + + // trimming più efficiente (senza Count) + while (currentCount > nLine2show) + { + if (logWatchString.TryDequeue(out _)) + Interlocked.Decrement(ref currentCount); + } + + var now = DateTime.Now; + + if (logWatchWriteVeto >= now) + return; + + if (!ShouldUpdateUI()) + return; + + logWatchWriteVeto = now.AddMilliseconds(delayShowLogMs); + + // snapshot consistente + var snapshot = logWatchString.ToArray(); + + this.UIThread(() => + { + // ultimi in alto + lblLogfile.Text = string.Join( + Environment.NewLine, + snapshot.Reverse() + ); + }); + } + catch (Exception exc) + { + lgError($"Errore in logWatcher --> {value}"); + if (isVerboseLog) + lgError($"{exc}"); + } + } + + /// + /// Avvio l'adapter + /// + /// indica se sia richiesto di SVUOTARE le code delle info + private void AvviaAdapter(bool resetQueue) + { + displayTaskAndLog("Adapter starting", true); + // se NON sta girando... + if (!iobObj.adpRunning) + { + iobObj.startAdapter(resetQueue); + displayTaskAndLog("Adapter started!"); + + // fix buttons start/stop/dump + btnStart.Enabled = false; + btnStop.Enabled = true; + restart.Enabled = true; + // 2026.01.02 fix timers al restart + gather.Enabled = true; + + // gestione worker + StartWorker(); + displayTaskAndLog("Start Timers", true); + + // inizializzo le scadenze dei timers... + DateTime adesso = DateTime.Now; + TimerMachine.Veto.Clear(); + TimerMachine.CycleCount.Clear(); + TimerMachine.CycleElaps.Clear(); + foreach (gatherCycle item in Enum.GetValues(typeof(gatherCycle))) + { + // setup iniziale deterministico dei 2 obj gestione + TimerMachine.Veto.Add(item, adesso.AddMilliseconds(iobObj.IOBConfFull.TimerMs(item, 0))); + TimerMachine.CycleCount.Add(item, 0); + TimerMachine.CycleElaps.Add(item, 0); + } + sendStartFLog = utils.CRB("sendStartFLog"); + displayTaskAndLog("Adapter Running...", true); + // init max queue + maxEvQueue = 1; + maxFlQueue = 1; + + try + { + if (sendStartFLog) + { + // segnalo reboot (programma)... + iobObj.QHelp.QueueFLog.Enqueue(iobObj.qEncodeFLog("IOB-STATUS", "IOB Adapter Started")); + } + } + catch (Exception exc) + { + lgError(string.Format("EXCEPTION in fase di chiamata URL di segnalazione AVVIO IOB:{0}{1}", Environment.NewLine, exc)); + } + } + else + { + displayTaskAndLog("Adapter STILL Running...", true); + } + } + private void btnForceAutoOdl_Click(object sender, EventArgs e) { iobObj.forceResetOdl(); @@ -1689,6 +1650,17 @@ namespace IOB_WIN_FORM toggleEditMes2Plc(); } + /// + /// Reset coda log messaggi + /// + /// + private void ClearLog(int id) + { + var queue = dMonValues[id]; + queue.Clear(); + lblRawData.Text = ""; + } + /// /// Chiusura adapter /// @@ -1864,8 +1836,8 @@ namespace IOB_WIN_FORM Task.Run(async () => { await iobObj.stopAdapter(tryRestart, forceDequeue); - stop.Enabled = false; - start.Enabled = true; + btnStop.Enabled = false; + btnStart.Enabled = true; restart.Enabled = false; _isSuspended = true; if (stopTimer) @@ -1957,7 +1929,7 @@ namespace IOB_WIN_FORM for (int i = 0; i < 4; i++) { dMonDisplVetoVeto.Add(i, DateTime.Now); - dMonValues.Add(i, new List()); + dMonValues.Add(i, new Queue()); } } @@ -2215,7 +2187,7 @@ namespace IOB_WIN_FORM int currentDelay = _isSuspended ? IOBConfFull.General.Timers.MsMF : IOBConfFull.General.Timers.MsHF; // in ogni caso max 15 sec di attesa... - currentDelay = currentDelay > 15000 ? 15000 : currentDelay; + currentDelay = currentDelay > 30000 ? 30000 : currentDelay; await Task.Delay(currentDelay, token); } @@ -2236,52 +2208,6 @@ namespace IOB_WIN_FORM // aggiorno labels counters... counterIob = $"pz MES {iobObj.contapezziIOB}"; counterMac = $"pz PLC {iobObj.contapezziPLC}"; -#if false - Dictionary setPar = new Dictionary(); - setPar.Add("IP", iobObj.IOBConfFull.Device.Connect.IpAddr); - setPar.Add("PORT", iobObj.IOBConfFull.Device.Connect.Port); - string note = $"{iobObj.IOBConfFull.General.FilenameIOB} | DT ultimo avvio: {iobObj.DtHelp.AvvioAdp}"; - // verifico IOB status - IobWinStatus currIobStatus = new IobWinStatus() - { - CodIob = iobObj.IOBConfFull.General.FilenameIOB, - IobType = $"{iobObj.IOBConfFull.General.IobType}",//iobObj.cIobConf.tipoIob.ToString(), - queueAlLen = alQueueLen, - queueEvLen = evQueueLen, - queueFlLen = flQueueLen, - queueMsLen = msQueueLen, - queueRawTransfLen = rtrQueueLen, - queueUlLen = ulQueueLen, - counterIOB = iobObj.contapezziIOB, - counterMAC = iobObj.contapezziPLC, - lastUpdate = lastIobStatus.lastUpdate > iobObj.DtHelp.lastWatchDog ? lastIobStatus.lastUpdate : iobObj.DtHelp.lastWatchDog, - online = utils.IOB_Online, - lastDataIn = iobObj.DtHelp.lastReadPLC, - lastDataOut = iobObj.DtHelp.lastIobOnline, - setupParams = setPar, - freeNotes = note - }; - // se diverso SALVO! - if (!currIobStatus.Equals(lastIobStatus)) - { - // aggiorno data - currIobStatus.lastUpdate = DateTime.Now; - // salvo su redis e in obj corrente - iobObj.redisMan.iobStatus = currIobStatus; - lastIobStatus = currIobStatus; - } - // se diverso SALVO MP IO status - if (lastSrvStatus.online != utils.MPIO_Online) - { - // aggiorno - ServerMpStatus currSrvStatus = iobObj.redisMan.servStatus; - currSrvStatus.online = utils.MPIO_Online; - currSrvStatus.lastUpdate = DateTime.Now; - // salvo su redis e in obj corrente - iobObj.redisMan.servStatus = currSrvStatus; - lastSrvStatus = currSrvStatus; - } -#endif } /// @@ -2311,10 +2237,10 @@ namespace IOB_WIN_FORM AvviaAdapter(true); displayTaskAndLog("RESTARTING: Adapter Started", true); // resetto i data monitor... - dataMonitor_0 = ""; - dataMonitor_1 = ""; - dataMonitor_2 = ""; - dataMonitor_3 = ""; + ClearLog(0); + ClearLog(1); + ClearLog(2); + ClearLog(3); } } @@ -2459,7 +2385,6 @@ namespace IOB_WIN_FORM lgInfo("--- WORKER MACHINE AVVIATO ---"); // avvio altro thread di verifica status IOB verso REDIS... - // 1. Controllo se è già in esecuzione if (_redisTask != null && !_redisTask.IsCompleted) { lgTrace("Worker Redis già in esecuzione. Richiesta ignorata."); @@ -2473,7 +2398,6 @@ namespace IOB_WIN_FORM } // avvio altro thread di comunicazione server... - // 1. Controllo se è già in esecuzione if (_workerTask != null && !_workerTask.IsCompleted) { lgTrace("Worker già in esecuzione. Richiesta ignorata."); @@ -2495,6 +2419,7 @@ namespace IOB_WIN_FORM private void stop_Click(object sender, EventArgs e) { stopForced = true; + btnStart.Enabled = true; fermaAdapter(false, chkForceDequeue.Checked, true); // salvo che ho fermato adapter lgInfo("UNLOAD Adapter"); diff --git a/IOB-WIN-FORM/MainForm.cs b/IOB-WIN-FORM/MainForm.cs index 7624385d..83f799cb 100644 --- a/IOB-WIN-FORM/MainForm.cs +++ b/IOB-WIN-FORM/MainForm.cs @@ -90,15 +90,13 @@ namespace IOB_WIN_FORM { set { - // se true --> comunica/verde, altrimenti grigio - lblApp.ForeColor = value ? Color.SeaGreen : Color.Black; - lblVers.ForeColor = value ? Color.SeaGreen : Color.DarkSlateGray; - if (!ShouldUpdateUI()) return; - this.UIThread(delegate { - statusStrip1.Refresh(); + // se true --> comunica/verde, altrimenti grigio + lblApp.ForeColor = value ? Color.SeaGreen : Color.Black; + lblVers.ForeColor = value ? Color.SeaGreen : Color.DarkSlateGray; + //statusStrip1.Invalidate(); }); } } @@ -221,8 +219,10 @@ namespace IOB_WIN_FORM { resetProgBar++; } +#if false // invalido x ridisegnare... - MainProgrBar.Invalidate(); + MainProgrBar.Invalidate(); +#endif } } catch (Exception exc) @@ -242,7 +242,7 @@ namespace IOB_WIN_FORM // mostro solo se non minimizzata if (!ShouldUpdateUI()) return; lblStatus.Text = txt2show; - lblStatus.Invalidate(); + //lblStatus.Invalidate(); } /// diff --git a/IOB-WIN-PING/AdapterFormNext.cs b/IOB-WIN-PING/AdapterFormNext.cs index a1e0c080..c4c855d4 100644 --- a/IOB-WIN-PING/AdapterFormNext.cs +++ b/IOB-WIN-PING/AdapterFormNext.cs @@ -42,7 +42,7 @@ namespace IOB_WIN_SHELLY case tipoAdapter.ND: default: iobObj = new IOB_WIN_FORM.Iob.Simula(this, IOBConfFull); - btnStart.Enabled = false; + btnStart.Enabled = true; break; } if (!await iobInitAsync()) From 4863a1b17edcbe2fd8cbd98d102e5684b8760bfd Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 26 May 2026 10:46:23 +0200 Subject: [PATCH 2/2] Forced update SIMS --- IOB-WIN-FANUC/DATA/CONF/MAIN.ini | 1 + IOB-WIN-FTP/DATA/CONF/MAIN.ini | 1 + IOB-WIN-MTC/DATA/CONF/MAIN.ini | 1 + IOB-WIN-OPC-UA/DATA/CONF/MAIN.ini | 1 + IOB-WIN-PING/DATA/CONF/MAIN.ini | 1 + IOB-WIN-SIEMENS/DATA/CONF/MAIN.ini | 1 + IOB-WIN-WPS/DATA/CONF/MAIN.ini | 1 + 7 files changed, 7 insertions(+) diff --git a/IOB-WIN-FANUC/DATA/CONF/MAIN.ini b/IOB-WIN-FANUC/DATA/CONF/MAIN.ini index 397d0fe7..5b64abf8 100644 --- a/IOB-WIN-FANUC/DATA/CONF/MAIN.ini +++ b/IOB-WIN-FANUC/DATA/CONF/MAIN.ini @@ -34,3 +34,4 @@ CLI_INST=SteamWareSim STARTLIST=SIMUL_02 MAXCNC=10 + diff --git a/IOB-WIN-FTP/DATA/CONF/MAIN.ini b/IOB-WIN-FTP/DATA/CONF/MAIN.ini index ebc0bef6..2cb4f81c 100644 --- a/IOB-WIN-FTP/DATA/CONF/MAIN.ini +++ b/IOB-WIN-FTP/DATA/CONF/MAIN.ini @@ -20,3 +20,4 @@ CLI_INST=SteamWareSim STARTLIST=FTP_SONATEST MAXCNC=10 + diff --git a/IOB-WIN-MTC/DATA/CONF/MAIN.ini b/IOB-WIN-MTC/DATA/CONF/MAIN.ini index 084f3aef..39a789eb 100644 --- a/IOB-WIN-MTC/DATA/CONF/MAIN.ini +++ b/IOB-WIN-MTC/DATA/CONF/MAIN.ini @@ -27,3 +27,4 @@ CLI_INST=SteamWareSim STARTLIST=SIMUL_01 MAXCNC=10 + diff --git a/IOB-WIN-OPC-UA/DATA/CONF/MAIN.ini b/IOB-WIN-OPC-UA/DATA/CONF/MAIN.ini index 72bea7db..a9d47506 100644 --- a/IOB-WIN-OPC-UA/DATA/CONF/MAIN.ini +++ b/IOB-WIN-OPC-UA/DATA/CONF/MAIN.ini @@ -34,3 +34,4 @@ STARTLIST=SIMUL_01 ;STARTLIST=3026 MAXCNC=10 + diff --git a/IOB-WIN-PING/DATA/CONF/MAIN.ini b/IOB-WIN-PING/DATA/CONF/MAIN.ini index 01bdf429..14fc4c91 100644 --- a/IOB-WIN-PING/DATA/CONF/MAIN.ini +++ b/IOB-WIN-PING/DATA/CONF/MAIN.ini @@ -20,3 +20,4 @@ STARTLIST=SIMUL_01 ;STARTLIST=3023-PING MAXCNC=10 + diff --git a/IOB-WIN-SIEMENS/DATA/CONF/MAIN.ini b/IOB-WIN-SIEMENS/DATA/CONF/MAIN.ini index 41774edf..babafa34 100644 --- a/IOB-WIN-SIEMENS/DATA/CONF/MAIN.ini +++ b/IOB-WIN-SIEMENS/DATA/CONF/MAIN.ini @@ -42,3 +42,4 @@ CLI_INST=SteamWareSim STARTLIST=3010 MAXCNC=10 + diff --git a/IOB-WIN-WPS/DATA/CONF/MAIN.ini b/IOB-WIN-WPS/DATA/CONF/MAIN.ini index a81dc4b2..5b0016e5 100644 --- a/IOB-WIN-WPS/DATA/CONF/MAIN.ini +++ b/IOB-WIN-WPS/DATA/CONF/MAIN.ini @@ -24,3 +24,4 @@ STARTLIST=3018 ;STARTLIST=SIMUL_06 MAXCNC=10 +