using AsbCloudApp.Data; using AsbCloudInfrastructure.Background; using AsbCloudWebApi.SignalR.Clients; using Microsoft.AspNetCore.Authorization; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudWebApi.SignalR { // SignalR manual: // https://docs.microsoft.com/ru-ru/aspnet/core/signalr/introduction?view=aspnetcore-5.0 //[SignalRHub] /// /// ReportsHub /// [Authorize] public class ReportsHub : BaseHub { private readonly BackgroundWorker backgroundWorker; public ReportsHub(BackgroundWorker backgroundWorker) { this.backgroundWorker = backgroundWorker; } /// /// Добавление в группу, отправка данных о формировании отчета /// /// /// public override async Task AddToGroup(string groupName) { await base.AddToGroup(groupName); var workId = groupName.Replace("Report_", ""); var work = backgroundWorker.Works.FirstOrDefault(work => work.Id == workId); var progress = new ReportProgressDto() { Operation = "Ожидает начала в очереди.", Progress = 0f, }; var state = work?.CurrentState; if (state is not null) { progress.Operation = state.State; progress.Progress = (float)state.Progress; } await Clients.Group(groupName).GetReportProgress(progress, CancellationToken.None); } } }