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 : BaseHub, IIntegrationEventHandler { private readonly IHubContext hubContext; private readonly WellInfoService wellInfoService; public WellInfoHub(IHubContext hubContext, WellInfoService wellInfoService) { this.hubContext = hubContext; 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 wellInfo = wellInfoService.FirstOrDefault(w => w.Id == integrationEvent.IdWell); if (wellInfo is null) return; await hubContext.Clients.Group($"well_info_{integrationEvent.IdWell}") .SendAsync(method, wellInfo, cancellationToken); } }