From 331155f2f22b97aa6a60d448a3682eeae0977f33 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Wed, 6 Jan 2021 12:18:08 +0800 Subject: [PATCH] update Former-commit-id: 94c0d20fd94a9bcaee8acc99d96df2e2037f1398 Former-commit-id: 855a0693d6380ad91439712a0a00eecd5e54ab2e --- .../Application/Models/EditIoTDataModel.cs | 2 + .../IoT.Shared/Application/Models/Methods.cs | 4 +- projects/IoT.Shared/Services/DataService.cs | 3 +- .../IoT.Shared/Services/OpenApiService.cs | 2 +- .../DeviceServices/BaseDeviceService.cs | 2 +- .../SerialPort/SerialPortService.cs | 27 +++--- projects/IoTNode/Services/IoTNodeClient.cs | 13 ++- .../IoTNode/Services/IoTNodeEventHandler.cs | 52 ++++++++--- projects/IoTNode/Views/Home/Index.cshtml | 2 +- .../Services/IoTCenterEventHandler.cs | 34 +++---- projects/Platform/Services/IoTCenterHub.cs | 90 +++---------------- 11 files changed, 99 insertions(+), 132 deletions(-) diff --git a/projects/IoT.Shared/Application/Models/EditIoTDataModel.cs b/projects/IoT.Shared/Application/Models/EditIoTDataModel.cs index 09596993..6349b2c4 100644 --- a/projects/IoT.Shared/Application/Models/EditIoTDataModel.cs +++ b/projects/IoT.Shared/Application/Models/EditIoTDataModel.cs @@ -48,6 +48,8 @@ namespace IoT.Shared.Application.Models [Required(ErrorMessage = nameof(RequiredAttribute))] public IoTValueType? ValueType { get; set; } + [SkipSearch] + [SkipList] [Display(Name = "对象模式")] [Description("描述枚举、对象和数组的类型定义")] public string ValueSchema { get; set; } diff --git a/projects/IoT.Shared/Application/Models/Methods.cs b/projects/IoT.Shared/Application/Models/Methods.cs index 30cbb5ce..a77e40b8 100644 --- a/projects/IoT.Shared/Application/Models/Methods.cs +++ b/projects/IoT.Shared/Application/Models/Methods.cs @@ -14,11 +14,9 @@ public const string ExecSceneRsponse = nameof(ExecSceneRsponse); 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 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 UpdateDataResponse = nameof(UpdateDataResponse);//上传数据 diff --git a/projects/IoT.Shared/Services/DataService.cs b/projects/IoT.Shared/Services/DataService.cs index 1441e607..2b4ad46b 100644 --- a/projects/IoT.Shared/Services/DataService.cs +++ b/projects/IoT.Shared/Services/DataService.cs @@ -34,11 +34,10 @@ namespace IoT.Shared.Services #region private - public void Update(string message, Expression> predicate = null,string[] skips=null) where T : BaseEntity + public void Update(T model, Expression> predicate = null,string[] skips=null) where T : BaseEntity { using var scope = this._services.CreateScope(); var repo = scope.ServiceProvider.GetService>(); - var model = message.FromJson(); //delete entity where entity.Id!=model.Id if (predicate != null) diff --git a/projects/IoT.Shared/Services/OpenApiService.cs b/projects/IoT.Shared/Services/OpenApiService.cs index 1f93dc51..19589ad7 100644 --- a/projects/IoT.Shared/Services/OpenApiService.cs +++ b/projects/IoT.Shared/Services/OpenApiService.cs @@ -10,7 +10,7 @@ namespace IoT.Shared.Services { public static void UpdateApi(IoTProduct product) { - if (product == null || string.IsNullOrEmpty(product.ApiJson)) + if (product == null || string.IsNullOrEmpty(product.ApiJson)||product.IoTApis.Any()) { return; } diff --git a/projects/IoTNode/DeviceServices/BaseDeviceService.cs b/projects/IoTNode/DeviceServices/BaseDeviceService.cs index fe51a6f6..a3473c20 100644 --- a/projects/IoTNode/DeviceServices/BaseDeviceService.cs +++ b/projects/IoTNode/DeviceServices/BaseDeviceService.cs @@ -125,7 +125,7 @@ namespace IoTNode.DeviceServices var response = httpClient.GetStringAsync(url).Result.FromJson(); if (!response) { - scope.ServiceProvider.GetRequiredService().ClientToServer(Methods.UpdateProductResponse, product, null); + scope.ServiceProvider.GetRequiredService().ClientToServer($"{nameof(IoTProduct)}EntityUpdated", product, null); } } catch (Exception ex) diff --git a/projects/IoTNode/DeviceServices/SerialPort/SerialPortService.cs b/projects/IoTNode/DeviceServices/SerialPort/SerialPortService.cs index faa926b7..fe05a2b0 100644 --- a/projects/IoTNode/DeviceServices/SerialPort/SerialPortService.cs +++ b/projects/IoTNode/DeviceServices/SerialPort/SerialPortService.cs @@ -83,6 +83,7 @@ namespace IoTNode.DeviceServices.SerialPortManager public void Notify() { + //上传产品信息 this.UpdateProduct("串口控制器", _productNumber, "/SerialPort/", "serialport"); using var scope = _applicationServices.CreateScope(); var repo = scope.ServiceProvider.GetService>(); @@ -108,17 +109,17 @@ namespace IoTNode.DeviceServices.SerialPortManager }; repo.Add(device); repo.SaveChanges(); - var buttons = new List() { - new SPCommandModel { - Name = "测试", - PortName = "/dev/ttyS0", - BaudRate = 9600, - Parity = 0, - DataBits = 8, - StopBits = 1, - Message = "0123456789ABCDEF" - } - }.ToJson(true); + //var buttons = new List() { + // new SPCommandModel { + // Name = "测试", + // PortName = "/dev/ttyS0", + // BaudRate = 9600, + // Parity = 0, + // DataBits = 8, + // StopBits = 1, + // Message = "0123456789ABCDEF" + // } + //}.ToJson(true); var data = device.Data.FirstOrDefault(o => o.Key == "Buttons"); if(data==null) { @@ -128,12 +129,12 @@ namespace IoTNode.DeviceServices.SerialPortManager DataType= IoTDataType.Data, ValueType= IoTValueType.Array, ValueSchema= JsonSchema.FromType( typeof(SPCommandModel)).ToJson(), - Value=buttons.ToJson(true), + Value= new List().ToJson(true), Timestamp= DateTimeOffset.Now.ToUnixTimeMilliseconds() }; device.Data.Add(data); + repo.SaveChanges(); } - repo.SaveChanges(); } } } diff --git a/projects/IoTNode/Services/IoTNodeClient.cs b/projects/IoTNode/Services/IoTNodeClient.cs index b88b25b3..e2ba04bb 100644 --- a/projects/IoTNode/Services/IoTNodeClient.cs +++ b/projects/IoTNode/Services/IoTNodeClient.cs @@ -212,16 +212,16 @@ namespace IoT.Shared.Services public void OnConnected() { //上传网关 - this.UpdateEntityIdList(null, Methods.UpdateNodeResponse); + this.UpdateEntityIdList(null, $"{nameof(IoTGateway)}EntityUpdated"); //上传产品 - this.UpdateEntityIdList(null, Methods.UpdateProductResponse); + this.UpdateEntityIdList(null, $"{nameof(IoTProduct)}EntityUpdated"); //上传设备Id列表、设备 - this.UpdateEntityIdList(Methods.UpdateDeviceIdListResponse, Methods.UpdateDeviceResponse); + this.UpdateEntityIdList(Methods.UpdateDeviceIdList, $"{nameof(IoTDevice)}EntityUpdated"); //上传数据 - this.UpdateEntityIdList(null, Methods.UpdateDataResponse, null, o => !o.Hidden); + this.UpdateEntityIdList(null, $"{nameof(IoTData)}EntityUpdated", null, o => !o.Hidden); } 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)}")//服务端编辑设备 { var model = message.FromJson(); - //if node category not had,get category from serever by http sync dataService.Edit(model); this.ClientToServer(method, model, null); } @@ -259,13 +258,13 @@ namespace IoT.Shared.Services dataService.Delete(model); 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(); dataService.Edit(model); this.ClientToServer(method, model, null); } - else if (method == $"Delete{nameof(IoTData)}")//服务端删除数据 + else if (method == $"{nameof(IoTData)}EntityDeleted")//服务端删除数据 { var model = message.FromJson(); dataService.Delete(model); diff --git a/projects/IoTNode/Services/IoTNodeEventHandler.cs b/projects/IoTNode/Services/IoTNodeEventHandler.cs index f442ef0a..926dde19 100644 --- a/projects/IoTNode/Services/IoTNodeEventHandler.cs +++ b/projects/IoTNode/Services/IoTNodeEventHandler.cs @@ -1,20 +1,15 @@ -using Infrastructure.Application.Entites.Settings; -using Infrastructure.Data; +using Application.Domain.Entities; +using Infrastructure.Application.Entites.Settings; using Infrastructure.Events; using Infrastructure.Extensions; +using Infrastructure.Web.SignalR; +using IoT.Shared.Application.Models; using IoT.Shared.Services; using IoTNode.DeviceServices.Onvif; -using Application.Domain.Entities; -using Jint; -using Jint.Native; -using Microsoft.EntityFrameworkCore; +using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Logging; using System; -using System.Linq; using System.Threading.Tasks; -using Microsoft.AspNetCore.SignalR; -using Infrastructure.Web.SignalR; -using IoT.Shared.Application.Models; namespace IoTNode.Services { @@ -22,7 +17,11 @@ namespace IoTNode.Services IEventHander>, IEventHander>, IEventHander>, - IEventHander> + IEventHander>, + IEventHander>, + IEventHander>, + IEventHander> + { private readonly ILogger _logger; private readonly IHubContext _hub; @@ -72,6 +71,22 @@ namespace IoTNode.Services { this.Notify(message); } + + public void Handle(EntityInsertedEvent message) + { + this.Upload(message); + } + + public void Handle(EntityUpdatedEvent message) + { + this.Upload(message); + } + + public void Handle(EntityDeletedEvent message) + { + this.Upload(message); + } + private void Notify(BaseEvent message) { Task.Run(() => @@ -86,5 +101,20 @@ namespace IoTNode.Services } }); } + + private void Upload(BaseEvent message) + { + Task.Run(() => + { + try + { + this._ioTNodeClient.ClientToServer($"{typeof(T).Name}{message.Arg}",message.Data,null); + } + catch (Exception ex) + { + ex.PrintStack(); + } + }); + } } } diff --git a/projects/IoTNode/Views/Home/Index.cshtml b/projects/IoTNode/Views/Home/Index.cshtml index addbf3f2..c8cfcb32 100644 --- a/projects/IoTNode/Views/Home/Index.cshtml +++ b/projects/IoTNode/Views/Home/Index.cshtml @@ -17,7 +17,7 @@