From a713ba59b53354399e5491307e6746a7002d472a Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Mon, 24 Jun 2019 13:24:05 +0800 Subject: [PATCH] update Former-commit-id: 9faebbd0ea03bdc92a1f37c16c83c304c1eedead --- .../Extensions/ByteExtensions.cs | 7 + .../Application/Domain/Entities/FBeeDevice.cs | 25 -- .../Application/Domain/Entities/Gateway.cs | 15 + .../Domain/Entities/MessageType.cs | 9 +- .../Infrastructure/DeviceService.cs | 333 +++++++++++++----- .../FBeeService/Views/Home/Index.cshtml | 17 +- 6 files changed, 280 insertions(+), 126 deletions(-) diff --git a/projects/Infrastructure/Extensions/ByteExtensions.cs b/projects/Infrastructure/Extensions/ByteExtensions.cs index dedb9066..aca101c5 100644 --- a/projects/Infrastructure/Extensions/ByteExtensions.cs +++ b/projects/Infrastructure/Extensions/ByteExtensions.cs @@ -24,6 +24,13 @@ namespace Infrastructure.Extensions return BitConverter.ToInt16(buffer); } + public static string ReadIntString(this Stream bytes, int length) + { + var buffer = new byte[length]; + bytes.Read(buffer); + return string.Concat(buffer.Select(o => o.ToString())); + } + public static string ReadASIIString(this Stream bytes, int length) { var buffer = new byte[length]; diff --git a/projects/IoT/IoTServices/FBeeService/Application/Domain/Entities/FBeeDevice.cs b/projects/IoT/IoTServices/FBeeService/Application/Domain/Entities/FBeeDevice.cs index 107b5917..93514c3b 100644 --- a/projects/IoT/IoTServices/FBeeService/Application/Domain/Entities/FBeeDevice.cs +++ b/projects/IoT/IoTServices/FBeeService/Application/Domain/Entities/FBeeDevice.cs @@ -34,30 +34,5 @@ namespace Application.Domain.Entities public int CategoryNumber { get; set; } public string CategoryName { get; set; } public string EName { get; set; } - - public void Update(byte[] data) - { - using (var ms = new MemoryStream(data)) - { - this.DataType = ms.ReadByte(); - this.DataLength = ms.ReadByte(); - this.Address = ms.ReadHexString(2); - this.Endpoint = ms.ReadByte(); - this.ProfileId = ms.ReadInt(); - this.DeviceId = ms.ReadInt(); - this.Status = ms.ReadByte(); - this.NameLength = ms.ReadByte(); - this.CName = ms.ReadASIIString(this.NameLength); - this.Online = ms.ReadByte(); - this.IEEE = ms.ReadHexString(8); - this.SNLength = ms.ReadByte(); - this.RawSN = ms.ReadHexString(this.SNLength); - this.ZoneType = ms.ReadByte(); - this.Power = ms.ReadByte(); - this.Data = ms.ReadHexString(4); - this.Safe = ms.ReadHexString(2); - this.RawValue = BitConverter.ToString(data); - } - } } } \ No newline at end of file diff --git a/projects/IoT/IoTServices/FBeeService/Application/Domain/Entities/Gateway.cs b/projects/IoT/IoTServices/FBeeService/Application/Domain/Entities/Gateway.cs index f76af492..b0e426d7 100644 --- a/projects/IoT/IoTServices/FBeeService/Application/Domain/Entities/Gateway.cs +++ b/projects/IoT/IoTServices/FBeeService/Application/Domain/Entities/Gateway.cs @@ -6,7 +6,22 @@ namespace Application.Domain.Entities { public string Sn { get; set; } public string Ip { get; set; } + + // + public string Version { get; set; } + + public string UserName { get; internal set; } + public string Password { get; internal set; } + public int DeviceCount { get; internal set; } + public int GroupCount { get; internal set; } + public int TimerCount { get; internal set; } + public int SceneCount { get; internal set; } + public int TaskCount { get; internal set; } + public string CompileVersion { get; internal set; } + + // public string NodeName { get; set; } + public bool Enable { get; set; } } } \ No newline at end of file diff --git a/projects/IoT/IoTServices/FBeeService/Application/Domain/Entities/MessageType.cs b/projects/IoT/IoTServices/FBeeService/Application/Domain/Entities/MessageType.cs index e76d1e84..3490701b 100644 --- a/projects/IoT/IoTServices/FBeeService/Application/Domain/Entities/MessageType.cs +++ b/projects/IoT/IoTServices/FBeeService/Application/Domain/Entities/MessageType.cs @@ -2,9 +2,10 @@ { public class MessageType { - public const int DeviceResponse = 0x01; - public const int DevicePowerResponse = 0x07; - public const int DeviceNotify = 0x70; - public const int DeviceNotiryExt = 0x72; + public const int x01 = 0x01; + public const int x07 = 0x07; + public const int x70 = 0x70; + public const int x72 = 0x72; + public const int x15 = 0x15; } } \ 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 29766a92..28d9f65e 100644 --- a/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs +++ b/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs @@ -86,6 +86,10 @@ namespace FBeeService #region api + /// + /// 0x81获取当前连接所有设备 + /// + /// public void X81(string sn) { Console.WriteLine($"refresh {sn}"); @@ -97,6 +101,62 @@ namespace FBeeService this.Write(sn, list.ToArray()); } + private void X01(string sn, byte[] data) + { + using (var ms = new MemoryStream(data)) + { + var responseType = ms.ReadByte(); + var dataLength = ms.ReadByte(); + var address = ms.ReadHexString(2); + var endpoint = ms.ReadByte(); + using (var scope = _applicationServices.CreateScope()) + { + var profileId = ms.ReadInt(); + var deviceId = ms.ReadInt(); + var deviceRepo = scope.ServiceProvider.GetService>(); + var device = deviceRepo.Table().FirstOrDefault(o => o.Sn == sn && o.Address == address); + if (device == null) + { + var deviceType = DeviceId.List.FirstOrDefault(o => o.RawDeviceId == deviceId); + device = new FBeeDevice + { + Sn = sn, + Name = deviceType.Name, + EName = deviceType.EName, + Icon = deviceType.Icon, + CategoryNumber = deviceType.RawDeviceId, + CategoryName = deviceType.Category + }; + deviceRepo.Add(device); + } + device.DataType = responseType; + device.DataLength = dataLength; + device.Address = address; + device.Endpoint = endpoint; + device.ProfileId = profileId; + device.DeviceId = deviceId; + device.Status = ms.ReadByte(); + device.NameLength = ms.ReadByte(); + device.CName = ms.ReadASIIString(device.NameLength); + device.Online = ms.ReadByte(); + device.IEEE = ms.ReadHexString(8); + device.SNLength = ms.ReadByte(); + device.RawSN = ms.ReadHexString(device.SNLength); + device.ZoneType = ms.ReadByte(); + device.Power = ms.ReadByte(); + device.Data = ms.ReadHexString(4); + device.Safe = ms.ReadHexString(2); + device.RawValue = BitConverter.ToString(data); + deviceRepo.SaveChanges(); + } + } + } + + /// + /// 0x95删除指定设备 + /// + /// + /// public void X95(string sn, string ieee) { Console.WriteLine($"delete {ieee} from {sn}"); @@ -119,6 +179,13 @@ namespace FBeeService } } + //0x94更改指定设备名 + /// + /// 0x82设置指定设备的开关状态 + /// + /// + /// + /// public void X82(string sn, string ieee, byte status) { using (var scope = _applicationServices.CreateScope()) @@ -139,12 +206,104 @@ namespace FBeeService list.Add(0x01); list.AddRange(new byte[] { 0x00, 0x00 }); list.Add(status); - var client = this.Clients[sn]; this.Write(sn, list.ToArray()); } } } + //0x83 + //0x84 + //0x85 + //0x86 + //0x87 + //0x88 + //0x8d + //0x96 + //0x8e + //0x8f + //0x97 + //0x98 + //0x90 + //0x91 + //0x92 + //0x93 + //0x8a + //0x8b + //0x8c + //0x99 + //0x9a + //0x9b + //0x9c + //0x9d获取网关信息 + public void X9d(string sn) + { + var list = new List() { 0x08, 0x00 }; + list.AddRange(sn.HexToBytes().Reverse()); + list.Add(0xfe); + list.Add(0x9d); + this.Write(sn, list.ToArray()); + } + + private void X15(string sn, byte[] data) + { + using (var ms = new MemoryStream(data)) + { + var responseType = ms.ReadByte(); + var dataLength = ms.ReadByte(); + var version = ms.ReadASIIString(5); + var snid = ms.ReadHexString(4); + using (var scope = _applicationServices.CreateScope()) + { + var gatewayRepo = scope.ServiceProvider.GetService>(); + var gateway = gatewayRepo.Table().FirstOrDefault(o => o.Sn == sn); + if (gateway == null) + { + Console.WriteLine($"gateway {sn} hasn't save in database"); + } + else + { + gateway.Version = version; + gateway.UserName = ms.ReadASIIString(20); + gateway.Password = ms.ReadASIIString(20); + gateway.DeviceCount = ms.ReadByte(); + gateway.GroupCount = ms.ReadByte(); + gateway.TimerCount = ms.ReadByte(); + gateway.SceneCount = ms.ReadByte(); + gateway.TaskCount = ms.ReadByte(); + var hex = ms.ReadHexString(5); + gateway.CompileVersion = ms.ReadHexString(5); + } + gatewayRepo.SaveChanges(); + } + } + } + + //0x9e + //0x9f + //0xa0 + //0xa1 + //0xa2 + //0xa3 + //0xa4 + //0xa5 + //0xa8 + //0xa9 + //0xab + //0xa7 + //0xac + //0xaf + //0xb0 + //0x70 + //0x72 + //0xb1 + //0xb2 + //0xc1 + //0xc2 + //0xc3 + //0xc4 + //0xc5 + //0x75 + #endregion api private void CheckConnection() @@ -292,6 +451,7 @@ namespace FBeeService } } }).Start(); + this.X9d(client.Sn); } catch (Exception ex) { @@ -315,103 +475,84 @@ namespace FBeeService private void HandleInternal(string sn, byte[] data) { Console.WriteLine($"read:{BitConverter.ToString(data).Replace("-", " ")}"); - using (var ms = new MemoryStream(data)) + var responseType = data[0]; + if (responseType == MessageType.x15) { - var responseType = ms.ReadByte(); - var dataLength = ms.ReadByte(); - var address = ms.ReadHexString(2); - var endpoint = ms.ReadByte(); - using (var scope = _applicationServices.CreateScope()) - { - var deviceRepo = scope.ServiceProvider.GetService>(); - var device = deviceRepo.Table().FirstOrDefault(o => o.Address == address); - if (device == null) - { - Console.WriteLine($"{address} hasn't save in database"); - } - if (responseType == MessageType.DeviceResponse)//获取设备返回值 - { - var profileId = ms.ReadInt(); - var deviceId = ms.ReadInt(); - var deviceType = DeviceId.List.FirstOrDefault(o => o.RawDeviceId == deviceId); - if (deviceType == null) - { - Console.WriteLine($"{deviceId} hasn't config in database"); - } - else - { - if (device == null) - { - device = new FBeeDevice - { - Sn = sn, - Name = deviceType.Name, - EName = deviceType.EName, - Icon = deviceType.Icon, - CategoryNumber = deviceType.RawDeviceId, - CategoryName = deviceType.Category - }; - deviceRepo.Add(device); - } - device.Update(data); - } - } - else if (responseType == MessageType.DeviceNotify || responseType == MessageType.DeviceNotiryExt)//设备上报 - { - var clusterId = ms.ReadInt(); - if (device != null) - { - var reportCount = ms.ReadByte(); - var props = new Dictionary(); - for (int i = 0; i < reportCount; i++) - { - var propId = ms.ReadInt(); - var propDataTypeValue = ms.ReadByte(); - int propDataLength; - if (Enum.IsDefined(typeof(DataType), propDataTypeValue)) - { - var propDataType = (DataType)propDataTypeValue; - if (propDataType == DataType.bitstring || propDataType == DataType.characterstring) - { - propDataLength = ms.ReadByte(); - } - else if (propDataType == DataType.longbitstring || propDataType == DataType.longcharacterstring) - { - propDataLength = ms.ReadInt(); - } - else if (propDataType == DataType.sequence || propDataType == DataType.set || propDataType == DataType.bag) - { - propDataLength = ms.ReadInt(); - } - else if (propDataType == DataType.unknown) - { - propDataLength = 0; - } - else - { - propDataLength = Convert.ToInt32(Regex.Match(propDataType.GetName(), @"\d+").Groups[1].Value); - } - } - else - { - propDataLength = 1; - } - var propData = ms.Read(propDataLength); - props.Add(propId, propData); - } - } - } - else if (responseType == MessageType.DevicePowerResponse) - { - var powerStatus = ms.ReadByte(); - if (device != null) - { - device.Power = powerStatus; - } - } - deviceRepo.SaveChanges(); - } + this.X15(sn, data); + } + else if (responseType == MessageType.x01) + { + this.X01(sn, data); } + //using (var ms = new MemoryStream(data)) + //{ + // var dataLength = ms.ReadByte(); + // var address = ms.ReadHexString(2); + // var endpoint = ms.ReadByte(); + // using (var scope = _applicationServices.CreateScope()) + // { + // var deviceRepo = scope.ServiceProvider.GetService>(); + // var device = deviceRepo.Table().FirstOrDefault(o => o.Address == address); + // if (device == null) + // { + // Console.WriteLine($"device {address} hasn't save in database"); + // } + // else if (responseType == MessageType.x70 || responseType == MessageType.x72)//设备上报 + // { + // var clusterId = ms.ReadInt(); + // if (device != null) + // { + // var reportCount = ms.ReadByte(); + // var props = new Dictionary(); + // for (int i = 0; i < reportCount; i++) + // { + // var propId = ms.ReadInt(); + // var propDataTypeValue = ms.ReadByte(); + // int propDataLength; + // if (Enum.IsDefined(typeof(DataType), propDataTypeValue)) + // { + // var propDataType = (DataType)propDataTypeValue; + // if (propDataType == DataType.bitstring || propDataType == DataType.characterstring) + // { + // propDataLength = ms.ReadByte(); + // } + // else if (propDataType == DataType.longbitstring || propDataType == DataType.longcharacterstring) + // { + // propDataLength = ms.ReadInt(); + // } + // else if (propDataType == DataType.sequence || propDataType == DataType.set || propDataType == DataType.bag) + // { + // propDataLength = ms.ReadInt(); + // } + // else if (propDataType == DataType.unknown) + // { + // propDataLength = 0; + // } + // else + // { + // propDataLength = Convert.ToInt32(Regex.Match(propDataType.GetName(), @"\d+").Groups[1].Value); + // } + // } + // else + // { + // propDataLength = 1; + // } + // var propData = ms.Read(propDataLength); + // props.Add(propId, propData); + // } + // } + // } + // else if (responseType == MessageType.x07) + // { + // var powerStatus = ms.ReadByte(); + // if (device != null) + // { + // device.Power = powerStatus; + // } + // } + // deviceRepo.SaveChanges(); + // } + //} } private byte[] Command(byte[] data) diff --git a/projects/IoT/IoTServices/FBeeService/Views/Home/Index.cshtml b/projects/IoT/IoTServices/FBeeService/Views/Home/Index.cshtml index 3db082bd..03af299e 100644 --- a/projects/IoT/IoTServices/FBeeService/Views/Home/Index.cshtml +++ b/projects/IoT/IoTServices/FBeeService/Views/Home/Index.cshtml @@ -2,6 +2,10 @@ @{ ViewBag.HideBread = true; } +
@@ -9,7 +13,12 @@ - + + + + + + @foreach (var item in Model) @@ -19,6 +28,12 @@ + + + + + +
Ip地址 节点 推送查询所有网关版本用户名密码设备编译版本查询网关设备 查看
@item.Ip @item.NodeName @Html.DisplayFor(o => item.Enable)@item.Version@item.UserName@item.Password@item.DeviceCount@item.CompileVersion查询网关信息 查询网关设备 查看设备