diff --git a/AsbCloudWebApi/SignalR/ReportsHub.cs b/AsbCloudWebApi/SignalR/ReportsHub.cs index bb0da640..ac19dc97 100644 --- a/AsbCloudWebApi/SignalR/ReportsHub.cs +++ b/AsbCloudWebApi/SignalR/ReportsHub.cs @@ -1,8 +1,9 @@ -using AsbCloudApp.Data; +using AsbCloudApp.Requests; +using AsbCloudApp.Services; using AsbCloudInfrastructure.Background; using AsbCloudWebApi.SignalR.Clients; using Microsoft.AspNetCore.Authorization; -using System.Linq; +using Microsoft.AspNetCore.Mvc; using System.Threading; using System.Threading.Tasks; @@ -17,39 +18,44 @@ namespace AsbCloudWebApi.SignalR [Authorize] public class ReportsHub : BaseHub { - private readonly BackgroundWorker backgroundWorker; + private readonly IWellService wellService; + private readonly IReportService reportService; - public ReportsHub(BackgroundWorker backgroundWorker) + public ReportsHub(IWellService wellService, IReportService reportService) { - this.backgroundWorker = backgroundWorker; + this.wellService = wellService; + this.reportService = reportService; } /// /// Добавление в группу, отправка данных о формировании отчета /// - /// + /// группа + /// ключ скважины + /// параметры запроса /// - public override async Task AddToGroup(string groupName) + public async Task CreateReport(string groupName, int idWell, ReportParametersRequest request) { + var idUser = Context.User?.GetUserId(); + var idCompany = Context.User?.GetCompanyId(); + + if ((idCompany is null) || (idUser is null)) + return; + + if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, CancellationToken.None).ConfigureAwait(false)) + return; + await base.AddToGroup(groupName); - var workId = groupName.Replace("Report_", ""); - var work = backgroundWorker.Works.FirstOrDefault(work => work.Id == workId); + void HandleReportProgressAsync(object progress, string id) => + Task.Run(async () => + { + await Clients.Group(groupName) + .GetReportProgress(progress, CancellationToken.None); + }, CancellationToken.None); - 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); + var id = reportService.EnqueueCreateReportWork(idWell, (int)idUser, + request.StepSeconds, request.Format, request.Begin, request.End, HandleReportProgressAsync); } } }