diff --git a/projects/Infrastructure/Extensions/ObjectMapperExtensions.cs b/projects/Infrastructure/Extensions/ObjectMapperExtensions.cs index 9465a672..6fd09751 100644 --- a/projects/Infrastructure/Extensions/ObjectMapperExtensions.cs +++ b/projects/Infrastructure/Extensions/ObjectMapperExtensions.cs @@ -31,6 +31,16 @@ namespace Infrastructure.Extensions { return (T)(Activator.CreateInstance(typeof(T)).InjectFrom(source)); } + + public static T Update(this T target, object source) + { + return (T)target.InjectFrom(new PlainInjection(null), source); + } + + public static T UpdateWithoutId(this T target, object source) + { + return (T)target.InjectFrom(new PlainInjection("Id"), source); + } } public class NullableInjection : LoopInjection @@ -86,15 +96,11 @@ namespace Infrastructure.Extensions { return false; } - return sourceType == targetType; + return base.MatchTypes(sourceType, targetType); } protected override void SetValue(object source, object target, PropertyInfo sp, PropertyInfo tp) { - if (sp.Name == "RowVersion" || sp.Name == "UpdatedOn") - { - return; - } if (this._skip != null && this._skip.Length > 0) { if (this._skip.Contains(sp.Name)) diff --git a/projects/Infrastructure/Web/SignalR/BasePageHub.cs b/projects/Infrastructure/Web/SignalR/BasePageHub.cs index 33a8a6d3..c1924c54 100644 --- a/projects/Infrastructure/Web/SignalR/BasePageHub.cs +++ b/projects/Infrastructure/Web/SignalR/BasePageHub.cs @@ -30,7 +30,7 @@ namespace Infrastructure.Web.SignalR Clients.Group(group).SendAsync(method, message, fromConnectinId); } - public virtual void OnClientToServer(string method, string message, string connectionId) + public virtual void OnClientToServer(string method, string message, string fromConnectinId) { } } diff --git a/projects/IoT/IoT.Shared/Application/Domain/Entities/Category.cs b/projects/IoT/IoT.Shared/Application/Domain/Entities/Category.cs new file mode 100644 index 00000000..37a30afc --- /dev/null +++ b/projects/IoT/IoT.Shared/Application/Domain/Entities/Category.cs @@ -0,0 +1,12 @@ +using Infrastructure.Domain; +using System.Collections.Generic; + +namespace Application.Domain.Entities +{ + public class Category : BaseEntity + { + public string Name { get; set; } + public string Number { get; set; } + public List Devices { get; set; } = new List(); + } +} \ No newline at end of file diff --git a/projects/IoT/IoT.Shared/Application/Domain/Entities/Device.cs b/projects/IoT/IoT.Shared/Application/Domain/Entities/Device.cs index e8339301..a292000b 100644 --- a/projects/IoT/IoT.Shared/Application/Domain/Entities/Device.cs +++ b/projects/IoT/IoT.Shared/Application/Domain/Entities/Device.cs @@ -57,6 +57,12 @@ namespace Application.Domain.Entities [Display(Name = "型号")] public string InfoNumber { get; set; } + [Display(Name = "分类Id")] + public Guid? CategoryId { get; set; } + + [Display(Name = "分类")] + public Category Category { get; set; } + [Display(Name = "节点Id")] public Guid? NodeId { get; set; } diff --git a/projects/IoT/IoT.Shared/Infrastructure/BaseClientService.cs b/projects/IoT/IoT.Shared/Infrastructure/BaseClientService.cs index a5c73d59..7661e784 100644 --- a/projects/IoT/IoT.Shared/Infrastructure/BaseClientService.cs +++ b/projects/IoT/IoT.Shared/Infrastructure/BaseClientService.cs @@ -126,13 +126,13 @@ namespace IoT.Shared.Infrastructure { } - public void ClientToServer(string method, string message) + public void ClientToServer(string method, string message, string fromConnectionId = null) { Task.Run(() => { try { - this.Connection.SendAsync("ClientToServer", method, message); + this.Connection.SendAsync("OnClientToServer", method, message, fromConnectionId); } catch (Exception ex) { diff --git a/projects/IoT/IoT.Shared/IoTServiceStartup.cs b/projects/IoT/IoT.Shared/IoTServiceStartup.cs index 60798e36..cedf7902 100644 --- a/projects/IoT/IoT.Shared/IoTServiceStartup.cs +++ b/projects/IoT/IoT.Shared/IoTServiceStartup.cs @@ -82,17 +82,14 @@ namespace IoT.UI.Shard modelBuilder.Entity().HasIndex(o => new { o.RoleId, o.PermissionId }).IsUnique(); // modelBuilder.Entity().HasIndex(o => o.Number).IsUnique(); + modelBuilder.Entity().HasIndex(o => o.Number).IsUnique(); modelBuilder.Entity().HasIndex(o => o.Number).IsUnique(); modelBuilder.Entity().HasOne(o => o.Info).WithMany(o => o.Apis).HasForeignKey(o => o.InfoId); + modelBuilder.Entity().HasOne(o => o.Category).WithMany(o => o.Devices).HasForeignKey(o => o.CategoryId); + modelBuilder.Entity().HasOne(o => o.Info).WithMany(o => o.Devices).HasForeignKey(o => o.InfoId); modelBuilder.Entity().HasOne(o => o.Node).WithMany(o => o.Devices).HasForeignKey(o => o.NodeId); modelBuilder.Entity().HasOne(o => o.Device).WithMany(o => o.Data).HasForeignKey(o => o.DeviceId); modelBuilder.Entity().HasOne(o => o.Api).WithMany(o => o.Parameters).HasForeignKey(o => o.ApiId); - //modelBuilder.Entity().HasOne(o => o.Node).WithMany(o => o.Sences).HasForeignKey(o => o.NodeId); - //modelBuilder.Entity().HasOne(o => o.Sence).WithMany(o => o.Commands).HasForeignKey(o => o.SenceId); - //modelBuilder.Entity().HasIndex(o => o.Number).IsUnique(); - //modelBuilder.Entity().HasIndex(o => new { o.DeviceId, o.Key }).IsUnique(); - //modelBuilder.Entity().HasIndex(o => new { o.DeviceId, o.Path, o.Command }).IsUnique(); - //modelBuilder.Entity().HasIndex(o => new { o.ApiId, o.Name }).IsUnique(); } public override void Seed(DbContext dbContext, IServiceProvider serviceProvider, IConfiguration configuration) @@ -156,6 +153,12 @@ namespace IoT.UI.Shard UserRoles = new List { new UserRole { Role = adminRole } } }); dbContext.SaveChanges(); + dbContext.Set().Add(new Category { Number = "00", Name = "网关" }); + dbContext.Set().Add(new Category { Number = "10", Name = "安防" }); + dbContext.Set().Add(new Category { Number = "20", Name = "电器" }); + dbContext.Set().Add(new Category { Number = "30", Name = "照明" }); + dbContext.Set().Add(new Category { Number = "40", Name = "监测" }); + dbContext.SaveChanges(); } } } \ No newline at end of file diff --git a/projects/IoT/IoTNode/Startup.cs b/projects/IoT/IoTNode/Startup.cs index 45b69351..6ae5ca28 100644 --- a/projects/IoT/IoTNode/Startup.cs +++ b/projects/IoT/IoTNode/Startup.cs @@ -104,6 +104,7 @@ namespace IoTNode modelBuilder.Entity().HasIndex(o => new { o.UserId, o.RoleId }).IsUnique(); modelBuilder.Entity().HasIndex(o => new { o.RoleId, o.PermissionId }).IsUnique(); // + modelBuilder.Entity().HasOne(o => o.Node).WithMany(o => o.Devices).HasForeignKey(o => o.NodeId); modelBuilder.Entity().HasOne(o => o.Node).WithMany(o => o.Devices).HasForeignKey(o => o.NodeId); modelBuilder.Entity().HasOne(o => o.Device).WithMany(o => o.Data).HasForeignKey(o => o.DeviceId); modelBuilder.Entity().HasOne(o => o.Device).WithMany(o => o.Apis).HasForeignKey(o => o.DeviceId); diff --git a/projects/IoT/IoTServices/FBeeService/Infrastructure/ClientService.cs b/projects/IoT/IoTServices/FBeeService/Infrastructure/ClientService.cs index 450992ea..3ca4e759 100644 --- a/projects/IoT/IoTServices/FBeeService/Infrastructure/ClientService.cs +++ b/projects/IoT/IoTServices/FBeeService/Infrastructure/ClientService.cs @@ -24,16 +24,34 @@ namespace FBeeService { var deviceInfoRepo = scope.ServiceProvider.GetService>(); var deviceInfos = deviceInfoRepo.ReadOnlyTable().ToList(); - foreach (var item in deviceInfos) + foreach (var deviceInfo in deviceInfos) { - this.ClientToServer("UpdateDeviceInfo", item.ToJson()); + this.SendDeviceInfo(deviceInfo); } var deviceRepo = scope.ServiceProvider.GetService>(); var devices = deviceRepo.ReadOnlyTable().Include(o => o.Data).ToList(); - foreach (var item in devices) + foreach (var device in devices) { - this.ClientToServer("UpdateDevice", item.ToJson()); + this.SendDevice(device); + } + } + } + + public override void OnServerToClient(string method, string message, string fromConnectionId) + { + if (method == "GetDeviceInfo") + { + var infoNumber = message; + using (var scope = this.applicationServices.CreateScope()) + { + var deviceInfoRepo = scope.ServiceProvider.GetService>(); + var deviceInfo = deviceInfoRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == infoNumber); + if (deviceInfo == null) + { + throw new Exception($"not has infoNumber:{infoNumber}"); + } + this.SendDeviceInfo(deviceInfo); } } } @@ -45,7 +63,7 @@ namespace FBeeService try { Console.WriteLine("send device to server"); - this.Connection.SendAsync("ClientToServer", "UpdateDevice", device.ToJson(), null); + this.ClientToServer("UpdateDevice", device.ToJson()); } catch (Exception ex) { @@ -54,22 +72,20 @@ namespace FBeeService }); } - public override void OnServerToClient(string method, string message, string fromConnectionId) + public void SendDeviceInfo(DeviceInfo deviceInfo) { - if (method == "GetDeviceInfo") + Task.Run(() => { - var infoNumber = message; - using (var scope = this.applicationServices.CreateScope()) + try { - var deviceInfoRepo = scope.ServiceProvider.GetService>(); - var deviceInfo = deviceInfoRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == infoNumber); - if (deviceInfo == null) - { - throw new Exception($"not has infoNumber:{infoNumber}"); - } + Console.WriteLine("send device to server"); this.ClientToServer("UpdateDeviceInfo", deviceInfo.ToJson()); } - } + catch (Exception ex) + { + ex.PrintStack(); + } + }); } //public override void ConnectionOn() diff --git a/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs b/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs index f17411df..ebd16860 100644 --- a/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs +++ b/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs @@ -148,6 +148,7 @@ namespace FBeeService }; deviceInfoRepo.Add(deviceInfo); deviceInfoRepo.SaveChanges(); + this.SendDeviceInfo(deviceInfo); } var deviceRepo = scope.ServiceProvider.GetService>(); foreach (var result in list) @@ -163,12 +164,16 @@ namespace FBeeService Number = sn, Name = "网关", Enable = true, + Icon = "gateway", + CategoryNumber = "0", + InfoNumber = deviceInfoNumber, InfoId = deviceInfo.Id }; deviceRepo.Add(device); } device.Ip = ip; deviceRepo.SaveChanges(); + this.SendDevice(device); } var gateways = deviceRepo.ReadOnlyTable().Where(o => o.Info.DeviceType == DeviceType.Gateway).ToList(); foreach (var gateway in gateways) @@ -500,10 +505,10 @@ namespace FBeeService } deviceInfoRepo.Add(deviceInfo); deviceInfoRepo.SaveChanges(); + this.SendDeviceInfo(deviceInfo); } var deviceRepo = scope.ServiceProvider.GetService>(); var device = this.GetDevice(deviceRepo, sn, ieee); - var clientService = scope.ServiceProvider.GetService(); if (device == null) { create = true; @@ -542,7 +547,7 @@ namespace FBeeService { this.UpdateStatus(device); } - clientService.SendDevice(device); + this.SendDevice(device); } else { @@ -634,6 +639,7 @@ namespace FBeeService { device.AddorUpdateData("State", ms.ReadInt(), DeviceDataType.Int, "状态"); deviceRepo.SaveChanges(); + this.SendDevice(device); } else { @@ -680,6 +686,7 @@ namespace FBeeService { device.AddorUpdateData(Keys.Brightness, ms.ReadByte(), DeviceDataType.Int, "亮度"); deviceRepo.SaveChanges(); + this.SendDevice(device); } else { @@ -724,6 +731,7 @@ namespace FBeeService { device.AddorUpdateData(Keys.Hue, ms.ReadByte(), DeviceDataType.Int, "色调"); deviceRepo.SaveChanges(); + this.SendDevice(device); } else { @@ -754,6 +762,7 @@ namespace FBeeService { device.AddorUpdateData(Keys.Saturation, ms.ReadByte(), DeviceDataType.Int, "饱和度"); deviceRepo.SaveChanges(); + this.SendDevice(device); } else { @@ -869,6 +878,7 @@ namespace FBeeService var compileVersion = ms.ReadHexString(5); } gatewayRepo.SaveChanges(); + this.SendDevice(gateway); } } } @@ -911,7 +921,9 @@ namespace FBeeService var device = this.GetDeviceByAddress(deviceRepo, sn, address); if (device != null) { - device.AddorUpdateData(Keys.ColorTemperature, ms.ReadInt(), DeviceDataType.Int, "色温"); deviceRepo.SaveChanges(); + device.AddorUpdateData(Keys.ColorTemperature, ms.ReadInt(), DeviceDataType.Int, "色温"); + deviceRepo.SaveChanges(); + this.SendDevice(device); } else { @@ -1248,5 +1260,25 @@ namespace FBeeService return null; } } + + public void SendDevice(Device device) + { + Console.WriteLine("send device to server"); + using (var scope = _applicationServices.CreateScope()) + { + var clientService = scope.ServiceProvider.GetService(); + clientService.SendDevice(device); + } + } + + public void SendDeviceInfo(DeviceInfo deviceInfo) + { + Console.WriteLine("send device to server"); + using (var scope = _applicationServices.CreateScope()) + { + var clientService = scope.ServiceProvider.GetService(); + clientService.SendDeviceInfo(deviceInfo); + }; + } } } \ No newline at end of file diff --git a/projects/IoT/IoTServices/FBeeService/Views/Home/Index.cshtml b/projects/IoT/IoTServices/FBeeService/Views/Home/Index.cshtml index 9931abcc..94a0b40b 100644 --- a/projects/IoT/IoTServices/FBeeService/Views/Home/Index.cshtml +++ b/projects/IoT/IoTServices/FBeeService/Views/Home/Index.cshtml @@ -3,7 +3,7 @@ ViewBag.HideBread = true; }
diff --git a/projects/IoTCenter/Services/PageHub.cs b/projects/IoTCenter/Services/PageHub.cs index a1f53735..5c3375e5 100644 --- a/projects/IoTCenter/Services/PageHub.cs +++ b/projects/IoTCenter/Services/PageHub.cs @@ -43,7 +43,7 @@ namespace IoTCenter.Services var deviceInfo = deviceInfoRepo.Table().FirstOrDefault(o => o.Number == newDeviceInfo.Number); if (deviceInfo == null) { - deviceInfo = new DeviceInfo().From(newDeviceInfo); + deviceInfo = new DeviceInfo().Update(newDeviceInfo); deviceInfoRepo.Add(deviceInfo); if (!string.IsNullOrEmpty(deviceInfo.ApiJson)) { @@ -74,6 +74,19 @@ namespace IoTCenter.Services this.ServerToClient(newDevice.ConnectId, "GetDeviceInfo", newDevice.InfoNumber); throw new Exception("need device info"); } + var categoryRepo = scope.ServiceProvider.GetService>(); + var category = categoryRepo.Table().FirstOrDefault(o => o.Number == newDevice.CategoryNumber); + if (category == null) + { + category = new Category + { + Number = newDevice.CategoryNumber, + Name = newDevice.CategoryNumber + }; + categoryRepo.Add(category); + categoryRepo.SaveChanges(); + } + var nodeRepo = scope.ServiceProvider.GetService>(); var node = nodeRepo.Table().FirstOrDefault(o => o.Number == newDevice.NodeNumber); if (node == null) @@ -90,8 +103,9 @@ namespace IoTCenter.Services var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.Number == newDevice.Number); if (device == null) { - device = new Device().From(newDevice); + device = new Device().Update(newDevice); device.InfoId = deviceInfo.Id; + device.CategoryId = category.Id; device.NodeId = node.Id; deviceRepo.Add(device); } @@ -102,11 +116,11 @@ namespace IoTCenter.Services var data = device.Data.FirstOrDefault(o => o.Key == newData.Key); if (data == null) { - data = new Data().From(data); + data = new Data().Update(data); data.DeviceId = device.Id; device.Data.Add(data); } - data.From(newDevice); + data.UpdateWithoutId(newDevice); data.DeviceId = device.Id; } }