From 5e80746333c7709047f5add5ff9518e037f753a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Thu, 8 Apr 2021 17:54:02 +0500 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20Si?= =?UTF-8?q?gnalR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/TelemetryController.cs | 11 ++++- AsbCloudWebApi/DependencyInjection.cs | 17 ++++++++ AsbCloudWebApi/Startup.cs | 18 +++++++- .../WebSocket/ITelemetryHubClient.cs | 13 ++++++ AsbCloudWebApi/WebSocket/TelemetryHub.cs | 22 ++++++++++ ConsoleApp1/ConsoleApp1.csproj | 1 + ConsoleApp1/Program.cs | 42 ++++++++++--------- 7 files changed, 102 insertions(+), 22 deletions(-) create mode 100644 AsbCloudWebApi/WebSocket/ITelemetryHubClient.cs create mode 100644 AsbCloudWebApi/WebSocket/TelemetryHub.cs diff --git a/AsbCloudWebApi/Controllers/TelemetryController.cs b/AsbCloudWebApi/Controllers/TelemetryController.cs index 66b98e75..f65bf2b0 100644 --- a/AsbCloudWebApi/Controllers/TelemetryController.cs +++ b/AsbCloudWebApi/Controllers/TelemetryController.cs @@ -1,7 +1,9 @@ using AsbCloudApp.Data; using AsbCloudApp.Services; +using AsbCloudWebApi.WebSocket; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; using System; using System.Collections.Generic; using System.Linq; @@ -17,15 +19,19 @@ namespace AsbCloudWebApi.Controllers public class TelemetryController : ControllerBase { private readonly ITelemetryService telemetryService; + private readonly IHubContext telemetryHubContext; - public TelemetryController(ITelemetryService telemetryService) + public TelemetryController(ITelemetryService telemetryService, IHubContext telemetryHubContext) { this.telemetryService = telemetryService; + this.telemetryHubContext = telemetryHubContext; } /// /// Принимает общую информацию по скважине /// + /// Уникальный идентификатор отправителя + /// нформация об отправителе /// [HttpPost] [Route("{uid}/info")] @@ -38,12 +44,15 @@ namespace AsbCloudWebApi.Controllers /// /// Принимает данные от разных систем по скважине /// + /// Уникальный идентификатор отправителя + /// Данные /// [HttpPost] [Route("{uid}/data")] public IActionResult Data(string uid, [FromBody] TelemetryDataDto data) { telemetryService.UpdateData(uid, data); + //telemetryHubContext.Clients.Group(""). return Ok(); } diff --git a/AsbCloudWebApi/DependencyInjection.cs b/AsbCloudWebApi/DependencyInjection.cs index 65efd95f..6199143b 100644 --- a/AsbCloudWebApi/DependencyInjection.cs +++ b/AsbCloudWebApi/DependencyInjection.cs @@ -9,6 +9,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Reflection; +using System.Threading.Tasks; namespace AsbCloudWebApi { @@ -76,6 +77,22 @@ namespace AsbCloudWebApi IssuerSigningKey = AuthService.securityKey, ValidateIssuerSigningKey = true, }; + + options.Events = new JwtBearerEvents + { + OnMessageReceived = context => + { + var accessToken = context.Request.Query["access_token"]; + + var path = context.HttpContext.Request.Path; + if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/hubs")) + { + context.Token = accessToken; + } + + return Task.CompletedTask; + } + }; }); } } diff --git a/AsbCloudWebApi/Startup.cs b/AsbCloudWebApi/Startup.cs index 81b404de..9b8bc99e 100644 --- a/AsbCloudWebApi/Startup.cs +++ b/AsbCloudWebApi/Startup.cs @@ -1,4 +1,5 @@ using AsbCloudInfrastructure; +using AsbCloudWebApi.WebSocket; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -25,6 +26,19 @@ namespace AsbCloudWebApi services.AddInfrastructure(Configuration); services.AddJWTAuthentication(Configuration); + + services.AddSignalR(); + + services.AddCors(options => + { + options.AddPolicy("ClientPermission", policy => + { + policy.AllowAnyHeader() + .AllowAnyMethod() + .WithOrigins("http://localhost:3000") + .AllowCredentials(); + }); + }); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) @@ -32,7 +46,7 @@ namespace AsbCloudWebApi app.UseSwagger(); app.UseSwaggerUI(c => { - c.SwaggerEndpoint("/swagger/v1/swagger.json", "Charging station V1"); + c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1"); }); if (env.IsDevelopment()) @@ -42,6 +56,7 @@ namespace AsbCloudWebApi //app.UseHttpsRedirection(); app.UseStaticFiles(); + app.UseCors("ClientPermission"); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); @@ -49,6 +64,7 @@ namespace AsbCloudWebApi app.UseEndpoints(endpoints => { endpoints.MapControllers(); + endpoints.MapHub("/hubs/telemetry"); }); } } diff --git a/AsbCloudWebApi/WebSocket/ITelemetryHubClient.cs b/AsbCloudWebApi/WebSocket/ITelemetryHubClient.cs new file mode 100644 index 00000000..086f4fa7 --- /dev/null +++ b/AsbCloudWebApi/WebSocket/ITelemetryHubClient.cs @@ -0,0 +1,13 @@ +using AsbCloudApp.Data; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace AsbCloudWebApi.WebSocket +{ + public interface ITelemetryHubClient + { + Task ReceiveDataSaub(DataSaubBaseDto data); + } +} diff --git a/AsbCloudWebApi/WebSocket/TelemetryHub.cs b/AsbCloudWebApi/WebSocket/TelemetryHub.cs new file mode 100644 index 00000000..d09eed4a --- /dev/null +++ b/AsbCloudWebApi/WebSocket/TelemetryHub.cs @@ -0,0 +1,22 @@ +using AsbCloudApp.Data; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.SignalR; +using System.Threading.Tasks; + +namespace AsbCloudWebApi.WebSocket +{ + + [Authorize] + public class TelemetryHub : Hub + { + public Task AddToGroup(string groupName) + => Groups.AddToGroupAsync(Context.ConnectionId, groupName.ToString()); + + public Task RemoveFromGroup(string groupName) + => Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName); + + public Task SendDataSaub(int wellId, DataSaubBaseDto data) + => Clients.All.ReceiveDataSaub(data); + + } +} \ No newline at end of file diff --git a/ConsoleApp1/ConsoleApp1.csproj b/ConsoleApp1/ConsoleApp1.csproj index fbf72ce7..0f15dd87 100644 --- a/ConsoleApp1/ConsoleApp1.csproj +++ b/ConsoleApp1/ConsoleApp1.csproj @@ -8,6 +8,7 @@ + diff --git a/ConsoleApp1/Program.cs b/ConsoleApp1/Program.cs index 1fdbd139..e155756e 100644 --- a/ConsoleApp1/Program.cs +++ b/ConsoleApp1/Program.cs @@ -30,29 +30,31 @@ namespace ConsoleApp1 // .UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True") // .Options; //var context = new AsbCloudDbContext(options); - - var table = new Table - { - Headers = new List
- { - new Header {Name = "P1", PropertyType = typeof(int) }, - new Header {Name = "P2", PropertyType = typeof(int) }, - new Header {Name = "P3", PropertyType = typeof(int) }, - }, - Rows = new List - { - new object[] {1,2,3 }, - new object[] {4,5,6 }, - new object[] {7,8,9 }, - } - }; - var mapper = new TableMapper(); + //var table = new Table + //{ + // Headers = new List
+ // { + // new Header {Name = "P1", PropertyType = typeof(int) }, + // new Header {Name = "P2", PropertyType = typeof(int) }, + // new Header {Name = "P3", PropertyType = typeof(int) }, + // }, - var b = new B(); - mapper.UpdateObjectFromTable(ref b, table, 1); + // Rows = new List + // { + // new object[] {1,2,3 }, + // new object[] {4,5,6 }, + // new object[] {7,8,9 }, + // } + //}; + //var mapper = new TableMapper(); - Console.WriteLine("Done"); + //var b = new B(); + //mapper.UpdateObjectFromTable(ref b, table, 1); + + + Console.WriteLine("Done. Press any key to quit."); + Console.ReadKey(); }