ReportsHub, метод AddToGroup, который оповещает группу о статусе загрузки рапорта

This commit is contained in:
Olga Nemt 2023-10-25 16:03:07 +05:00
parent 399393fd55
commit 1b8d9bef61

View File

@ -1,4 +1,10 @@
using Microsoft.AspNetCore.Authorization;
using AsbCloudApp.Data;
using AsbCloudInfrastructure.Background;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.SignalR
{
@ -8,6 +14,43 @@ namespace AsbCloudWebApi.SignalR
[Authorize]
public class ReportsHub : BaseHub<IReportHubClient>
{
private readonly BackgroundWorker backgroundWorker;
public ReportsHub(BackgroundWorker backgroundWorker, IHubContext<ReportsHub> hubContext)
{
this.backgroundWorker = backgroundWorker;
}
public override async Task AddToGroup(string groupName)
{
var workId = groupName.Replace("Report_", "");
var work = backgroundWorker.WorkStore.RunOnceQueue.FirstOrDefault(work => work.Id == workId);
if (work is null) return;
await base.AddToGroup(groupName);
var state = work.CurrentState;
var progress = new ReportProgressDto()
{
Operation = "Ожидает начала в очереди.",
Progress = 0f,
};
if (state is not null)
{
progress = new ReportProgressDto
{
CurrentPage = 0,
Operation = state.State,
Progress = (float)state.Progress,
TotalPages = 0,
};
}
await Clients.Group(groupName).SendAsync(
nameof(IReportHubClient.GetReportProgress),
progress,
CancellationToken.None
);
}
}
}