using DigitalData.Modules.Logging; using DigitalData.Modules.Messaging.WCF; using ECM.JobRunner.Common; namespace ECM.JobRunner.Web.Data { public class DashboardService { private readonly Common.JobRunnerReference.IEDMIServiceChannel channel; private Logger logger; private readonly System.Timers.Timer pollingTimer = new(); public event EventHandler? DataUpdated; public DashboardService(LoggingService Logging, WcfService Wcf) { logger = Logging.LogConfig.GetLogger(); channel = Wcf.Channel; pollingTimer.Interval = 1000; pollingTimer.Elapsed += PollingTimer_Elapsed; pollingTimer.Enabled = true; } protected virtual void OnDataUpdated(DashboardResponse e) { DataUpdated?.Invoke(this, e); } private async void PollingTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e) { OnDataUpdated(await GetData()); } public async Task GetData() { List jobStatus = await GetStatusItems(); return new DashboardResponse() { jobStatus = jobStatus.OrderByDescending(e => e.CompleteTime).ToList() }; } private async Task> GetStatusItems() { try { var oResponse = await channel.GetJobStatusAsync(); if (oResponse.OK) { return oResponse.StatusItems.ToList(); } else { return new(); } } catch (Exception e) { logger.Error(e); return new(); } } } }