From 9700e86f6bdc8d96eb3a25d3f9dc1af01acfe922 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Wed, 10 Jul 2019 10:14:11 +0800 Subject: [PATCH] update Former-commit-id: 704be4f60256d8c940b6e6235caac3bbb3372e4b --- .../Application/Domain/Entities/Data.cs | 3 + .../Application/Domain/Entities/Device.cs | 22 ++++ .../Infrastructure/DeviceService.cs | 121 +++++++++--------- .../FBeeService/Infrastructure/Keys.cs | 31 +++++ 4 files changed, 115 insertions(+), 62 deletions(-) create mode 100644 projects/IoT/IoTServices/FBeeService/Infrastructure/Keys.cs diff --git a/projects/IoT/IoT.Shared/Application/Domain/Entities/Data.cs b/projects/IoT/IoT.Shared/Application/Domain/Entities/Data.cs index 54e02b40..a5a6286d 100644 --- a/projects/IoT/IoT.Shared/Application/Domain/Entities/Data.cs +++ b/projects/IoT/IoT.Shared/Application/Domain/Entities/Data.cs @@ -33,5 +33,8 @@ namespace Application.Domain.Entities [Display(Name = "设备")] public Device Device { get; set; } + + [Display(Name = "隐藏")] + public bool Hidden { get; set; } } } \ 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 fbeb6fbb..d2f85ee0 100644 --- a/projects/IoT/IoT.Shared/Application/Domain/Entities/Device.cs +++ b/projects/IoT/IoT.Shared/Application/Domain/Entities/Device.cs @@ -2,6 +2,7 @@ using Infrastructure.Domain; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Linq; namespace Application.Domain.Entities { @@ -58,5 +59,26 @@ namespace Application.Domain.Entities [Display(Name = "数据")] public List Data { get; set; } = new List(); + + public Data GetData(string key) + { + return this.Data.FirstOrDefault(o => o.Key == key); + } + + public string GetDataValue(string key) + { + return this.GetData(key)?.Value; + } + + public void AddorUpdateData(string key, object value, DeviceDataType type, string name, string unit = null, string description = null, bool hidden = false) + { + var data = this.Data.FirstOrDefault(o => o.Key == key); + if (data == null) + { + data = new Data { Key = key, Type = type, Name = name, Unit = unit, Description = description, Hidden = hidden }; + this.Data.Add(data); + } + data.Value = Convert.ToString(value); + } } } \ No newline at end of file diff --git a/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs b/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs index 6b7f460d..479749bf 100644 --- a/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs +++ b/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs @@ -313,10 +313,10 @@ namespace FBeeService var payload = new List(); using (var scope = _applicationServices.CreateScope()) { - var repo = scope.ServiceProvider.GetService>(); - var device = repo.ReadOnlyTable().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Number == ieee); - var address = device.Data.FirstOrDefault(o => o.Key == "Address").Value.HexToBytes().Reverse(); - var endPoint = Convert.ToInt32(device.Data.FirstOrDefault(o => o.Key == "EndPoint").Value); + var deviceRepo = scope.ServiceProvider.GetService>(); + var device = this.GetDevice(deviceRepo, sn, ieee); + var address = device.GetDataValue(Keys.Address).HexToBytes().Reverse(); + var endPoint = Convert.ToInt32(device.GetDataValue(Keys.EndPoint)); if (format == 0) { payload.Add((byte)(command.Count() + 1 + 2));//数据总长 @@ -357,21 +357,6 @@ namespace FBeeService this.Send(client, bytes); } - private void AddLengthToData(string sn, byte[] data) - { - this.Clients.TryGetValue(sn, out TcpClientWrapper client); - var length = BitConverter.GetBytes((ushort)(data.Count() + 2)); - if (BitConverter.IsLittleEndian) - { - length.Reverse(); - } - var list = new List(); - list.AddRange(length); - list.AddRange(data); - var bytes = list.ToArray(); - this.Send(client, bytes); - } - private void Send(TcpClientWrapper client, byte[] bytes) { Console.WriteLine($"write:{BitConverter.ToString(bytes).Replace("-", " ")}"); @@ -454,7 +439,7 @@ namespace FBeeService deviceCategoryRepo.SaveChanges(); } var deviceRepo = scope.ServiceProvider.GetService>(); - var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Number == ieee); + var device = this.GetDevice(deviceRepo, sn, ieee); if (device == null) { create = true; @@ -500,21 +485,20 @@ namespace FBeeService device.Name = "3路开关"; } } - this.AddorUpdateData(device, "DeviceId", deviceId, DeviceDataType.Int, "DeviceId"); - this.AddorUpdateData(device, "Address", address, DeviceDataType.String, "地址"); - this.AddorUpdateData(device, "EndPoint", endpoint, DeviceDataType.Int, "EndPoint"); + device.AddorUpdateData(Keys.DeviceId, deviceId, DeviceDataType.Int, Keys.DeviceId, hidden: true); + device.AddorUpdateData(Keys.Address, address, DeviceDataType.String, Keys.Address, hidden: true); + device.AddorUpdateData(Keys.EndPoint, endpoint, DeviceDataType.Int, Keys.EndPoint, hidden: true); if (new int[] { 0x0002, 0x0009, 0x0081, 0x0202, 0x0220, 0x0051 }.Contains(deviceId)) { - this.AddorUpdateData(device, "State", switchState, DeviceDataType.Int, "状态"); + device.AddorUpdateData(Keys.State, switchState, DeviceDataType.Int, "状态"); } var battery = ms.ReadByte(); - this.AddorUpdateData(device, "Battery", battery, DeviceDataType.Int, "Battery"); + device.AddorUpdateData(Keys.Battery, battery, DeviceDataType.Int, Keys.Battery); var epCount = ms.ReadByte(); - this.AddorUpdateData(device, "EndPointCount", battery, DeviceDataType.Int, "EndPoint数量"); - var data2 = ms.ReadHexString(4); - this.AddorUpdateData(device, "Data", data2, DeviceDataType.String, "Data"); - this.AddorUpdateData(device, "RawData", BitConverter.ToString(data), DeviceDataType.String, "RawData"); - device.Description = $"{ieee}/{address}/{deviceId}/{zoneType}"; + device.AddorUpdateData(Keys.EndPointCount, battery, DeviceDataType.Int, Keys.EndPointCount); + var historyData = ms.ReadHexString(4); + device.AddorUpdateData(Keys.Data, historyData, DeviceDataType.String, Keys.Data, hidden: true); + device.AddorUpdateData(Keys.ZoneType, zoneType, DeviceDataType.String, Keys.ZoneType, hidden: true); deviceRepo.SaveChanges(); if (create) { @@ -536,7 +520,7 @@ namespace FBeeService private void UpdateStatus(Device device) { - var deviceId = Convert.ToInt32(device.Data.FirstOrDefault(o => o.Key == "DeviceId").Value); + var deviceId = Convert.ToInt32(device.GetDataValue(Keys.DeviceId)); var sn = device.GatewayNumber; var ieee = device.Number; if (new int[] { 0x0002, 0x0009, 0x0081, 0x0202, 0x0220, 0x0051 }.Contains(deviceId)) @@ -606,10 +590,10 @@ namespace FBeeService using (var scope = _applicationServices.CreateScope()) { var deviceRepo = scope.ServiceProvider.GetService>(); - var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Data.Any(d => d.Key == "Address" && d.Value == address)); + var device = this.GetDeviceByAddress(deviceRepo, sn, address); if (device != null) { - this.AddorUpdateData(device, "State", ms.ReadInt(), DeviceDataType.Int, "状态"); + device.AddorUpdateData("State", ms.ReadInt(), DeviceDataType.Int, "状态"); deviceRepo.SaveChanges(); } else @@ -652,10 +636,10 @@ namespace FBeeService using (var scope = _applicationServices.CreateScope()) { var deviceRepo = scope.ServiceProvider.GetService>(); - var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Data.Any(d => d.Key == "Address" && d.Value == address)); + var device = this.GetDeviceByAddress(deviceRepo, sn, address); if (device != null) { - this.AddorUpdateData(device, "Brightness", ms.ReadByte(), DeviceDataType.Int, "亮度"); + device.AddorUpdateData(Keys.Brightness, ms.ReadByte(), DeviceDataType.Int, "亮度"); deviceRepo.SaveChanges(); } else @@ -696,10 +680,10 @@ namespace FBeeService using (var scope = _applicationServices.CreateScope()) { var deviceRepo = scope.ServiceProvider.GetService>(); - var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Data.Any(d => d.Key == "Address" && d.Value == address)); + var device = this.GetDeviceByAddress(deviceRepo, sn, address); if (device != null) { - this.AddorUpdateData(device, "Hue", ms.ReadByte(), DeviceDataType.Int, "色调"); + device.AddorUpdateData(Keys.Hue, ms.ReadByte(), DeviceDataType.Int, "色调"); deviceRepo.SaveChanges(); } else @@ -726,10 +710,10 @@ namespace FBeeService using (var scope = _applicationServices.CreateScope()) { var deviceRepo = scope.ServiceProvider.GetService>(); - var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Data.Any(d => d.Key == "Address" && d.Value == address)); + var device = this.GetDeviceByAddress(deviceRepo, sn, address); if (device != null) { - this.AddorUpdateData(device, "Saturation", ms.ReadByte(), DeviceDataType.Int, "饱和度"); + device.AddorUpdateData(Keys.Saturation, ms.ReadByte(), DeviceDataType.Int, "饱和度"); deviceRepo.SaveChanges(); } else @@ -835,10 +819,9 @@ namespace FBeeService { gateway.UserName = ms.ReadASIIString(20); gateway.Password = ms.ReadASIIString(20); - gateway.Description = version; - this.AddorUpdateData(gateway, "Version", version, DeviceDataType.String, "版本"); - this.AddorUpdateData(gateway, "DeviceCount", ms.ReadByte(), DeviceDataType.Int, "设备数量"); + gateway.AddorUpdateData(Keys.Version, version, DeviceDataType.String, "版本"); + gateway.AddorUpdateData(Keys.DeviceCount, ms.ReadByte(), DeviceDataType.Int, "设备数量"); ms.ReadByte(); ms.ReadByte(); ms.ReadByte(); @@ -851,12 +834,12 @@ namespace FBeeService } } - private void AddorUpdateData(Device gateway, string key, object value, DeviceDataType type, string name, string unit = null) + private void AddorUpdateData(Device gateway, string key, object value, DeviceDataType type, string name, string unit = null, bool hidden = false) { var data = gateway.Data.FirstOrDefault(o => o.Key == key); if (data == null) { - data = new Data { Key = key, Type = type, Name = name }; + data = new Data { Key = key, Type = type, Name = name, Hidden = hidden }; gateway.Data.Add(data); } data.Value = Convert.ToString(value); @@ -901,10 +884,10 @@ namespace FBeeService using (var scope = _applicationServices.CreateScope()) { var deviceRepo = scope.ServiceProvider.GetService>(); - var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Data.Any(d => d.Key == "Address" && d.Value == address)); + var device = this.GetDeviceByAddress(deviceRepo, sn, address); if (device != null) { - this.AddorUpdateData(device, "ColorTemperature", ms.ReadInt(), DeviceDataType.Int, "色温"); deviceRepo.SaveChanges(); + device.AddorUpdateData(Keys.ColorTemperature, ms.ReadInt(), DeviceDataType.Int, "色温"); deviceRepo.SaveChanges(); } else { @@ -973,7 +956,7 @@ namespace FBeeService { var deviceRepo = scope.ServiceProvider.GetService>(); var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Number == ieee); - var version = device.Data.FirstOrDefault(o => o.Key == "Version").Value; + var version = device.GetDataValue(Keys.Version); list.AddRange(version.HexToBytes()); } list.AddRange(new byte[] { 0x82, 0x00 }); @@ -1012,7 +995,7 @@ namespace FBeeService { var repo = scope.ServiceProvider.GetService>(); var device = repo.ReadOnlyTable().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Number == ieee); - var version = device.Data.FirstOrDefault(o => o.Key == "Version").Value; + var version = device.GetDataValue(Keys.Version); list.AddRange(version.HexToBytes()); } list.AddRange(new byte[] { 0x83, 0x00 }); @@ -1076,8 +1059,8 @@ namespace FBeeService using (var scope = _applicationServices.CreateScope()) { var deviceRepo = scope.ServiceProvider.GetService>(); - var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Data.Any(d => d.Key == "Address" && d.Value == address)); - var deviceId = Convert.ToInt32(device.Data.FirstOrDefault(o => o.Key == "DeviceId").Value); + var device = this.GetDeviceByAddress(deviceRepo, sn, address); + var deviceId = Convert.ToInt32(device.GetDataValue(Keys.DeviceId)); var clusterId = (ClusterId)ms.ReadInt(); if (device != null) { @@ -1141,30 +1124,30 @@ namespace FBeeService } else if (clusterId == ClusterId.light) { - this.AddorUpdateData(device, "Light", BitConverter.ToInt16(props[0x0000]), DeviceDataType.Int, "光照"); + device.AddorUpdateData(Keys.Light, BitConverter.ToInt16(props[0x0000]), DeviceDataType.Int, "光照"); } else if (clusterId == ClusterId.alarm) { - this.AddorUpdateData(device, "Warning", BitConverter.ToInt16(props[0x0080]), DeviceDataType.Int, "警报"); - this.AddorUpdateData(device, "UnderVoltage", new BitArray(props[0x0080])[3], DeviceDataType.Int, "低电量"); + device.AddorUpdateData(Keys.Warning, BitConverter.ToInt16(props[0x0080]), DeviceDataType.Int, "警报"); + device.AddorUpdateData(Keys.UnderVoltage, new BitArray(props[0x0080])[3], DeviceDataType.Int, "低电量"); } else if (clusterId == ClusterId.pm25) { - this.AddorUpdateData(device, "PM25", BitConverter.ToInt16(props[0x0000]), DeviceDataType.Int, "PM2.5"); - this.AddorUpdateData(device, "PM100", BitConverter.ToInt16(props[0x0002]), DeviceDataType.Int, "PM10"); - this.AddorUpdateData(device, "PM10", BitConverter.ToInt16(props[0x0001]), DeviceDataType.Int, "PM1"); + device.AddorUpdateData(Keys.PM25, BitConverter.ToInt16(props[0x0000]), DeviceDataType.Int, "PM2.5"); + device.AddorUpdateData(Keys.PM100, BitConverter.ToInt16(props[0x0002]), DeviceDataType.Int, "PM10"); + device.AddorUpdateData(Keys.PM10, BitConverter.ToInt16(props[0x0001]), DeviceDataType.Int, "PM1"); } else if (clusterId == ClusterId.temperature) { - this.AddorUpdateData(device, "Temperature", BitConverter.ToInt16(props[0x0000]) / 100f, DeviceDataType.Float, "温度"); + device.AddorUpdateData(Keys.Temperature, BitConverter.ToInt16(props[0x0000]) / 100f, DeviceDataType.Float, "温度", "℃"); } else if (clusterId == ClusterId.humidity) { - this.AddorUpdateData(device, "Humidity", BitConverter.ToInt16(props[0x0000]) / 100f, DeviceDataType.Float, "湿度"); + device.AddorUpdateData(Keys.Humidity, BitConverter.ToInt16(props[0x0000]) / 100f, DeviceDataType.Float, "湿度", "RH%"); } else if (clusterId == ClusterId.voltage) { - this.AddorUpdateData(device, "Voltage", props[0x21][0] / 2f, DeviceDataType.Float, "电量"); + device.AddorUpdateData(Keys.Voltage, props[0x21][0] / 2f, DeviceDataType.Float, "电量"); } else if (clusterId == ClusterId.socket) { @@ -1175,7 +1158,7 @@ namespace FBeeService tempBytes.Add(0x00); tempBytes.Add(0x00); var electricity = BitConverter.ToInt64(tempBytes.ToArray()) / 1000f; - this.AddorUpdateData(device, "Electricity", electricity, DeviceDataType.Long, "电量", "kw/h"); + device.AddorUpdateData(Keys.Electricity, electricity, DeviceDataType.Long, "电量", "kw/h"); } } if (deviceId == 0x0163) @@ -1185,7 +1168,7 @@ namespace FBeeService var irdata = props[0x400a]; if (irdata.Length == 10) { - this.AddorUpdateData(device, "Version", BitConverter.ToString((props[0x400a].Skip(3).Take(6).ToArray())).Replace("-", "").ToLower(), DeviceDataType.String, "红外版本"); + device.AddorUpdateData(Keys.Version, BitConverter.ToString((props[0x400a].Skip(3).Take(6).ToArray())).Replace("-", "").ToLower(), DeviceDataType.String, "红外版本"); } else { @@ -1196,7 +1179,7 @@ namespace FBeeService if (irDeviceType == 0x01) { var keyCode = BitConverter.ToInt16(irdata.Skip(12).Take(2).ToArray()); - this.AddorUpdateData(device, "KeyPress", keyCode, DeviceDataType.Int, "按键"); + device.AddorUpdateData(Keys.KeyPress, keyCode, DeviceDataType.Int, "按键"); } } } @@ -1257,5 +1240,19 @@ namespace FBeeService } #endregion notify server + + private Device GetDevice(IRepository deviceRepo, string sn, string ieee) + { + return deviceRepo.Table() + .Include(o => o.Data) + .FirstOrDefault(o => o.GatewayNumber == sn && o.Number == ieee); + } + + private Device GetDeviceByAddress(IRepository deviceRepo, string sn, string address) + { + return deviceRepo.Table() + .Include(o => o.Data) + .FirstOrDefault(o => o.GatewayNumber == sn && o.Data.Any(d => d.Key == Keys.Address && d.Value == address)); + } } } \ No newline at end of file diff --git a/projects/IoT/IoTServices/FBeeService/Infrastructure/Keys.cs b/projects/IoT/IoTServices/FBeeService/Infrastructure/Keys.cs new file mode 100644 index 00000000..5fce733a --- /dev/null +++ b/projects/IoT/IoTServices/FBeeService/Infrastructure/Keys.cs @@ -0,0 +1,31 @@ +namespace FBeeService +{ + public static class Keys + { + public const string DeviceId = "DeviceId"; + public const string Address = "Address"; + public const string EndPoint = "EndPoint"; + public const string Version = "Version"; + public const string Electricity = "Electricity"; + public const string KeyPress = "KeyPress"; + public const string State = "State"; + public const string Battery = "Battery"; + public const string EndPointCount = "EndPointCount"; + public const string Data = "Data"; + public const string ZoneType = "ZoneType"; + public const string Brightness = "Brightness"; + public const string Hue = "Hue"; + public const string Saturation = "Saturation"; + public const string ColorTemperature = "ColorTemperature"; + public const string Light = "Light"; + public const string Warning = "Warning"; + public const string UnderVoltage = "UnderVoltage"; + public const string PM25 = "PM25"; + public const string PM100 = "PM100"; + public const string PM10 = "PM10"; + public const string Temperature = "Temperature"; + public const string Humidity = "Humidity"; + public const string Voltage = "Voltage"; + public const string DeviceCount = "DeviceCount"; + } +} \ No newline at end of file