diff --git a/AsbCloudApp/Services/IReportService.cs b/AsbCloudApp/Services/IReportService.cs index ee759b8d..0110e5ff 100644 --- a/AsbCloudApp/Services/IReportService.cs +++ b/AsbCloudApp/Services/IReportService.cs @@ -6,7 +6,8 @@ namespace AsbCloudApp.Services public interface IReportService { string RootPath { get; } - int CreateReport(int wellId, int stepSeconds, int format, DateTime begin, DateTime end); + int CreateReport(int wellId, int stepSeconds, int format, DateTime begin, DateTime end, + EventHandler<(float progress, string operation)> handleReportProgress); int GetReportPagesCount(int wellId, DateTime begin, DateTime end, int stepSeconds, int format); DatesRangeDto GetReportsDatesRange(int wellId); } diff --git a/AsbCloudInfrastructure/Services/ReportService.cs b/AsbCloudInfrastructure/Services/ReportService.cs index f1e95e1e..05e91dc3 100644 --- a/AsbCloudInfrastructure/Services/ReportService.cs +++ b/AsbCloudInfrastructure/Services/ReportService.cs @@ -27,7 +27,8 @@ namespace AsbCloudInfrastructure.Services public string RootPath { get ; private set; } - public int CreateReport(int wellId, int stepSeconds, int format, DateTime begin, DateTime end) + public int CreateReport(int wellId, int stepSeconds, int format, DateTime begin, + DateTime end, EventHandler<(float progress, string operation)> progressHandler) { var newReportId = queue.EnqueueTask(() => { @@ -37,6 +38,7 @@ namespace AsbCloudInfrastructure.Services using (var context = new AsbCloudDbContext(optionsBuilder.Options)) { var generator = GetReportGenerator(wellId, begin, end, stepSeconds, format, context); + generator.OnProgress += progressHandler; generator.Make(); } }); diff --git a/AsbCloudWebApi/Controllers/ReportController.cs b/AsbCloudWebApi/Controllers/ReportController.cs index dcffe590..600162d2 100644 --- a/AsbCloudWebApi/Controllers/ReportController.cs +++ b/AsbCloudWebApi/Controllers/ReportController.cs @@ -3,8 +3,8 @@ using System.IO; using Microsoft.AspNetCore.Mvc; using AsbCloudApp.Data; using AsbCloudApp.Services; -using AsbCloudDb.Model; -using Microsoft.AspNetCore.Hosting; +using AsbCloudWebApi.SignalR; +using Microsoft.AspNetCore.SignalR; namespace AsbCloudWebApi.Controllers { @@ -17,27 +17,40 @@ namespace AsbCloudWebApi.Controllers { private readonly IReportService reportService; private readonly IWellService wellService; + private readonly IHubContext reportsHubContext; - public ReportController(IReportService reportService, IWellService wellService) + public ReportController(IReportService reportService, IWellService wellService, IHubContext reportsHubContext) { this.reportService = reportService; this.wellService = wellService; + this.reportsHubContext = reportsHubContext; + } + + private string signalRConnectionId = "0"; + + private void HandleReportProgress(object sender, (float progress, string operation) e) + { + reportsHubContext.Clients.Client(signalRConnectionId).SendAsync("GetReportProgress", e.progress); } /// /// Создает отчет по скважине с указанными параметрами /// /// id скважины - /// дата начала интервала - /// дата окончания интервала /// шаг интервала /// формат отчета (0-PDF, 1-LASS) + /// SignalR id подключения клиента + /// дата начала интервала + /// дата окончания интервала /// id фоновой задачи формирования отчета [HttpPost] [Route("{wellId}/report")] [ProducesResponseType(typeof(string), (int)System.Net.HttpStatusCode.OK)] - public IActionResult CreateReport(int wellId, int stepSeconds, int format, DateTime begin = default, DateTime end = default) + public IActionResult CreateReport(int wellId, int stepSeconds, int format, string signalRConnectionId, + DateTime begin = default, DateTime end = default) { + this.signalRConnectionId = signalRConnectionId; + int? idCustomer = User.GetCustomerId(); if (idCustomer is null) @@ -46,7 +59,7 @@ namespace AsbCloudWebApi.Controllers if (!wellService.CheckWellOwnership((int)idCustomer, wellId)) return Forbid(); - var id = reportService.CreateReport(wellId, stepSeconds, format, begin, end); + var id = reportService.CreateReport(wellId, stepSeconds, format, begin, end, HandleReportProgress); return Ok(id); } diff --git a/AsbCloudWebApi/SignalR/IReportHubClient.cs b/AsbCloudWebApi/SignalR/IReportHubClient.cs new file mode 100644 index 00000000..a45af1fe --- /dev/null +++ b/AsbCloudWebApi/SignalR/IReportHubClient.cs @@ -0,0 +1,7 @@ +namespace AsbCloudWebApi.SignalR +{ + public interface IReportHubClient + { + + } +} diff --git a/AsbCloudWebApi/SignalR/ReportsHub.cs b/AsbCloudWebApi/SignalR/ReportsHub.cs new file mode 100644 index 00000000..e7521d8f --- /dev/null +++ b/AsbCloudWebApi/SignalR/ReportsHub.cs @@ -0,0 +1,14 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.SignalR; + +namespace AsbCloudWebApi.SignalR +{ + // SignalR manual: + // https://docs.microsoft.com/ru-ru/aspnet/core/signalr/introduction?view=aspnetcore-5.0 + + [Authorize] + public class ReportsHub : Hub + { + + } +} diff --git a/AsbCloudWebApi/Startup.cs b/AsbCloudWebApi/Startup.cs index 8747f3c2..6abe83b7 100644 --- a/AsbCloudWebApi/Startup.cs +++ b/AsbCloudWebApi/Startup.cs @@ -67,6 +67,7 @@ namespace AsbCloudWebApi { endpoints.MapControllers(); endpoints.MapHub("/hubs/telemetry"); + endpoints.MapHub("/hubs/reports"); }); } }