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

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

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

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

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

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

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

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

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

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

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

@ -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<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;
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<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 device = this.GetDevice(deviceRepo, sn, ieee);
var clientService = scope.ServiceProvider.GetService<ClientService>();
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)

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

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

Loading…
Cancel
Save