From dfb224e088445eeb8aea3d0e52f9fae145eb4b66 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Sat, 15 Sep 2018 12:06:51 +0200 Subject: [PATCH] Modifica massiva x portare adapterRed a gestire start/stop --- MTC_Adapter/MTC/MTC.csproj | 1 + MTC_Adapter/MTC/enumerations.cs | 124 +++ MTC_Adapter/OPC-UA-REDIS/AdapterRed.cs | 207 ++++- MTC_Adapter/OPC-UA-REDIS/OPC-UA-REDIS.csproj | 11 +- MTC_Adapter/OPC-UA-REDIS/packages.config | 8 +- MTC_Adapter/OPC-UA-REDIS/redUtil.cs | 751 +++++++++++++++++++ MTC_Adapter/SCMA/AdapterCom/Gateway.cs | 8 +- MTC_Adapter/SCMA/AdapterCom/GatewaySOURS.cs | 30 +- 8 files changed, 1120 insertions(+), 20 deletions(-) create mode 100644 MTC_Adapter/MTC/enumerations.cs create mode 100644 MTC_Adapter/OPC-UA-REDIS/redUtil.cs diff --git a/MTC_Adapter/MTC/MTC.csproj b/MTC_Adapter/MTC/MTC.csproj index 661a490..8aa227c 100644 --- a/MTC_Adapter/MTC/MTC.csproj +++ b/MTC_Adapter/MTC/MTC.csproj @@ -56,6 +56,7 @@ + diff --git a/MTC_Adapter/MTC/enumerations.cs b/MTC_Adapter/MTC/enumerations.cs new file mode 100644 index 0000000..f9eb400 --- /dev/null +++ b/MTC_Adapter/MTC/enumerations.cs @@ -0,0 +1,124 @@ +namespace MTC +{ + /// + /// Classe item node (tipo/obj) + /// + public class itemNode + { + /// + /// Tipo oggetto (per cast) + /// + public itemType cType; + /// + /// Object specifico + /// + public object cObject; + /// + /// costruttore + /// + public itemNode() + { } + /// + /// costruttore + /// + /// + /// + public itemNode(itemType _tipo, object _obj) + { + cType = _tipo; + cObject = _obj; + } + } + /// + /// Tipologia di ITEM + /// + public enum itemType + { + /// + /// CONDIZION = ALLARME + /// + Condition, + /// + /// Evento = point in time data + /// + Event, + /// + /// Messaggio generico + /// + Message, + /// + /// Campionamento continuo + /// + Sample + } + /// + /// Varibili STATO ATTIVO (es attuatori, sistemi...) + /// + public enum actStatus + { + /// + /// Stato inattivo + /// + INACTIVE = 0, + /// + /// Stato Attivo + /// + ACTIVE + } + /// + /// Varibili STATO OnOff + /// + public enum onOffStatus + { + /// + /// Stato OFF + /// + OFF = 0, + /// + /// Stato ON + /// + ON + } + /// + /// Varibili STATO per EMERGENZA + /// + public enum emStatus + { + /// + /// Stato ARMATO + /// + ARMED = 0, + /// + /// Stato EMERGENZA PREMUTA + /// + TRIGGERED + } + /// + /// Varibili STATO per AVAIL + /// + public enum availStatus + { + /// + /// Stato DISPONIBILE + /// + AVAILABLE = 0, + /// + /// Stato NON disponibile + /// + UNAVAILABLE + } + /// + /// Varibili STATO per AVAIL + /// + public enum pathType + { + /// + /// Stato LAVORO + /// + LAVORO = 0, + /// + /// Stato ASSERV + /// + ASSERV + } +} diff --git a/MTC_Adapter/OPC-UA-REDIS/AdapterRed.cs b/MTC_Adapter/OPC-UA-REDIS/AdapterRed.cs index f93cfcc..7026115 100644 --- a/MTC_Adapter/OPC-UA-REDIS/AdapterRed.cs +++ b/MTC_Adapter/OPC-UA-REDIS/AdapterRed.cs @@ -1,12 +1,49 @@ -namespace OPC_UA_REDIS +using MTC; +using System; +using System.Collections.Generic; + +namespace OPC_UA_REDIS { public class AdapterRed { + + /// + /// Oggetto elenco allarmi + /// + public allarme[] elencoAllarmi { get; set; } + /// + /// Dizionario conversione nome variabili COMPLETE (per gestione REDIS "_" --> ":") + /// + public Dictionary nameRemap { get; set; } + /// + /// Dizionario conversione nome variabili con replace "like" (per gestione REDIS "_" --> ":") + /// + public Dictionary nameReplace { get; set; } + + /// + /// Elenco di TUTTI i NODI ITEMS gestiti dal gateway (item/variabile)... + /// + public Dictionary itemNodes { get; set; } + /// + /// Elenco di TUTTI i NODI CONDITIONS gestiti dal gateway (allarme/condizione)... + /// + public Dictionary conditionNodes { get; set; } + + /// + /// Classe adapter verso REDIS + /// + /// + /// + /// public AdapterRed(int sPort = 6379, string sAddress = "127.0.0.1", bool verbose = false) { ServerPort = sPort; ServerAddr = sAddress; Verbose = verbose; + nameRemap = new Dictionary(); + nameReplace = new Dictionary(); + itemNodes = new Dictionary(); + conditionNodes = new Dictionary(); } #if false @@ -29,9 +66,173 @@ public void SendAllTo(Stream aClient); public void SendChanged(string timestamp = null); public void SendToAll(string line); - public void Start(); - public void Stop(); public void Unavailable(); #endif + + #region metodi esposti + + /// + /// Arresto adapter + /// + public void Stop() + { + // var accessorie + string hashKey = ""; + string hashVal = ""; + + // Imposto CONF + hashKey = redUtil.redHash("Adp:Status"); + hashVal = string.Format("stopped"); + redUtil.setRSV(hashKey, hashVal); + hashKey = redUtil.redHash("Adp:Heartbeat"); + hashVal = DateTime.Now.ToString(); + redUtil.setRSV(hashKey, hashVal); + + // ora il valore delle chaivi diventa unavailable... + hashVal = availStatus.UNAVAILABLE.ToString(); + // percorro tutto l'albero nodi item ed allarmi e SCRIVO!!!! + foreach (var item in itemNodes) + { + hashKey = machineHash(item.Key); + redUtil.setRSV(hashKey, hashVal); + } + } + + /// + /// Avvio adapter (popolamento iniziale su redis...) + /// + public void Start() + { + // var accessorie + string hashKey = ""; + string hashVal = ""; + var list = new List>(); + string nLevel = ""; + // Imposto CONF + hashKey = redUtil.redHash("Adp:Vers"); + hashVal = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); + redUtil.setRSV(hashKey, hashVal); + hashKey = redUtil.redHash("Adp:Status"); + hashVal = "started"; + redUtil.setRSV(hashKey, hashVal); + hashKey = redUtil.redHash("Adp:Heartbeat"); + hashVal = DateTime.Now.ToString(); + redUtil.setRSV(hashKey, hashVal); + // allarmi CNC + hashKey = confHash("CNC"); + foreach (var item in elencoAllarmi) + { + // se è PLC aggiungo + if (item.gruppo == "CNC") + { + nLevel = item.livello == "FAULT" ? "900" : "500"; + list.Add(new KeyValuePair(item.codNum, nLevel + "|" + item.descrizione)); + } + } + redUtil.redSaveHashList(hashKey, list); + // allarmi PLC + hashKey = confHash("PLC"); + foreach (var item in elencoAllarmi) + { + // se è PLC aggiungo + if (item.gruppo == "PLC") + { + nLevel = item.livello == "FAULT" ? "900" : "500"; + list.Add(new KeyValuePair(item.codNum, nLevel + "|" + item.descrizione)); + } + } + redUtil.redSaveHashList(hashKey, list); + + // DataModel !!!FARE!!! + //hashKey = confHash("DataModel"); + //hashVal = ""; + //setRSV(hashKey, hashVal); + + // imposto VETO + hashKey = vetoHash("CNC"); + hashVal = "0"; + redUtil.setRSV(hashKey, hashVal); + hashKey = vetoHash("PLC"); + hashVal = "0"; + redUtil.setRSV(hashKey, hashVal); + + // Imposto DATI: percorro tutto l'albero nodi item ed allarmi e SCRIVO!!!! + foreach (var item in itemNodes) + { + hashKey = machineHash(item.Key); + hashVal = availStatus.UNAVAILABLE.ToString(); // item.Value.cObject.ToString(); + redUtil.setRSV(hashKey, hashVal); + } + } + + #endregion + + #region helper gestione SOUR + + /// + /// Fix KEY x standard redis ("_" --> ":") + /// + /// + /// + public string fixRedKey(string origKey) + { + // parto da variabile dichiarata + string answ = origKey; + // cerco sostituzioni COMPLETE (REMAP) da dizionario ricodifica + if (nameRemap.ContainsKey(answ)) + { + answ = nameRemap[answ]; + } + else + { + // se NON HO un MATCH processo il replace e se trovo corrispondeza processo + foreach (var item in nameReplace.Keys) + { + // PER ORA SOLO SE inizia per quel valore... + if (answ.IndexOf(item) == 0) + { + // effetuo sostituzione chiave/valore + answ = answ.Replace(item, nameReplace[item]); + } + } + } + // ora sostituzione "_" --> ":" + try + { + answ = answ.Replace("_", ":"); + } + catch + { } + return answ; + } + /// + /// HASH x la parte MACHINE del server REDIS + /// + /// + /// + public string machineHash(string keyName) + { + return redUtil.redHash("Machine:" + fixRedKey(keyName)); + } + /// + /// HASH x la parte MACHINE del server REDIS + /// + /// + /// + public string confHash(string keyName) + { + return redUtil.redHash("CONF:" + fixRedKey(keyName)); + } + /// + /// HASH x la parte MACHINE del server REDIS + /// + /// + /// + public string vetoHash(string keyName) + { + return redUtil.redHash("VETO:" + fixRedKey(keyName)); + } + + #endregion } } diff --git a/MTC_Adapter/OPC-UA-REDIS/OPC-UA-REDIS.csproj b/MTC_Adapter/OPC-UA-REDIS/OPC-UA-REDIS.csproj index c76d59d..256d4d5 100644 --- a/MTC_Adapter/OPC-UA-REDIS/OPC-UA-REDIS.csproj +++ b/MTC_Adapter/OPC-UA-REDIS/OPC-UA-REDIS.csproj @@ -33,10 +33,10 @@ - ..\packages\NLog.4.5.8\lib\net45\NLog.dll + ..\packages\NLog.4.5.9\lib\net45\NLog.dll - ..\packages\StackExchange.Redis.1.2.6\lib\net46\StackExchange.Redis.dll + ..\packages\StackExchange.Redis.1.2.6\lib\net45\StackExchange.Redis.dll @@ -56,6 +56,7 @@ + @@ -66,5 +67,11 @@ + + + {ec83d80e-9f3b-4de9-b16a-ca216543b7ec} + MTC + + \ No newline at end of file diff --git a/MTC_Adapter/OPC-UA-REDIS/packages.config b/MTC_Adapter/OPC-UA-REDIS/packages.config index 5a20623..f0c9d8e 100644 --- a/MTC_Adapter/OPC-UA-REDIS/packages.config +++ b/MTC_Adapter/OPC-UA-REDIS/packages.config @@ -1,7 +1,7 @@  - - - - + + + + \ No newline at end of file diff --git a/MTC_Adapter/OPC-UA-REDIS/redUtil.cs b/MTC_Adapter/OPC-UA-REDIS/redUtil.cs new file mode 100644 index 0000000..5803ad9 --- /dev/null +++ b/MTC_Adapter/OPC-UA-REDIS/redUtil.cs @@ -0,0 +1,751 @@ +using MTC; +using NLog; +using StackExchange.Redis; +using System; +using System.Collections.Generic; + +namespace OPC_UA_REDIS +{ + public class redUtil + { + /// + /// wrapper di log + /// + public static Logger lg; + + + #region gestione valori in RedisCache + + /// + /// Nome della variabile HASH da utilizzare (dato CodModulo / Server / DB impiegato da funzionalita' DbConfig) + keyName richiesto... + /// + public static string redHash(string keyName) + { + string answ = keyName; + try + { + answ = string.Format("{0}:{1}", baseUtils.CRS("SOURS_baseHash"), keyName); + } + catch + { } + return answ; + } + + /// + /// Connessione lazy a redis... + /// + private static Lazy lazyConnection = new Lazy(() => + { + return ConnectionMultiplexer.Connect("127.0.0.1,abortConnect=false,ssl=false"); + }); + /// + /// Connessione lazy a redis... + /// + private static Lazy lazyConnectionAdmin = new Lazy(() => + { + return ConnectionMultiplexer.Connect("127.0.0.1,abortConnect=false,ssl=false,allowAdmin=true"); + }); + /// + /// Oggetto statico connessione redis + /// + public static ConnectionMultiplexer connRedis + { + get + { + return lazyConnection.Value; + } + } + /// + /// Oggetto statico connessione redis + /// + public static 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) + { + lg.Error("Eccezione in redServInfo: " + exc.ToString()); + } + return answ; + } + + /// + /// Restituisce una chiave salvata in RedisCache + /// + /// + /// + public string getRSV(string chiave) + { + string answ = ""; + try + { + IDatabase cache = connRedis.GetDatabase(); + answ = cache.StringGet(chiave); + } + catch (Exception exc) + { + lg.Info(string.Format("Errore in getRSV:{0}{1}", Environment.NewLine, exc)); + } + return answ; + } + /// + /// Salva una chiave in RedisCache + /// + /// + /// + /// + public static bool setRSV(string chiave, string valore) + { + bool answ = false; + try + { + IDatabase cache = connRedis.GetDatabase(); + cache.StringSet(chiave, valore); + answ = true; + } + catch (Exception exc) + { + lg.Info(string.Format("Errore in setRSV:{0}{1}", Environment.NewLine, exc)); + } + return answ; + } + /// + /// Salva una chiave in RedisCache + /// + /// + /// + /// in secondi + /// + public bool setRSV(string chiave, string valore, int TTL_sec) + { + bool answ = false; + try + { + IDatabase cache = connRedis.GetDatabase(); + TimeSpan expT = new TimeSpan(0, 0, TTL_sec); + // salvo con expyry... + cache.StringSet(chiave, valore, expT); + answ = true; + } + catch (Exception exc) + { + lg.Info(string.Format("Errore in setRSV:{0}{1}", Environment.NewLine, exc)); + } + return answ; + } + /// + /// Incrementa un contatore in Redis + /// + /// + /// + public long setRCntI(string chiave) + { + long answ = 0; + try + { + IDatabase cache = connRedis.GetDatabase(); + answ = cache.StringIncrement(chiave, 1); + } + catch (Exception exc) + { + lg.Info(string.Format("Errore in setRCI:{0}{1}", Environment.NewLine, exc)); + } + return answ; + } + /// + /// Decrementa un contatore in Redis + /// + /// + /// + public long setRCntD(string chiave) + { + long answ = 0; + try + { + IDatabase cache = connRedis.GetDatabase(); + answ = cache.StringDecrement(chiave, 1); + } + catch (Exception exc) + { + lg.Info(string.Format("Errore in setRCD:{0}{1}", Environment.NewLine, exc)); + } + return answ; + } + /// + /// Restituisce una chiave COUNTER in RedisCache + /// + /// + /// + public int getRCnt(string chiave) + { + int answInt = 0; + string answ = ""; + try + { + IDatabase cache = connRedis.GetDatabase(); + answ = cache.StringGet(chiave); + answInt = Convert.ToInt32(answ); + } + catch (Exception exc) + { + lg.Info(string.Format("Errore in getRSV:{0}{1}", Environment.NewLine, exc)); + } + return answInt; + } + /// + /// Resetta (elimina) un contatore in Redis + /// + /// + /// + public bool resetRCnt(string chiave) + { + bool answ = false; + try + { + IDatabase cache = connRedis.GetDatabase(); + answ = cache.KeyDelete(chiave); + } + catch (Exception exc) + { + lg.Info(string.Format("Errore in resetRCnt:{0}{1}", Environment.NewLine, exc)); + } + return answ; + } + /// + /// Restituisce un set KVP (Key Value Pair) salvati in RedisCache + /// + /// + /// + public RedisValue[] getRKeys(RedisKey[] chiavi) + { + RedisValue[] answ = null; + try + { + IDatabase cache = connRedis.GetDatabase(); + answ = cache.StringGet(chiavi); + } + catch (Exception exc) + { + lg.Info(string.Format("Errore in getRKeys:{0}{1}", Environment.NewLine, exc)); + } + 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 + { + IDatabase cache = connRedis.GetDatabase(); + cache.StringSet(valori); + answ = true; + } + catch (Exception exc) + { + lg.Info(string.Format("Errore in setRKeys:{0}{1}", Environment.NewLine, exc)); + } + 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... + IDatabase cache = connRedis.GetDatabase(); + try + { + answ = cache.HashGetAll(key).Length > 0; + } + catch + { } + 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... + IDatabase cache = connRedis.GetDatabase(); + 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... + IDatabase cache = connRedis.GetDatabase(); + 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... + IDatabase cache = connRedis.GetDatabase(); + 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... + IDatabase cache = connRedis.GetDatabase(); + 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 + /// + /// chiave + /// valori come lista KVP + /// + public static bool redSaveHashList(string hashKey, List> hashListKVP) + { + bool answ = false; + // 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 in formato Dictionary + /// + /// chiave + /// valori + /// + public bool redSaveHashDict(string hashKey, Dictionary hashFields) + { + bool answ = false; + // cerco se ci sia valore in redis... + IDatabase cache = connRedis.GetDatabase(); + 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 + /// 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... + IDatabase cache = connRedis.GetDatabase(); + 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... + IDatabase cache = connRedis.GetDatabase(); + 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... + IDatabase cache = connRedis.GetDatabase(); + 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... + IDatabase cache = connRedis.GetDatabase(); + // se vuoto = ALL... + keyPattern = keyPattern == "" ? "**" : keyPattern; + try + { + foreach (var ep in connRedis.GetEndPoints()) + { + var server = connRedis.GetServer(ep); + foreach (var key in server.Keys(pattern: keyPattern)) + { + cache.KeyDelete(key); + } + } + answ = true; + } + catch (Exception exc) + { + lg.Error(string.Format("Eccezione: {0}", exc)); + } + 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... + IDatabase cache = connRedis.GetDatabase(); + // se vuoto = ALL... + keyPattern = 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) + { + lg.Error(string.Format("Eccezione: {0}", exc)); + } + 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 = 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) + { + lg.Error(string.Format("Eccezione: {0}", exc)); + } + // 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 + } +} diff --git a/MTC_Adapter/SCMA/AdapterCom/Gateway.cs b/MTC_Adapter/SCMA/AdapterCom/Gateway.cs index 9d5cf36..ece848f 100644 --- a/MTC_Adapter/SCMA/AdapterCom/Gateway.cs +++ b/MTC_Adapter/SCMA/AdapterCom/Gateway.cs @@ -472,6 +472,7 @@ namespace SCMA.AdapterCom #endregion } +#if false /// /// Classe item node (tipo/obj) /// @@ -500,7 +501,8 @@ namespace SCMA.AdapterCom cType = _tipo; cObject = _obj; } - } + } +#endif /// /// Tipologia protocolli di comunicazione ammessi @@ -520,6 +522,7 @@ namespace SCMA.AdapterCom /// SOURS } +#if false /// /// Tipologia di ITEM /// @@ -611,5 +614,6 @@ namespace SCMA.AdapterCom /// Stato ASSERV /// ASSERV - } + } +#endif } diff --git a/MTC_Adapter/SCMA/AdapterCom/GatewaySOURS.cs b/MTC_Adapter/SCMA/AdapterCom/GatewaySOURS.cs index f3f15d2..321cd3b 100644 --- a/MTC_Adapter/SCMA/AdapterCom/GatewaySOURS.cs +++ b/MTC_Adapter/SCMA/AdapterCom/GatewaySOURS.cs @@ -869,11 +869,21 @@ namespace SCMA.AdapterCom /// public override void start() { -#if false - mAdapter.Port = port; - mAdapter.Start(); -#endif + // passo le tabelle di remap/replace + mAdapter.ServerPort = 6379; // !!! port hard coded + mAdapter.ServerAddr = "127.0.0.1"; // !!! IP hard coded + mAdapter.nameRemap = nameRemap; + mAdapter.nameReplace = nameReplace; + // !!! TOGLIERE?!? + mAdapter.elencoAllarmi = elencoAllarmi; + mAdapter.itemNodes = itemNodes; + mAdapter.conditionNodes = conditionNodes; + // avvio oggetto + mAdapter.Start(); + + // spostato in adapterRed +#if false // var accessorie string hashKey = ""; string hashVal = ""; @@ -933,17 +943,18 @@ namespace SCMA.AdapterCom hashKey = machineHash(item.Key); hashVal = availStatus.UNAVAILABLE.ToString(); // item.Value.cObject.ToString(); setRSV(hashKey, hashVal); - } + } +#endif } /// /// Wrapper metodo STOP /// public override void stop() { -#if false - mAdapter.Stop(); -#endif + mAdapter.Stop(); + // spostato in adapterRed +#if false // var accessorie string hashKey = ""; string hashVal = ""; @@ -962,7 +973,8 @@ namespace SCMA.AdapterCom { hashKey = machineHash(item.Key); setRSV(hashKey, hashVal); - } + } +#endif } #endregion