Former-commit-id: 34698c995dcfcff4f6679b43a6de6720695c50a6
TangShanKaiPing
wanggang 6 years ago
parent 47898a2d54
commit d9363d1c16

@ -30,7 +30,7 @@
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" /> <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" /> <PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" /> <PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc2" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0-rc4" /> <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0-rc4" />
<PackageReference Include="System.Drawing.Common" Version="4.6.0" /> <PackageReference Include="System.Drawing.Common" Version="4.6.0" />
<PackageReference Include="System.Management" Version="4.6.0" /> <PackageReference Include="System.Management" Version="4.6.0" />

@ -1,5 +1,6 @@
using Application.Models; using Application.Models;
using Infrastructure.Extensions; using Infrastructure.Extensions;
using IoT.Shared.Infrastructure;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -23,30 +24,14 @@ namespace IoT.Shared.Controllers
[ApiExplorerSettings(IgnoreApi = true)] [ApiExplorerSettings(IgnoreApi = true)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")]
public string GetApiJson() public string Api()
{ {
try try
{ {
var prefix = $"/{RouteData.Values["controller"]}/"; var prefix = $"/{RouteData.Values["controller"]}/";
using var scope = this.applicationService.CreateScope(); using var scope = applicationService.CreateScope();
var serviceProvider = scope.ServiceProvider; var deviceService = scope.ServiceProvider.GetService<NodeService>();
var cfg = serviceProvider.GetService<IConfiguration>(); return deviceService.GetApiJson(prefix);
var port = cfg["server.urls"].Split(':')[2];
var url = $"http://localhost:{port}/swagger/v1/swagger.json";
var hc = serviceProvider.GetService<IHttpClientFactory>().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) catch (Exception ex)
{ {

@ -104,6 +104,7 @@ namespace IoT.Shared.DeviceServices.FBee
} }
using (var scope = _applicationServices.CreateScope()) using (var scope = _applicationServices.CreateScope())
{ {
var nodeService = scope.ServiceProvider.GetService<NodeService>();
var categoryRepo = scope.ServiceProvider.GetService<IRepository<Category>>(); var categoryRepo = scope.ServiceProvider.GetService<IRepository<Category>>();
var productNumber = "fbee:gateway"; var productNumber = "fbee:gateway";
var productRepo = scope.ServiceProvider.GetService<IRepository<Product>>(); var productRepo = scope.ServiceProvider.GetService<IRepository<Product>>();
@ -116,7 +117,7 @@ namespace IoT.Shared.DeviceServices.FBee
Number = productNumber, Number = productNumber,
Name = "FBee网关", Name = "FBee网关",
Icon = "gateway", Icon = "gateway",
ApiJson = this.GetApiJson("/Gateway/"), ApiJson = nodeService.GetApiJson("/Gateway/"),
CategoryId = category.Id CategoryId = category.Id
}; };
OpenApiService.UpdateApi(product); OpenApiService.UpdateApi(product);
@ -425,6 +426,7 @@ namespace IoT.Shared.DeviceServices.FBee
var endpoint = ms.ReadByte(); var endpoint = ms.ReadByte();
using (var scope = _applicationServices.CreateScope()) using (var scope = _applicationServices.CreateScope())
{ {
var nodeService = scope.ServiceProvider.GetService<NodeService>();
var profileId = ms.ReadInt(); var profileId = ms.ReadInt();
var deviceId = ms.ReadInt(); var deviceId = ms.ReadInt();
var switchState = ms.ReadByte(); var switchState = ms.ReadByte();
@ -486,31 +488,31 @@ namespace IoT.Shared.DeviceServices.FBee
}; };
if (deviceName == "插座" || deviceName == "智能插座") if (deviceName == "插座" || deviceName == "智能插座")
{ {
product.ApiJson = this.GetApiJson("/Socket/"); product.ApiJson = nodeService.GetApiJson("/Socket/");
} }
else if (deviceName == "一路开关") else if (deviceName == "一路开关")
{ {
product.ApiJson = this.GetApiJson("/Switch/"); product.ApiJson = nodeService.GetApiJson("/Switch/");
} }
else if (deviceName == "二路开关") else if (deviceName == "二路开关")
{ {
product.ApiJson = this.GetApiJson("/Switch2/"); product.ApiJson = nodeService.GetApiJson("/Switch2/");
} }
else if (deviceName == "三路开关") else if (deviceName == "三路开关")
{ {
product.ApiJson = this.GetApiJson("/Switch3/"); product.ApiJson = nodeService.GetApiJson("/Switch3/");
} }
else if (deviceName == "窗帘") else if (deviceName == "窗帘")
{ {
product.ApiJson = this.GetApiJson("/Curtain/"); product.ApiJson = nodeService.GetApiJson("/Curtain/");
} }
else if (deviceName == "调色灯") else if (deviceName == "调色灯")
{ {
product.ApiJson = this.GetApiJson("/ColorLight/"); product.ApiJson = nodeService.GetApiJson("/ColorLight/");
} }
else if (deviceName == "红外转发器") else if (deviceName == "红外转发器")
{ {
product.ApiJson = this.GetApiJson("/Ir/"); product.ApiJson = nodeService.GetApiJson("/Ir/");
} }
OpenApiService.UpdateApi(product); OpenApiService.UpdateApi(product);
productRepo.Add(product); productRepo.Add(product);

@ -88,6 +88,7 @@ namespace IoT.Shared.DeviceServices.Onvif
{ {
using (var scope = _applicationServices.CreateScope()) using (var scope = _applicationServices.CreateScope())
{ {
var nodeService = scope.ServiceProvider.GetService<NodeService>();
var productNumber = "onvifcamera"; var productNumber = "onvifcamera";
var categoryRepo = scope.ServiceProvider.GetService<IRepository<Category>>(); var categoryRepo = scope.ServiceProvider.GetService<IRepository<Category>>();
var productRepo = scope.ServiceProvider.GetService<IRepository<Product>>(); var productRepo = scope.ServiceProvider.GetService<IRepository<Product>>();
@ -99,7 +100,7 @@ namespace IoT.Shared.DeviceServices.Onvif
{ {
Number = productNumber, Number = productNumber,
Name = "ONVIF摄像头", Name = "ONVIF摄像头",
ApiJson = this.GetApiJson("/Camera/"), ApiJson = nodeService.GetApiJson("/Onvif/"),
CategoryId = category.Id CategoryId = category.Id
}; };
OpenApiService.UpdateApi(product); OpenApiService.UpdateApi(product);

@ -29,6 +29,7 @@ namespace IoT.Shared.DeviceServices.SerialPort
Task.Run(() => Task.Run(() =>
{ {
using var scope = _applicationServices.CreateScope(); using var scope = _applicationServices.CreateScope();
var nodeService = scope.ServiceProvider.GetService<NodeService>();
var productNumber = "serialport"; var productNumber = "serialport";
var categoryRepo = scope.ServiceProvider.GetService<IRepository<Category>>(); var categoryRepo = scope.ServiceProvider.GetService<IRepository<Category>>();
var productRepo = scope.ServiceProvider.GetService<IRepository<Product>>(); var productRepo = scope.ServiceProvider.GetService<IRepository<Product>>();
@ -39,7 +40,7 @@ namespace IoT.Shared.DeviceServices.SerialPort
{ {
Number = productNumber, Number = productNumber,
Name = "串口", Name = "串口",
ApiJson = this.GetApiJson("/SerialPort/"), ApiJson = nodeService.GetApiJson("/SerialPort/"),
CategoryId = categoryRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == "20").Id CategoryId = categoryRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == "20").Id
}; };
OpenApiService.UpdateApi(product); OpenApiService.UpdateApi(product);

@ -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
{
}
}

@ -50,38 +50,6 @@ namespace IoT.Shared.Infrastructure
return Task.CompletedTask; 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<IConfiguration>();
var port = cfg["server.urls"].Split(':')[2];
var url = $"http://localhost:{port}/swagger/v1/swagger.json";
var hc = serviceProvider.GetService<IHttpClientFactory>().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) public void SendToServer(string method, object data)
{ {
Console.WriteLine("send device to server"); Console.WriteLine("send device to server");

@ -1,9 +0,0 @@
using System;
namespace IoT.Shared.Infrastructure
{
public interface INodeService : IDisposable
{
void Start();
}
}

@ -8,6 +8,8 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; 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<IConfiguration>();
var port = cfg["server.urls"].Split(':')[2];
var url = $"http://localhost:{port}/swagger/v1/swagger.json";
var hc = serviceProvider.GetService<IHttpClientFactory>().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;
}
}
} }
} }

@ -125,6 +125,7 @@ namespace IoTCenter.Controllers
[Authorize] [Authorize]
[Route("/Node")] [Route("/Node")]
[ApiExplorerSettings(IgnoreApi = true)]
public IActionResult Node(string number) public IActionResult Node(string number)
{ {
return View(model: number); return View(model: number);

Loading…
Cancel
Save