Modifica massiva x portare adapterRed a gestire start/stop
This commit is contained in:
@@ -56,6 +56,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="BinaryFormatter.cs" />
|
||||
<Compile Include="enumerations.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="baseUtils.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
namespace MTC
|
||||
{
|
||||
/// <summary>
|
||||
/// Classe item node (tipo/obj)
|
||||
/// </summary>
|
||||
public class itemNode
|
||||
{
|
||||
/// <summary>
|
||||
/// Tipo oggetto (per cast)
|
||||
/// </summary>
|
||||
public itemType cType;
|
||||
/// <summary>
|
||||
/// Object specifico
|
||||
/// </summary>
|
||||
public object cObject;
|
||||
/// <summary>
|
||||
/// costruttore
|
||||
/// </summary>
|
||||
public itemNode()
|
||||
{ }
|
||||
/// <summary>
|
||||
/// costruttore
|
||||
/// </summary>
|
||||
/// <param name="_tipo"></param>
|
||||
/// <param name="_obj"></param>
|
||||
public itemNode(itemType _tipo, object _obj)
|
||||
{
|
||||
cType = _tipo;
|
||||
cObject = _obj;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Tipologia di ITEM
|
||||
/// </summary>
|
||||
public enum itemType
|
||||
{
|
||||
/// <summary>
|
||||
/// CONDIZION = ALLARME
|
||||
/// </summary>
|
||||
Condition,
|
||||
/// <summary>
|
||||
/// Evento = point in time data
|
||||
/// </summary>
|
||||
Event,
|
||||
/// <summary>
|
||||
/// Messaggio generico
|
||||
/// </summary>
|
||||
Message,
|
||||
/// <summary>
|
||||
/// Campionamento continuo
|
||||
/// </summary>
|
||||
Sample
|
||||
}
|
||||
/// <summary>
|
||||
/// Varibili STATO ATTIVO (es attuatori, sistemi...)
|
||||
/// </summary>
|
||||
public enum actStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// Stato inattivo
|
||||
/// </summary>
|
||||
INACTIVE = 0,
|
||||
/// <summary>
|
||||
/// Stato Attivo
|
||||
/// </summary>
|
||||
ACTIVE
|
||||
}
|
||||
/// <summary>
|
||||
/// Varibili STATO OnOff
|
||||
/// </summary>
|
||||
public enum onOffStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// Stato OFF
|
||||
/// </summary>
|
||||
OFF = 0,
|
||||
/// <summary>
|
||||
/// Stato ON
|
||||
/// </summary>
|
||||
ON
|
||||
}
|
||||
/// <summary>
|
||||
/// Varibili STATO per EMERGENZA
|
||||
/// </summary>
|
||||
public enum emStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// Stato ARMATO
|
||||
/// </summary>
|
||||
ARMED = 0,
|
||||
/// <summary>
|
||||
/// Stato EMERGENZA PREMUTA
|
||||
/// </summary>
|
||||
TRIGGERED
|
||||
}
|
||||
/// <summary>
|
||||
/// Varibili STATO per AVAIL
|
||||
/// </summary>
|
||||
public enum availStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// Stato DISPONIBILE
|
||||
/// </summary>
|
||||
AVAILABLE = 0,
|
||||
/// <summary>
|
||||
/// Stato NON disponibile
|
||||
/// </summary>
|
||||
UNAVAILABLE
|
||||
}
|
||||
/// <summary>
|
||||
/// Varibili STATO per AVAIL
|
||||
/// </summary>
|
||||
public enum pathType
|
||||
{
|
||||
/// <summary>
|
||||
/// Stato LAVORO
|
||||
/// </summary>
|
||||
LAVORO = 0,
|
||||
/// <summary>
|
||||
/// Stato ASSERV
|
||||
/// </summary>
|
||||
ASSERV
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,49 @@
|
||||
namespace OPC_UA_REDIS
|
||||
using MTC;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OPC_UA_REDIS
|
||||
{
|
||||
public class AdapterRed
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Oggetto elenco allarmi
|
||||
/// </summary>
|
||||
public allarme[] elencoAllarmi { get; set; }
|
||||
/// <summary>
|
||||
/// Dizionario conversione nome variabili COMPLETE (per gestione REDIS "_" --> ":")
|
||||
/// </summary>
|
||||
public Dictionary<string, string> nameRemap { get; set; }
|
||||
/// <summary>
|
||||
/// Dizionario conversione nome variabili con replace "like" (per gestione REDIS "_" --> ":")
|
||||
/// </summary>
|
||||
public Dictionary<string, string> nameReplace { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Elenco di TUTTI i NODI ITEMS gestiti dal gateway (item/variabile)...
|
||||
/// </summary>
|
||||
public Dictionary<string, itemNode> itemNodes { get; set; }
|
||||
/// <summary>
|
||||
/// Elenco di TUTTI i NODI CONDITIONS gestiti dal gateway (allarme/condizione)...
|
||||
/// </summary>
|
||||
public Dictionary<string, itemNode> conditionNodes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Classe adapter verso REDIS
|
||||
/// </summary>
|
||||
/// <param name="sPort"></param>
|
||||
/// <param name="sAddress"></param>
|
||||
/// <param name="verbose"></param>
|
||||
public AdapterRed(int sPort = 6379, string sAddress = "127.0.0.1", bool verbose = false)
|
||||
{
|
||||
ServerPort = sPort;
|
||||
ServerAddr = sAddress;
|
||||
Verbose = verbose;
|
||||
nameRemap = new Dictionary<string, string>();
|
||||
nameReplace = new Dictionary<string, string>();
|
||||
itemNodes = new Dictionary<string, itemNode>();
|
||||
conditionNodes = new Dictionary<string, itemNode>();
|
||||
}
|
||||
|
||||
#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
|
||||
|
||||
/// <summary>
|
||||
/// Arresto adapter
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Avvio adapter (popolamento iniziale su redis...)
|
||||
/// </summary>
|
||||
public void Start()
|
||||
{
|
||||
// var accessorie
|
||||
string hashKey = "";
|
||||
string hashVal = "";
|
||||
var list = new List<KeyValuePair<string, string>>();
|
||||
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<string, string>(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<string, string>(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
|
||||
|
||||
/// <summary>
|
||||
/// Fix KEY x standard redis ("_" --> ":")
|
||||
/// </summary>
|
||||
/// <param name="origKey"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// HASH x la parte MACHINE del server REDIS
|
||||
/// </summary>
|
||||
/// <param name="keyName"></param>
|
||||
/// <returns></returns>
|
||||
public string machineHash(string keyName)
|
||||
{
|
||||
return redUtil.redHash("Machine:" + fixRedKey(keyName));
|
||||
}
|
||||
/// <summary>
|
||||
/// HASH x la parte MACHINE del server REDIS
|
||||
/// </summary>
|
||||
/// <param name="keyName"></param>
|
||||
/// <returns></returns>
|
||||
public string confHash(string keyName)
|
||||
{
|
||||
return redUtil.redHash("CONF:" + fixRedKey(keyName));
|
||||
}
|
||||
/// <summary>
|
||||
/// HASH x la parte MACHINE del server REDIS
|
||||
/// </summary>
|
||||
/// <param name="keyName"></param>
|
||||
/// <returns></returns>
|
||||
public string vetoHash(string keyName)
|
||||
{
|
||||
return redUtil.redHash("VETO:" + fixRedKey(keyName));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,10 +33,10 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NLog.4.5.8\lib\net45\NLog.dll</HintPath>
|
||||
<HintPath>..\packages\NLog.4.5.9\lib\net45\NLog.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="StackExchange.Redis, Version=1.2.6.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\StackExchange.Redis.1.2.6\lib\net46\StackExchange.Redis.dll</HintPath>
|
||||
<HintPath>..\packages\StackExchange.Redis.1.2.6\lib\net45\StackExchange.Redis.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Configuration" />
|
||||
@@ -56,6 +56,7 @@
|
||||
<Compile Include="AdapterRed.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="DataItemRed.cs" />
|
||||
<Compile Include="redUtil.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="NLog.config">
|
||||
@@ -66,5 +67,11 @@
|
||||
</None>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MTC\MTC.csproj">
|
||||
<Project>{ec83d80e-9f3b-4de9-b16a-ca216543b7ec}</Project>
|
||||
<Name>MTC</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="NLog" version="4.5.8" targetFramework="net461" />
|
||||
<package id="NLog.Config" version="4.5.8" targetFramework="net461" />
|
||||
<package id="NLog.Schema" version="4.5.8" targetFramework="net461" />
|
||||
<package id="StackExchange.Redis" version="1.2.6" targetFramework="net461" requireReinstallation="true" />
|
||||
<package id="NLog" version="4.5.9" targetFramework="net452" />
|
||||
<package id="NLog.Config" version="4.5.9" targetFramework="net452" />
|
||||
<package id="NLog.Schema" version="4.5.9" targetFramework="net452" />
|
||||
<package id="StackExchange.Redis" version="1.2.6" targetFramework="net452" />
|
||||
</packages>
|
||||
@@ -0,0 +1,751 @@
|
||||
using MTC;
|
||||
using NLog;
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OPC_UA_REDIS
|
||||
{
|
||||
public class redUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// wrapper di log
|
||||
/// </summary>
|
||||
public static Logger lg;
|
||||
|
||||
|
||||
#region gestione valori in RedisCache
|
||||
|
||||
/// <summary>
|
||||
/// Nome della variabile HASH da utilizzare (dato CodModulo / Server / DB impiegato da funzionalita' DbConfig) + keyName richiesto...
|
||||
/// </summary>
|
||||
public static string redHash(string keyName)
|
||||
{
|
||||
string answ = keyName;
|
||||
try
|
||||
{
|
||||
answ = string.Format("{0}:{1}", baseUtils.CRS("SOURS_baseHash"), keyName);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
return answ;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Connessione lazy a redis...
|
||||
/// </summary>
|
||||
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
|
||||
{
|
||||
return ConnectionMultiplexer.Connect("127.0.0.1,abortConnect=false,ssl=false");
|
||||
});
|
||||
/// <summary>
|
||||
/// Connessione lazy a redis...
|
||||
/// </summary>
|
||||
private static Lazy<ConnectionMultiplexer> lazyConnectionAdmin = new Lazy<ConnectionMultiplexer>(() =>
|
||||
{
|
||||
return ConnectionMultiplexer.Connect("127.0.0.1,abortConnect=false,ssl=false,allowAdmin=true");
|
||||
});
|
||||
/// <summary>
|
||||
/// Oggetto statico connessione redis
|
||||
/// </summary>
|
||||
public static ConnectionMultiplexer connRedis
|
||||
{
|
||||
get
|
||||
{
|
||||
return lazyConnection.Value;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Oggetto statico connessione redis
|
||||
/// </summary>
|
||||
public static ConnectionMultiplexer connRedisAdmin
|
||||
{
|
||||
get
|
||||
{
|
||||
return lazyConnectionAdmin.Value;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Restituisce info dei server connessi...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restituisce una chiave salvata in RedisCache
|
||||
/// </summary>
|
||||
/// <param name="chiave"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Salva una chiave in RedisCache
|
||||
/// </summary>
|
||||
/// <param name="chiave"></param>
|
||||
/// <param name="valore"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Salva una chiave in RedisCache
|
||||
/// </summary>
|
||||
/// <param name="chiave"></param>
|
||||
/// <param name="valore"></param>
|
||||
/// <param name="TTL_sec">in secondi</param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Incrementa un contatore in Redis
|
||||
/// </summary>
|
||||
/// <param name="chiave"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Decrementa un contatore in Redis
|
||||
/// </summary>
|
||||
/// <param name="chiave"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Restituisce una chiave COUNTER in RedisCache
|
||||
/// </summary>
|
||||
/// <param name="chiave"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Resetta (elimina) un contatore in Redis
|
||||
/// </summary>
|
||||
/// <param name="chiave"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Restituisce un set KVP (Key Value Pair) salvati in RedisCache
|
||||
/// </summary>
|
||||
/// <param name="chiavi"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Salva un set KVP (Key Value Pair) in RedisCache
|
||||
/// </summary>
|
||||
/// <param name="valori">Set KVP chiave-valore da salvare</param>
|
||||
/// <returns></returns>
|
||||
public bool setRKeys(KeyValuePair<RedisKey, RedisValue>[] 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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Verifica se ci siano valori nella hash indicata...
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Verifica se ci siano valori nella hash indicata (string)
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public bool redHashPresentSz(string key)
|
||||
{
|
||||
bool answ = false;
|
||||
try
|
||||
{
|
||||
RedisKey chiave = key;
|
||||
answ = redHashPresent(chiave);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
return answ;
|
||||
}
|
||||
/// <summary>
|
||||
/// Recupera tutti i valori dalla hash
|
||||
/// </summary>
|
||||
/// <param name="hashKey"></param>
|
||||
/// <returns></returns>
|
||||
public KeyValuePair<string, string>[] redGetHash(string hashKey)
|
||||
{
|
||||
KeyValuePair<string, string>[] answ = new KeyValuePair<string, string>[1];
|
||||
// cerco se ci sia valore in redis...
|
||||
IDatabase cache = connRedis.GetDatabase();
|
||||
try
|
||||
{
|
||||
RedisKey chiave = hashKey;
|
||||
HashEntry[] valori = cache.HashGetAll(chiave);
|
||||
answ = new KeyValuePair<string, string>[valori.Length];
|
||||
int i = 0;
|
||||
foreach (HashEntry item in valori)
|
||||
{
|
||||
answ[i] = new KeyValuePair<string, string>(item.Name, item.Value);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
return answ;
|
||||
}
|
||||
/// <summary>
|
||||
/// Recupera tutti i valori dalla hash in formato Dictionary
|
||||
/// </summary>
|
||||
/// <param name="hashKey"></param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, string> redGetHashDict(string hashKey)
|
||||
{
|
||||
Dictionary<string, string> answ = new Dictionary<string, string>();
|
||||
// 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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Recupera UN SINGOLO VALORE dalla hash per un dato field
|
||||
/// </summary>
|
||||
/// <param name="hashKey"></param>
|
||||
/// <param name="hashField"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Salvataggio di una hash di valori
|
||||
/// </summary>
|
||||
/// <param name="hashKey">chiave</param>
|
||||
/// <param name="hashFields">valori</param>
|
||||
/// <returns></returns>
|
||||
public bool redSaveHash(string hashKey, KeyValuePair<string, string>[] 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<string, string> kvp in hashFields)
|
||||
{
|
||||
valori[i] = new HashEntry(kvp.Key, kvp.Value);
|
||||
i++;
|
||||
}
|
||||
cache.HashSet(chiave, valori);
|
||||
answ = true;
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
return answ;
|
||||
}
|
||||
/// <summary>
|
||||
/// Salvataggio di una hash di valori
|
||||
/// </summary>
|
||||
/// <param name="hashKey">chiave</param>
|
||||
/// <param name="hashListKVP">valori come lista KVP</param>
|
||||
/// <returns></returns>
|
||||
public static bool redSaveHashList(string hashKey, List<KeyValuePair<string, string>> 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<string, string> kvp in hashListKVP)
|
||||
{
|
||||
valori[i] = new HashEntry(kvp.Key, kvp.Value);
|
||||
i++;
|
||||
}
|
||||
cache.HashSet(chiave, valori);
|
||||
answ = true;
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
return answ;
|
||||
}
|
||||
/// <summary>
|
||||
/// Salvataggio di una hash di valori in formato Dictionary
|
||||
/// </summary>
|
||||
/// <param name="hashKey">chiave</param>
|
||||
/// <param name="hashFields">valori</param>
|
||||
/// <returns></returns>
|
||||
public bool redSaveHashDict(string hashKey, Dictionary<string, string> 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<string, string> kvp in hashFields)
|
||||
{
|
||||
valori[i] = new HashEntry(kvp.Key, kvp.Value);
|
||||
i++;
|
||||
}
|
||||
cache.HashSet(chiave, valori);
|
||||
answ = true;
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
return answ;
|
||||
}
|
||||
/// <summary>
|
||||
/// Salvataggio di una hash di valori
|
||||
/// </summary>
|
||||
/// <param name="hashKey">chiave</param>
|
||||
/// <param name="hashFields">valori</param>
|
||||
/// <param name="expireSeconds">scadenza preimpostata hash (secondi) | defaoult = -1 (non scade)</param>
|
||||
/// <returns></returns>
|
||||
public bool redSaveHash(string hashKey, KeyValuePair<string, string>[] 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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Salvataggio di una hash di valori in formato Dictionary
|
||||
/// </summary>
|
||||
/// <param name="hashKey">chiave</param>
|
||||
/// <param name="hashFields">valori</param>
|
||||
/// <param name="expireSeconds">scadenza preimpostata hash (secondi) | defaoult = -1 (non scade)</param>
|
||||
/// <returns></returns>
|
||||
public bool redSaveHashDict(string hashKey, Dictionary<string, string> 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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Elimina una key (hash, string)
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Flush completo cache redis
|
||||
/// </summary>
|
||||
/// <param name="keyPattern">** = tutti</param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Conta num oggetti cache redis che rispondono a pattern
|
||||
/// </summary>
|
||||
/// <param name="keyPattern">** = tutti</param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restituisce numero record in Redis DB
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Restituisce oggetti cache redis che rispondono a pattern
|
||||
/// </summary>
|
||||
/// <param name="keyPattern">** = tutti</param>
|
||||
/// <param name="orderBy">Tipo di ordinamento per kvp</param>
|
||||
/// <returns></returns>
|
||||
public List<KeyValuePair<string, int>> redGetCounterByKey(string keyPattern, kvpOrderBy orderBy)
|
||||
{
|
||||
int numAnsw = redCountKey(keyPattern);
|
||||
RedisKey[] chiavi = new RedisKey[numAnsw];
|
||||
List<KeyValuePair<string, int>> answ = new List<KeyValuePair<string, int>>();
|
||||
// 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<string, int>(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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Effettua comaprazione x CHIAVE in KVP ASC
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <returns></returns>
|
||||
private int CompareKey(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
|
||||
{
|
||||
return x.Key.CompareTo(y.Key);
|
||||
}
|
||||
/// <summary>
|
||||
/// Effettua comaprazione x VALORE in KVP ASC
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <returns></returns>
|
||||
public int CompareVal(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
|
||||
{
|
||||
return x.Value.CompareTo(y.Value);
|
||||
}
|
||||
/// <summary>
|
||||
/// Effettua comaprazione x CHIAVE in KVP DESC
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <returns></returns>
|
||||
private int CompareKeyDesc(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
|
||||
{
|
||||
return y.Key.CompareTo(x.Key);
|
||||
}
|
||||
/// <summary>
|
||||
/// Effettua comaprazione x VALORE in KVP DESC
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <returns></returns>
|
||||
public int CompareValDesc(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
|
||||
{
|
||||
return y.Value.CompareTo(x.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tipologia di ordinamento x liste KVP
|
||||
/// </summary>
|
||||
public enum kvpOrderBy
|
||||
{
|
||||
/// <summary>
|
||||
/// Ordinamento ASCending per KEY
|
||||
/// </summary>
|
||||
KeyAsc,
|
||||
/// <summary>
|
||||
/// Ordinamento DESCending per KEY
|
||||
/// </summary>
|
||||
KeyDesc,
|
||||
/// <summary>
|
||||
/// Ordinamento ASCending per VAL
|
||||
/// </summary>
|
||||
ValAsc,
|
||||
/// <summary>
|
||||
/// Ordinamento DESCending per VAL
|
||||
/// </summary>
|
||||
ValDesc
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -472,6 +472,7 @@ namespace SCMA.AdapterCom
|
||||
#endregion
|
||||
|
||||
}
|
||||
#if false
|
||||
/// <summary>
|
||||
/// Classe item node (tipo/obj)
|
||||
/// </summary>
|
||||
@@ -500,7 +501,8 @@ namespace SCMA.AdapterCom
|
||||
cType = _tipo;
|
||||
cObject = _obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Tipologia protocolli di comunicazione ammessi
|
||||
@@ -520,6 +522,7 @@ namespace SCMA.AdapterCom
|
||||
/// </summary>
|
||||
SOURS
|
||||
}
|
||||
#if false
|
||||
/// <summary>
|
||||
/// Tipologia di ITEM
|
||||
/// </summary>
|
||||
@@ -611,5 +614,6 @@ namespace SCMA.AdapterCom
|
||||
/// Stato ASSERV
|
||||
/// </summary>
|
||||
ASSERV
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -869,11 +869,21 @@ namespace SCMA.AdapterCom
|
||||
/// </summary>
|
||||
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
|
||||
}
|
||||
/// <summary>
|
||||
/// Wrapper metodo STOP
|
||||
/// </summary>
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user