From ea071a95ee2321eb48a24146b7b6ded4af51c4cb Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Wed, 22 Jan 2020 14:24:09 +0100 Subject: [PATCH 1/5] Inizio sostituzione service redis con "solito" x problemi licenza... NON VA!!! --- IOB-UT/IOB-UT.csproj | 13 ++----- IOB-UT/Objects.cs | 8 ++--- IOB-UT/RedisMan.cs | 81 ++++++++++++++++++++++++++++++++---------- IOB-UT/baseUtils.cs | 14 ++++---- IOB-UT/packages.config | 5 +-- IOB-WIN/App.config | 2 +- IOB-WIN/IobGeneric.cs | 17 ++++++--- IOB-WIN/MainForm.cs | 22 +++++++++--- 8 files changed, 106 insertions(+), 56 deletions(-) diff --git a/IOB-UT/IOB-UT.csproj b/IOB-UT/IOB-UT.csproj index 77657eee..965bc47f 100644 --- a/IOB-UT/IOB-UT.csproj +++ b/IOB-UT/IOB-UT.csproj @@ -72,17 +72,8 @@ ..\packages\NLog.4.6.8\lib\net40-client\NLog.dll - - ..\packages\ServiceStack.Common.4.0.62\lib\net40\ServiceStack.Common.dll - - - ..\packages\ServiceStack.Interfaces.4.0.62\lib\portable-wp80+sl5+net40+win8+wpa81+monotouch+monoandroid+xamarin.ios10\ServiceStack.Interfaces.dll - - - ..\packages\ServiceStack.Redis.4.0.62\lib\net40\ServiceStack.Redis.dll - - - ..\packages\ServiceStack.Text.4.0.62\lib\net40\ServiceStack.Text.dll + + ..\packages\StackExchange.Redis.1.2.0\lib\net40\StackExchange.Redis.dll diff --git a/IOB-UT/Objects.cs b/IOB-UT/Objects.cs index 5f004136..3d4dcad2 100644 --- a/IOB-UT/Objects.cs +++ b/IOB-UT/Objects.cs @@ -473,7 +473,7 @@ namespace IOB_UT /// /// ID univoco /// - public long Id { get; set; } + public string Id { get; set; } /// /// Status del server /// @@ -495,11 +495,7 @@ namespace IOB_UT /// /// ID univoco /// - public long Id { get; set; } - /// - /// ID univoco - /// - public string IdxMacchina { get; set; } = "000"; + public string Id { get; set; } = "0"; /// /// Status del SINGOLO IOB /// diff --git a/IOB-UT/RedisMan.cs b/IOB-UT/RedisMan.cs index 6ce8870c..05269320 100644 --- a/IOB-UT/RedisMan.cs +++ b/IOB-UT/RedisMan.cs @@ -7,7 +7,7 @@ using System.Text; namespace IOB_UT { - public class RedisCache + public class RedisIobCache { /// /// Pool manager @@ -26,38 +26,43 @@ namespace IOB_UT /// protected IRedisTypedClient redIobStatus; /// - /// init classe + /// Codice server MP /// - public RedisCache() + protected string CodServer = "1"; + /// + /// Cod IOB + /// + protected string CodIob = "000"; + /// + /// init classe gestione dati IOB su Redis + /// + /// + /// + public RedisIobCache(string codServer, string codIob) { + // init dati di base... + CodServer = codServer; + CodIob = codIob; // init oggetti connessione - redisManPool = new RedisManagerPool(baseUtils.CRS("redisConf")); + redisManPool = new RedisManagerPool(baseUtils.CRS("SSRedisConf")); redisClient = redisManPool.GetClient(); // init oggetto server redServerStatus = redisClient.As(); var newSrvStatus = new ServerMpStatus() { - Id = 1, - online = false, - dtVetoCall = DateTime.Now.AddMinutes(-1) + Id = CodServer, + online = false }; redServerStatus.Store(newSrvStatus); // init oggetto IOB redIobStatus = redisClient.As(); var newIobStatus = new IobWinStatus() { - Id = 1, - online = false, - IdxMacchina="000" + Id = CodIob, + online = false }; redIobStatus.Store(newIobStatus); } -#if false - /// - /// Singleton gestione RedisCache - /// - public static RedisCache man = new RedisCache(); -#endif /// /// Accesso all'oggetto stato server da esterno /// @@ -65,7 +70,18 @@ namespace IOB_UT { get { - return redServerStatus.GetById(1); + ServerMpStatus answ = redServerStatus.GetById(CodServer); + if(answ==null) + { + answ = new ServerMpStatus() + { + Id = CodServer, + online = false + }; + + redServerStatus.Store(answ); + } + return answ; } set { @@ -79,12 +95,41 @@ namespace IOB_UT { get { - return redIobStatus.GetById(1); + IobWinStatus answ = redIobStatus.GetById(CodServer); + if (answ == null) + { + answ = new IobWinStatus() + { + Id = CodServer, + online = false + }; + + redIobStatus.Store(answ); + } + return answ; } set { redIobStatus.Store(value); } } + + /// + /// Variabile in cui viene salvato lo stato del server MP-IO + /// + public bool MPIO_Online + { + get + { + return servStatus.online; + } + set + { + var currData = servStatus; + currData.online = value; + servStatus = currData; + } + } + } } diff --git a/IOB-UT/baseUtils.cs b/IOB-UT/baseUtils.cs index 03d18445..3885b74f 100644 --- a/IOB-UT/baseUtils.cs +++ b/IOB-UT/baseUtils.cs @@ -45,11 +45,7 @@ namespace IOB_UT public class baseUtils { - - /// - /// Oggetto gestione persistenza su REDIS (statico) - /// - public static RedisCache redManager = new RedisCache(); +#if false /// /// Variabile in cui viene salvato lo stato del server MP-IO /// @@ -63,9 +59,11 @@ namespace IOB_UT { var currData = redManager.servStatus; currData.online = value; + //currData.dtVetoCall = DateTime.Now; redManager.servStatus = currData; } - } + } +#endif /// /// Indicazione VETO PING a server sino alla data-ora indicata /// @@ -90,10 +88,12 @@ namespace IOB_UT return answ; } } +#if false /// /// oggetto static x gestione baseUtils /// - public static baseUtils mgr = new baseUtils(); + public static baseUtils mgr = new baseUtils(); +#endif /// /// Classe logger /// diff --git a/IOB-UT/packages.config b/IOB-UT/packages.config index 7e6fba6a..78d59d0a 100644 --- a/IOB-UT/packages.config +++ b/IOB-UT/packages.config @@ -6,9 +6,6 @@ - - - - + \ No newline at end of file diff --git a/IOB-WIN/App.config b/IOB-WIN/App.config index dcc75e67..2a695014 100644 --- a/IOB-WIN/App.config +++ b/IOB-WIN/App.config @@ -40,7 +40,7 @@ - + diff --git a/IOB-WIN/IobGeneric.cs b/IOB-WIN/IobGeneric.cs index aea815ca..cad31eec 100644 --- a/IOB-WIN/IobGeneric.cs +++ b/IOB-WIN/IobGeneric.cs @@ -49,6 +49,10 @@ namespace IOB_WIN { #region variabili ed oggetti base + /// + /// Oggetto connessioen REDIS + /// + public RedisIobCache redisMan; /// /// Dizionario valori impostati x produzione /// @@ -442,21 +446,21 @@ namespace IOB_WIN /// /// stato Online/Offline della IOB /// - public bool IobOnline; + public bool IobOnline; /// - /// stato Online/Offline del server MP IO (su utils condiviso) + /// stato Online/Offline del server MP IO (su REDIS) /// public bool MPOnline { get { - return utils.MPIO_Online; + return redisMan.MPIO_Online; } set { - utils.MPIO_Online = value; + redisMan.MPIO_Online = value; } - } + } /// /// Data/ora ultima volta che IOB è stato dichairato online /// @@ -519,6 +523,9 @@ namespace IOB_WIN /// public IobGeneric(AdapterForm caller, IobConfiguration IOBConf) { + // init oggetto redis... + redisMan = new RedisIobCache(IOBConf.serverData.MPIP, IOBConf.codIOB); + // salvo il form chiamante parentForm = caller; // configurazione... diff --git a/IOB-WIN/MainForm.cs b/IOB-WIN/MainForm.cs index a70057a0..a1f7dcb9 100644 --- a/IOB-WIN/MainForm.cs +++ b/IOB-WIN/MainForm.cs @@ -22,6 +22,10 @@ namespace IOB_WIN { #region variabili globali, utils ed helpers + /// + /// Oggetto connessioen REDIS + /// + public RedisIobCache redisMan; /// /// Parametri (opzionali) di avvio /// @@ -362,6 +366,7 @@ namespace IOB_WIN protected void myInit() { + DateTime adesso = DateTime.Now; resetProgBar = 0; formStartTime = adesso; @@ -541,6 +546,15 @@ namespace IOB_WIN IOB2START.Add("NONE"); } } + + + // init redis... + string firstIob = "00"; + if (IOB2START.Count > 0) + { + firstIob = IOB2START[0]; + } + redisMan = new RedisIobCache(MPIP, firstIob); } #endregion @@ -624,7 +638,7 @@ namespace IOB_WIN lgError($"Errore in checkServerAlive:{Environment.NewLine}{exc}"); } // verifico SE è variato stato online/offline... - if (utils.MPIO_Online != answ) + if (redisMan.MPIO_Online != answ) { // se ORA sono online riporto... if (answ) @@ -637,7 +651,7 @@ namespace IOB_WIN lgInfo("SERVER OFFLINE in MainForm:testServer"); } // salvo nuovo status... - utils.MPIO_Online = answ; + redisMan.MPIO_Online = answ; } else { @@ -648,7 +662,7 @@ namespace IOB_WIN else { lgInfo($"SERVER NOT RESPONDING (PING at {MPIP})"); - utils.MPIO_Online = false; + redisMan.MPIO_Online = false; updateComStats(0, 0, false); // imposto veto a 10 volte reinvio dati standard... utils.dtVetoPing = DateTime.Now.AddMilliseconds(baseUtils.nextPauseSendMSec * 10); @@ -658,7 +672,7 @@ namespace IOB_WIN else { // altrimenti passo ultimo valore noto... - answ = utils.MPIO_Online; + answ = redisMan.MPIO_Online; } } return answ; From bcf7639e2c0e62b9f1cb71766d52a23eaf968955 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Wed, 22 Jan 2020 14:59:20 +0100 Subject: [PATCH 2/5] OK con stackexchange!!!! --- IOB-UT/RedisMan.cs | 926 +++++++++++++++++++++++++++++++++++++++++++-- IOB-WIN/App.config | 4 +- 2 files changed, 898 insertions(+), 32 deletions(-) diff --git a/IOB-UT/RedisMan.cs b/IOB-UT/RedisMan.cs index 05269320..969160d6 100644 --- a/IOB-UT/RedisMan.cs +++ b/IOB-UT/RedisMan.cs @@ -1,5 +1,5 @@ -using ServiceStack.Redis; -using ServiceStack.Redis.Generic; +using Newtonsoft.Json; +using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; @@ -9,22 +9,860 @@ namespace IOB_UT { public class RedisIobCache { + + #region gestione valori in RedisCache + /// - /// Pool manager + /// Restituisce un pò di info sul server redis connesso /// - protected RedisManagerPool redisManPool; + /// + public string getRedisInfoData() + { + string answ = ""; + StringBuilder sb = new StringBuilder(); + try + { + sb.AppendLine($"Configuration: {connRedis.Configuration}"); + sb.AppendLine($"Connected: {connRedis.IsConnected}"); + sb.AppendLine($"ClientName: {connRedis.ClientName}"); + sb.AppendLine($"Total Ops: {connRedis.OperationCount}"); + sb.AppendLine($"Status: {connRedis.GetStatus()}"); + answ = sb.ToString(); + } + catch + { } + return answ; + } /// - /// Client corrente + /// Oggetto currentDb locale /// - protected IRedisClient redisClient; + private IDatabase _currDB { get; set; } /// - /// Oggetto descrizione stato SERVER MP-IO + /// Oggetto DB REDIS corrente /// - protected IRedisTypedClient redServerStatus; + public IDatabase cache //currDB + { + get + { + IDatabase answ; + // se già valorizzato uso oggetto private... + if (_currDB != null) + { + answ = _currDB; + } + else + { + // init DB (sullo 0) + answ = connRedis.GetDatabase(); + // gestione override... + if (baseUtils.CRI("redisDb") >= 0) + { + // in questo caso uso il DB configurato in app.config... + answ = connRedis.GetDatabase(baseUtils.CRI("redisDb")); + } + _currDB = answ; + } + // restituisco oggetto DB + return answ; + } + } /// - /// Oggetto descrizione stato CLIENT IOB + /// Nome della variabile HASH da utilizzare (dato CodModulo / Server / DB impiegato da funzionalita' DbConfig) + keyName richiesto... /// - protected IRedisTypedClient redIobStatus; + public string redHash(string keyName) + { + keyName = keyName.Replace("\\", "_"); + string answ = keyName; + try + { + answ = string.Format($"{baseUtils.CRS("appName")}:{keyName}"); + } + catch + { } + return answ; + } + /// + /// Serializzazione di un oggetto generico + /// + /// + /// + public string serializeVal(object origVal) + { + string answ = ""; + try + { + answ = JsonConvert.SerializeObject(origVal); + } + catch { } + return answ; + } + /// + /// Deserializzazione di un valore string in oggetto generico + /// + /// + /// + public object deserializeVal(string serVal) + { + object answ = ""; + try + { + answ = JsonConvert.DeserializeObject(serVal); + } + catch { } + return answ; + } + /// + /// Connessione lazy a redis... + /// + private Lazy lazyConnection = new Lazy(() => + { + string RedisConn = baseUtils.CRS("RedisConn"); + if (string.IsNullOrEmpty(RedisConn)) + { + RedisConn = "localhost,abortConnect=false,ssl=false"; + } + + return ConnectionMultiplexer.Connect(RedisConn); + }); + /// + /// Connessione lazy a redis... + /// + private Lazy lazyConnectionAdmin = new Lazy(() => + { + string RedisConnAdmin = baseUtils.CRS("RedisConnAdmin"); + if (string.IsNullOrEmpty(RedisConnAdmin)) + { + RedisConnAdmin = "localhost,abortConnect=false,ssl=false,allowAdmin=true"; + } + + return ConnectionMultiplexer.Connect(RedisConnAdmin); + }); + /// + /// Oggetto statico connessione redis + /// + public ConnectionMultiplexer connRedis + { + get + { + return lazyConnection.Value; + } + } + /// + /// Oggetto statico connessione redis + /// + public ConnectionMultiplexer connRedisAdmin + { + get + { + return lazyConnectionAdmin.Value; + } + } + /// + /// Restituisce info dei server connessi... + /// + /// + public IServer[] redServInfo() + { + IServer[] answ = new IServer[1]; + try + { + answ = new IServer[connRedisAdmin.GetEndPoints().Length]; + int i = 0; + foreach (var ep in connRedisAdmin.GetEndPoints()) + { + var server = connRedisAdmin.GetServer(ep); + answ[i] = server; + i++; + } + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("{0}", exc), tipoLog.EXCEPTION); + } + return answ; + } + /// + /// Restituisce una chiave salvata in RedisCache + /// + /// + /// + public string getRSV(string chiave) + { + string answ = ""; + try + { + answ = cache.StringGet(chiave); + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("Errore in getRSV:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + } + return answ; + } + /// + /// Salva una chiave in RedisCache + /// + /// + /// + /// + public bool setRSV(string chiave, string valore) + { + bool answ = false; + try + { + cache.StringSet(chiave, valore); + answ = true; + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("Errore in setRSV:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + } + return answ; + } + /// + /// Salva una chiave in RedisCache + /// + /// + /// + /// in secondi + /// + public bool setRSV(string chiave, string valore, int TTL_sec) + { + bool answ = false; + try + { + TimeSpan expT = new TimeSpan(0, 0, TTL_sec); + // salvo con expyry... + cache.StringSet(chiave, valore, expT); + answ = true; + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("Errore in setRSV:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + } + return answ; + } + /// + /// Incrementa un contatore in Redis + /// + /// + /// + public long setRCntI(string chiave) + { + long answ = 0; + try + { + answ = cache.StringIncrement(chiave, 1); + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("Errore in setRCI:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + } + return answ; + } + /// + /// Decrementa un contatore in Redis + /// + /// + /// + public long setRCntD(string chiave) + { + long answ = 0; + try + { + answ = cache.StringDecrement(chiave, 1); + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("Errore in setRCD:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + } + return answ; + } + /// + /// Restituisce una chiave COUNTER in RedisCache + /// + /// + /// + public int getRCnt(string chiave) + { + int answInt = 0; + string answ = ""; + try + { + answ = cache.StringGet(chiave); + answInt = Convert.ToInt32(answ); + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("Errore in getRSV:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + } + return answInt; + } + /// + /// Resetta (elimina) un contatore in Redis + /// + /// + /// + public bool resetRCnt(string chiave) + { + bool answ = false; + try + { + answ = cache.KeyDelete(chiave); + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("Errore in resetRCnt:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + } + return answ; + } + /// + /// Restituisce un set KVP (Key Value Pair) salvati in RedisCache + /// + /// + /// + public RedisValue[] getRKeys(RedisKey[] chiavi) + { + RedisValue[] answ = null; + try + { + answ = cache.StringGet(chiavi); + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("Errore in getRKeys:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + } + return answ; + } + /// + /// Salva un set KVP (Key Value Pair) in RedisCache + /// + /// Set KVP chiave-valore da salvare + /// + public bool setRKeys(KeyValuePair[] valori) + { + bool answ = false; + try + { + cache.StringSet(valori); + answ = true; + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("Errore in setRKeys:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); + } + return answ; + } + /// + /// Verifica se ci siano valori nella KEY indicata... + /// + /// + /// + public bool redKeyPresent(RedisKey key) + { + bool answ = false; + // cerco se ci sia valore in redis... + try + { + answ = cache.KeyExists(key); + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("Errore in redKeyPresent per la key {2}:{0}{1}", Environment.NewLine, exc, key), tipoLog.EXCEPTION); + } + return answ; + } + /// + /// Verifica se ci siano valori nella KEY indicata (string) + /// + /// + /// + public bool redKeyPresentSz(string key) + { + bool answ = false; + try + { + RedisKey chiave = key; + answ = redKeyPresent(chiave); + } + catch + { } + return answ; + } + /// + /// Verifica se ci siano valori nella hash indicata... + /// + /// + /// + public bool redHashPresent(RedisKey key) + { + bool answ = false; + // cerco se ci sia valore in redis... + try + { + answ = cache.HashGetAll(key).Length > 0; + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("Errore in redHashPresent per la key {2}{0}{1}", Environment.NewLine, exc, key), tipoLog.EXCEPTION); + } + return answ; + } + /// + /// Verifica se ci siano valori nella hash indicata (string) + /// + /// + /// + public bool redHashPresentSz(string key) + { + bool answ = false; + try + { + RedisKey chiave = key; + answ = redHashPresent(chiave); + } + catch + { } + return answ; + } + /// + /// Recupera tutti i valori dalla hash + /// + /// + /// + public KeyValuePair[] redGetHash(string hashKey) + { + KeyValuePair[] answ = new KeyValuePair[1]; + // cerco se ci sia valore in redis... + try + { + RedisKey chiave = hashKey; + HashEntry[] valori = cache.HashGetAll(chiave); + answ = new KeyValuePair[valori.Length]; + int i = 0; + foreach (HashEntry item in valori) + { + answ[i] = new KeyValuePair(item.Name, item.Value); + i++; + } + } + catch + { } + return answ; + } + /// + /// Recupera tutti i valori dalla hash in formato Dictionary + /// + /// + /// + public Dictionary redGetHashDict(string hashKey) + { + Dictionary answ = new Dictionary(); + // cerco se ci sia valore in redis... + try + { + RedisKey chiave = hashKey; + HashEntry[] valori = cache.HashGetAll(chiave); + foreach (HashEntry item in valori) + { + answ.Add(item.Name, item.Value); + } + } + catch + { } + return answ; + } + /// + /// Recupera UN SINGOLO VALORE dalla hash per un dato field + /// + /// + /// + /// + public string redGetHashField(string hashKey, string hashField) + { + string answ = ""; + // cerco se ci sia valore in redis... + try + { + RedisKey chiave = hashKey; + RedisValue campo = hashField; + RedisValue valOut = cache.HashGet(chiave, campo); + answ = valOut.ToString(); + } + catch + { } + return answ; + } + /// + /// Salvataggio di una hash di valori + /// + /// chiave + /// valori + /// + public bool redSaveHash(string hashKey, KeyValuePair[] hashFields) + { + bool answ = false; + // cerco se ci sia valore in redis... + try + { + RedisKey chiave = hashKey; + HashEntry[] valori = new HashEntry[hashFields.Length]; + int i = 0; + foreach (KeyValuePair kvp in hashFields) + { + valori[i] = new HashEntry(kvp.Key, kvp.Value); + i++; + } + cache.HashSet(chiave, valori); + answ = true; + } + catch + { } + return answ; + } + /// + /// Salvataggio di una hash di valori in formato Dictionary + /// + /// chiave + /// valori + /// + public bool redSaveHashDict(string hashKey, Dictionary hashFields) + { + bool answ = false; + // cerco se ci sia valore in redis... + try + { + RedisKey chiave = hashKey; + HashEntry[] valori = new HashEntry[hashFields.Count]; + int i = 0; + foreach (KeyValuePair kvp in hashFields) + { + valori[i] = new HashEntry(kvp.Key, kvp.Value); + i++; + } + cache.HashSet(chiave, valori); + answ = true; + } + catch + { } + return answ; + } + + /// + /// Salvataggio di una hash di valori + /// + /// chiave + /// valori come lista KVP + /// + public bool redSaveHashList(string hashKey, List> hashListKVP) + { + bool answ = false; + if (connRedis.IsConnected) + { + // cerco se ci sia valore in redis... + IDatabase cache = connRedis.GetDatabase(); + try + { + RedisKey chiave = hashKey; + HashEntry[] valori = new HashEntry[hashListKVP.Count]; + int i = 0; + foreach (KeyValuePair kvp in hashListKVP) + { + valori[i] = new HashEntry(kvp.Key, kvp.Value); + i++; + } + cache.HashSet(chiave, valori); + answ = true; + } + catch + { } + } + return answ; + } + /// + /// Salvataggio di una hash di valori + /// + /// chiave + /// valori + /// scadenza preimpostata hash (secondi) | defaoult = -1 (non scade) + /// + public bool redSaveHash(string hashKey, KeyValuePair[] hashFields, double expireSeconds = -1) + { + bool answ = false; + // cerco se ci sia valore in redis... + try + { + RedisKey chiave = hashKey; + answ = redSaveHash(hashKey, hashFields); + if (expireSeconds > 0) + { + cache.KeyExpire(chiave, DateTime.Now.AddSeconds(expireSeconds)); + } + //answ = true; + } + catch + { } + return answ; + } + /// + /// Salvataggio di una hash di valori in formato Dictionary + /// + /// chiave + /// valori + /// scadenza preimpostata hash (secondi) | defaoult = -1 (non scade) + /// + public bool redSaveHashDict(string hashKey, Dictionary hashFields, double expireSeconds = -1) + { + bool answ = false; + // cerco se ci sia valore in redis... + try + { + RedisKey chiave = hashKey; + answ = redSaveHashDict(hashKey, hashFields); + if (expireSeconds > 0) + { + cache.KeyExpire(chiave, DateTime.Now.AddSeconds(expireSeconds)); + } + //answ = true; + } + catch + { } + return answ; + } + /// + /// Elimina una key (hash, string) + /// + /// + /// + public bool redDelKey(string key) + { + bool answ = false; + // cerco se ci sia valore in redis... + try + { + RedisKey chiave = key; + cache.KeyDelete(chiave); + answ = true; + } + catch + { } + return answ; + } + /// + /// Flush completo cache redis + /// + /// ** = tutti + /// + public bool redFlushKey(string keyPattern) + { + bool answ = false; + // cerco se ci sia valore in redis... + // se vuoto = ALL... + keyPattern = string.IsNullOrEmpty(keyPattern) ? "**" : keyPattern; + try + { + foreach (var ep in connRedis.GetEndPoints()) + { + var server = connRedis.GetServer(ep); + var keys = server.Keys(database: baseUtils.CRI("redisDb"), pattern: $"{keyPattern}*"); + foreach (var key in keys) + { + cache.KeyDelete(key); + } + } + answ = true; + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("{0}", exc), tipoLog.EXCEPTION); + } + return answ; + } + /// + /// Conta num oggetti cache redis che rispondono a pattern + /// + /// ** = tutti + /// + public int redCountKey(string keyPattern) + { + int answ = 0; + // cerco se ci sia valore in redis... + // se vuoto = ALL... + keyPattern = string.IsNullOrEmpty(keyPattern) ? "**" : keyPattern; + try + { + foreach (var ep in connRedis.GetEndPoints()) + { + var server = connRedis.GetServer(ep); + foreach (var key in server.Keys(pattern: keyPattern)) + { + answ++; + } + } + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("{0}", exc), tipoLog.EXCEPTION); + } + return answ; + } + + /// + /// Restituisce numero record in Redis DB + /// + public long numRecRedis + { + get + { + long answ = 0; + try + { + foreach (var ep in connRedis.GetEndPoints()) + { + var server = connRedis.GetServer(ep); + answ += server.DatabaseSize(); + } + } + catch + { } + return answ; + } + } + /// + /// Restituisce oggetti cache redis che rispondono a pattern + /// + /// ** = tutti + /// Tipo di ordinamento per kvp + /// + public List> redGetCounterByKey(string keyPattern, kvpOrderBy orderBy) + { + int numAnsw = redCountKey(keyPattern); + RedisKey[] chiavi = new RedisKey[numAnsw]; + List> answ = new List>(); + // se vuoto = ALL... + keyPattern = string.IsNullOrEmpty(keyPattern) ? "**" : keyPattern; + + // recupero in primis elenco chiavi + try + { + int i = 0; + foreach (var ep in connRedis.GetEndPoints()) + { + var server = connRedis.GetServer(ep); + foreach (var key in server.Keys(pattern: keyPattern)) + { + chiavi[i] = key; + i++; + } + } + } + catch (Exception exc) + { + //logger.lg.scriviLog(string.Format("{0}", exc), tipoLog.EXCEPTION); + } + // ora recupero valori! + var valori = getRKeys(chiavi); + int currVal = 0; + // popolo rispsota + try + { + for (int i = 0; i < numAnsw; i++) + { + Int32.TryParse(valori[i], out currVal); + answ.Add(new KeyValuePair(chiavi[i], currVal)); + } + } + catch + { } + // se richiesto riordino... + switch (orderBy) + { + case kvpOrderBy.KeyAsc: + answ.Sort(CompareKey); + break; + case kvpOrderBy.KeyDesc: + answ.Sort(CompareKeyDesc); + break; + case kvpOrderBy.ValAsc: + answ.Sort(CompareVal); + break; + case kvpOrderBy.ValDesc: + answ.Sort(CompareValDesc); + break; + default: + break; + } + return answ; + } + /// + /// Effettua comaprazione x CHIAVE in KVP ASC + /// + /// + /// + /// + private int CompareKey(KeyValuePair x, KeyValuePair y) + { + return x.Key.CompareTo(y.Key); + } + /// + /// Effettua comaprazione x VALORE in KVP ASC + /// + /// + /// + /// + public int CompareVal(KeyValuePair x, KeyValuePair y) + { + return x.Value.CompareTo(y.Value); + } + /// + /// Effettua comaprazione x CHIAVE in KVP DESC + /// + /// + /// + /// + private int CompareKeyDesc(KeyValuePair x, KeyValuePair y) + { + return y.Key.CompareTo(x.Key); + } + /// + /// Effettua comaprazione x VALORE in KVP DESC + /// + /// + /// + /// + public int CompareValDesc(KeyValuePair x, KeyValuePair y) + { + return y.Value.CompareTo(x.Value); + } + + /// + /// Tipologia di ordinamento x liste KVP + /// + public enum kvpOrderBy + { + /// + /// Ordinamento ASCending per KEY + /// + KeyAsc, + /// + /// Ordinamento DESCending per KEY + /// + KeyDesc, + /// + /// Ordinamento ASCending per VAL + /// + ValAsc, + /// + /// Ordinamento DESCending per VAL + /// + ValDesc + } + + + + #endregion + + /// /// Codice server MP /// @@ -34,6 +872,14 @@ namespace IOB_UT /// protected string CodIob = "000"; /// + /// Hash redis x dati server + /// + protected string redServKey = ""; + /// + /// Hash redis x dati IOB + /// + protected string redIobKey = ""; + /// /// init classe gestione dati IOB su Redis /// /// @@ -43,25 +889,23 @@ namespace IOB_UT // init dati di base... CodServer = codServer; CodIob = codIob; - // init oggetti connessione - redisManPool = new RedisManagerPool(baseUtils.CRS("SSRedisConf")); - redisClient = redisManPool.GetClient(); - // init oggetto server - redServerStatus = redisClient.As(); - var newSrvStatus = new ServerMpStatus() + redServKey = redHash($"MP:{CodServer}"); + redIobKey = redHash($"IOB:{CodIob}"); + // init oggetti gestiti + ServerMpStatus newSrvStatus = new ServerMpStatus() { Id = CodServer, online = false }; - redServerStatus.Store(newSrvStatus); // init oggetto IOB - redIobStatus = redisClient.As(); - var newIobStatus = new IobWinStatus() + IobWinStatus newIobStatus = new IobWinStatus() { Id = CodIob, online = false }; - redIobStatus.Store(newIobStatus); + // salvo in area REDIS + servStatus = newSrvStatus; + iobStatus = newIobStatus; } /// /// Accesso all'oggetto stato server da esterno @@ -70,22 +914,32 @@ namespace IOB_UT { get { - ServerMpStatus answ = redServerStatus.GetById(CodServer); - if(answ==null) + ServerMpStatus answ = null; + // cerco in REDIS + string rawData = getRSV(redServKey); + // se non ci fosse creo e salvo + if (string.IsNullOrEmpty(rawData)) { answ = new ServerMpStatus() { Id = CodServer, online = false }; - - redServerStatus.Store(answ); + // salvo serializzando... + rawData = JsonConvert.SerializeObject(answ); + setRSV(redServKey, rawData); + } + else + { + // deserializzo + answ = JsonConvert.DeserializeObject(rawData); } return answ; } set { - redServerStatus.Store(value); + string rawData = JsonConvert.SerializeObject(value); + setRSV(redServKey, rawData); } } /// @@ -95,22 +949,32 @@ namespace IOB_UT { get { - IobWinStatus answ = redIobStatus.GetById(CodServer); - if (answ == null) + IobWinStatus answ = null; + // cerco in REDIS + string rawData = getRSV(redIobKey); + // se non ci fosse creo e salvo + if (string.IsNullOrEmpty(rawData)) { answ = new IobWinStatus() { - Id = CodServer, + Id = CodIob, online = false }; - - redIobStatus.Store(answ); + // salvo serializzando... + rawData = JsonConvert.SerializeObject(answ); + setRSV(redIobKey, rawData); + } + else + { + // deserializzo + answ = JsonConvert.DeserializeObject(rawData); } return answ; } set { - redIobStatus.Store(value); + string rawData = JsonConvert.SerializeObject(value); + setRSV(redIobKey, rawData); } } diff --git a/IOB-WIN/App.config b/IOB-WIN/App.config index 2a695014..ac6b3c65 100644 --- a/IOB-WIN/App.config +++ b/IOB-WIN/App.config @@ -40,7 +40,9 @@ - + + + From e5ccff73b5016746c8a05f163f6de29ecbbe8a11 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Wed, 22 Jan 2020 17:56:46 +0100 Subject: [PATCH 3/5] Riportate alcune variabili in memoria (NO REDIS) --- IOB-UT/Objects.cs | 18 ++- IOB-UT/RedisMan.cs | 48 ++++++-- IOB-UT/baseUtils.cs | 56 +++++---- IOB-WIN/DATA/CONF/MAIN.ini | 9 +- IOB-WIN/IobFanuc.cs | 30 +++-- IOB-WIN/IobGeneric.cs | 236 +++++++++++++++++++++++++++---------- IOB-WIN/IobMTC.cs | 4 +- IOB-WIN/IobSiemens.cs | 4 +- 8 files changed, 275 insertions(+), 130 deletions(-) diff --git a/IOB-UT/Objects.cs b/IOB-UT/Objects.cs index 3d4dcad2..411525f6 100644 --- a/IOB-UT/Objects.cs +++ b/IOB-UT/Objects.cs @@ -471,21 +471,13 @@ namespace IOB_UT public class ServerMpStatus { /// - /// ID univoco + /// IP server /// - public string Id { get; set; } + public string IP { get; set; } /// /// Status del server /// public bool online { get; set; } = false; - /// - /// DataOra veto invio chaimate a server - /// - public DateTime dtVetoPing { get; set; } = DateTime.Now.AddDays(-1); - /// - /// DataOra veto invio chaimate a server - /// - public DateTime dtVetoCall { get; set; } = DateTime.Now.AddDays(-1); } /// /// Rappresentazione dello stato corrente dell'IOB @@ -495,12 +487,16 @@ namespace IOB_UT /// /// ID univoco /// - public string Id { get; set; } = "0"; + public string CodIob { get; set; } = "0"; /// /// Status del SINGOLO IOB /// public bool online { get; set; } = false; /// + /// Indica se sia correntemente in setup + /// + public bool inSetup { get; set; } = false; + /// /// Semaforo IN (IOB-PLC) /// public Semaforo SemIn { get; set; } = Semaforo.ND; diff --git a/IOB-UT/RedisMan.cs b/IOB-UT/RedisMan.cs index 969160d6..b55251bd 100644 --- a/IOB-UT/RedisMan.cs +++ b/IOB-UT/RedisMan.cs @@ -9,7 +9,6 @@ namespace IOB_UT { public class RedisIobCache { - #region gestione valori in RedisCache /// @@ -862,11 +861,8 @@ namespace IOB_UT #endregion + #region gestione redis specifica - /// - /// Codice server MP - /// - protected string CodServer = "1"; /// /// Cod IOB /// @@ -880,6 +876,17 @@ namespace IOB_UT /// protected string redIobKey = ""; /// + /// init classe "implicita" + /// + /// + /// + public RedisIobCache() + { + // init dati di base... + redServKey = redHash($"MP"); + redIobKey = redHash($"IOB:{CodIob}"); + } + /// /// init classe gestione dati IOB su Redis /// /// @@ -887,20 +894,19 @@ namespace IOB_UT public RedisIobCache(string codServer, string codIob) { // init dati di base... - CodServer = codServer; CodIob = codIob; - redServKey = redHash($"MP:{CodServer}"); + redServKey = redHash($"MP"); redIobKey = redHash($"IOB:{CodIob}"); // init oggetti gestiti ServerMpStatus newSrvStatus = new ServerMpStatus() { - Id = CodServer, + IP = codServer, online = false }; // init oggetto IOB IobWinStatus newIobStatus = new IobWinStatus() { - Id = CodIob, + CodIob = CodIob, online = false }; // salvo in area REDIS @@ -922,7 +928,6 @@ namespace IOB_UT { answ = new ServerMpStatus() { - Id = CodServer, online = false }; // salvo serializzando... @@ -957,7 +962,7 @@ namespace IOB_UT { answ = new IobWinStatus() { - Id = CodIob, + CodIob = CodIob, online = false }; // salvo serializzando... @@ -977,9 +982,8 @@ namespace IOB_UT setRSV(redIobKey, rawData); } } - /// - /// Variabile in cui viene salvato lo stato del server MP-IO + /// Variabile in cui viene salvato lo stato ONLINE del server MP-IO /// public bool MPIO_Online { @@ -994,6 +998,24 @@ namespace IOB_UT servStatus = currData; } } + /// + /// Variabile in cui viene salvato lo stato ONLINE dell'IOB + /// + public bool IOB_Online + { + get + { + return iobStatus.online; + } + set + { + var currData = iobStatus; + currData.online = value; + iobStatus = currData; + } + } + + #endregion } } diff --git a/IOB-UT/baseUtils.cs b/IOB-UT/baseUtils.cs index 3885b74f..ec6275a8 100644 --- a/IOB-UT/baseUtils.cs +++ b/IOB-UT/baseUtils.cs @@ -45,33 +45,49 @@ namespace IOB_UT public class baseUtils { -#if false + /// - /// Variabile in cui viene salvato lo stato del server MP-IO + /// Oggetto connessione REDIS /// - public static bool MPIO_Online - { - get - { - return redManager.servStatus.online; - } - set - { - var currData = redManager.servStatus; - currData.online = value; - //currData.dtVetoCall = DateTime.Now; - redManager.servStatus = currData; - } - } -#endif + public static RedisIobCache redisMan = new RedisIobCache(); /// /// Indicazione VETO PING a server sino alla data-ora indicata /// public static DateTime dtVetoPing = DateTime.Now; +#if false + { + get + { + return redisMan.servStatus.dtVetoPing; + } + set + { + var currData = redisMan.servStatus; + currData.dtVetoPing = value; + redisMan.servStatus = currData; + } + } +#endif /// /// Indicazione VETO invio a server sino alla data-ora indicata /// public static DateTime dtVetoSend = DateTime.Now; + #if false + { + get + { + return redisMan.servStatus.dtVetoSend; + } + set + { + var currData = redisMan.servStatus; + currData.dtVetoSend = value; + redisMan.servStatus = currData; + } + } + #endif + + /// /// Calcola una pausa con errore casuale x il prossimo send programmato verso SERVER /// @@ -88,12 +104,6 @@ namespace IOB_UT return answ; } } -#if false - /// - /// oggetto static x gestione baseUtils - /// - public static baseUtils mgr = new baseUtils(); -#endif /// /// Classe logger /// diff --git a/IOB-WIN/DATA/CONF/MAIN.ini b/IOB-WIN/DATA/CONF/MAIN.ini index 7a474602..748b548c 100644 --- a/IOB-WIN/DATA/CONF/MAIN.ini +++ b/IOB-WIN/DATA/CONF/MAIN.ini @@ -7,12 +7,13 @@ NAME=master ;MPIP=192.168.1.7 ; server VALVITAL ;MPIP=192.168.214.4 -; Server Steamware -;MPIP=10.74.82.218 ; server interclays -MPIP=10.0.0.233 +;MPIP=10.0.0.233 ; server REMOSA -MPIP=192.168.0.110 +;MPIP=192.168.0.110 + +; Server Steamware +MPIP=10.74.82.218 MPURL=/MP/IO CMDREBO=/IOB/sendRebootGateway?GWIP= CMDIOB2CALL=/IOB/getIob2call?GWIP= diff --git a/IOB-WIN/IobFanuc.cs b/IOB-WIN/IobFanuc.cs index 65a3ce97..0ec13149 100644 --- a/IOB-WIN/IobFanuc.cs +++ b/IOB-WIN/IobFanuc.cs @@ -203,13 +203,13 @@ namespace IOB_WIN // aggiungo referenza obj FANUC FANUC_ref = (FANUC)Runtime.NC; - if (isVerboseLog) + if (isVerboseLog) { lgInfo("FANUC_ref da CncLib"); } // disconnetto e connetto... - if (isVerboseLog) + if (isVerboseLog) { lgInfo("FANUC: tryDisconnect"); } @@ -361,7 +361,7 @@ namespace IOB_WIN { memAddressFanuc areaCounter = new memAddressFanuc(memAddr); - if (isVerboseLog) + if (isVerboseLog) { lgInfo("[0] area memoria: {0}.{1}.{2}", areaCounter.mType, areaCounter.mPos, areaCounter.vType); } @@ -936,14 +936,16 @@ namespace IOB_WIN } // salvo ultimo conteggio rilevato - Int32.TryParse(outputVal.ToString(), out lastCountCNC); + int newVal = -1; + Int32.TryParse(outputVal.ToString(), out newVal); + lastCountCNC = newVal > -1 ? newVal : lastCountCNC; } // altrimenti se legge da area memoria specifica leggo da li... formto tipo STD.D.1604.DW else { memAddressFanuc areaCounter = new memAddressFanuc(memAddr); - if (isVerboseLog) + if (isVerboseLog) { lgInfo("[0] area memoria: {0}.{1}.{2}", areaCounter.mType, areaCounter.mPos, areaCounter.vType); } @@ -966,13 +968,13 @@ namespace IOB_WIN case "DW": uint valDW = 0; FANUC_ref.F_RW_DWord(false, areaCounter.mType, areaCounter.mPos, ref valDW); - if (isVerboseLog) + if (isVerboseLog) { lgInfo("[1] valDW contapezzi: {0}", valDW); } outputVal = valDW; - if (isVerboseLog) + if (isVerboseLog) { lgInfo("[2] outputVal contapezzi: {0}", outputVal); } @@ -987,8 +989,10 @@ namespace IOB_WIN } // salvo... - Int32.TryParse(outputVal.ToString(), out lastCountCNC); - if (isVerboseLog) + int newVal = -1; + Int32.TryParse(outputVal.ToString(), out newVal); + lastCountCNC = newVal > -1 ? newVal : lastCountCNC; + if (isVerboseLog) { lgInfo("[3] lastCountCNC contapezzi: {0}", lastCountCNC); } @@ -1091,7 +1095,7 @@ namespace IOB_WIN { memAddressFanuc areaCounter = new memAddressFanuc(memAddr); - if (isVerboseLog) + if (isVerboseLog) { lgInfo("[0] area memoria: {0}.{1}.{2}", areaCounter.mType, areaCounter.mPos, areaCounter.vType); } @@ -1114,13 +1118,13 @@ namespace IOB_WIN case "DW": uint valDW = 0; FANUC_ref.F_RW_DWord(false, areaCounter.mType, areaCounter.mPos, ref valDW); - if (isVerboseLog) + if (isVerboseLog) { lgInfo("[1] valDW PAR: {0}", valDW); } outputVal = valDW; - if (isVerboseLog) + if (isVerboseLog) { lgInfo("[2] outputVal PAR: {0}", outputVal); } @@ -1136,7 +1140,7 @@ namespace IOB_WIN // salvo... answ = outputVal.ToString(); - if (isVerboseLog) + if (isVerboseLog) { lgInfo("[3] PAR letto: {0} | {1}", varName, answ); } diff --git a/IOB-WIN/IobGeneric.cs b/IOB-WIN/IobGeneric.cs index cad31eec..673c65f2 100644 --- a/IOB-WIN/IobGeneric.cs +++ b/IOB-WIN/IobGeneric.cs @@ -46,13 +46,163 @@ namespace IOB_WIN } public class IobGeneric - { + { + #region variabili serializzate in REDIS + + /// + /// Oggetto connessione REDIS + /// + public RedisIobCache redisMan; + /// + /// Indica impianto IN SETUP (fino a quando SMETTE di esserlo...) + /// + protected bool inSetup + { + get + { + return redisMan.iobStatus.inSetup; + } + set + { + var currData = redisMan.iobStatus; + currData.inSetup = value; + redisMan.iobStatus = currData; + } + } + /// + /// Indicazione VETO PING a server sino alla data-ora indicata + /// + public DateTime dtVetoPing + { + get + { + return utils.dtVetoPing; + } + set + { + utils.dtVetoPing = value; + } +#if false + get + { + return redisMan.servStatus.dtVetoPing; + } + set + { + var currData = redisMan.servStatus; + currData.dtVetoPing = value; + redisMan.servStatus = currData; + } +#endif + } + /// + /// Indicazione VETO invio a server sino alla data-ora indicata + /// + public DateTime dtVetoSend + { + get + { + return utils.dtVetoSend; + } + set + { + utils.dtVetoSend = value; + } +#if false + get + { + return redisMan.servStatus.dtVetoSend; + } + set + { + var currData = redisMan.servStatus; + currData.dtVetoSend = value; + redisMan.servStatus = currData; + } +#endif + } + /// + /// Indicazione VETO check status IOB x evitare loop troppo stretti... + /// + public DateTime dtVetoCheckIOB = DateTime.Now.AddDays(-1); +#if false + { + get + { + return redisMan.iobStatus.dtVetoCheckIOB; + } + set + { + var currData = redisMan.iobStatus; + currData.dtVetoCheckIOB = value; + redisMan.iobStatus = currData; + } + } +#endif + /// + /// Contapezzi attuale + /// + public Int32 contapezzi + { + get + { + return (Int32)redisMan.iobStatus.counterIOB; + } + set + { + var currData = redisMan.iobStatus; + currData.counterIOB = value; + redisMan.iobStatus = currData; + } + } + /// + /// Ultima lettura variabile contapezzi da CNC + /// + public Int32 lastCountCNC + { + get + { + return (Int32)redisMan.iobStatus.counterMAC; + } + set + { + var currData = redisMan.iobStatus; + currData.counterMAC = value; + redisMan.iobStatus = currData; + } + } + /// + /// stato Online/Offline della IOB + /// + public bool IobOnline + { + get + { + return redisMan.IOB_Online; + } + set + { + redisMan.IOB_Online = value; + } + } + /// + /// stato Online/Offline del server MP IO (su REDIS) + /// + public bool MPOnline + { + get + { + return redisMan.MPIO_Online; + } + set + { + redisMan.MPIO_Online = value; + } + } + #endregion + #region variabili ed oggetti base - /// - /// Oggetto connessioen REDIS - /// - public RedisIobCache redisMan; /// /// Dizionario valori impostati x produzione /// @@ -61,10 +211,6 @@ namespace IOB_WIN /// TImeout x ping al server /// protected int pingServerMsTimeout = utils.CRI("pingMsTimeout"); - /// - /// Indica impianto IN SETUP (fino a quando SMETTE di esserlo...) - /// - protected bool inSetup = false; /// /// Log verboso da configurazione (SOLO CHAIVE "verbose"... /// @@ -72,33 +218,9 @@ namespace IOB_WIN /// /// Coda massima ammessa per FLog (se <=0 disattivata...) /// - protected int maxQueueFLog { get; set; } = utils.CRI("maxQueueFLog"); - /// - /// Indicazione VETO invio a server sino alla data-ora indicata - /// - public static DateTime dtVetoPing - { - get - { - return utils.dtVetoPing; - } - set - { - utils.dtVetoPing = value; - } - } - /// - /// Indicazione VETO check status IOB x evitare loop troppo stretti... - /// - public DateTime dtVetoCheckIOB = DateTime.Now; - /// - /// Contapezzi attuale - /// - public Int32 contapezzi; - /// - /// Ultima lettura variabile contapezzi da CNC - /// - public Int32 lastCountCNC; + protected int maxQueueFLog { get; set; } = utils.CRI("maxQueueFLog"); + + /// /// ODL attualmente sulla macchina /// @@ -443,24 +565,7 @@ namespace IOB_WIN /// Conteggio ATTUALE ore macchina IN LAVORO /// public double contOreMaccLav; - /// - /// stato Online/Offline della IOB - /// - public bool IobOnline; - /// - /// stato Online/Offline del server MP IO (su REDIS) - /// - public bool MPOnline - { - get - { - return redisMan.MPIO_Online; - } - set - { - redisMan.MPIO_Online = value; - } - } + /// /// Data/ora ultima volta che IOB è stato dichairato online /// @@ -476,10 +581,11 @@ namespace IOB_WIN /// /// Dizionario di VARIABILI da trattare come eventi (da inviare quando cambiano oppure a scadenza periodo...) /// - protected Dictionary VarArray = new Dictionary(); - + protected Dictionary VarArray = new Dictionary(); + #endregion + /// /// Form chiamante /// @@ -1010,8 +1116,10 @@ namespace IOB_WIN { // se "-1" resto a ultimo... if (currServerCount != "-1") - { - int.TryParse(currServerCount, out contapezzi); + { + int newVal = -1; + Int32.TryParse(currServerCount, out newVal); + contapezzi = newVal > -1 ? newVal : contapezzi; lgInfo("Ricevuta conferma da server di {0} pezzi registrati per ODL", currServerCount); } else @@ -2873,16 +2981,16 @@ namespace IOB_WIN } // salvo status... IobOnline = answ; - // se offline imposto veto check a 5 x tempo reinvio... - if (!answ) + // se offline imposto veto check a 2 x tempo reinvio... + if (answ) { - dtVetoCheckIOB = DateTime.Now.AddMilliseconds(baseUtils.nextPauseSendMSec * 10); + // non controllo x meno... + dtVetoCheckIOB = DateTime.Now.AddMilliseconds(baseUtils.nextPauseSendMSec * 2); + lastIobOnline = DateTime.Now; } else { - // non controllo x meno... - dtVetoCheckIOB = DateTime.Now.AddMilliseconds(baseUtils.nextPauseSendMSec * 5); - lastIobOnline = DateTime.Now; + dtVetoCheckIOB = DateTime.Now.AddMilliseconds(baseUtils.nextPauseSendMSec * 10); } } catch diff --git a/IOB-WIN/IobMTC.cs b/IOB-WIN/IobMTC.cs index 18939a16..f9725df7 100644 --- a/IOB-WIN/IobMTC.cs +++ b/IOB-WIN/IobMTC.cs @@ -912,7 +912,9 @@ namespace IOB_WIN // se ho un contapezzi... processo... if (!string.IsNullOrEmpty(currPzCount)) { - int.TryParse(currPzCount, out lastCountCNC); + int newVal = -1; + Int32.TryParse(currPzCount, out newVal); + lastCountCNC = newVal > -1 ? newVal : lastCountCNC; } } } diff --git a/IOB-WIN/IobSiemens.cs b/IOB-WIN/IobSiemens.cs index 708b6a68..c4945a33 100644 --- a/IOB-WIN/IobSiemens.cs +++ b/IOB-WIN/IobSiemens.cs @@ -1106,7 +1106,9 @@ namespace IOB_WIN break; } // salvo... - Int32.TryParse(outputVal.ToString(), out lastCountCNC); + int newVal = -1; + Int32.TryParse(outputVal.ToString(), out newVal); + lastCountCNC = newVal > -1 ? newVal : lastCountCNC; if (isVerboseLog) { lgInfo("[2] outputVal contapezzi: {0}", outputVal); From bfdf065a034a5de03d3d0cd82b8682f9a7903af7 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Wed, 22 Jan 2020 18:21:30 +0100 Subject: [PATCH 4/5] ok bilanciato chaimate e numero dati in REDIS --- IOB-UT/Objects.cs | 12 +++-- IOB-UT/RedisMan.cs | 2 +- IOB-UT/baseUtils.cs | 9 ++-- IOB-WIN/AdapterForm.cs | 9 ++++ IOB-WIN/IobGeneric.cs | 107 ++++++----------------------------------- 5 files changed, 39 insertions(+), 100 deletions(-) diff --git a/IOB-UT/Objects.cs b/IOB-UT/Objects.cs index 411525f6..ff2ccd6b 100644 --- a/IOB-UT/Objects.cs +++ b/IOB-UT/Objects.cs @@ -492,6 +492,7 @@ namespace IOB_UT /// Status del SINGOLO IOB /// public bool online { get; set; } = false; +#if false /// /// Indica se sia correntemente in setup /// @@ -503,7 +504,8 @@ namespace IOB_UT /// /// Semaforo OUT (IOB-MPserver) /// - public Semaforo SemOut { get; set; } = Semaforo.ND; + public Semaforo SemOut { get; set; } = Semaforo.ND; +#endif /// /// Contatore IOB /// @@ -519,7 +521,7 @@ namespace IOB_UT /// /// Lunghezza coda FluxLog in uscita /// - public int queueFLogLen { get; set; } = 0; + public int queueFlLen { get; set; } = 0; /// /// Lungh coda ALLARMI in uscita /// @@ -527,7 +529,8 @@ namespace IOB_UT /// /// Lungh coda MESSAGGI in uscita /// - public int queueMsgLen { get; set; } = 0; + public int queueMsLen { get; set; } = 0; +#if false /// /// DataOra ultima comunicazione IN (con PLC) /// @@ -539,7 +542,8 @@ namespace IOB_UT /// /// Ultimo stato noto dei parametri in memoria letti da PLC /// - public Dictionary currParams { get; set; } = null; + public Dictionary currParams { get; set; } = null; +#endif } } diff --git a/IOB-UT/RedisMan.cs b/IOB-UT/RedisMan.cs index b55251bd..66e11327 100644 --- a/IOB-UT/RedisMan.cs +++ b/IOB-UT/RedisMan.cs @@ -997,7 +997,7 @@ namespace IOB_UT currData.online = value; servStatus = currData; } - } + } /// /// Variabile in cui viene salvato lo stato ONLINE dell'IOB /// diff --git a/IOB-UT/baseUtils.cs b/IOB-UT/baseUtils.cs index ec6275a8..8ce69b0c 100644 --- a/IOB-UT/baseUtils.cs +++ b/IOB-UT/baseUtils.cs @@ -72,7 +72,7 @@ namespace IOB_UT /// Indicazione VETO invio a server sino alla data-ora indicata /// public static DateTime dtVetoSend = DateTime.Now; - #if false +#if false { get { @@ -85,8 +85,11 @@ namespace IOB_UT redisMan.servStatus = currData; } } - #endif - +#endif + /// + /// Status MP_IO + /// + public static bool MPIO_Online = false; /// /// Calcola una pausa con errore casuale x il prossimo send programmato verso SERVER diff --git a/IOB-WIN/AdapterForm.cs b/IOB-WIN/AdapterForm.cs index 7a266ee8..c77291ec 100644 --- a/IOB-WIN/AdapterForm.cs +++ b/IOB-WIN/AdapterForm.cs @@ -464,6 +464,15 @@ namespace IOB_WIN // aggiorno labels counters... counterIob = $"pz iob {iobObj.contapezzi}"; counterMac = $"pz mac {iobObj.lastCountCNC}"; + // salvo ANCHE in redis... + var currIobStatus = iobObj.redisMan.iobStatus; + currIobStatus.queueEvLen = evQueueLen; + currIobStatus.queueFlLen = flQueueLen; + currIobStatus.queueAlLen = alQueueLen; + currIobStatus.queueMsLen = msQueueLen; + currIobStatus.counterIOB = iobObj.contapezzi; + currIobStatus.counterMAC = iobObj.lastCountCNC; + iobObj.redisMan.iobStatus = currIobStatus; } private void gather_Tick(object sender, EventArgs e) { diff --git a/IOB-WIN/IobGeneric.cs b/IOB-WIN/IobGeneric.cs index 673c65f2..b97acb81 100644 --- a/IOB-WIN/IobGeneric.cs +++ b/IOB-WIN/IobGeneric.cs @@ -56,19 +56,7 @@ namespace IOB_WIN /// /// Indica impianto IN SETUP (fino a quando SMETTE di esserlo...) /// - protected bool inSetup - { - get - { - return redisMan.iobStatus.inSetup; - } - set - { - var currData = redisMan.iobStatus; - currData.inSetup = value; - redisMan.iobStatus = currData; - } - } + protected bool inSetup = false; /// /// Indicazione VETO PING a server sino alla data-ora indicata /// @@ -81,19 +69,7 @@ namespace IOB_WIN set { utils.dtVetoPing = value; - } -#if false - get - { - return redisMan.servStatus.dtVetoPing; } - set - { - var currData = redisMan.servStatus; - currData.dtVetoPing = value; - redisMan.servStatus = currData; - } -#endif } /// /// Indicazione VETO invio a server sino alla data-ora indicata @@ -107,42 +83,17 @@ namespace IOB_WIN set { utils.dtVetoSend = value; - } -#if false - get - { - return redisMan.servStatus.dtVetoSend; } - set - { - var currData = redisMan.servStatus; - currData.dtVetoSend = value; - redisMan.servStatus = currData; - } -#endif } /// /// Indicazione VETO check status IOB x evitare loop troppo stretti... /// public DateTime dtVetoCheckIOB = DateTime.Now.AddDays(-1); -#if false - { - get - { - return redisMan.iobStatus.dtVetoCheckIOB; - } - set - { - var currData = redisMan.iobStatus; - currData.dtVetoCheckIOB = value; - redisMan.iobStatus = currData; - } - } -#endif /// /// Contapezzi attuale /// - public Int32 contapezzi + public Int32 contapezzi = 0; +#if false { get { @@ -154,23 +105,26 @@ namespace IOB_WIN currData.counterIOB = value; redisMan.iobStatus = currData; } - } + } +#endif /// /// Ultima lettura variabile contapezzi da CNC /// - public Int32 lastCountCNC + public Int32 lastCountCNC = 0; +#if false { get { - return (Int32)redisMan.iobStatus.counterMAC; - } - set + return (Int32) redisMan.iobStatus.counterMAC; + } + set { var currData = redisMan.iobStatus; - currData.counterMAC = value; + currData.counterMAC = value; redisMan.iobStatus = currData; } - } + } +#endif /// /// stato Online/Offline della IOB /// @@ -189,7 +143,7 @@ namespace IOB_WIN /// stato Online/Offline del server MP IO (su REDIS) /// public bool MPOnline - { + { get { return redisMan.MPIO_Online; @@ -197,7 +151,7 @@ namespace IOB_WIN set { redisMan.MPIO_Online = value; - } + } } #endregion @@ -4380,15 +4334,6 @@ namespace IOB_WIN accodaSignaInlData(newString); #endif } -#if false - /// - /// Mostra cosa ha/avrebbe inviato - /// - public void displayOutData() - { - accodaUrlData(lastUrl); - } -#endif /// /// Mostra cosa ha/avrebbe inviato /// @@ -4398,28 +4343,6 @@ namespace IOB_WIN // mostro update... accodaOtherData(newData); } -#if false - /// - /// Accoda (visualizzando in cima allo stack) la nuova stringa di output per area OTHER DATA - /// - /// - public void accodaSignaInlData(string newLine) - { - // inserisco in cima allo stack, trimmo e aggiorno display - parentForm.dataMonitor_1 = limitLine2show(string.Format("{0}{1}{2}", newLine, Environment.NewLine, parentForm.dataMonitor_1)); - } -#endif -#if false - /// - /// Accoda (visualizzando in cima allo stack) la nuova stringa di output per area OTHER DATA - /// - /// - public void accodaUrlData(string newLine) - { - // inserisco in cima allo stack, trimmo e aggiorno display - parentForm.dataMonitor_2 = limitLine2show(string.Format("{0}{1}{2}", newLine, Environment.NewLine, parentForm.dataMonitor_2)); - } -#endif /// /// Accoda (visualizzando in cima allo stack) la nuova stringa di output per area OTHER DATA /// From 544ca3fd724e39ffde6987344ddea1931809c315 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Wed, 22 Jan 2020 18:58:27 +0100 Subject: [PATCH 5/5] OK SIM e abbassati parametri, trasmette anche lastUpdate... --- IOB-UT/Objects.cs | 8 ++ IOB-UT/RedisMan.cs | 2 + IOB-WIN/AdapterForm.cs | 1 + IOB-WIN/App.config | 3 +- IOB-WIN/IobGeneric.cs | 165 +++++++++++++++++------------------------ Jenkinsfile | 2 +- 6 files changed, 84 insertions(+), 97 deletions(-) diff --git a/IOB-UT/Objects.cs b/IOB-UT/Objects.cs index ff2ccd6b..8af05708 100644 --- a/IOB-UT/Objects.cs +++ b/IOB-UT/Objects.cs @@ -478,6 +478,10 @@ namespace IOB_UT /// Status del server /// public bool online { get; set; } = false; + /// + /// DataOra ultima comunicazione OUT (con MP Server) + /// + public DateTime lastUpdate { get; set; } = DateTime.Now.AddDays(-1); } /// /// Rappresentazione dello stato corrente dell'IOB @@ -544,6 +548,10 @@ namespace IOB_UT /// public Dictionary currParams { get; set; } = null; #endif + /// + /// DataOra ultima comunicazione OUT (con MP Server) + /// + public DateTime lastUpdate { get; set; } = DateTime.Now.AddDays(-1); } } diff --git a/IOB-UT/RedisMan.cs b/IOB-UT/RedisMan.cs index 66e11327..de47d35f 100644 --- a/IOB-UT/RedisMan.cs +++ b/IOB-UT/RedisMan.cs @@ -995,6 +995,7 @@ namespace IOB_UT { var currData = servStatus; currData.online = value; + currData.lastUpdate = DateTime.Now; servStatus = currData; } } @@ -1011,6 +1012,7 @@ namespace IOB_UT { var currData = iobStatus; currData.online = value; + currData.lastUpdate = DateTime.Now; iobStatus = currData; } } diff --git a/IOB-WIN/AdapterForm.cs b/IOB-WIN/AdapterForm.cs index c77291ec..a1220b41 100644 --- a/IOB-WIN/AdapterForm.cs +++ b/IOB-WIN/AdapterForm.cs @@ -472,6 +472,7 @@ namespace IOB_WIN currIobStatus.queueMsLen = msQueueLen; currIobStatus.counterIOB = iobObj.contapezzi; currIobStatus.counterMAC = iobObj.lastCountCNC; + currIobStatus.lastUpdate = DateTime.Now; iobObj.redisMan.iobStatus = currIobStatus; } private void gather_Tick(object sender, EventArgs e) diff --git a/IOB-WIN/App.config b/IOB-WIN/App.config index ac6b3c65..b5ce548d 100644 --- a/IOB-WIN/App.config +++ b/IOB-WIN/App.config @@ -38,7 +38,7 @@ - + @@ -47,6 +47,7 @@ + diff --git a/IOB-WIN/IobGeneric.cs b/IOB-WIN/IobGeneric.cs index b97acb81..e67ccca2 100644 --- a/IOB-WIN/IobGeneric.cs +++ b/IOB-WIN/IobGeneric.cs @@ -53,78 +53,7 @@ namespace IOB_WIN /// Oggetto connessione REDIS /// public RedisIobCache redisMan; - /// - /// Indica impianto IN SETUP (fino a quando SMETTE di esserlo...) - /// - protected bool inSetup = false; - /// - /// Indicazione VETO PING a server sino alla data-ora indicata - /// - public DateTime dtVetoPing - { - get - { - return utils.dtVetoPing; - } - set - { - utils.dtVetoPing = value; - } - } - /// - /// Indicazione VETO invio a server sino alla data-ora indicata - /// - public DateTime dtVetoSend - { - get - { - return utils.dtVetoSend; - } - set - { - utils.dtVetoSend = value; - } - } - /// - /// Indicazione VETO check status IOB x evitare loop troppo stretti... - /// - public DateTime dtVetoCheckIOB = DateTime.Now.AddDays(-1); - /// - /// Contapezzi attuale - /// - public Int32 contapezzi = 0; -#if false - { - get - { - return (Int32)redisMan.iobStatus.counterIOB; - } - set - { - var currData = redisMan.iobStatus; - currData.counterIOB = value; - redisMan.iobStatus = currData; - } - } -#endif - /// - /// Ultima lettura variabile contapezzi da CNC - /// - public Int32 lastCountCNC = 0; -#if false - { - get - { - return (Int32) redisMan.iobStatus.counterMAC; - } - set - { - var currData = redisMan.iobStatus; - currData.counterMAC = value; - redisMan.iobStatus = currData; - } - } -#endif + /// /// stato Online/Offline della IOB /// @@ -173,8 +102,50 @@ namespace IOB_WIN /// Coda massima ammessa per FLog (se <=0 disattivata...) /// protected int maxQueueFLog { get; set; } = utils.CRI("maxQueueFLog"); - - + /// + /// Indica impianto IN SETUP (fino a quando SMETTE di esserlo...) + /// + protected bool inSetup = false; + /// + /// Indicazione VETO PING a server sino alla data-ora indicata + /// + public DateTime dtVetoPing + { + get + { + return utils.dtVetoPing; + } + set + { + utils.dtVetoPing = value; + } + } + /// + /// Indicazione VETO invio a server sino alla data-ora indicata + /// + public DateTime dtVetoSend + { + get + { + return utils.dtVetoSend; + } + set + { + utils.dtVetoSend = value; + } + } + /// + /// Indicazione VETO check status IOB x evitare loop troppo stretti... + /// + public DateTime dtVetoCheckIOB = DateTime.Now.AddDays(-1); + /// + /// Contapezzi attuale + /// + public Int32 contapezzi = 0; + /// + /// Ultima lettura variabile contapezzi da CNC + /// + public Int32 lastCountCNC = 0; /// /// ODL attualmente sulla macchina /// @@ -1376,8 +1347,17 @@ namespace IOB_WIN // verifico se risponde il server... if (checkServerAlive) { - // verifico SE posso inviare dati - if (checkIobEnabled) + bool iobOk = false; + if (utils.CRB("sendDataByThread")) + { + Task taskCheck = TaskEx.Run(() => iobOk = checkIobEnabled); + } + else + { + iobOk = checkIobEnabled; + } + // verifico SE posso inviare dati + if (iobOk) { currDispData.semOut = Semaforo.SV; // verificare come gestire il task secondario senza interferenza (chiamate update su FORM da thread secondari danno errori) @@ -2907,12 +2887,12 @@ namespace IOB_WIN answ = (callResp == "OK"); // attesa casuale se necessario var rand = new Random(); - // primi 3 test - int maxTry = 3; + // primi 2 test + int maxTry = 2; while (maxTry > 0 && !answ) { - Thread.Sleep(rand.Next(150, 500)); - callResp = callUrl(urlIobEnabled, false); + Thread.Sleep(rand.Next(250, 500)); + callResp = callUrl(urlIobEnabled, true); answ = (callResp == "OK"); maxTry--; } @@ -2920,32 +2900,27 @@ namespace IOB_WIN if (!answ) { resetwebClients(); - Thread.Sleep(rand.Next(500, 1000)); + Thread.Sleep(rand.Next(250, 1000)); callResp = callUrl(urlIobEnabled, false); answ = (callResp == "OK"); - } - // altri 3 - maxTry = 3; + } + // altri 2 + maxTry = 2; while (maxTry > 0 && !answ) { - Thread.Sleep(rand.Next(150, 500)); + Thread.Sleep(rand.Next(250, 500)); callResp = callUrl(urlIobEnabled, false); answ = (callResp == "OK"); maxTry--; - } - // salvo status... + } + // salvo status... IobOnline = answ; - // se offline imposto veto check a 2 x tempo reinvio... + // se online imposto veto check a 5 x tempo reinvio... if (answ) { - // non controllo x meno... - dtVetoCheckIOB = DateTime.Now.AddMilliseconds(baseUtils.nextPauseSendMSec * 2); lastIobOnline = DateTime.Now; - } - else - { - dtVetoCheckIOB = DateTime.Now.AddMilliseconds(baseUtils.nextPauseSendMSec * 10); - } + } + dtVetoCheckIOB = DateTime.Now.AddMilliseconds(baseUtils.nextPauseSendMSec * 5); } catch { } diff --git a/Jenkinsfile b/Jenkinsfile index 1ee20332..fa7bce1b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -16,7 +16,7 @@ pipeline { /* calcolo numero versione... diverso x branch MASTER/DEVELOP */ script { - withEnv(['NEXT_BUILD_NUMBER=638']) { + withEnv(['NEXT_BUILD_NUMBER=641']) { // env.versionNumber = VersionNumber(versionNumberString : '3.0.${BUILD_DATE_FORMATTED, "yyMM"}.${BUILDS_ALL_TIME}', projectStartDate : '2006-01-01', skipFailedBuilds: true) env.versionNumber = VersionNumber(versionNumberString : '3.0.${BUILD_DATE_FORMATTED, "yyMM"}.${BUILDS_ALL_TIME}', projectStartDate : '2006-01-01', skipFailedBuilds: true, overrideBuildsAllTime: '${NEXT_BUILD_NUMBER}') env.APP_NAME = 'MAPO-IOB-WIN'