diff --git a/projects/Infrastructure/Infrastructure.csproj b/projects/Infrastructure/Infrastructure.csproj index 59aed2c2..ba1dc54c 100644 --- a/projects/Infrastructure/Infrastructure.csproj +++ b/projects/Infrastructure/Infrastructure.csproj @@ -30,7 +30,7 @@ - + diff --git a/projects/IoT/IoT.Shared/Controllers/BaseDeviceController.cs b/projects/IoT/IoT.Shared/Controllers/BaseDeviceController.cs index 59502aa2..6775f479 100644 --- a/projects/IoT/IoT.Shared/Controllers/BaseDeviceController.cs +++ b/projects/IoT/IoT.Shared/Controllers/BaseDeviceController.cs @@ -1,5 +1,6 @@ using Application.Models; using Infrastructure.Extensions; +using IoT.Shared.Infrastructure; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -23,30 +24,14 @@ namespace IoT.Shared.Controllers [ApiExplorerSettings(IgnoreApi = true)] [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")] - public string GetApiJson() + public string Api() { try { var prefix = $"/{RouteData.Values["controller"]}/"; - using var scope = this.applicationService.CreateScope(); - 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) - { - if (!item.StartsWith(prefix)) - { - paths.Remove(item); - } - } - var realResult = JsonConvert.SerializeObject(json); - return realResult; + using var scope = applicationService.CreateScope(); + var deviceService = scope.ServiceProvider.GetService(); + return deviceService.GetApiJson(prefix); } catch (Exception ex) { diff --git a/projects/IoT/IoT.Shared/DeviceServices/FBee/FBeeService.cs b/projects/IoT/IoT.Shared/DeviceServices/FBee/FBeeService.cs index b05e543b..16d6866f 100644 --- a/projects/IoT/IoT.Shared/DeviceServices/FBee/FBeeService.cs +++ b/projects/IoT/IoT.Shared/DeviceServices/FBee/FBeeService.cs @@ -104,6 +104,7 @@ namespace IoT.Shared.DeviceServices.FBee } using (var scope = _applicationServices.CreateScope()) { + var nodeService = scope.ServiceProvider.GetService(); var categoryRepo = scope.ServiceProvider.GetService>(); var productNumber = "fbee:gateway"; var productRepo = scope.ServiceProvider.GetService>(); @@ -116,7 +117,7 @@ namespace IoT.Shared.DeviceServices.FBee Number = productNumber, Name = "FBee网关", Icon = "gateway", - ApiJson = this.GetApiJson("/Gateway/"), + ApiJson = nodeService.GetApiJson("/Gateway/"), CategoryId = category.Id }; OpenApiService.UpdateApi(product); @@ -425,6 +426,7 @@ namespace IoT.Shared.DeviceServices.FBee var endpoint = ms.ReadByte(); using (var scope = _applicationServices.CreateScope()) { + var nodeService = scope.ServiceProvider.GetService(); var profileId = ms.ReadInt(); var deviceId = ms.ReadInt(); var switchState = ms.ReadByte(); @@ -486,31 +488,31 @@ namespace IoT.Shared.DeviceServices.FBee }; if (deviceName == "插座" || deviceName == "智能插座") { - product.ApiJson = this.GetApiJson("/Socket/"); + product.ApiJson = nodeService.GetApiJson("/Socket/"); } else if (deviceName == "一路开关") { - product.ApiJson = this.GetApiJson("/Switch/"); + product.ApiJson = nodeService.GetApiJson("/Switch/"); } else if (deviceName == "二路开关") { - product.ApiJson = this.GetApiJson("/Switch2/"); + product.ApiJson = nodeService.GetApiJson("/Switch2/"); } else if (deviceName == "三路开关") { - product.ApiJson = this.GetApiJson("/Switch3/"); + product.ApiJson = nodeService.GetApiJson("/Switch3/"); } else if (deviceName == "窗帘") { - product.ApiJson = this.GetApiJson("/Curtain/"); + product.ApiJson = nodeService.GetApiJson("/Curtain/"); } else if (deviceName == "调色灯") { - product.ApiJson = this.GetApiJson("/ColorLight/"); + product.ApiJson = nodeService.GetApiJson("/ColorLight/"); } else if (deviceName == "红外转发器") { - product.ApiJson = this.GetApiJson("/Ir/"); + product.ApiJson = nodeService.GetApiJson("/Ir/"); } OpenApiService.UpdateApi(product); productRepo.Add(product); diff --git a/projects/IoT/IoT.Shared/DeviceServices/Onvif/OnvifService.cs b/projects/IoT/IoT.Shared/DeviceServices/Onvif/OnvifService.cs index 5dc2a6fb..624d5be8 100644 --- a/projects/IoT/IoT.Shared/DeviceServices/Onvif/OnvifService.cs +++ b/projects/IoT/IoT.Shared/DeviceServices/Onvif/OnvifService.cs @@ -88,6 +88,7 @@ namespace IoT.Shared.DeviceServices.Onvif { using (var scope = _applicationServices.CreateScope()) { + var nodeService = scope.ServiceProvider.GetService(); var productNumber = "onvifcamera"; var categoryRepo = scope.ServiceProvider.GetService>(); var productRepo = scope.ServiceProvider.GetService>(); @@ -99,7 +100,7 @@ namespace IoT.Shared.DeviceServices.Onvif { Number = productNumber, Name = "ONVIF摄像头", - ApiJson = this.GetApiJson("/Camera/"), + ApiJson = nodeService.GetApiJson("/Onvif/"), CategoryId = category.Id }; OpenApiService.UpdateApi(product); diff --git a/projects/IoT/IoT.Shared/DeviceServices/SerialPort/SerialPortService.cs b/projects/IoT/IoT.Shared/DeviceServices/SerialPort/SerialPortService.cs index 9f0f096e..049b0f1c 100644 --- a/projects/IoT/IoT.Shared/DeviceServices/SerialPort/SerialPortService.cs +++ b/projects/IoT/IoT.Shared/DeviceServices/SerialPort/SerialPortService.cs @@ -29,6 +29,7 @@ namespace IoT.Shared.DeviceServices.SerialPort Task.Run(() => { using var scope = _applicationServices.CreateScope(); + var nodeService = scope.ServiceProvider.GetService(); var productNumber = "serialport"; var categoryRepo = scope.ServiceProvider.GetService>(); var productRepo = scope.ServiceProvider.GetService>(); @@ -39,7 +40,7 @@ namespace IoT.Shared.DeviceServices.SerialPort { Number = productNumber, Name = "串口", - ApiJson = this.GetApiJson("/SerialPort/"), + ApiJson = nodeService.GetApiJson("/SerialPort/"), CategoryId = categoryRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == "20").Id }; OpenApiService.UpdateApi(product); diff --git a/projects/IoT/IoT.Shared/Infrastructure/BaseNodeService.cs b/projects/IoT/IoT.Shared/Infrastructure/BaseNodeService.cs deleted file mode 100644 index 6ea5ef44..00000000 --- a/projects/IoT/IoT.Shared/Infrastructure/BaseNodeService.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Application.Models; -using Infrastructure.Extensions; -using Microsoft.AspNetCore.SignalR.Client; -using Microsoft.Extensions.Configuration; -using System; -using System.Threading; -using System.Threading.Tasks; - -namespace IoT.Shared.Infrastructure -{ - public class BaseNodeService - { - } -} \ No newline at end of file diff --git a/projects/IoT/IoT.Shared/Infrastructure/DeviceService.cs b/projects/IoT/IoT.Shared/Infrastructure/DeviceService.cs index 154e08c3..c317caf0 100644 --- a/projects/IoT/IoT.Shared/Infrastructure/DeviceService.cs +++ b/projects/IoT/IoT.Shared/Infrastructure/DeviceService.cs @@ -50,38 +50,6 @@ namespace IoT.Shared.Infrastructure return Task.CompletedTask; } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")] - public string GetApiJson(string prefix) - { - try - { - using var scope = _applicationServices.CreateScope(); - 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) - { - if (!item.StartsWith(prefix)) - { - paths.Remove(item); - } - } - var realResult = JsonConvert.SerializeObject(json); - return realResult; - } - catch (Exception ex) - { - ex.PrintStack(); - return null; - } - } - public void SendToServer(string method, object data) { Console.WriteLine("send device to server"); diff --git a/projects/IoT/IoT.Shared/Infrastructure/INodeService.cs b/projects/IoT/IoT.Shared/Infrastructure/INodeService.cs deleted file mode 100644 index 1fa31a5a..00000000 --- a/projects/IoT/IoT.Shared/Infrastructure/INodeService.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace IoT.Shared.Infrastructure -{ - public interface INodeService : IDisposable - { - void Start(); - } -} \ No newline at end of file diff --git a/projects/IoT/IoT.Shared/Infrastructure/NodeService.cs b/projects/IoT/IoT.Shared/Infrastructure/NodeService.cs index 2ed6f55d..e25eaf97 100644 --- a/projects/IoT/IoT.Shared/Infrastructure/NodeService.cs +++ b/projects/IoT/IoT.Shared/Infrastructure/NodeService.cs @@ -8,6 +8,8 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; @@ -546,5 +548,46 @@ namespace IoT.Shared.Infrastructure } }); } + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")] + public string GetApiJson(string prefix) + { + try + { + using var scope = applicationServices.CreateScope(); + 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) + { + if (!item.StartsWith(prefix)) + { + paths.Remove(item); + } + } + var tags = json.Properties().FirstOrDefault(o => o.Name == "tags").Value as JArray; + var names2 = tags.Select(o => (o as JObject).Properties().FirstOrDefault(o => o.Name == "name").Value.ToString()).ToList(); + foreach (var item in names2) + { + if (item != prefix.Trim('/')) + { + tags.Remove(tags.FirstOrDefault(o => (o as JObject).Properties().FirstOrDefault(o => o.Name == "name").Value.ToString() != prefix.Trim('/'))); + } + } + var realResult = JsonConvert.SerializeObject(json); + return realResult; + } + catch (Exception ex) + { + ex.PrintStack(); + return null; + } + } } } \ No newline at end of file diff --git a/projects/IoTCenter/Controllers/HomeController.cs b/projects/IoTCenter/Controllers/HomeController.cs index 2bc08d33..4f6c569f 100644 --- a/projects/IoTCenter/Controllers/HomeController.cs +++ b/projects/IoTCenter/Controllers/HomeController.cs @@ -125,6 +125,7 @@ namespace IoTCenter.Controllers [Authorize] [Route("/Node")] + [ApiExplorerSettings(IgnoreApi = true)] public IActionResult Node(string number) { return View(model: number);