Former-commit-id: bc10819b1bee4ce39c9d7a4c363ac8d30269823b
TangShanKaiPing
wanggang 6 years ago
parent adf38d5362
commit 61a609003a

@ -3,7 +3,7 @@ using System.Text;
namespace Infrastructure.Extensions namespace Infrastructure.Extensions
{ {
public static class GuidToBase62Extensions public static class GuidExtensions
{ {
private const uint radix = 62; private const uint radix = 62;
private const ulong carry = 297528130221121800; // 2^64 / 62 private const ulong carry = 297528130221121800; // 2^64 / 62

@ -51,6 +51,9 @@ namespace Application.Domain.Entities
[Display(Name = "分类")] [Display(Name = "分类")]
public DeviceInfo Info { get; set; } public DeviceInfo Info { get; set; }
[Display(Name = "节点编号")]
public string NodeNumber { get; set; }
[Display(Name = "节点Id")] [Display(Name = "节点Id")]
public Guid? NodeId { get; set; } public Guid? NodeId { get; set; }

@ -1,6 +1,8 @@
using Application.Models; using Application.Models;
using Infrastructure.Extensions; using Infrastructure.Extensions;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
@ -12,24 +14,24 @@ namespace IoT.Shared.Controllers
{ {
public class BaseDeviceController : Controller 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)] [ApiExplorerSettings(IgnoreApi = true)]
public string GetApiJson() 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 prefix = $"/{RouteData.Values["controller"]}/";
var hc = this._httpClientFactory.CreateClient(); using (var scope = this.applicationService.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 result = hc.GetStringAsync(url).Result;
var json = JsonConvert.DeserializeObject(result) as JObject; var json = JsonConvert.DeserializeObject(result) as JObject;
var paths = json.Properties().FirstOrDefault(o => o.Name == "paths").Value as JObject; var paths = json.Properties().FirstOrDefault(o => o.Name == "paths").Value as JObject;
@ -44,6 +46,7 @@ namespace IoT.Shared.Controllers
var realResult = JsonConvert.SerializeObject(json); var realResult = JsonConvert.SerializeObject(json);
return realResult; return realResult;
} }
}
public ApiResponse AsyncAction(Action action) public ApiResponse AsyncAction(Action action)
{ {

@ -111,7 +111,7 @@ namespace IoT.Shared.Infrastructure
private void InitConnection() private void InitConnection()
{ {
this._notifyHost = this._cfg["notify:host"]; 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) if (this.Connection != null)
{ {
this.Connection.DisposeAsync(); this.Connection.DisposeAsync();

@ -1,16 +1,16 @@
using Application.Models; using Application.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.Annotations;
using System.Net.Http; using System;
namespace FBeeService.Controllers namespace FBeeService.Controllers
{ {
[SwaggerTag("窗帘")] [SwaggerTag("窗帘")]
public class CurtainController : SwitchController 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; this._deviceService = deviceService;
} }

@ -2,7 +2,7 @@
using IoT.Shared.Controllers; using IoT.Shared.Controllers;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.Annotations;
using System.Net.Http; using System;
namespace FBeeService.Controllers namespace FBeeService.Controllers
{ {
@ -11,7 +11,7 @@ namespace FBeeService.Controllers
{ {
private readonly DeviceService _deviceService; private readonly DeviceService _deviceService;
public GatewayController(IHttpClientFactory httpClientFactory, DeviceService deviceService) : base(httpClientFactory) public GatewayController(IServiceProvider applicationServices, DeviceService deviceService) : base(applicationServices)
{ {
this._deviceService = deviceService; this._deviceService = deviceService;
} }

@ -1,12 +1,12 @@
using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.Annotations;
using System.Net.Http; using System;
namespace FBeeService.Controllers namespace FBeeService.Controllers
{ {
[SwaggerTag("1路插座")] [SwaggerTag("1路插座")]
public class SocketController : SwitchController public class SocketController : SwitchController
{ {
public SocketController(IHttpClientFactory httpClientFactory, DeviceService deviceService) : base(httpClientFactory, deviceService) public SocketController(IServiceProvider applicationService, DeviceService deviceService) : base(applicationService, deviceService)
{ {
} }
} }

@ -2,7 +2,7 @@
using IoT.Shared.Controllers; using IoT.Shared.Controllers;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.Annotations;
using System.Net.Http; using System;
namespace FBeeService.Controllers namespace FBeeService.Controllers
{ {
@ -11,7 +11,7 @@ namespace FBeeService.Controllers
{ {
private readonly DeviceService _deviceService; private readonly DeviceService _deviceService;
public Switch2Controller(IHttpClientFactory httpClientFactory, DeviceService deviceService) : base(httpClientFactory) public Switch2Controller(IServiceProvider applicationServices, DeviceService deviceService) : base(applicationServices)
{ {
this._deviceService = deviceService; this._deviceService = deviceService;
} }

@ -2,7 +2,7 @@
using IoT.Shared.Controllers; using IoT.Shared.Controllers;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.Annotations;
using System.Net.Http; using System;
namespace FBeeService.Controllers namespace FBeeService.Controllers
{ {
@ -11,7 +11,7 @@ namespace FBeeService.Controllers
{ {
private readonly DeviceService _deviceService; private readonly DeviceService _deviceService;
public Switch3Controller(IHttpClientFactory httpClientFactory, DeviceService deviceService) : base(httpClientFactory) public Switch3Controller(IServiceProvider applicationServices, DeviceService deviceService) : base(applicationServices)
{ {
this._deviceService = deviceService; this._deviceService = deviceService;
} }

@ -2,7 +2,7 @@
using IoT.Shared.Controllers; using IoT.Shared.Controllers;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.Annotations;
using System.Net.Http; using System;
namespace FBeeService.Controllers namespace FBeeService.Controllers
{ {
@ -11,7 +11,7 @@ namespace FBeeService.Controllers
{ {
private readonly DeviceService _deviceService; private readonly DeviceService _deviceService;
public SwitchController(IHttpClientFactory httpClientFactory, DeviceService deviceService) : base(httpClientFactory) public SwitchController(IServiceProvider applicationServices, DeviceService deviceService) : base(applicationServices)
{ {
this._deviceService = deviceService; this._deviceService = deviceService;
} }

@ -3,10 +3,12 @@ using Infrastructure.Data;
using Infrastructure.Extensions; using Infrastructure.Extensions;
using IoT.Shared.Infrastructure; using IoT.Shared.Infrastructure;
using Microsoft.AspNetCore.SignalR.Client; using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System; using System;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace FBeeService namespace FBeeService
@ -22,10 +24,10 @@ namespace FBeeService
using (var scope = this.applicationServices.CreateScope()) using (var scope = this.applicationServices.CreateScope())
{ {
var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>(); var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
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; this.Notify(device);
deviceRepo.SaveChanges();
} }
} }
} }
@ -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<IConfiguration>();
var port = cfg["server.urls"].Split(':')[2];
var url = $"http://localhost:{port}{path}{queryString}";
var httpClient = serviceProvider.GetService<IHttpClientFactory>().CreateClient();
}
});
}
} }
} }

@ -440,7 +440,6 @@ namespace FBeeService
var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>(); var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
var device = this.GetDevice(deviceRepo, sn, ieee); var device = this.GetDevice(deviceRepo, sn, ieee);
var clientService = scope.ServiceProvider.GetService<ClientService>(); var clientService = scope.ServiceProvider.GetService<ClientService>();
var connectId = clientService.ConnectionId;
if (device == null) if (device == null)
{ {
create = true; create = true;
@ -448,19 +447,17 @@ namespace FBeeService
{ {
Number = ieee, Number = ieee,
Name = deviceType.Name, Name = deviceType.Name,
IsOnline = isOnline != 0x00,
Icon = deviceType.Icon, Icon = deviceType.Icon,
CategoryNumber = deviceType.Category, CategoryNumber = deviceType.Category,
GatewayNumber = sn, GatewayNumber = sn,
ConnectId = connectId,
InfoId = deviceInfo.Id InfoId = deviceInfo.Id
}; };
deviceRepo.Add(device); 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 (deviceId == 0x402)
{ {
if (zoneType == 0x0028) if (zoneType == 0x0028)

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Infrastructure.Application; using Infrastructure.Application;
using Infrastructure.Configuration; using Infrastructure.Configuration;
@ -28,6 +29,8 @@ namespace FBeeService
new EFConfigurationValue { Id = "notify:enabled", Value= "true"}, new EFConfigurationValue { Id = "notify:enabled", Value= "true"},
new EFConfigurationValue { Id = "notify:host", Value= $"{host}:8001"}, new EFConfigurationValue { Id = "notify:host", Value= $"{host}:8001"},
new EFConfigurationValue { Id = "timer.seconds", Value="600"}, 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 = "name", Value= "FBeeService服务"},
new EFConfigurationValue { Id = "logo", Value= "/images/logo.png",Type= InputType.ImageUrl}, new EFConfigurationValue { Id = "logo", Value= "/images/logo.png",Type= InputType.ImageUrl},

@ -14,6 +14,7 @@
<th>电量</th> <th>电量</th>
<th>在线状态</th> <th>在线状态</th>
<th>设备状态</th> <th>设备状态</th>
<th>连接Id</th>
<th>操作</th> <th>操作</th>
<th>删除</th> <th>删除</th>
</tr> </tr>
@ -92,6 +93,7 @@
} }
</td> </td>
<td>@item.ConnectId</td>
<td> <td>
@if (new int[] { 0x0002, 0x0009, 0x0081, 0x0202, 0x0220, 0x0051 }.Contains(deviceId)) @if (new int[] { 0x0002, 0x0009, 0x0081, 0x0202, 0x0220, 0x0051 }.Contains(deviceId))
{ {

Loading…
Cancel
Save