diff --git a/IOB-UT-NEXT/IOB-UT-NEXT.csproj b/IOB-UT-NEXT/IOB-UT-NEXT.csproj index 14903e11..1fdf8d09 100644 --- a/IOB-UT-NEXT/IOB-UT-NEXT.csproj +++ b/IOB-UT-NEXT/IOB-UT-NEXT.csproj @@ -123,6 +123,7 @@ + diff --git a/IOB-UT-NEXT/IobWinStatus.cs b/IOB-UT-NEXT/IobWinStatus.cs new file mode 100644 index 00000000..b360546b --- /dev/null +++ b/IOB-UT-NEXT/IobWinStatus.cs @@ -0,0 +1,169 @@ +using System; +using System.Collections.Generic; + +namespace IOB_UT_NEXT +{ + /// + /// Rappresentazione dello stato corrente dell'IOB + /// + public class IobWinStatus + { + #region Public Properties + + /// + /// ID univoco + /// + public string CodIob { get; set; } = "0"; + + /// + /// Contatore IOB + /// + public float counterIOB { get; set; } = 0; + + /// + /// Contatore Macchina + /// + public float counterMAC { get; set; } = 0; + + /// + /// Ultimo stato noto dei parametri in memoria letti dalla macchina + /// + public Dictionary currParams { get; set; } = new Dictionary(); + + /// + /// Note (libere) da child a controller + /// + public string freeNotes { get; set; } = ""; + + /// + /// Tipo IOB + /// + public string IobType { get; set; } = ""; + + /// + /// DataOra ultima comunicazione IN (con PLC) + /// + public DateTime lastDataIn { get; set; } = DateTime.Now.AddMinutes(-1); + + /// + /// DataOra ultima comunicazione OUT (con MP Server) + /// + public DateTime lastDataOut { get; set; } = DateTime.Now.AddDays(-1); + + /// + /// DataOra ultima comunicazione OUT (con MP Server) + /// + public DateTime lastUpdate { get; set; } = DateTime.Now.AddDays(-1); + + /// + /// Status del SINGOLO IOB + /// + public bool online { get; set; } = false; + + /// + /// Lungh coda ALLARMI in uscita + /// + public int queueAlLen { get; set; } = 0; + + /// + /// Lunghezza coda EVENTI in uscita + /// + public int queueEvLen { get; set; } = 0; + + /// + /// Lunghezza coda FluxLog in uscita + /// + public int queueFlLen { get; set; } = 0; + + /// + /// Lungh coda MESSAGGI in uscita + /// + public int queueMsLen { get; set; } = 0; + + /// + /// Lungh coda UserLog in uscita + /// + public int queueRawTransfLen { get; set; } = 0; + + /// + /// Lungh coda UserLog in uscita + /// + public int queueUlLen { get; set; } = 0; + + /// + /// Semaforo IN (IOB-Macchina) + /// + public string semIn { get; set; } = "ND"; + + /// + /// Semaforo OUT (IOB-MPServer) + /// + public string semOut { get; set; } = "ND"; + + /// + /// Parametri configurazione (es IP target, porta, ...) + /// + public Dictionary setupParams { get; set; } = new Dictionary(); + + #endregion Public Properties + + #region Public Methods + + /// + /// Override metodo di equality + /// + /// + /// + public override bool Equals(object obj) + { + if (!(obj is IobWinStatus item)) + return false; + + if (online != item.online) + return false; + if (lastDataIn != item.lastDataIn) + return false; + if (lastDataOut != item.lastDataOut) + return false; + if (semIn != item.semIn) + return false; + if (semOut != item.semOut) + return false; + if (currParams != item.currParams) + return false; + if (counterIOB != item.counterIOB) + return false; + if (counterMAC != item.counterMAC) + return false; + if (queueAlLen != item.queueAlLen) + return false; + if (queueEvLen != item.queueEvLen) + return false; + if (queueFlLen != item.queueFlLen) + return false; + if (queueMsLen != item.queueMsLen) + return false; + if (queueRawTransfLen != item.queueRawTransfLen) + return false; + if (queueUlLen != item.queueUlLen) + return false; + if (freeNotes != item.freeNotes) + return false; + if (setupParams != item.setupParams) + return false; + if (IobType != item.IobType) + return false; + if (CodIob != item.CodIob) + return false; + + return true; + } + + public override int GetHashCode() + { + return base.GetHashCode(); + } + + #endregion Public Methods + } +} \ No newline at end of file diff --git a/IOB-UT-NEXT/Objects.cs b/IOB-UT-NEXT/Objects.cs index 5b893604..d10b609e 100644 --- a/IOB-UT-NEXT/Objects.cs +++ b/IOB-UT-NEXT/Objects.cs @@ -205,6 +205,7 @@ namespace IOB_UT_NEXT #endregion Public Properties } +#if false /// /// Rappresentazione dello stato corrente dell'IOB /// @@ -321,7 +322,8 @@ namespace IOB_UT_NEXT } #endregion Public Methods - } + } +#endif /// /// Elenco oggetti del monitoraggio (DynData, Status) per WPS diff --git a/IOB-UT-NEXT/RedisMan.cs b/IOB-UT-NEXT/RedisMan.cs index 73f0cb7a..9bb725c2 100644 --- a/IOB-UT-NEXT/RedisMan.cs +++ b/IOB-UT-NEXT/RedisMan.cs @@ -168,7 +168,32 @@ namespace IOB_UT_NEXT set { string rawData = JsonConvert.SerializeObject(value); - setRSV(redIobKey, rawData); + saveAndSendMessage(redIobKey, "", rawData); + //setRSV(redIobKey, rawData); + } + } + + /// + /// MEssage Dispatcher: oggetto comunicazione pub/sub via REDIS channels corrente + /// + public ISubscriber messageDisp + { + get + { + ISubscriber answ; + // se già valorizzato uso oggetto private... + if (_currSub != null) + { + answ = _currSub; + } + else + { + // init DB (sullo 0) + answ = connRedis.GetSubscriber(); + _currSub = answ; + } + // restituisco oggetto DB + return answ; } } @@ -897,6 +922,43 @@ namespace IOB_UT_NEXT return answ; } + /// + /// Invia un messaggio tramite notify channel + salva in cache (se scaduto periodo veto) + /// + /// + /// + /// + /// + public bool saveAndSendMessage(string memKey, string notifyChannel, string message) + { + bool fatto = false; + // effettuo la scrittura nell'area di memoria indicata SE passato intervallo minimo + bool doSave = true; + if (LastKeySave.ContainsKey(memKey)) + { + if (DateTime.Now.Subtract(LastKeySave[memKey]).TotalSeconds < 30) + { + doSave = false; + } + } + else + { + LastKeySave.Add(memKey, DateTime.Now); + } + if (doSave) + { + cache.StringSet(memKey, message); + LastKeySave[memKey] = DateTime.Now; + Logging.Instance.Trace($"Redis Cache Key: {memKey}"); + } + + // invio notifica tramite il canale richiesto + messageDisp.Publish(notifyChannel, message); + + // restituisco! + return fatto; + } + /// /// Serializzazione di un oggetto generico /// @@ -1042,12 +1104,17 @@ namespace IOB_UT_NEXT protected string currIobType = "ND"; /// - /// Hash redis x dati IOB + /// Hash REDIS x dati IOB /// protected string redIobKey = ""; /// - /// Hash redis x dati server + /// Nome del channel REDIS x dati IOB + /// + protected string redIobChannel = "IobChannel"; + + /// + /// Hash REDIS x dati server /// protected string redServKey = ""; @@ -1055,6 +1122,11 @@ namespace IOB_UT_NEXT #region Private Fields + /// + /// Dizionario delle ultime scritture in cache redis dei messaggi + /// + private Dictionary LastKeySave = new Dictionary(); + /// /// Connessione lazy a redis... /// @@ -1088,10 +1160,15 @@ namespace IOB_UT_NEXT #region Private Properties /// - /// Oggetto currentDb locale + /// Oggetto currentDb REDIS locale /// private IDatabase _currDB { get; set; } + /// + /// Oggetto subscriber x pubblicazione/sottoscrizione canali REDIS + /// + private ISubscriber _currSub { get; set; } + #endregion Private Properties #region Private Methods @@ -1123,6 +1200,7 @@ namespace IOB_UT_NEXT // init dati di base... redServKey = redHash($"MP"); redIobKey = redHash($"IOB:{currCodIob}"); + redIobChannel = $"IobChannel_{currCodIob}"; } #endregion Private Methods