using System; using System.Collections.Generic; using ApacKernel; using ApacKernel.Date; using ApacKernel.Services.SignalR; using Microsoft.AspNet.SignalR; using Microsoft.AspNet.SignalR.Hubs; using Now.Business.WorkFlows; using Now.Entities; using Now.Web.Hubs; namespace Now.Web.Tickers { public class NowCombinedTicker : TickerBase { protected readonly static Lazy _instance = new Lazy(() => new NowCombinedTicker(GlobalHost.ConnectionManager.GetHubContext().Clients)); public static NowCombinedTicker Instance { get { return _instance.Value; } } private int _counter; private Dictionary _speedoSectors; private DateTime _lastCheckTime; protected override TimeSpan UpdateInterval { get { return ApacConfig.AppSettings.NowTicks(); } } public NowCombinedTicker(IHubConnectionContext clients) : base(clients) { UpdateSectors(); } protected override void GetAndSendData(object state = null) { _counter++; if (_counter > 60) { RunMinutesStatistics(); _counter = 0; } RunSecondsStatistics(); } private void RunSecondsStatistics() { Clients.All.MomentRevenue(DashboardsWorkflow.Instance.GetNowMomentRevenue(PeriodTypes.Today)); } private void RunMinutesStatistics() { var now = DateTime.Now; var period = now - _lastCheckTime; if (period.Hours > 0) { UpdateSectors(); } if (_speedoSectors != null && _speedoSectors.ContainsKey(now.Hour)) Clients.All.updateSpeedoSectors(_speedoSectors[now.Hour]); Clients.All.SitesSummaries(DashboardsWorkflow.Instance.GetCachedNowSitesSummaries(PeriodTypes.Today)); Clients.All.SalesSummaries(DashboardsWorkflow.Instance.GetNowSalesSummaries(PeriodTypes.Today)); Clients.All.HistorySitesSummaries(DashboardsWorkflow.Instance.GetNowHistorySitesSummaries()); } private void UpdateSectors() { var sectors = DashboardsWorkflow.Instance.GetSectors(); if (sectors != null) { _speedoSectors = sectors; _lastCheckTime = DateTime.Now; } } } }