Merge branch 'release/AddRedisMEssagePipe'

This commit is contained in:
Samuele Locatelli
2022-12-09 19:39:38 +01:00
9 changed files with 325 additions and 4 deletions
+39 -1
View File
@@ -89,7 +89,7 @@ namespace MP_IO.Controllers
DataLayer DataLayerObj = new DataLayer();
try
{
// recupero dati macchina...
// chiamata diretta sul DB...
DataLayerObj.taODL.forceClose(idxOdl, id);
answ = true;
}
@@ -98,6 +98,44 @@ namespace MP_IO.Controllers
return answ;
}
/// <summary>
/// Richiesta chiusura manuale ODL x macchina (popup utente):
///
/// GET: IOB/askCloseODL/SIMUL_03?idxOdl=123
/// </summary>
/// <param name="id">id macchina</param>
/// <param name="idxOdl">idx dell'ODL da chiudere</param>
/// <returns>bool esecuzione</returns>
public bool askCloseODL(string id, int idxOdl)
{
bool answ = false;
// init obj DataLayer
DataLayer DataLayerObj = new DataLayer();
try
{
// preparo una richiesta di chiusura...
DisplayAction CurrAction = new DisplayAction()
{
Topic = "Chiusura ODL",
Message = "Rilevato possibile fine operazioni, Vuoi chiudere la commessa?",
ShowCancel = true,
ShowClose = true,
ShowConfirm = true,
CancelAction = "DisableAction",
ConfirmAction = "CloseODL",
DtReq = DateTime.Now,
IsActive = true,
Parameter = $"{idxOdl}"
};
answ = DataLayerObj.ActionSetReq(CurrAction);
}
catch
{ }
return answ;
}
// GET: IOB/enabled/SIMUL_03
public string enabled(string id)
{
+2
View File
@@ -17,6 +17,8 @@
<add key="RedisConn" value="localhost,abortConnect=false,ssl=false" />
<add key="RedisConnAdmin" value="localhost,abortConnect=false,ssl=false" />
<add key="redisDb" value="1" />
<add key="RedisConnSPEC" value="localhost:6379,DefaultDatabase=1,connectTimeout=5000,syncTimeout=5000,asyncTimeout=5000,abortConnect=false,ssl=false" />
<add key="RedisConnSPECAdmin" value="localhost:6379,DefaultDatabase=1,connectTimeout=5000,syncTimeout=5000,asyncTimeout=5000,abortConnect=false,ssl=false,allowAdmin=true" />
<!--altri parametri-->
<add key="CodModulo" value="MoonPro" />
<add key="cacheOnRedis" value="true" />
+26
View File
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MapoDb
{
public class Constants
{
#region Public Fields
public static readonly string BROADCAST_M_PIPE = $"BroadCastMsg";
public static readonly string BROADCAST_CURR_KEY = $"{BASE_HASH}:Current:BroadCast";
// dati conf REDIS Cache
public static readonly string BASE_HASH = "MAPO";
private const string redisBaseAddr = "MP:";
public static readonly string redisActionReq = redisBaseAddr + "IO:Action:Req";
#endregion Public Fields
}
}
+76
View File
@@ -1,10 +1,13 @@
using MapoSDK;
using Newtonsoft.Json;
using NLog.Fluent;
using StackExchange.Redis;
using SteamWare;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
@@ -145,6 +148,9 @@ namespace MapoDb
#endregion Public Fields
public MessagePipe BroadastMsgPipe { get; set; } = null;
#region Public Constructors
/// <summary>
@@ -160,6 +166,76 @@ namespace MapoDb
// init oggetto MapoDb
MapoDbObj = new MapoDb();
// init message channel...
BroadastMsgPipe = new MessagePipe(connRedis, Constants.BROADCAST_M_PIPE);
}
/// <summary>
/// Connessione lazy a redis...
/// </summary>
private Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
string RedisConn = memLayer.ML.confReadString("RedisConnSPEC");
if (string.IsNullOrEmpty(RedisConn))
{
RedisConn = memLayer.ML.confReadString("RedisConn");
}
if (string.IsNullOrEmpty(RedisConn))
{
RedisConn = "localhost,abortConnect=false,ssl=false";
}
return ConnectionMultiplexer.Connect(RedisConn);
});
/// <summary>
/// Connessione lazy a redis...
/// </summary>
private Lazy<ConnectionMultiplexer> lazyConnectionAdmin = new Lazy<ConnectionMultiplexer>(() =>
{
string RedisConnAdmin = memLayer.ML.confReadString("RedisConnSPECAdmin");
if (string.IsNullOrEmpty(RedisConnAdmin))
{
RedisConnAdmin = memLayer.ML.confReadString("RedisConnAdmin");
}
if (string.IsNullOrEmpty(RedisConnAdmin))
{
RedisConnAdmin = "localhost,abortConnect=false,ssl=false,allowAdmin=true";
}
return ConnectionMultiplexer.Connect(RedisConnAdmin);
});
/// <summary>
/// Oggetto statico connessione redis
/// </summary>
public ConnectionMultiplexer connRedis
{
get
{
return lazyConnection.Value;
}
}
/// <summary>
/// Salva richiesta azione
/// </summary>
/// <param name="act2save"></param>
/// <returns></returns>
public bool ActionSetReq(DisplayAction act2save)
{
bool fatto = false;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// cerco in redis...
string rawData = JsonConvert.SerializeObject(act2save);
// invio broadcast + salvo in redis
BroadastMsgPipe.saveAndSendMessage(Constants.redisActionReq, rawData);
fatto = true;
//await redisDb.StringSetAsync(redisActionReq, rawData);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
logger.lg.scriviLog($"ActionSetReq REDIS send to broadcast + Write cache: {ts.TotalMilliseconds}ms");
return fatto;
}
#endregion Public Constructors
+26
View File
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MapoDb
{
public class DisplayAction
{
#region Public Properties
public string CancelAction { get; set; } = "";
public string ConfirmAction { get; set; } = "";
public DateTime DtReq { get; set; } = DateTime.Now;
public bool IsActive { get; set; } = false;
public string Message { get; set; } = "New Message";
public bool ShowCancel { get; set; } = true;
public bool ShowClose { get; set; } = true;
public bool ShowConfirm { get; set; } = true;
public string Topic { get; set; } = "New Topic";
public string Parameter { get; set; } = "";
#endregion Public Properties
}
}
+3
View File
@@ -211,7 +211,9 @@
<Link>MoonPro.cs</Link>
</Compile>
<Compile Include="AlarmsArchive.cs" />
<Compile Include="Constants.cs" />
<Compile Include="DataLayer.cs" />
<Compile Include="DisplayAction.cs" />
<Compile Include="DS_applicazione.cs">
<DependentUpon>DS_applicazione.xsd</DependentUpon>
</Compile>
@@ -272,6 +274,7 @@
<Compile Include="FluxArchive.cs" />
<Compile Include="IntServTrData.cs" />
<Compile Include="MapoDb.cs" />
<Compile Include="MessagePipe.cs" />
<Compile Include="MtcDataModelArchive.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Settings.Designer.cs">
+150
View File
@@ -0,0 +1,150 @@
using NLog;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MapoDb
{
public class MessagePipe
{
#region Public Constructors
public MessagePipe(IConnectionMultiplexer redisConn, string channelName, bool enableLog = false)
{
_channel = channelName;
redis = redisConn;
redisDb = redis.GetDatabase();
this.enableLog = enableLog;
// aggiungo sottoscrittore
setupSubscriber();
}
#endregion Public Constructors
#region Public Events
public event EventHandler EA_NewMessage = delegate { };
#endregion Public Events
#region Public Methods
/// <summary>
/// Invio messaggio sul canale + salvataggio in cache REDIS
/// </summary>
/// <param name="memKey">Chiave REDIS x salvare valore</param>
/// <param name="message">Messaggio serializzato da inviare</param>
public bool saveAndSendMessage(string memKey, string message)
{
bool answ = false;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// invio notifica tramite il canale richiesto
answ = sendMessage(message);
if (redisDb != null)
{
redisDb.StringSetAsync(memKey, message);
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
if (numSent.ContainsKey(memKey))
{
numSent[memKey]++;
}
else
{
numSent.Add(memKey, 1);
}
if (enableLog || numSent[memKey] > 30)
{
Log.Info($"saveAndSendMessage| mKey {memKey} x {numSent[memKey]} | {message.Length} size | {ts.TotalMilliseconds} ms");
numSent[memKey] = 0;
}
return answ;
}
/// <summary>
/// Invio messaggio sul canale
/// </summary>
/// <param name="newMess"></param>
/// <returns></returns>
public bool sendMessage(string newMess)
{
bool answ = false;
ISubscriber sub = redis.GetSubscriber();
sub.Publish(_channel, newMess);
return answ;
}
#endregion Public Methods
#region Protected Fields
protected static Logger Log = LogManager.GetCurrentClassLogger();
#endregion Protected Fields
#region Private Fields
private bool enableLog = false;
private Dictionary<string, int> numSent = new Dictionary<string, int>();
private IConnectionMultiplexer redis;
private IDatabase redisDb;
#endregion Private Fields
#region Private Properties
/// <summary>
/// Canale associato al gestore pipeline messaggi
/// </summary>
private string _channel { get; set; } = "";
#endregion Private Properties
#region Private Methods
private void setupSubscriber()
{
ISubscriber sub = redis.GetSubscriber();
//Subscribe to the channel named messages
sub.Subscribe(_channel, (channel, message) =>
{
Log.Trace($"req setup ch {channel} | {message}");
// messaggio
PubSubEventArgs mea = new PubSubEventArgs(message);
// se qualcuno ascolta sollevo evento nuovo valore...
if (EA_NewMessage != null)
{
EA_NewMessage(this, mea);
}
});
Log.Info($"Subscribed {_channel}");
}
#endregion Private Methods
}
public class PubSubEventArgs : EventArgs
{
#region Public Constructors
public PubSubEventArgs(string messaggio)
{
this.newMessage = messaggio;
}
#endregion Public Constructors
#region Public Properties
public string newMessage { get; set; } = "";
#endregion Public Properties
}
}
+2 -2
View File
@@ -10,10 +10,10 @@ namespace MapoSDK
/// </summary>
public class WharehouseData
{
public class BatchRec
{
public string Ident { get; set; } = "NA";
public int IdxODL { get; set; } = 0;
public string IdentRG { get; set; } = "NA";
public string Product { get; set; } = "Prod";
public string Variety { get; set; } = "Var";
public string Supplier { get; set; } = "Suppl";
File diff suppressed because one or more lines are too long