added timer service to home controller

This commit is contained in:
chsakell
2016-09-30 11:32:27 +03:00
parent 63866a2746
commit 1f14c0c55f
10 changed files with 94 additions and 14 deletions

View File

@@ -1,21 +1,32 @@
using System;
using LiveGameFeed.Controllers;
using LiveGameFeed.Core.MvcTimer;
using LiveGameFeed.Hubs;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.SignalR.Infrastructure;
namespace ChatLe.Controllers
{
public class HomeController : Controller
public class HomeController : ApiHubController<Broadcaster>
{
static ILogger _logger;
public HomeController(ILoggerFactory factory)
public HomeController(IConnectionManager signalRConnectionManager,
ITimerService timerService)
: base(signalRConnectionManager)
{
if (_logger == null)
_logger = factory.CreateLogger("Unhandled Error");
timerService.TimerElapsed += _feed_Generate;
}
public IActionResult Index()
{
return View();
}
private async void _feed_Generate(object sender, EventArgs e)
{
TimerEventArgs eventsArgs = e as TimerEventArgs;
System.Diagnostics.Debug.WriteLine("hello from home ApiHubController.cs..");
await Clients.All.userConnected(DateTime.Now);
//_coolMessageHubContext.Clients.All.newCpuValue(eventsArgs.Value);
}
}
}

View File

@@ -1,6 +1,6 @@
using AutoMapper;
namespace LiveGameFeed.Mappings
namespace LiveGameFeed.Core.Mappings
{
public class AutoMapperConfiguration
{

View File

@@ -1,7 +1,7 @@
using AutoMapper;
using LiveGameFeed.Models;
namespace LiveGameFeed.Mappings
namespace LiveGameFeed.Core.Mappings
{
public class DomainToViewModelMappingProfile : Profile
{

View File

@@ -0,0 +1,9 @@
using System;
namespace LiveGameFeed.Core.MvcTimer
{
public interface ITimerService
{
event EventHandler TimerElapsed;
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace LiveGameFeed.Core.MvcTimer
{
public class TimerEventArgs : EventArgs
{
public TimerEventArgs(int value)
{
Value = value;
}
public int Value { get; }
}
}

View File

@@ -0,0 +1,28 @@
using System;
using System.Threading;
using Microsoft.Extensions.Options;
namespace LiveGameFeed.Core.MvcTimer
{
public class TimerService : ITimerService
{
private Timer _timer;
readonly Random _random = new Random();
public event EventHandler TimerElapsed;
public TimerService(IOptions<TimerServiceConfiguration> options)
{
var optionsTimerServiceConfiguration = options;
_timer = new Timer(
OnTimerElapsed,
null,
optionsTimerServiceConfiguration.Value.DueTime,
optionsTimerServiceConfiguration.Value.Period);
}
private void OnTimerElapsed(object sender)
{
TimerElapsed?.Invoke(this, new TimerEventArgs(_random.Next(0, 100)));
}
}
}

View File

@@ -0,0 +1,9 @@
namespace LiveGameFeed.Core.MvcTimer
{
public class TimerServiceConfiguration
{
public int DueTime { get; set; }
public int Period { get; set; }
}
}

View File

@@ -22,8 +22,8 @@ namespace LiveGameFeed.Data
{
Match match_01 = new Match
{
Host = "Panathinaikos",
Guest = "Olimpiakos",
Host = "Team 1",
Guest = "Team 2",
HostScore = 3,
GuestScore = 1,
MatchDate = DateTime.Now,
@@ -44,8 +44,8 @@ namespace LiveGameFeed.Data
};
Match match_02 = new Match
{
Host = "Real Madrit FC",
Guest = "Barchelona",
Host = "Team 3",
Guest = "Team 4",
HostScore = 5,
GuestScore = 3,
MatchDate = DateTime.Now,

View File

@@ -11,7 +11,8 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using LiveGameFeed.Mappings;
using LiveGameFeed.Core.Mappings;
using LiveGameFeed.Core.MvcTimer;
namespace LiveGameFeed
{
@@ -38,6 +39,10 @@ namespace LiveGameFeed
services.AddScoped<IMatchRepository, MatchRepository>();
services.AddScoped<IFeedRepository, FeedRepository>();
// Timer service configuration
services.AddSingleton<ITimerService, TimerService>();
services.Configure<TimerServiceConfiguration>(Configuration.GetSection("TimeService"));
// Automapper Configuration
AutoMapperConfiguration.Configure();

View File

@@ -6,5 +6,9 @@
"System": "Information",
"Microsoft": "Information"
}
},
"TimeService": {
"DueTime": 3000,
"Period": 1500
}
}