160 lines
4.7 KiB
C#
160 lines
4.7 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MP.Data.Services
|
|
{
|
|
public class TabDataFeeder : StatusData
|
|
{
|
|
#region Public Constructors
|
|
|
|
public TabDataFeeder(IConfiguration configuration, IConnectionMultiplexer redConn) : base(configuration, redConn)
|
|
{
|
|
// setup canali pub/sub
|
|
dataPipe = new MessagePipe(redisConn, Constants.TAB_ACT_MSE_DATA_KEY, false);
|
|
blinkPipe = new MessagePipe(redisConn, Constants.TAB_ACT_BLINK_KEY, false);
|
|
// avvio timers...
|
|
startTimers();
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
public MessagePipe blinkPipe { get; set; } = null!;
|
|
public MessagePipe dataPipe { get; set; } = null!;
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Impostazione forzata timer refresh
|
|
/// </summary>
|
|
/// <param name="FastTimerMs"></param>
|
|
public void setTimers(int FastTimerMs)
|
|
{
|
|
stopTimers();
|
|
fastRefreshMs = FastTimerMs;
|
|
// avvio timers...
|
|
startTimers();
|
|
}
|
|
|
|
public void startTimers()
|
|
{
|
|
fastTimer = new System.Timers.Timer(fastRefreshMs);
|
|
fastTimer.Elapsed += ElapsedFastTimer;
|
|
fastTimer.Enabled = true;
|
|
fastTimer.Start();
|
|
}
|
|
|
|
public void stopTimers()
|
|
{
|
|
// stop timers
|
|
if (fastTimer != null)
|
|
{
|
|
fastTimer.Elapsed -= ElapsedFastTimer;
|
|
fastTimer.Enabled = false;
|
|
fastTimer.Stop();
|
|
fastTimer.Dispose();
|
|
}
|
|
}
|
|
public void pauseTimers()
|
|
{
|
|
// stop timers
|
|
if (fastTimer != null)
|
|
{
|
|
fastTimer.Enabled = false;
|
|
fastTimer.Stop();
|
|
}
|
|
}
|
|
public void resumeTimers()
|
|
{
|
|
// riavvia timers
|
|
if (fastTimer != null)
|
|
{
|
|
fastTimer.Enabled = true;
|
|
fastTimer.Start();
|
|
}
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Methods
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (!_disposed)
|
|
{
|
|
if (disposing)
|
|
{
|
|
// Free managed resources here
|
|
stopTimers();
|
|
dataPipe = null;
|
|
blinkPipe = null;
|
|
}
|
|
|
|
// Free unmanaged resources here
|
|
_disposed = true;
|
|
}
|
|
|
|
// Call base class implementation.
|
|
base.Dispose(disposing);
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static System.Timers.Timer fastTimer = new System.Timers.Timer(4000);
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private bool _disposed = false;
|
|
private int fastRefreshMs = 1000;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Methods
|
|
|
|
private void ElapsedFastTimer(object? source, System.Timers.ElapsedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
var pUpd = Task.Run(async () =>
|
|
{
|
|
// secondi pari --> blink, secondi dispari --> ricarica
|
|
DateTime adesso = DateTime.Now;
|
|
int resto = 0;
|
|
Math.DivRem(adesso.Second, 2, out resto);
|
|
if (resto == 0)
|
|
{
|
|
// invio in channel blink il segnale
|
|
blinkPipe.sendMessage("true");
|
|
Log.Trace("Elapsed Fast Timer Blink");
|
|
}
|
|
else
|
|
{
|
|
// invio in channel blink segnale false
|
|
blinkPipe.sendMessage("false");
|
|
// rileggo dati...
|
|
var newData = await MseGetAll();
|
|
// invio tramite la pipe...
|
|
dataPipe.sendMessage(JsonConvert.SerializeObject(newData));
|
|
Log.Trace("Elapsed Fast Timer reload");
|
|
}
|
|
});
|
|
pUpd.Wait();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Errore in ElapsedFastTimer{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |