From 1f14c0c55fcc8e4b34c04b88ef1c9fb16b5c3b8d Mon Sep 17 00:00:00 2001 From: chsakell Date: Fri, 30 Sep 2016 11:32:27 +0300 Subject: [PATCH] added timer service to home controller --- Controllers/HomeController.cs | 25 ++++++++++++----- .../Mappings}/AutoMapperConfiguration.cs | 2 +- .../DomainToViewModelMappingProfile.cs | 2 +- Core/Timer/ITimerService.cs | 9 ++++++ Core/Timer/TimerEventArgs.cs | 14 ++++++++++ Core/Timer/TimerService.cs | 28 +++++++++++++++++++ Core/Timer/TimerServiceConfiguration.cs | 9 ++++++ Data/LiveGameDbInitializer.cs | 8 +++--- Startup.cs | 7 ++++- appsettings.json | 4 +++ 10 files changed, 94 insertions(+), 14 deletions(-) rename {Mappings => Core/Mappings}/AutoMapperConfiguration.cs (83%) rename {Mappings => Core/Mappings}/DomainToViewModelMappingProfile.cs (85%) create mode 100644 Core/Timer/ITimerService.cs create mode 100644 Core/Timer/TimerEventArgs.cs create mode 100644 Core/Timer/TimerService.cs create mode 100644 Core/Timer/TimerServiceConfiguration.cs diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index 20f7210..423c579 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -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 { - 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); + } } } \ No newline at end of file diff --git a/Mappings/AutoMapperConfiguration.cs b/Core/Mappings/AutoMapperConfiguration.cs similarity index 83% rename from Mappings/AutoMapperConfiguration.cs rename to Core/Mappings/AutoMapperConfiguration.cs index 6738f44..43e0d7a 100644 --- a/Mappings/AutoMapperConfiguration.cs +++ b/Core/Mappings/AutoMapperConfiguration.cs @@ -1,6 +1,6 @@ using AutoMapper; -namespace LiveGameFeed.Mappings +namespace LiveGameFeed.Core.Mappings { public class AutoMapperConfiguration { diff --git a/Mappings/DomainToViewModelMappingProfile.cs b/Core/Mappings/DomainToViewModelMappingProfile.cs similarity index 85% rename from Mappings/DomainToViewModelMappingProfile.cs rename to Core/Mappings/DomainToViewModelMappingProfile.cs index 5c64e14..6b87c6a 100644 --- a/Mappings/DomainToViewModelMappingProfile.cs +++ b/Core/Mappings/DomainToViewModelMappingProfile.cs @@ -1,7 +1,7 @@ using AutoMapper; using LiveGameFeed.Models; -namespace LiveGameFeed.Mappings +namespace LiveGameFeed.Core.Mappings { public class DomainToViewModelMappingProfile : Profile { diff --git a/Core/Timer/ITimerService.cs b/Core/Timer/ITimerService.cs new file mode 100644 index 0000000..cdf92d9 --- /dev/null +++ b/Core/Timer/ITimerService.cs @@ -0,0 +1,9 @@ +using System; + +namespace LiveGameFeed.Core.MvcTimer +{ + public interface ITimerService + { + event EventHandler TimerElapsed; + } +} \ No newline at end of file diff --git a/Core/Timer/TimerEventArgs.cs b/Core/Timer/TimerEventArgs.cs new file mode 100644 index 0000000..da581c2 --- /dev/null +++ b/Core/Timer/TimerEventArgs.cs @@ -0,0 +1,14 @@ +using System; + +namespace LiveGameFeed.Core.MvcTimer +{ + public class TimerEventArgs : EventArgs + { + public TimerEventArgs(int value) + { + Value = value; + } + + public int Value { get; } + } +} \ No newline at end of file diff --git a/Core/Timer/TimerService.cs b/Core/Timer/TimerService.cs new file mode 100644 index 0000000..82c6108 --- /dev/null +++ b/Core/Timer/TimerService.cs @@ -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 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))); + } + } +} diff --git a/Core/Timer/TimerServiceConfiguration.cs b/Core/Timer/TimerServiceConfiguration.cs new file mode 100644 index 0000000..418adb8 --- /dev/null +++ b/Core/Timer/TimerServiceConfiguration.cs @@ -0,0 +1,9 @@ +namespace LiveGameFeed.Core.MvcTimer +{ + public class TimerServiceConfiguration + { + public int DueTime { get; set; } + + public int Period { get; set; } + } +} diff --git a/Data/LiveGameDbInitializer.cs b/Data/LiveGameDbInitializer.cs index c05895b..b7f45aa 100644 --- a/Data/LiveGameDbInitializer.cs +++ b/Data/LiveGameDbInitializer.cs @@ -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, diff --git a/Startup.cs b/Startup.cs index 1769767..a242e4f 100644 --- a/Startup.cs +++ b/Startup.cs @@ -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(); services.AddScoped(); + // Timer service configuration + services.AddSingleton(); + services.Configure(Configuration.GetSection("TimeService")); + // Automapper Configuration AutoMapperConfiguration.Configure(); diff --git a/appsettings.json b/appsettings.json index 723c096..21a4481 100644 --- a/appsettings.json +++ b/appsettings.json @@ -6,5 +6,9 @@ "System": "Information", "Microsoft": "Information" } + }, + "TimeService": { + "DueTime": 3000, + "Period": 1500 } }