CS2-3: Добавлен SignalR ReportHub и его применение в ReportController

This commit is contained in:
KharchenkoVV 2021-05-20 11:07:45 +05:00
parent 75ea100186
commit 4507b26581
6 changed files with 47 additions and 9 deletions

View File

@ -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);
}

View File

@ -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();
}
});

View File

@ -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<ReportsHub> reportsHubContext;
public ReportController(IReportService reportService, IWellService wellService)
public ReportController(IReportService reportService, IWellService wellService, IHubContext<ReportsHub> 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);
}
/// <summary>
/// Создает отчет по скважине с указанными параметрами
/// </summary>
/// <param name="wellId">id скважины</param>
/// <param name="begin">дата начала интервала</param>
/// <param name="end">дата окончания интервала</param>
/// <param name="stepSeconds">шаг интервала</param>
/// <param name="format">формат отчета (0-PDF, 1-LASS)</param>
/// <param name="signalRConnectionId">SignalR id подключения клиента</param>
/// <param name="begin">дата начала интервала</param>
/// <param name="end">дата окончания интервала</param>
/// <returns>id фоновой задачи формирования отчета</returns>
[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);
}

View File

@ -0,0 +1,7 @@
namespace AsbCloudWebApi.SignalR
{
public interface IReportHubClient
{
}
}

View File

@ -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<IReportHubClient>
{
}
}

View File

@ -67,6 +67,7 @@ namespace AsbCloudWebApi
{
endpoints.MapControllers();
endpoints.MapHub<TelemetryHub>("/hubs/telemetry");
endpoints.MapHub<ReportsHub>("/hubs/reports");
});
}
}