forked from ddrilling/AsbCloudServer
Степанов Дмитрий Александрович
c50878f5f1
На клиенте нет возможности передавать аргументы в функции. Сделал реализацию через группы
56 lines
1.7 KiB
C#
56 lines
1.7 KiB
C#
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using AsbCloudApp.IntegrationEvents;
|
|
using AsbCloudApp.IntegrationEvents.Interfaces;
|
|
using AsbCloudApp.Repositories;
|
|
using AsbCloudApp.Requests;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudDb.Model;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
namespace AsbCloudWebApi.SignalR;
|
|
|
|
public class WellHub : BaseHub, IIntegrationEventHandler<UpdateWellEvent>
|
|
{
|
|
private readonly IHubContext<WellHub> hubContext;
|
|
private readonly IWellService wellService;
|
|
private readonly IWellOperationRepository wellOperationRepository;
|
|
|
|
public WellHub(IHubContext<WellHub> hubContext,
|
|
IWellService wellService,
|
|
IWellOperationRepository wellOperationRepository)
|
|
{
|
|
this.hubContext = hubContext;
|
|
this.wellService = wellService;
|
|
this.wellOperationRepository = wellOperationRepository;
|
|
}
|
|
|
|
public override async Task AddToGroup(string groupName)
|
|
{
|
|
var idWell = int.Parse(groupName.Split('_')[1]);
|
|
|
|
await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
|
|
|
|
await HandleAsync(new UpdateWellEvent(idWell), CancellationToken.None);
|
|
}
|
|
|
|
public async Task HandleAsync(UpdateWellEvent integrationEvent, CancellationToken cancellationToken)
|
|
{
|
|
const string method = "update_well";
|
|
|
|
var well = await wellService.GetOrDefaultAsync(integrationEvent.IdWell, cancellationToken);
|
|
|
|
if(well is null)
|
|
return;
|
|
|
|
well.Section = (await wellOperationRepository.GetAsync(new WellOperationRequest
|
|
{
|
|
IdWell = integrationEvent.IdWell,
|
|
OperationType = WellOperation.IdOperationTypeFact
|
|
}, cancellationToken)).MaxBy(o => o.DateStart)?.WellSectionTypeName;
|
|
|
|
await hubContext.Clients.Group($"well_{integrationEvent.IdWell}")
|
|
.SendAsync(method, new object[] { well }, cancellationToken);
|
|
}
|
|
} |