Former-commit-id: 94c0d20fd94a9bcaee8acc99d96df2e2037f1398
Former-commit-id: 855a0693d6380ad91439712a0a00eecd5e54ab2e
1.0
wanggang 5 years ago
parent 28b04242e3
commit 331155f2f2

@ -48,6 +48,8 @@ namespace IoT.Shared.Application.Models
[Required(ErrorMessage = nameof(RequiredAttribute))] [Required(ErrorMessage = nameof(RequiredAttribute))]
public IoTValueType? ValueType { get; set; } public IoTValueType? ValueType { get; set; }
[SkipSearch]
[SkipList]
[Display(Name = "对象模式")] [Display(Name = "对象模式")]
[Description("描述枚举、对象和数组的类型定义")] [Description("描述枚举、对象和数组的类型定义")]
public string ValueSchema { get; set; } public string ValueSchema { get; set; }

@ -14,11 +14,9 @@
public const string ExecSceneRsponse = nameof(ExecSceneRsponse); public const string ExecSceneRsponse = nameof(ExecSceneRsponse);
public const string ExecCommand = nameof(ExecCommand); public const string ExecCommand = nameof(ExecCommand);
public const string UpdateNodeResponse = nameof(UpdateNodeResponse); //上传节点
public const string UpdateProductResponse = nameof(UpdateProductResponse);//上传产品
public const string UpdateApiResponse = nameof(UpdateApiResponse);//上传接口 public const string UpdateApiResponse = nameof(UpdateApiResponse);//上传接口
public const string UpdateParameterResponse = nameof(UpdateParameterResponse);//上传参数 public const string UpdateParameterResponse = nameof(UpdateParameterResponse);//上传参数
public const string UpdateDeviceIdListResponse = nameof(UpdateDeviceIdListResponse);//上传节点设备Id列表 public const string UpdateDeviceIdList = nameof(UpdateDeviceIdList);//上传节点设备Id列表
public const string UpdateDeviceResponse = nameof(UpdateDeviceResponse);//上传设备 public const string UpdateDeviceResponse = nameof(UpdateDeviceResponse);//上传设备
public const string UpdateDataResponse = nameof(UpdateDataResponse);//上传数据 public const string UpdateDataResponse = nameof(UpdateDataResponse);//上传数据

@ -34,11 +34,10 @@ namespace IoT.Shared.Services
#region private #region private
public void Update<T>(string message, Expression<Func<T, bool>> predicate = null,string[] skips=null) where T : BaseEntity public void Update<T>(T model, Expression<Func<T, bool>> predicate = null,string[] skips=null) where T : BaseEntity
{ {
using var scope = this._services.CreateScope(); using var scope = this._services.CreateScope();
var repo = scope.ServiceProvider.GetService<IRepository<T>>(); var repo = scope.ServiceProvider.GetService<IRepository<T>>();
var model = message.FromJson<T>();
//delete entity where entity.Id!=model.Id //delete entity where entity.Id!=model.Id
if (predicate != null) if (predicate != null)

@ -10,7 +10,7 @@ namespace IoT.Shared.Services
{ {
public static void UpdateApi(IoTProduct product) public static void UpdateApi(IoTProduct product)
{ {
if (product == null || string.IsNullOrEmpty(product.ApiJson)) if (product == null || string.IsNullOrEmpty(product.ApiJson)||product.IoTApis.Any())
{ {
return; return;
} }

@ -125,7 +125,7 @@ namespace IoTNode.DeviceServices
var response = httpClient.GetStringAsync(url).Result.FromJson<bool>(); var response = httpClient.GetStringAsync(url).Result.FromJson<bool>();
if (!response) if (!response)
{ {
scope.ServiceProvider.GetRequiredService<IoTNodeClient>().ClientToServer(Methods.UpdateProductResponse, product, null); scope.ServiceProvider.GetRequiredService<IoTNodeClient>().ClientToServer($"{nameof(IoTProduct)}EntityUpdated", product, null);
} }
} }
catch (Exception ex) catch (Exception ex)

@ -83,6 +83,7 @@ namespace IoTNode.DeviceServices.SerialPortManager
public void Notify() public void Notify()
{ {
//上传产品信息
this.UpdateProduct("串口控制器", _productNumber, "/SerialPort/", "serialport"); this.UpdateProduct("串口控制器", _productNumber, "/SerialPort/", "serialport");
using var scope = _applicationServices.CreateScope(); using var scope = _applicationServices.CreateScope();
var repo = scope.ServiceProvider.GetService<IRepository<IoTDevice>>(); var repo = scope.ServiceProvider.GetService<IRepository<IoTDevice>>();
@ -108,17 +109,17 @@ namespace IoTNode.DeviceServices.SerialPortManager
}; };
repo.Add(device); repo.Add(device);
repo.SaveChanges(); repo.SaveChanges();
var buttons = new List<SPCommandModel>() { //var buttons = new List<SPCommandModel>() {
new SPCommandModel { // new SPCommandModel {
Name = "测试", // Name = "测试",
PortName = "/dev/ttyS0", // PortName = "/dev/ttyS0",
BaudRate = 9600, // BaudRate = 9600,
Parity = 0, // Parity = 0,
DataBits = 8, // DataBits = 8,
StopBits = 1, // StopBits = 1,
Message = "0123456789ABCDEF" // Message = "0123456789ABCDEF"
} // }
}.ToJson(true); //}.ToJson(true);
var data = device.Data.FirstOrDefault(o => o.Key == "Buttons"); var data = device.Data.FirstOrDefault(o => o.Key == "Buttons");
if(data==null) if(data==null)
{ {
@ -128,12 +129,12 @@ namespace IoTNode.DeviceServices.SerialPortManager
DataType= IoTDataType.Data, DataType= IoTDataType.Data,
ValueType= IoTValueType.Array, ValueType= IoTValueType.Array,
ValueSchema= JsonSchema.FromType( typeof(SPCommandModel)).ToJson(), ValueSchema= JsonSchema.FromType( typeof(SPCommandModel)).ToJson(),
Value=buttons.ToJson(true), Value= new List<SPCommandModel>().ToJson(true),
Timestamp= DateTimeOffset.Now.ToUnixTimeMilliseconds() Timestamp= DateTimeOffset.Now.ToUnixTimeMilliseconds()
}; };
device.Data.Add(data); device.Data.Add(data);
repo.SaveChanges();
} }
repo.SaveChanges();
} }
} }
} }

@ -212,16 +212,16 @@ namespace IoT.Shared.Services
public void OnConnected() public void OnConnected()
{ {
//上传网关 //上传网关
this.UpdateEntityIdList<IoTGateway>(null, Methods.UpdateNodeResponse); this.UpdateEntityIdList<IoTGateway>(null, $"{nameof(IoTGateway)}EntityUpdated");
//上传产品 //上传产品
this.UpdateEntityIdList<IoTProduct>(null, Methods.UpdateProductResponse); this.UpdateEntityIdList<IoTProduct>(null, $"{nameof(IoTProduct)}EntityUpdated");
//上传设备Id列表、设备 //上传设备Id列表、设备
this.UpdateEntityIdList<IoTDevice>(Methods.UpdateDeviceIdListResponse, Methods.UpdateDeviceResponse); this.UpdateEntityIdList<IoTDevice>(Methods.UpdateDeviceIdList, $"{nameof(IoTDevice)}EntityUpdated");
//上传数据 //上传数据
this.UpdateEntityIdList<IoTData>(null, Methods.UpdateDataResponse, null, o => !o.Hidden); this.UpdateEntityIdList<IoTData>(null, $"{nameof(IoTData)}EntityUpdated", null, o => !o.Hidden);
} }
public void OnServerToClient(string method, string message, string to, string from) public void OnServerToClient(string method, string message, string to, string from)
@ -249,7 +249,6 @@ namespace IoT.Shared.Services
else if (method == $"Edit{nameof(IoTDevice)}")//服务端编辑设备 else if (method == $"Edit{nameof(IoTDevice)}")//服务端编辑设备
{ {
var model = message.FromJson<EditIoTDeviceModel>(); var model = message.FromJson<EditIoTDeviceModel>();
//if node category not had,get category from serever by http sync
dataService.Edit<IoTDevice, EditIoTDeviceModel>(model); dataService.Edit<IoTDevice, EditIoTDeviceModel>(model);
this.ClientToServer(method, model, null); this.ClientToServer(method, model, null);
} }
@ -259,13 +258,13 @@ namespace IoT.Shared.Services
dataService.Delete<IoTDevice, EditIoTDeviceModel>(model); dataService.Delete<IoTDevice, EditIoTDeviceModel>(model);
this.ClientToServer(method, model, null); this.ClientToServer(method, model, null);
} }
else if (method == $"Edit{nameof(IoTData)}")//服务端编辑数据 else if (method == $"{nameof(IoTData)}EntityInserted"|| method == $"{nameof(IoTData)}EntityUpdated")//服务端插入或更新数据
{ {
var model = message.FromJson<EditIoTDataModel>(); var model = message.FromJson<EditIoTDataModel>();
dataService.Edit<IoTData, EditIoTDataModel>(model); dataService.Edit<IoTData, EditIoTDataModel>(model);
this.ClientToServer(method, model, null); this.ClientToServer(method, model, null);
} }
else if (method == $"Delete{nameof(IoTData)}")//服务端删除数据 else if (method == $"{nameof(IoTData)}EntityDeleted")//服务端删除数据
{ {
var model = message.FromJson<EditIoTDataModel>(); var model = message.FromJson<EditIoTDataModel>();
dataService.Delete<IoTData, EditIoTDataModel>(model); dataService.Delete<IoTData, EditIoTDataModel>(model);

@ -1,20 +1,15 @@
using Infrastructure.Application.Entites.Settings; using Application.Domain.Entities;
using Infrastructure.Data; using Infrastructure.Application.Entites.Settings;
using Infrastructure.Events; using Infrastructure.Events;
using Infrastructure.Extensions; using Infrastructure.Extensions;
using Infrastructure.Web.SignalR;
using IoT.Shared.Application.Models;
using IoT.Shared.Services; using IoT.Shared.Services;
using IoTNode.DeviceServices.Onvif; using IoTNode.DeviceServices.Onvif;
using Application.Domain.Entities; using Microsoft.AspNetCore.SignalR;
using Jint;
using Jint.Native;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Infrastructure.Web.SignalR;
using IoT.Shared.Application.Models;
namespace IoTNode.Services namespace IoTNode.Services
{ {
@ -22,7 +17,11 @@ namespace IoTNode.Services
IEventHander<EntityUpdatedEvent<Setting>>, IEventHander<EntityUpdatedEvent<Setting>>,
IEventHander<EntityInsertedEvent<IoTDevice>>, IEventHander<EntityInsertedEvent<IoTDevice>>,
IEventHander<EntityUpdatedEvent<IoTDevice>>, IEventHander<EntityUpdatedEvent<IoTDevice>>,
IEventHander<EntityDeletedEvent<IoTDevice>> IEventHander<EntityDeletedEvent<IoTDevice>>,
IEventHander<EntityInsertedEvent<IoTData>>,
IEventHander<EntityUpdatedEvent<IoTData>>,
IEventHander<EntityDeletedEvent<IoTData>>
{ {
private readonly ILogger<IoTNodeEventHandler> _logger; private readonly ILogger<IoTNodeEventHandler> _logger;
private readonly IHubContext<BasePageHub> _hub; private readonly IHubContext<BasePageHub> _hub;
@ -72,6 +71,22 @@ namespace IoTNode.Services
{ {
this.Notify(message); this.Notify(message);
} }
public void Handle(EntityInsertedEvent<IoTData> message)
{
this.Upload(message);
}
public void Handle(EntityUpdatedEvent<IoTData> message)
{
this.Upload(message);
}
public void Handle(EntityDeletedEvent<IoTData> message)
{
this.Upload(message);
}
private void Notify<T>(BaseEvent<T> message) private void Notify<T>(BaseEvent<T> message)
{ {
Task.Run(() => Task.Run(() =>
@ -86,5 +101,20 @@ namespace IoTNode.Services
} }
}); });
} }
private void Upload<T>(BaseEvent<T> message)
{
Task.Run(() =>
{
try
{
this._ioTNodeClient.ClientToServer($"{typeof(T).Name}{message.Arg}",message.Data,null);
}
catch (Exception ex)
{
ex.PrintStack();
}
});
}
} }
} }

@ -17,7 +17,7 @@
<template v-if="gateway"> <template v-if="gateway">
<tr v-for="(item,index) in gateway.devices"> <tr v-for="(item,index) in gateway.devices">
<td>{{index+1}}</td> <td>{{index+1}}</td>
<td>{{item.ioTProduct.Number}}</td> <td>{{item.ioTProduct.number}}</td>
<td>{{item.name}}</td> <td>{{item.name}}</td>
<td>{{item.displayName}}</td> <td>{{item.displayName}}</td>
<td>{{item.isOnline===true?"在线":"离线"}}</td> <td>{{item.isOnline===true?"在线":"离线"}}</td>

@ -35,8 +35,7 @@ namespace Platform.Services
IEventHander<EntityUpdatedEvent<IoTGateway>>, IEventHander<EntityUpdatedEvent<IoTGateway>>,
IEventHander<EntityDeletedEvent<IoTGateway>>, IEventHander<EntityDeletedEvent<IoTGateway>>,
IEventHander<EntityInsertedEvent<IoTProduct>>, IEventHander<EntityInsertedEvent<IoTProduct>>,
//IEventHander<EntityUpdatedEvent<IoTProduct>>, IEventHander<EntityUpdatedEvent<IoTProduct>>,
//IEventHander<EntityDeletedEvent<IoTProduct>>,
IEventHander<EntityInsertedEvent<IoTDevice>>, IEventHander<EntityInsertedEvent<IoTDevice>>,
IEventHander<EntityUpdatedEvent<IoTDevice>>, IEventHander<EntityUpdatedEvent<IoTDevice>>,
IEventHander<EntityDeletedEvent<IoTDevice>>, IEventHander<EntityDeletedEvent<IoTDevice>>,
@ -138,28 +137,29 @@ namespace Platform.Services
#region Product #region Product
public void Handle(EntityInsertedEvent<IoTProduct> message) public void Handle(EntityInsertedEvent<IoTProduct> message)
{
UpdateProduct(message.Data);
this.Notify(message);
}
public void Handle(EntityUpdatedEvent<IoTProduct> message)
{
UpdateProduct(message.Data);
this.Notify(message);
}
private void UpdateProduct(IoTProduct entity)
{ {
var product = this._productrepo.Table() var product = this._productrepo.Table()
.Include(o=>o.IoTApis) .Include(o => o.IoTApis)
.ThenInclude(o=>o.IoTParameters) .ThenInclude(o => o.IoTParameters)
.FirstOrDefault(o => o.Number == message.Data.Number); .FirstOrDefault(o => o.Number == entity.Number);
if(product!=null) if (product != null)
{ {
OpenApiService.UpdateApi(product); OpenApiService.UpdateApi(product);
this._productrepo.SaveChanges();
} }
this.Notify(message);
} }
//public void Handle(EntityUpdatedEvent<IoTProduct> message)
//{
// this.Notify(message);
//}
//public void Handle(EntityDeletedEvent<IoTProduct> message)
//{
// this.Notify(message);
//}
#endregion Product #endregion Product
#region Node #region Node

@ -1,8 +1,8 @@
using Infrastructure.Data; using Application.Domain.Entities;
using Infrastructure.Data;
using Infrastructure.Events; using Infrastructure.Events;
using Infrastructure.Extensions; using Infrastructure.Extensions;
using Infrastructure.Web.SignalR; using Infrastructure.Web.SignalR;
using Application.Domain.Entities;
using IoT.Shared.Application.Models; using IoT.Shared.Application.Models;
using IoT.Shared.Services; using IoT.Shared.Services;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
@ -36,15 +36,10 @@ namespace Platform.Services
private readonly IRepository<IoTApi> _apiRepo; private readonly IRepository<IoTApi> _apiRepo;
//private readonly IRepository<IoTCommand> _commandRepo;
private readonly IRepository<IoTProductCategory> _categoryRepo; private readonly IRepository<IoTProductCategory> _categoryRepo;
private readonly IRepository<IoTProduct> _productRepo; private readonly IRepository<IoTProduct> _productRepo;
//private readonly IRepository<IoTScene> _sceneRepo;
//private readonly IRepository<IoTSceneIoTCommand> _sceneCommandRepo;
//private readonly IRepository<IoTSceneTimer> _iotTimerRepo;
//private readonly IRepository<IoTSceneTigger> _iotTiggerRepo;
private readonly DataService _dataService; private readonly DataService _dataService;
private readonly IHttpClientFactory _httpClientFactory; private readonly IHttpClientFactory _httpClientFactory;
@ -57,13 +52,8 @@ namespace Platform.Services
IRepository<IoTDevice> deviceRepo, IRepository<IoTDevice> deviceRepo,
IRepository<IoTData> dataRepo, IRepository<IoTData> dataRepo,
IRepository<IoTApi> apiRepo, IRepository<IoTApi> apiRepo,
//IRepository<IoTCommand> commandRepo,
IRepository<IoTProductCategory> categoryRepo, IRepository<IoTProductCategory> categoryRepo,
IRepository<IoTProduct> productRepo, IRepository<IoTProduct> productRepo,
//IRepository<IoTScene> sceneRepo,
//IRepository<IoTSceneIoTCommand> sceneCommandRepo,
//IRepository<IoTSceneTimer> iotTimerRepo,
//IRepository<IoTSceneTigger> iotTiggerRepo,
DataService dataService, DataService dataService,
IHttpClientFactory httpClientFactory, IHttpClientFactory httpClientFactory,
IEventPublisher publisher) IEventPublisher publisher)
@ -74,13 +64,8 @@ namespace Platform.Services
this._deviceRepo = deviceRepo; this._deviceRepo = deviceRepo;
this._dataRepo = dataRepo; this._dataRepo = dataRepo;
this._apiRepo = apiRepo; this._apiRepo = apiRepo;
//this._commandRepo = commandRepo;
this._categoryRepo = categoryRepo; this._categoryRepo = categoryRepo;
this._productRepo = productRepo; this._productRepo = productRepo;
//this._sceneRepo = sceneRepo;
//this._sceneCommandRepo = sceneCommandRepo;
//this._iotTimerRepo = iotTimerRepo;
//this._iotTiggerRepo = iotTiggerRepo;
this._dataService = dataService; this._dataService = dataService;
this._httpClientFactory = httpClientFactory; this._httpClientFactory = httpClientFactory;
this._publisher = publisher; this._publisher = publisher;
@ -136,76 +121,29 @@ namespace Platform.Services
try try
{ {
//网关上传 //网关上传
if (method == Methods.UpdateNodeResponse)//接收网关 if (method == $"{nameof(IoTGateway)}EntityUpdated")//接收网关
{ {
var number = message.To<IoTGateway>().Number; var entity = message.FromJson<IoTGateway>();
this._dataService.Update<IoTGateway>(message, o => o.Number == number,new string[] { nameof(IoTGateway.BuildingId) }); this._dataService.Update(entity, o => o.Number == entity.Number, new string[] { nameof(IoTGateway.BuildingId) });
} }
else if (method == Methods.UpdateProductResponse)//接收产品 else if (method == $"{nameof(IoTProduct)}EntityUpdated")//接收产品
{ {
this._dataService.Update<IoTProduct>(message, skips: new string[] { nameof(IoTProduct.IoTProductCategoryId), nameof(IoTProduct.DisplayOrder) }); var entity = message.FromJson<IoTProduct>();
this._dataService.Update(entity, o => o.Number == entity.Number, skips: new string[] { nameof(IoTProduct.IoTProductCategoryId), nameof(IoTProduct.DisplayOrder) });
} }
else if (method == Methods.UpdateApiResponse)//接收接口 else if (method == Methods.UpdateDeviceIdList)//接收设备Id列表
{
this._dataService.Update<IoTApi>(message);
}
else if (method == Methods.UpdateParameterResponse)//接收参数
{
this._dataService.Update<IoTParameter>(message);
}
else if (method == Methods.UpdateDeviceIdListResponse)//接收设备Id列表
{ {
this._dataService.UpdateList<IoTDevice>(message, o => o.IoTGateway.Number == from); this._dataService.UpdateList<IoTDevice>(message, o => o.IoTGateway.Number == from);
} }
else if (method == Methods.UpdateDeviceResponse)//接收设备 else if (method == $"{nameof(IoTDevice)}EntityUpdated")//接收设备
{ {
var number = message.To<IoTDevice>().Number; var entity = message.FromJson<IoTDevice>();
this._dataService.Update<IoTDevice>(message, o => o.Number == number); this._dataService.Update(entity, o => o.Number == entity.Number);
} }
else if (method == Methods.UpdateDataResponse)//接收数据 else if (method == $"{nameof(IoTData)}EntityUpdated")//接收数据
{ {
this._dataService.Update<IoTData>(message); this._dataService.Update(message.FromJson<IoTData>());
} }
//else if (method == Methods.UpdateCommandIdListResponse)//接收命令Id列表
//{
// this._dataService.UpdateList<IoTCommand>(message, o => o.Device.Node.Number == from);
//}
//else if (method == Methods.UpdateCommandResponse)//接收命令
//{
// this._dataService.Update<IoTCommand>(message);
//}
//else if (method == Methods.UpdateSceneIdListResponse)//接收场景Id列表
//{
// this._dataService.UpdateList<IoTScene>(message, o => o.Node.Number == from);
//}
//else if (method == Methods.UpdateSceneResponse)//接收场景
//{
// this._dataService.Update<IoTScene>(message);
//}
//else if (method == Methods.UpdateIoTTimerIdListResponse)//接收定时器Id列表
//{
// this._dataService.UpdateList<IoTSceneTimer>(message, o => o.Scene.Node.Number == from);
//}
//else if (method == Methods.UpdateIoTTimerResponse)//接收定时器
//{
// this._dataService.Update<IoTSceneTimer>(message);
//}
//else if (method == Methods.UpdateIoTTiggerIdListResponse)//接收触发器Id列表
//{
// this._dataService.UpdateList<IoTSceneTigger>(message, o => o.Scene.Node.Number == from);
//}
//else if (method == Methods.UpdateIoTTiggerResponse)//接收触发器
//{
// this._dataService.Update<IoTSceneTigger>(message);
//}
//else if (method == Methods.UpdateSceneCommandIdListResponse)//接收场景命令Id列表
//{
// this._dataService.UpdateList<IoTSceneIoTCommand>(message, o => o.Scene.Node.Number == from);
//}
//else if (method == Methods.UpdateSceneCommandResponse)//接收场景命令
//{
// this._dataService.Update<IoTSceneIoTCommand>(message);
//}
//后台编辑 //后台编辑
else if (method == $"Edit{nameof(IoTGateway)}")//编辑节点返回 else if (method == $"Edit{nameof(IoTGateway)}")//编辑节点返回
{ {

Loading…
Cancel
Save