From 080fb1e9a8c2c8fb7efe88fe1cdcf3bd8f248e5b Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Tue, 2 Jul 2019 13:08:05 +0800 Subject: [PATCH] update Former-commit-id: 401e1d985fd6f003736372f9f508ba8bf341071a --- .../Extensions/ByteExtensions.cs | 7 + .../Infrastructure/DeviceService.cs | 145 ++++++------------ .../FBeeService/Views/Home/Gateway.cshtml | 12 +- 3 files changed, 59 insertions(+), 105 deletions(-) diff --git a/projects/Infrastructure/Extensions/ByteExtensions.cs b/projects/Infrastructure/Extensions/ByteExtensions.cs index aca101c5..8c139c30 100644 --- a/projects/Infrastructure/Extensions/ByteExtensions.cs +++ b/projects/Infrastructure/Extensions/ByteExtensions.cs @@ -45,6 +45,13 @@ namespace Infrastructure.Extensions return BitConverter.ToString(buffer).Replace("-", "").ToLower(); } + public static string ReadHexStringDesc(this Stream bytes, int length) + { + var buffer = new byte[length]; + bytes.Read(buffer); + return BitConverter.ToString(buffer.Reverse().ToArray()).Replace("-", "").ToLower(); + } + public static byte[] Read(this Stream bytes, int length) { var buffer = new byte[length]; diff --git a/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs b/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs index 3400ade6..9ae9794e 100644 --- a/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs +++ b/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs @@ -89,7 +89,7 @@ namespace FBeeService public void X81(string sn) { Console.WriteLine($"refresh {sn}"); - this.Write(sn, RequestType.x81, null); + this.Write(sn, RequestType.x81); } /// @@ -105,7 +105,7 @@ namespace FBeeService { var responseType = ms.ReadByte(); var dataLength = ms.ReadByte(); - var address = ms.ReadHexString(2); + var address = ms.ReadHexStringDesc(2); var endpoint = ms.ReadByte(); using (var scope = _applicationServices.CreateScope()) { @@ -184,20 +184,10 @@ namespace FBeeService public void X95(string sn, string ieee) { Console.WriteLine($"delete {ieee} from {sn}"); - this.Clients.TryGetValue(sn, out TcpClientWrapper client); - using (var scope = _applicationServices.CreateScope()) - { - var repo = scope.ServiceProvider.GetService>(); - var device = repo.ReadOnlyTable().FirstOrDefault(o => o.IsOnline != 0 && o.IEEE == ieee); - var address = device.Address; - var list = new List(); - list.Add(0x0c); - list.Add(0x02); - list.AddRange(address.HexToBytes()); - list.AddRange(ieee.HexToBytes()); - list.Add(0x01); - this.Write(sn, RequestType.x95, list); - } + var list = new List(); + list.AddRange(ieee.HexToBytes()); + list.Add(0x01); + this.Write(sn, RequestType.x95, ieee, list, false); } //0x94更改指定设备名 @@ -209,24 +199,9 @@ namespace FBeeService /// public void X82(string sn, string ieee, byte status) { - using (var scope = _applicationServices.CreateScope()) - { - var repo = scope.ServiceProvider.GetService>(); - var device = repo.ReadOnlyTable().FirstOrDefault(o => o.Sn == sn && o.IEEE == ieee); - if (device != null) - { - var address = device.Address; - var list = new List(); - list.Add(0x0d); - list.Add(0x02); - list.AddRange(address.HexToBytes()); - list.AddRange(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); - list.Add((byte)device.Endpoint); - list.AddRange(new byte[] { 0x00, 0x00 }); - list.Add(status); - this.Write(sn, RequestType.x82, list); - } - } + var list = new List(); + list.Add(status); + this.Write(sn, RequestType.x82, ieee, list); } /// @@ -240,7 +215,7 @@ namespace FBeeService { var responseType = ms.ReadByte(); var dataLength = ms.ReadByte(); - var address = ms.ReadHexString(2); + var address = ms.ReadHexStringDesc(2); var endpoint = ms.ReadByte(); using (var scope = _applicationServices.CreateScope()) { @@ -267,24 +242,10 @@ namespace FBeeService /// 0-255 public void X83(string sn, string ieee, [Range(0, 255)]byte brightness) { - using (var scope = _applicationServices.CreateScope()) - { - var repo = scope.ServiceProvider.GetService>(); - var device = repo.ReadOnlyTable().FirstOrDefault(o => o.Sn == sn && o.IEEE == ieee); - if (device != null) - { - var address = device.Address; - var list = new List(); - list.Add(0x02); - list.AddRange(address.HexToBytes()); - list.AddRange(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); - list.Add((byte)device.Endpoint); - list.AddRange(new byte[] { 0x00, 0x00 }); - list.Add(brightness); - list.AddRange(new byte[] { 0x00, 0x00 }); - this.Write(sn, RequestType.x83, list); - } - } + var list = new List(); + list.Add(brightness); + list.AddRange(new byte[] { 0x00, 0x00 }); + this.Write(sn, RequestType.x83, ieee, list); } /// @@ -292,25 +253,11 @@ namespace FBeeService /// public void X84(string sn, string ieee, [Range(0, 255)]byte hue, [Range(0, 255)]byte saturation) { - using (var scope = _applicationServices.CreateScope()) - { - var repo = scope.ServiceProvider.GetService>(); - var device = repo.ReadOnlyTable().FirstOrDefault(o => o.Sn == sn && o.IEEE == ieee); - if (device != null) - { - var address = device.Address; - var list = new List(); - list.Add(0x02); - list.AddRange(address.HexToBytes()); - list.AddRange(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); - list.Add((byte)device.Endpoint); - list.AddRange(new byte[] { 0x00, 0x00 }); - list.Add(hue); - list.Add(saturation); - list.AddRange(new byte[] { 0x00, 0x00 }); - this.Write(sn, RequestType.x84, list); - } - } + var list = new List(); + list.Add(hue); + list.Add(saturation); + list.AddRange(new byte[] { 0x00, 0x00 }); + this.Write(sn, RequestType.x84, ieee, list); } //0x84 @@ -339,7 +286,7 @@ namespace FBeeService //0x9d获取网关信息 public void X9d(string sn) { - this.Write(sn, RequestType.x9d, null); + this.Write(sn, RequestType.x9d); } /// @@ -394,25 +341,10 @@ namespace FBeeService /// public void XA8(string sn, string ieee, [Range(2700, 6500)]ushort color) { - using (var scope = _applicationServices.CreateScope()) - { - var repo = scope.ServiceProvider.GetService>(); - var device = repo.ReadOnlyTable().FirstOrDefault(o => o.Sn == sn && o.IEEE == ieee); - if (device != null) - { - var address = device.Address; - var list = new List(); - list.Add(0x10); - list.Add(0x02); - list.AddRange(address.HexToBytes()); - list.AddRange(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); - list.Add((byte)device.Endpoint); - list.AddRange(new byte[] { 0x00, 0x00 }); - list.AddRange(BitConverter.GetBytes(color)); - list.AddRange(new byte[] { 0x00, 0x00 }); - this.Write(sn, RequestType.xa8, list); - } - } + var list = new List(); + list.AddRange(BitConverter.GetBytes(color)); + list.AddRange(new byte[] { 0x00, 0x00 }); + this.Write(sn, RequestType.xa8, ieee, list); } //0xa9 @@ -454,7 +386,7 @@ namespace FBeeService { var responseType = ms.ReadByte(); var dataLength = ms.ReadByte(); - var address = ms.ReadHexString(2); + var address = ms.ReadHexStringDesc(2); var endpoint = ms.ReadByte(); using (var scope = _applicationServices.CreateScope()) { @@ -767,18 +699,28 @@ namespace FBeeService } } - private void Write(string sn, byte commandType, List command) + private void Write(string sn, byte commandType, string ieee = null, List command = null, bool needEndPoint = true, byte addressType = 0x02) { this.Clients.TryGetValue(sn, out TcpClientWrapper client); - if (command == null) - { - command = new List(); - } var data = new List(); data.AddRange(sn.HexToBytes().Reverse()); data.Add(RequestType.xfe); data.Add(commandType); - data.AddRange(command); + if (command != null && command.Count > 0) + { + data.Add((byte)(command.Count() + 13)); + data.Add(addressType); + using (var scope = _applicationServices.CreateScope()) + { + var repo = scope.ServiceProvider.GetService>(); + var device = repo.ReadOnlyTable().FirstOrDefault(o => o.Sn == sn && o.IEEE == ieee); + data.AddRange(device.Address.HexToBytes().Reverse()); + data.AddRange(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); + data.Add((byte)device.Endpoint); + data.AddRange(new byte[] { 0x00, 0x00 }); + } + data.AddRange(command); + } var head = BitConverter.GetBytes(data.Count() + 2).ToList(); if (BitConverter.IsLittleEndian) @@ -790,6 +732,11 @@ namespace FBeeService list.AddRange(head); 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("-", " ")}"); try { diff --git a/projects/IoT/IoTServices/FBeeService/Views/Home/Gateway.cshtml b/projects/IoT/IoTServices/FBeeService/Views/Home/Gateway.cshtml index cc9115e2..0bbc4ee8 100644 --- a/projects/IoT/IoTServices/FBeeService/Views/Home/Gateway.cshtml +++ b/projects/IoT/IoTServices/FBeeService/Views/Home/Gateway.cshtml @@ -68,7 +68,7 @@ } - @if (new int[] { 0x0002, 0x0009, 0x0081, 0x0202 }.Contains(item.DeviceId)) + @if (new int[] { 0x0002, 0x0009, 0x0081, 0x0202, 0x0051 }.Contains(item.DeviceId)) { @@ -83,11 +83,11 @@ } @if (item.DeviceId == 0x0220) { -
- - - -
+
+ + + +
} 删除设备