using System.Threading; using System.Threading.Tasks; using AsbCloudApp.IntegrationEvents; using AsbCloudApp.IntegrationEvents.Interfaces; using AsbCloudInfrastructure.Services; using Microsoft.AspNetCore.SignalR; namespace AsbCloudWebApi.SignalR; public class WellInfoHub : Hub, IIntegrationEventHandler { private const string groupTemplate = "well_info_id_well_{0}"; private readonly IHubContext hubContext; private readonly WellInfoService wellInfoService; public WellInfoHub(IHubContext hubContext, WellInfoService wellInfoService) { this.hubContext = hubContext; this.wellInfoService = wellInfoService; } public async Task OnConnectedAsync(int idWell) { await base.OnConnectedAsync(); await Groups.AddToGroupAsync(Context.ConnectionId, string.Format(groupTemplate, idWell)); await HandleAsync(new UpdateWellInfoEvent(idWell), CancellationToken.None); } public async Task HandleAsync(UpdateWellInfoEvent integrationEvent, CancellationToken cancellationToken) { const string method = "update_well_info"; var wellInfo = wellInfoService.FirstOrDefault(w => w.Id == integrationEvent.IdWell); if (wellInfo is null) return; await hubContext.Clients.Group(string.Format(groupTemplate, integrationEvent.IdWell)) .SendAsync(method, wellInfo, cancellationToken); } }