2018-09-26 12:32:50 +02:00
|
|
|
|
using System.Timers;
|
2018-09-26 11:29:39 +02:00
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2018-09-26 12:32:50 +02:00
|
|
|
|
using Result.Data;
|
2018-09-26 11:29:39 +02:00
|
|
|
|
using Result.Hubs;
|
|
|
|
|
|
|
|
|
|
namespace Result.Timers
|
|
|
|
|
{
|
|
|
|
|
public class PublishResultsTimer
|
|
|
|
|
{
|
|
|
|
|
private readonly IHubContext<ResultsHub> _hubContext;
|
2018-09-26 12:32:50 +02:00
|
|
|
|
private readonly IResultData _resultData;
|
2018-09-26 11:29:39 +02:00
|
|
|
|
private readonly Timer _timer;
|
|
|
|
|
|
2018-09-26 12:32:50 +02:00
|
|
|
|
public PublishResultsTimer(IHubContext<ResultsHub> hubContext, IResultData resultData, IConfiguration configuration)
|
2018-09-26 11:29:39 +02:00
|
|
|
|
{
|
|
|
|
|
_hubContext = hubContext;
|
2018-09-26 12:32:50 +02:00
|
|
|
|
_resultData = resultData;
|
2018-09-26 11:29:39 +02:00
|
|
|
|
var publishMilliseconds = configuration.GetValue<int>("ResultsTimer:PublishMilliseconds");
|
|
|
|
|
_timer = new Timer(publishMilliseconds)
|
|
|
|
|
{
|
|
|
|
|
Enabled = false
|
|
|
|
|
};
|
|
|
|
|
_timer.Elapsed += PublishResults;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
if (!_timer.Enabled)
|
|
|
|
|
{
|
|
|
|
|
_timer.Start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PublishResults(object sender, ElapsedEventArgs e)
|
|
|
|
|
{
|
2018-09-26 12:32:50 +02:00
|
|
|
|
var model = _resultData.GetResults();
|
2018-09-26 11:29:39 +02:00
|
|
|
|
_hubContext.Clients.All.SendAsync("UpdateResults", model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|