добавил SignalR

This commit is contained in:
Фролов 2021-04-08 17:54:02 +05:00
parent ef9bb8f39f
commit 5e80746333
7 changed files with 102 additions and 22 deletions

View File

@ -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<TelemetryHub> telemetryHubContext;
public TelemetryController(ITelemetryService telemetryService)
public TelemetryController(ITelemetryService telemetryService, IHubContext<TelemetryHub> telemetryHubContext)
{
this.telemetryService = telemetryService;
this.telemetryHubContext = telemetryHubContext;
}
/// <summary>
/// Принимает общую информацию по скважине
/// </summary>
/// <param name="uid">Уникальный идентификатор отправителя</param>
/// <param name="info">нформация об отправителе</param>
/// <returns></returns>
[HttpPost]
[Route("{uid}/info")]
@ -38,12 +44,15 @@ namespace AsbCloudWebApi.Controllers
/// <summary>
/// Принимает данные от разных систем по скважине
/// </summary>
/// <param name="uid">Уникальный идентификатор отправителя</param>
/// <param name="data">Данные</param>
/// <returns></returns>
[HttpPost]
[Route("{uid}/data")]
public IActionResult Data(string uid, [FromBody] TelemetryDataDto data)
{
telemetryService.UpdateData(uid, data);
//telemetryHubContext.Clients.Group("").
return Ok();
}

View File

@ -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;
}
};
});
}
}

View File

@ -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<TelemetryHub>("/hubs/telemetry");
});
}
}

View File

@ -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);
}
}

View File

@ -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<ITelemetryHubClient>
{
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);
}
}

View File

@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="5.0.5" />
</ItemGroup>
<ItemGroup>

View File

@ -31,28 +31,30 @@ namespace ConsoleApp1
// .Options;
//var context = new AsbCloudDbContext(options);
var table = new Table
{
Headers = new List<Header>
{
new Header {Name = "P1", PropertyType = typeof(int) },
new Header {Name = "P2", PropertyType = typeof(int) },
new Header {Name = "P3", PropertyType = typeof(int) },
},
//var table = new Table
//{
// Headers = new List<Header>
// {
// new Header {Name = "P1", PropertyType = typeof(int) },
// new Header {Name = "P2", PropertyType = typeof(int) },
// new Header {Name = "P3", PropertyType = typeof(int) },
// },
Rows = new List<object[]>
{
new object[] {1,2,3 },
new object[] {4,5,6 },
new object[] {7,8,9 },
}
};
var mapper = new TableMapper<B>();
// Rows = new List<object[]>
// {
// new object[] {1,2,3 },
// new object[] {4,5,6 },
// new object[] {7,8,9 },
// }
//};
//var mapper = new TableMapper<B>();
var b = new B();
mapper.UpdateObjectFromTable(ref b, table, 1);
//var b = new B();
//mapper.UpdateObjectFromTable(ref b, table, 1);
Console.WriteLine("Done");
Console.WriteLine("Done. Press any key to quit.");
Console.ReadKey();
}