From 61a609003a3175df275224deab1311caf91ab7fb Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Thu, 11 Jul 2019 09:34:24 +0800 Subject: [PATCH] update Former-commit-id: bc10819b1bee4ce39c9d7a4c363ac8d30269823b --- ...oBase62Extensions.cs => GuidExtensions.cs} | 2 +- .../Application/Domain/Entities/Device.cs | 3 ++ .../Controllers/BaseDeviceController.cs | 41 ++++++++++--------- .../Infrastructure/BaseClientService.cs | 2 +- .../Controllers/CurtainController.cs | 6 +-- .../Controllers/GatewayController.cs | 4 +- .../Controllers/SocketController.cs | 4 +- .../Controllers/Switch2Controller.cs | 4 +- .../Controllers/Switch3Controller.cs | 4 +- .../Controllers/SwitchController.cs | 4 +- .../Infrastructure/ClientService.cs | 23 +++++++++-- .../Infrastructure/DeviceService.cs | 11 ++--- .../IoT/IoTServices/FBeeService/Program.cs | 3 ++ .../FBeeService/Views/Home/Gateway.cshtml | 2 + 14 files changed, 69 insertions(+), 44 deletions(-) rename projects/Infrastructure/Extensions/{GuidToBase62Extensions.cs => GuidExtensions.cs} (98%) diff --git a/projects/Infrastructure/Extensions/GuidToBase62Extensions.cs b/projects/Infrastructure/Extensions/GuidExtensions.cs similarity index 98% rename from projects/Infrastructure/Extensions/GuidToBase62Extensions.cs rename to projects/Infrastructure/Extensions/GuidExtensions.cs index 6134e0d4..444ca486 100644 --- a/projects/Infrastructure/Extensions/GuidToBase62Extensions.cs +++ b/projects/Infrastructure/Extensions/GuidExtensions.cs @@ -3,7 +3,7 @@ using System.Text; namespace Infrastructure.Extensions { - public static class GuidToBase62Extensions + public static class GuidExtensions { private const uint radix = 62; private const ulong carry = 297528130221121800; // 2^64 / 62 diff --git a/projects/IoT/IoT.Shared/Application/Domain/Entities/Device.cs b/projects/IoT/IoT.Shared/Application/Domain/Entities/Device.cs index 3c394940..c3efc731 100644 --- a/projects/IoT/IoT.Shared/Application/Domain/Entities/Device.cs +++ b/projects/IoT/IoT.Shared/Application/Domain/Entities/Device.cs @@ -51,6 +51,9 @@ namespace Application.Domain.Entities [Display(Name = "分类")] public DeviceInfo Info { get; set; } + [Display(Name = "节点编号")] + public string NodeNumber { get; set; } + [Display(Name = "节点Id")] public Guid? NodeId { get; set; } diff --git a/projects/IoT/IoT.Shared/Controllers/BaseDeviceController.cs b/projects/IoT/IoT.Shared/Controllers/BaseDeviceController.cs index 1b410f01..c9e03777 100644 --- a/projects/IoT/IoT.Shared/Controllers/BaseDeviceController.cs +++ b/projects/IoT/IoT.Shared/Controllers/BaseDeviceController.cs @@ -1,6 +1,8 @@ using Application.Models; using Infrastructure.Extensions; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; @@ -12,37 +14,38 @@ namespace IoT.Shared.Controllers { public class BaseDeviceController : Controller { - private readonly IHttpClientFactory _httpClientFactory; + protected readonly IServiceProvider applicationService; - public BaseDeviceController(IHttpClientFactory httpClientFactory) + public BaseDeviceController(IServiceProvider applicationServices) { - this._httpClientFactory = httpClientFactory; + this.applicationService = applicationServices; } [ApiExplorerSettings(IgnoreApi = true)] public string GetApiJson() { - var url = new UriBuilder - { - Host = Request.Host.Host, - Port = Request.Host.Port ?? 80, - Path = "/swagger/v1/swagger.json" - }.ToString(); var prefix = $"/{RouteData.Values["controller"]}/"; - var hc = this._httpClientFactory.CreateClient(); - var result = hc.GetStringAsync(url).Result; - var json = JsonConvert.DeserializeObject(result) as JObject; - var paths = json.Properties().FirstOrDefault(o => o.Name == "paths").Value as JObject; - var names = paths.Properties().Select(o => o.Name).ToList(); - foreach (var item in names) + using (var scope = this.applicationService.CreateScope()) { - if (!item.StartsWith(prefix)) + var serviceProvider = scope.ServiceProvider; + var cfg = serviceProvider.GetService(); + var port = cfg["server.urls"].Split(':')[2]; + var url = $"http://localhost:{port}/swagger/v1/swagger.json"; + var hc = serviceProvider.GetService().CreateClient(); + var result = hc.GetStringAsync(url).Result; + var json = JsonConvert.DeserializeObject(result) as JObject; + var paths = json.Properties().FirstOrDefault(o => o.Name == "paths").Value as JObject; + var names = paths.Properties().Select(o => o.Name).ToList(); + foreach (var item in names) { - paths.Remove(item); + if (!item.StartsWith(prefix)) + { + paths.Remove(item); + } } + var realResult = JsonConvert.SerializeObject(json); + return realResult; } - var realResult = JsonConvert.SerializeObject(json); - return realResult; } public ApiResponse AsyncAction(Action action) diff --git a/projects/IoT/IoT.Shared/Infrastructure/BaseClientService.cs b/projects/IoT/IoT.Shared/Infrastructure/BaseClientService.cs index c60fd124..d4eb808c 100644 --- a/projects/IoT/IoT.Shared/Infrastructure/BaseClientService.cs +++ b/projects/IoT/IoT.Shared/Infrastructure/BaseClientService.cs @@ -111,7 +111,7 @@ namespace IoT.Shared.Infrastructure private void InitConnection() { this._notifyHost = this._cfg["notify:host"]; - var url = $"http://{this._notifyHost}/hub?group={ConnectionId}"; + var url = $"http://{this._notifyHost}/hub?group={this._cfg["connectId"]}"; if (this.Connection != null) { this.Connection.DisposeAsync(); diff --git a/projects/IoT/IoTServices/FBeeService/Controllers/CurtainController.cs b/projects/IoT/IoTServices/FBeeService/Controllers/CurtainController.cs index b3d1a126..6f23dd7e 100644 --- a/projects/IoT/IoTServices/FBeeService/Controllers/CurtainController.cs +++ b/projects/IoT/IoTServices/FBeeService/Controllers/CurtainController.cs @@ -1,16 +1,16 @@ using Application.Models; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; -using System.Net.Http; +using System; namespace FBeeService.Controllers { [SwaggerTag("窗帘")] public class CurtainController : SwitchController { - private readonly DeviceService _deviceService; + protected readonly DeviceService _deviceService; - public CurtainController(IHttpClientFactory httpClientFactory, DeviceService deviceService) : base(httpClientFactory, deviceService) + public CurtainController(IServiceProvider applicationServices, DeviceService deviceService) : base(applicationServices, deviceService) { this._deviceService = deviceService; } diff --git a/projects/IoT/IoTServices/FBeeService/Controllers/GatewayController.cs b/projects/IoT/IoTServices/FBeeService/Controllers/GatewayController.cs index 7f11d7a1..ab0b7dcc 100644 --- a/projects/IoT/IoTServices/FBeeService/Controllers/GatewayController.cs +++ b/projects/IoT/IoTServices/FBeeService/Controllers/GatewayController.cs @@ -2,7 +2,7 @@ using IoT.Shared.Controllers; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; -using System.Net.Http; +using System; namespace FBeeService.Controllers { @@ -11,7 +11,7 @@ namespace FBeeService.Controllers { private readonly DeviceService _deviceService; - public GatewayController(IHttpClientFactory httpClientFactory, DeviceService deviceService) : base(httpClientFactory) + public GatewayController(IServiceProvider applicationServices, DeviceService deviceService) : base(applicationServices) { this._deviceService = deviceService; } diff --git a/projects/IoT/IoTServices/FBeeService/Controllers/SocketController.cs b/projects/IoT/IoTServices/FBeeService/Controllers/SocketController.cs index f4b30017..9dd63567 100644 --- a/projects/IoT/IoTServices/FBeeService/Controllers/SocketController.cs +++ b/projects/IoT/IoTServices/FBeeService/Controllers/SocketController.cs @@ -1,12 +1,12 @@ using Swashbuckle.AspNetCore.Annotations; -using System.Net.Http; +using System; namespace FBeeService.Controllers { [SwaggerTag("1路插座")] public class SocketController : SwitchController { - public SocketController(IHttpClientFactory httpClientFactory, DeviceService deviceService) : base(httpClientFactory, deviceService) + public SocketController(IServiceProvider applicationService, DeviceService deviceService) : base(applicationService, deviceService) { } } diff --git a/projects/IoT/IoTServices/FBeeService/Controllers/Switch2Controller.cs b/projects/IoT/IoTServices/FBeeService/Controllers/Switch2Controller.cs index abe0aaec..7c9770ce 100644 --- a/projects/IoT/IoTServices/FBeeService/Controllers/Switch2Controller.cs +++ b/projects/IoT/IoTServices/FBeeService/Controllers/Switch2Controller.cs @@ -2,7 +2,7 @@ using IoT.Shared.Controllers; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; -using System.Net.Http; +using System; namespace FBeeService.Controllers { @@ -11,7 +11,7 @@ namespace FBeeService.Controllers { private readonly DeviceService _deviceService; - public Switch2Controller(IHttpClientFactory httpClientFactory, DeviceService deviceService) : base(httpClientFactory) + public Switch2Controller(IServiceProvider applicationServices, DeviceService deviceService) : base(applicationServices) { this._deviceService = deviceService; } diff --git a/projects/IoT/IoTServices/FBeeService/Controllers/Switch3Controller.cs b/projects/IoT/IoTServices/FBeeService/Controllers/Switch3Controller.cs index cb92c0b3..07bd8dfb 100644 --- a/projects/IoT/IoTServices/FBeeService/Controllers/Switch3Controller.cs +++ b/projects/IoT/IoTServices/FBeeService/Controllers/Switch3Controller.cs @@ -2,7 +2,7 @@ using IoT.Shared.Controllers; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; -using System.Net.Http; +using System; namespace FBeeService.Controllers { @@ -11,7 +11,7 @@ namespace FBeeService.Controllers { private readonly DeviceService _deviceService; - public Switch3Controller(IHttpClientFactory httpClientFactory, DeviceService deviceService) : base(httpClientFactory) + public Switch3Controller(IServiceProvider applicationServices, DeviceService deviceService) : base(applicationServices) { this._deviceService = deviceService; } diff --git a/projects/IoT/IoTServices/FBeeService/Controllers/SwitchController.cs b/projects/IoT/IoTServices/FBeeService/Controllers/SwitchController.cs index 9c6b7504..071ec326 100644 --- a/projects/IoT/IoTServices/FBeeService/Controllers/SwitchController.cs +++ b/projects/IoT/IoTServices/FBeeService/Controllers/SwitchController.cs @@ -2,7 +2,7 @@ using IoT.Shared.Controllers; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; -using System.Net.Http; +using System; namespace FBeeService.Controllers { @@ -11,7 +11,7 @@ namespace FBeeService.Controllers { private readonly DeviceService _deviceService; - public SwitchController(IHttpClientFactory httpClientFactory, DeviceService deviceService) : base(httpClientFactory) + public SwitchController(IServiceProvider applicationServices, DeviceService deviceService) : base(applicationServices) { this._deviceService = deviceService; } diff --git a/projects/IoT/IoTServices/FBeeService/Infrastructure/ClientService.cs b/projects/IoT/IoTServices/FBeeService/Infrastructure/ClientService.cs index 6918c933..6e0751ee 100644 --- a/projects/IoT/IoTServices/FBeeService/Infrastructure/ClientService.cs +++ b/projects/IoT/IoTServices/FBeeService/Infrastructure/ClientService.cs @@ -3,10 +3,12 @@ using Infrastructure.Data; using Infrastructure.Extensions; using IoT.Shared.Infrastructure; using Microsoft.AspNetCore.SignalR.Client; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; using System.Linq; +using System.Net.Http; using System.Threading.Tasks; namespace FBeeService @@ -22,10 +24,10 @@ namespace FBeeService using (var scope = this.applicationServices.CreateScope()) { var deviceRepo = scope.ServiceProvider.GetService>(); - foreach (var device in deviceRepo.Table().ToList()) + var devices = deviceRepo.Table().Include(o => o.Data).ToList(); + foreach (var device in devices) { - device.ConnectId = this.ConnectionId; - deviceRepo.SaveChanges(); + this.Notify(device); } } } @@ -44,5 +46,20 @@ namespace FBeeService } }); } + + public override void ConnectionOn() + { + this.Connection.On("Request", (string path, string queryString) => + { + using (var scope = this.applicationServices.CreateScope()) + { + var serviceProvider = scope.ServiceProvider; + var cfg = serviceProvider.GetService(); + var port = cfg["server.urls"].Split(':')[2]; + var url = $"http://localhost:{port}{path}{queryString}"; + var httpClient = serviceProvider.GetService().CreateClient(); + } + }); + } } } \ No newline at end of file diff --git a/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs b/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs index cd667ef8..f0b4efa5 100644 --- a/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs +++ b/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs @@ -440,7 +440,6 @@ namespace FBeeService var deviceRepo = scope.ServiceProvider.GetService>(); var device = this.GetDevice(deviceRepo, sn, ieee); var clientService = scope.ServiceProvider.GetService(); - var connectId = clientService.ConnectionId; if (device == null) { create = true; @@ -448,19 +447,17 @@ namespace FBeeService { Number = ieee, Name = deviceType.Name, - IsOnline = isOnline != 0x00, Icon = deviceType.Icon, CategoryNumber = deviceType.Category, GatewayNumber = sn, - ConnectId = connectId, InfoId = deviceInfo.Id }; deviceRepo.Add(device); } - else - { - device.IsOnline = isOnline != 0x00; - } + device.IsOnline = isOnline != 0x00; + device.ConnectId = this._configuration["connectId"]; + device.NodeNumber = this._configuration["node.Number"]; + if (deviceId == 0x402) { if (zoneType == 0x0028) diff --git a/projects/IoT/IoTServices/FBeeService/Program.cs b/projects/IoT/IoTServices/FBeeService/Program.cs index 2c4869a1..6178b166 100644 --- a/projects/IoT/IoTServices/FBeeService/Program.cs +++ b/projects/IoT/IoTServices/FBeeService/Program.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Infrastructure.Application; using Infrastructure.Configuration; @@ -28,6 +29,8 @@ namespace FBeeService new EFConfigurationValue { Id = "notify:enabled", Value= "true"}, new EFConfigurationValue { Id = "notify:host", Value= $"{host}:8001"}, new EFConfigurationValue { Id = "timer.seconds", Value="600"}, + new EFConfigurationValue { Id = "connectId", Value= Guid.NewGuid().ToBase62() }, + new EFConfigurationValue { Id = "node.number", Value= "所属节点编号" }, // new EFConfigurationValue { Id = "name", Value= "FBeeService服务"}, new EFConfigurationValue { Id = "logo", Value= "/images/logo.png",Type= InputType.ImageUrl}, diff --git a/projects/IoT/IoTServices/FBeeService/Views/Home/Gateway.cshtml b/projects/IoT/IoTServices/FBeeService/Views/Home/Gateway.cshtml index bb0b083d..695b0bb8 100644 --- a/projects/IoT/IoTServices/FBeeService/Views/Home/Gateway.cshtml +++ b/projects/IoT/IoTServices/FBeeService/Views/Home/Gateway.cshtml @@ -14,6 +14,7 @@ 电量 在线状态 设备状态 + 连接Id 操作 删除 @@ -92,6 +93,7 @@ } + @item.ConnectId @if (new int[] { 0x0002, 0x0009, 0x0081, 0x0202, 0x0220, 0x0051 }.Contains(deviceId)) {