forked from ddrilling/AsbCloudServer
e0d3187ef2
1. Два хаба избыточно, объеденил всё в один хаб 2. Уведомление клиенту будет отправляться только при обновлении кэша в сервисе WellInfoService 3. В WellInfoService теперь формируется статистика по всем скважинам, а не только по активным 4. Небольшой рефакторинг
53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using AsbCloudApp.IntegrationEvents;
|
|
using AsbCloudApp.IntegrationEvents.Interfaces;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudInfrastructure.Services;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
namespace AsbCloudWebApi.SignalR;
|
|
|
|
public class WellInfoHub : BaseHub, IIntegrationEventHandler<UpdateWellInfoEvent>
|
|
{
|
|
private readonly IHubContext<WellInfoHub> hubContext;
|
|
private readonly IWellService wellService;
|
|
private readonly WellInfoService wellInfoService;
|
|
|
|
public WellInfoHub(IHubContext<WellInfoHub> hubContext,
|
|
IWellService wellService,
|
|
WellInfoService wellInfoService)
|
|
{
|
|
this.hubContext = hubContext;
|
|
this.wellService = wellService;
|
|
this.wellInfoService = wellInfoService;
|
|
}
|
|
|
|
public override async Task AddToGroup(string groupName)
|
|
{
|
|
var idWell = int.Parse(groupName.Split('_')[2]);
|
|
|
|
await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
|
|
|
|
await HandleAsync(new UpdateWellInfoEvent(idWell), CancellationToken.None);
|
|
}
|
|
|
|
public async Task HandleAsync(UpdateWellInfoEvent integrationEvent, CancellationToken cancellationToken)
|
|
{
|
|
const string method = "update_well_info";
|
|
|
|
var well = await wellService.GetOrDefaultAsync(integrationEvent.IdWell, cancellationToken);
|
|
|
|
if(well is null)
|
|
return;
|
|
|
|
var wellInfo = wellInfoService.FirstOrDefault(w => w.Id == well.Id);
|
|
|
|
await hubContext.Clients.Group($"well_info_{integrationEvent.IdWell}")
|
|
.SendAsync(method, new
|
|
{
|
|
Well = well,
|
|
WellInfo = wellInfo
|
|
}, cancellationToken);
|
|
}
|
|
} |