From 47bd9cb56b60c9d110181803aeca1a444599fc26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Wed, 16 Aug 2023 17:30:03 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D0=BE=D0=B1=D1=80=D0=B0=D0=B6?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85?= =?UTF-8?q?=20=D0=B2=20=D1=80=D0=B5=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D0=BC=20?= =?UTF-8?q?=D0=B2=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Добавил инфраструктуру для доменных событий. 2. Сделал Hub отправки для информации о скважине. --- .../Events/IIntegrationEvent.cs | 6 +++ .../Events/WellInfoUpdaterEvent.cs | 7 +++ .../IIntegrationEventHandler.cs | 20 ++++++++ .../Services/WellInfoService.cs | 10 +++- AsbCloudWebApi/DependencyInjection.cs | 8 ++- AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs | 49 +++++++++++++++++++ AsbCloudWebApi/Startup.cs | 3 ++ SignalRTestClient/Program.cs | 6 +-- 8 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 AsbCloudApp/IntegrationEvents/Events/IIntegrationEvent.cs create mode 100644 AsbCloudApp/IntegrationEvents/Events/WellInfoUpdaterEvent.cs create mode 100644 AsbCloudApp/IntegrationEvents/IIntegrationEventHandler.cs create mode 100644 AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs diff --git a/AsbCloudApp/IntegrationEvents/Events/IIntegrationEvent.cs b/AsbCloudApp/IntegrationEvents/Events/IIntegrationEvent.cs new file mode 100644 index 00000000..c44d83a0 --- /dev/null +++ b/AsbCloudApp/IntegrationEvents/Events/IIntegrationEvent.cs @@ -0,0 +1,6 @@ +namespace AsbCloudApp.IntegrationEvents.Events; + +/// +/// Интерфейс маркер для доменных событий +/// +public interface IIntegrationEvent { } \ No newline at end of file diff --git a/AsbCloudApp/IntegrationEvents/Events/WellInfoUpdaterEvent.cs b/AsbCloudApp/IntegrationEvents/Events/WellInfoUpdaterEvent.cs new file mode 100644 index 00000000..6e95e72e --- /dev/null +++ b/AsbCloudApp/IntegrationEvents/Events/WellInfoUpdaterEvent.cs @@ -0,0 +1,7 @@ +namespace AsbCloudApp.IntegrationEvents.Events; + +/// +/// Обновление информации о скважине +/// +/// +public record WellInfoUpdaterEvent(int IdWell) : IIntegrationEvent; \ No newline at end of file diff --git a/AsbCloudApp/IntegrationEvents/IIntegrationEventHandler.cs b/AsbCloudApp/IntegrationEvents/IIntegrationEventHandler.cs new file mode 100644 index 00000000..1fec260a --- /dev/null +++ b/AsbCloudApp/IntegrationEvents/IIntegrationEventHandler.cs @@ -0,0 +1,20 @@ +using System.Threading; +using System.Threading.Tasks; +using AsbCloudApp.IntegrationEvents.Events; + +namespace AsbCloudApp.IntegrationEvents; + +/// +/// Обработчик событий +/// +/// +public interface IIntegrationEventHandler where T: IIntegrationEvent +{ + /// + /// Метод обработки события + /// + /// + /// + /// + Task HandleAsync(T integrationEvent, CancellationToken cancellationToken); +} \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/WellInfoService.cs b/AsbCloudInfrastructure/Services/WellInfoService.cs index 7066ef22..c05b51a2 100644 --- a/AsbCloudInfrastructure/Services/WellInfoService.cs +++ b/AsbCloudInfrastructure/Services/WellInfoService.cs @@ -16,6 +16,8 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using AsbCloudApp.IntegrationEvents; +using AsbCloudApp.IntegrationEvents.Events; namespace AsbCloudInfrastructure.Services { @@ -36,7 +38,7 @@ namespace AsbCloudInfrastructure.Services private readonly IWitsRecordRepository witsRecord1Repository; private readonly IGtrRepository gtrRepository; private static IEnumerable WellMapInfo = Enumerable.Empty(); - + public WellInfoService( TelemetryDataCache telemetryDataSaubCache, TelemetryDataCache telemetryDataSpinCache, @@ -69,6 +71,7 @@ namespace AsbCloudInfrastructure.Services var processMapRepository = serviceProvider.GetRequiredService(); var subsystemOperationTimeService = serviceProvider.GetRequiredService(); var telemetryDataSaubCache = serviceProvider.GetRequiredService>(); + var messageHub = serviceProvider.GetRequiredService>(); var activeWells = await wellService.GetAsync(new() {IdState = 1}, token); @@ -167,6 +170,11 @@ namespace AsbCloudInfrastructure.Services return wellMapInfo; }).ToArray(); + + foreach (var idWell in activeWellsIds) + { + await messageHub.HandleAsync(new WellInfoUpdaterEvent(idWell), token); + } } private WellMapInfoWithTelemetryStat Convert(WellMapInfoWithComanies wellInfo) diff --git a/AsbCloudWebApi/DependencyInjection.cs b/AsbCloudWebApi/DependencyInjection.cs index 7477f745..a0a6ab24 100644 --- a/AsbCloudWebApi/DependencyInjection.cs +++ b/AsbCloudWebApi/DependencyInjection.cs @@ -3,7 +3,6 @@ using AsbCloudApp.Repositories; using AsbCloudDb.Model; using AsbCloudInfrastructure.Services; using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; @@ -12,8 +11,12 @@ using System.Collections.Generic; using System.IO; using System.Reflection; using System.Threading.Tasks; +using AsbCloudApp.IntegrationEvents; +using AsbCloudApp.IntegrationEvents.Events; using AsbCloudApp.Services.Notifications; +using AsbCloudWebApi.SignalR; using AsbCloudWebApi.SignalR.Services; +using Microsoft.AspNetCore.Mvc; using Microsoft.OpenApi.Any; namespace AsbCloudWebApi @@ -140,5 +143,8 @@ namespace AsbCloudWebApi { services.AddTransient(); } + + public static void AddIntegrationEvents(this IServiceCollection services) => services + .AddTransient, WellInfoUpdaterHub>(); } } diff --git a/AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs b/AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs new file mode 100644 index 00000000..9490df4d --- /dev/null +++ b/AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs @@ -0,0 +1,49 @@ +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using AsbCloudApp.IntegrationEvents; +using AsbCloudApp.IntegrationEvents.Events; +using AsbCloudInfrastructure.Services; +using Microsoft.AspNetCore.SignalR; + +namespace AsbCloudWebApi.SignalR; + +public class WellInfoUpdaterHub : Hub, IIntegrationEventHandler +{ + private const string groupTemplate = "system_operation_updater_well_{0}"; + + private readonly IHubContext hubContext; + private readonly WellInfoService wellInfoService; + + public WellInfoUpdaterHub(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 WellInfoUpdaterEvent(idWell), CancellationToken.None); + } + + public async Task HandleAsync(WellInfoUpdaterEvent integrationEvent, CancellationToken cancellationToken) + { + const string method = "well_info_update"; + + var wellInfo = wellInfoService.FirstOrDefault(w => w.Id == integrationEvent.IdWell); + + if (wellInfo != null) + { + var serializedObject = JsonSerializer.Serialize(wellInfo); + + await hubContext.Clients.Group(string.Format(groupTemplate, integrationEvent.IdWell)) + .SendCoreAsync(method, new object[] { serializedObject }, cancellationToken); + } + } + +} \ No newline at end of file diff --git a/AsbCloudWebApi/Startup.cs b/AsbCloudWebApi/Startup.cs index 0fb3ba87..aadaf829 100644 --- a/AsbCloudWebApi/Startup.cs +++ b/AsbCloudWebApi/Startup.cs @@ -45,6 +45,8 @@ namespace AsbCloudWebApi services.AddNotificationTransportServices(); + services.AddIntegrationEvents(); + services.AddJWTAuthentication(); services.AddSignalR() @@ -151,6 +153,7 @@ namespace AsbCloudWebApi app.UseEndpoints(endpoints => { endpoints.MapControllers(); + endpoints.MapHub("/hubs/limitingParameters"); endpoints.MapHub("/hubs/notifications"); endpoints.MapHub("/hubs/telemetry"); endpoints.MapHub("/hubs/reports"); diff --git a/SignalRTestClient/Program.cs b/SignalRTestClient/Program.cs index 487f1253..9ae72e04 100644 --- a/SignalRTestClient/Program.cs +++ b/SignalRTestClient/Program.cs @@ -10,7 +10,7 @@ internal class Program { var connectionBuilder = new HubConnectionBuilder(); var connection = connectionBuilder - .WithUrl("http://localhost:5000/hubs/notifications", connectionOptions => { + .WithUrl("http://localhost:5000/hubs/limitingParameters", connectionOptions => { connectionOptions.AccessTokenProvider = AccessTokenProvider; }) .WithAutomaticReconnect() @@ -26,9 +26,9 @@ internal class Program connection.StartAsync().Wait(); //Console.WriteLine("OnConnected"); - //connection.SendCoreAsync("OnConnected", new object[] { }, CancellationToken.None).Wait(); + connection.SendCoreAsync("OnConnectedAsync", new object[] { 1 }, CancellationToken.None).Wait(); - var subsction = connection.On("receiveNotifications", (str1) => { + var subsction = connection.On("well_info_update", (str1) => { Console.WriteLine(str1); } );