From 52a4593652d3c06b1241e7e7dcb9c8f7ba724e8d Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Fri, 21 Jun 2019 16:48:46 +0800 Subject: [PATCH] update Former-commit-id: 057bbe760af6ce3283239febd8be8e2e3476c2bf --- .../Application/Models/TcpClientWrapper.cs | 1 + .../FBeeService/Controllers/HomeController.cs | 204 +----------------- .../Infrastructure/DeviceService.cs | 187 +++------------- 3 files changed, 37 insertions(+), 355 deletions(-) diff --git a/projects/IoT/IoTServices/FBeeService/Application/Models/TcpClientWrapper.cs b/projects/IoT/IoTServices/FBeeService/Application/Models/TcpClientWrapper.cs index 94441d75..87dec9d0 100644 --- a/projects/IoT/IoTServices/FBeeService/Application/Models/TcpClientWrapper.cs +++ b/projects/IoT/IoTServices/FBeeService/Application/Models/TcpClientWrapper.cs @@ -4,6 +4,7 @@ namespace Application.Models { public class TcpClientWrapper { + public string Sn { get; set; } public string Ip { get; set; } public TcpClient Client { get; set; } } diff --git a/projects/IoT/IoTServices/FBeeService/Controllers/HomeController.cs b/projects/IoT/IoTServices/FBeeService/Controllers/HomeController.cs index bf959dd2..7d384663 100644 --- a/projects/IoT/IoTServices/FBeeService/Controllers/HomeController.cs +++ b/projects/IoT/IoTServices/FBeeService/Controllers/HomeController.cs @@ -1,7 +1,3 @@ -using System; -using System.Linq; -using System.Net.Http; -using System.Threading.Tasks; using Application.Domain.Entities; using Application.Models; using Infrastructure.Data; @@ -9,9 +5,11 @@ using Infrastructure.Extensions; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using Polly; -using SPService.Applicaiton.Models; using Swashbuckle.AspNetCore.Annotations; +using System; +using System.Linq; +using System.Net.Http; +using System.Threading.Tasks; namespace FBeeService.Controllers { @@ -37,6 +35,8 @@ namespace FBeeService.Controllers return View(this._repo.ReadOnlyTable().ToList()); } + #region smart plug + [HttpGet] [Route("/smartplug/api")] [SwaggerOperation("API")] @@ -46,8 +46,6 @@ namespace FBeeService.Controllers return GetApiJson("/smartplug/"); } - #region api - [SwaggerOperation("开")] [HttpGet] [Route("/smartplug/21on")] @@ -82,23 +80,7 @@ namespace FBeeService.Controllers return ApiResponse.AsyncSuccess(); } - [HttpGet] - [Route("/socket1/api")] - [SwaggerOperation("API")] - [ApiExplorerSettings(IgnoreApi = true)] - public string Socket1Api() - { - return GetApiJson("/socket1/"); - } - - [HttpGet] - [Route("/socket2/api")] - [SwaggerOperation("API")] - [ApiExplorerSettings(IgnoreApi = true)] - public string Socket2Api() - { - return GetApiJson("/socket2/"); - } + #endregion smart plug private string GetApiJson(string prefix) { @@ -123,177 +105,5 @@ namespace FBeeService.Controllers var realResult = JsonConvert.SerializeObject(json); return realResult; } - - #endregion api - - [SwaggerOperation("全开")] - [HttpGet] - [Route("/switch2/21on")] - public ApiResponse On([SwaggerParameter("设备Id")]string id) - { - try - { - this._deviceService.Switch2AllOn(id); - } - catch (Exception ex) - { - ex.PrintStack(); - return ApiResponse.Error(ex.Message); - } - return ApiResponse.AsyncSuccess(); - } - - [SwaggerOperation("全关")] - [HttpGet] - [Route("/switch2/22off")] - public ApiResponse Off([SwaggerParameter("设备Id")]string id) - { - try - { - this._deviceService.Switch2AllOff(id); - } - catch (Exception ex) - { - ex.PrintStack(); - return ApiResponse.Error(ex.Message); - } - return ApiResponse.AsyncSuccess(); - } - - [SwaggerOperation("L1开")] - [HttpGet] - [Route("/switch2/23l1on")] - public ApiResponse L1On([SwaggerParameter("设备Id")]string id) - { - try - { - this._deviceService.Switch2L1On(id); - } - catch (Exception ex) - { - ex.PrintStack(); - return ApiResponse.Error(ex.Message); - } - return ApiResponse.AsyncSuccess(); - } - - [SwaggerOperation("L1关")] - [HttpGet] - [Route("/switch2/24l1off")] - public ApiResponse L1Off([SwaggerParameter("设备Id")]string id) - { - try - { - this._deviceService.Switch2L1Off(id); - } - catch (Exception ex) - { - ex.PrintStack(); - return ApiResponse.Error(ex.Message); - } - return ApiResponse.AsyncSuccess(); - } - - [SwaggerOperation("L2开")] - [HttpGet] - [Route("/switch2/25l2on")] - public ApiResponse L2On([SwaggerParameter("设备Id")]string id) - { - try - { - this._deviceService.Switch2L2On(id); - } - catch (Exception ex) - { - ex.PrintStack(); - return ApiResponse.Error(ex.Message); - } - return ApiResponse.AsyncSuccess(); - } - - [SwaggerOperation("L2关")] - [HttpGet] - [Route("/switch2/26l2off")] - public ApiResponse L2Off([SwaggerParameter("设备Id")]string id) - { - try - { - this._deviceService.Switch2L2Off(id); - } - catch (Exception ex) - { - ex.PrintStack(); - return ApiResponse.Error(ex.Message); - } - return ApiResponse.AsyncSuccess(); - } - - [SwaggerOperation("开")] - [HttpGet] - [Route("/socket1/21on")] - public ApiResponse Socket1On([SwaggerParameter("设备Id")]string id) - { - try - { - this._deviceService.SocketOn(id); - } - catch (Exception ex) - { - ex.PrintStack(); - return ApiResponse.Error(ex.Message); - } - return ApiResponse.AsyncSuccess(); - } - - [SwaggerOperation("关")] - [HttpGet] - [Route("/socket1/22off")] - public ApiResponse Socket1Off([SwaggerParameter("设备Id")]string id) - { - try - { - this._deviceService.SocketOff(id); - } - catch (Exception ex) - { - ex.PrintStack(); - return ApiResponse.Error(ex.Message); - } - return ApiResponse.AsyncSuccess(); - } - - [SwaggerOperation("开")] - [HttpGet] - [Route("/socket2/21on")] - public ApiResponse Socket2On([SwaggerParameter("设备Id")]string id) - { - try - { - this._deviceService.SocketOn(id); - } - catch (Exception ex) - { - ex.PrintStack(); - return ApiResponse.Error(ex.Message); - } - return ApiResponse.AsyncSuccess(); - } - - [SwaggerOperation("关")] - [HttpGet] - [Route("/socket2/22off")] - public ApiResponse Socket2Off([SwaggerParameter("设备Id")]string id) - { - try - { - this._deviceService.SocketOff(id); - } - catch (Exception ex) - { - ex.PrintStack(); - return ApiResponse.Error(ex.Message); - } - return ApiResponse.AsyncSuccess(); - } } } \ 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 0665f620..5263d2bd 100644 --- a/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs +++ b/projects/IoT/IoTServices/FBeeService/Infrastructure/DeviceService.cs @@ -30,7 +30,6 @@ namespace FBeeService private readonly IServiceProvider _applicationServices; private CancellationTokenSource _tokenSource; private static readonly object lockObject = new Object(); - private readonly TcpClient _tcpClient; private string _tcpIp; private int _tcpPort; private byte[] _address; @@ -45,7 +44,6 @@ namespace FBeeService this._configuration = configuration; this._httpClientFactory = httpClientFactory; this._tokenSource = new CancellationTokenSource(); - this._tcpClient = new TcpClient(); this._tcpIp = this._configuration["tcp.ip"]; this._tcpPort = this._configuration.GetValue("tcp.port"); //this.Connect(); @@ -117,64 +115,12 @@ namespace FBeeService list.Add(0x01); list.AddRange(new byte[] { 0x00, 0x00 }); list.Add(status); - this.Clients[sn].Client.GetStream().Write(list.ToArray()); + var client = this.Clients[sn]; + this.Write(client, list.ToArray()); } } } - public void Switch2AllOn(string id) - { - this.Command16(new byte[] { this.GetNumber(id), 0x0f, 0x00, 0x00, 0x00, 0x03, 0x01, 0x07 }); - this.SearchSwitchStatus(this.GetNumber(id), 0x02); - } - - public void Switch2AllOff(string id) - { - this.Command16(new byte[] { this.GetNumber(id), 0x0f, 0x00, 0x00, 0x00, 0x03, 0x01, 0x00 }); - this.SearchSwitchStatus(this.GetNumber(id), 0x02); - } - - public void Switch2L1On(string id) - { - this.Command16(new byte[] { this.GetNumber(id), 0x05, 0x00, 0x00, 0xff, 0x00 }); - this.SearchSwitchStatus(this.GetNumber(id), 0x02); - } - - public void Switch2L1Off(string id) - { - this.Command16(new byte[] { this.GetNumber(id), 0x05, 0x00, 0x00, 0x00, 0x00 }); - this.SearchSwitchStatus(this.GetNumber(id), 0x02); - } - - public void Switch2L2On(string id) - { - this.Command16(new byte[] { this.GetNumber(id), 0x05, 0x00, 0x01, 0xff, 0x00 }); - this.SearchSwitchStatus(this.GetNumber(id), 0x02); - } - - public void Switch2L2Off(string id) - { - this.Command16(new byte[] { this.GetNumber(id), 0x05, 0x00, 0x01, 0x00, 0x00 }); - this.SearchSwitchStatus(this.GetNumber(id), 0x02); - } - - public void SocketOn(string id) - { - this.Command16(new byte[] { this.GetNumber(id), 0x05, 0x00, 0x00, 0xff, 0x00 }); - this.SearchSwitchStatus(this.GetNumber(id), 0x10); - } - - public void SocketOff(string id) - { - this.Command16(new byte[] { this.GetNumber(id), 0x05, 0x00, 0x00, 0x00, 0x00 }); - this.SearchSwitchStatus(this.GetNumber(id), 0x10); - } - - private byte GetNumber(string id) - { - return Convert.ToByte(id.Split("-")[1]); - } - private void CheckConnection() { var ips = NetworkInterface.GetAllNetworkInterfaces() @@ -254,25 +200,21 @@ namespace FBeeService Clients.TryGetValue(gateway.Sn, out TcpClientWrapper client); if (client.Ip != gateway.Ip) { - if (client.Client.Connected) - { - client.Client.Close(); - } - this.Connect(gateway.Sn, client); + this.Connect(client); } else { if (!client.Client.Connected) { - this.Connect(gateway.Sn, client); + this.Connect(client); } } } else { - var client = new TcpClientWrapper { Ip = gateway.Ip, Client = new TcpClient() }; + var client = new TcpClientWrapper { Sn = gateway.Sn, Ip = gateway.Ip, Client = new TcpClient() }; Clients.TryAdd(gateway.Sn, client); - this.Connect(gateway.Sn, client); + this.Connect(client); } this.Refresh(gateway.Sn); } @@ -291,17 +233,23 @@ namespace FBeeService } } - private void Connect(string sn, TcpClientWrapper client) + private void Connect(TcpClientWrapper client) { try { + if (client != null) + { + client.Client.Dispose(); + client.Client = new TcpClient(); + } + Console.WriteLine($"connect {client.Ip} for {client.Sn}"); client.Client.Connect(client.Ip, 8001); new Thread(() => { var stream = client.Client.GetStream(); while (client.Client.Connected) { - if (stream.CanRead) + if (stream.DataAvailable) { try { @@ -309,13 +257,11 @@ namespace FBeeService var length = stream.Read(buffer); var data = buffer.ToList().Take(length).ToArray(); Console.WriteLine($"response:{BitConverter.ToString(data)}"); - var test = new byte[] { - 0x16,0x00, 0x34, 0xF5, 0x41, 0x11, 0xFE, 0x82, 0x0D, 0x02, 0x42 ,0x20 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x01 ,0x00 ,0x00 ,0x00 }; - this.Handle(sn, data); + this.Handle(client.Sn, data); } catch (Exception ex) { - ex.PrintStack(); + ex.PrintStack("read stream error"); } } } @@ -335,14 +281,7 @@ namespace FBeeService list.AddRange(sn.HexToBytes().Reverse()); list.Add(0xfe); list.Add(0x81); - this.Write(client.Client, list.ToArray()); - } - - private void SearchAddress() - { - var list = new List() { 0x0c }; - var command = Command(list.ToArray()); - this.Write(command); + this.Write(client, list.ToArray()); } private void Handle(string sn, byte[] data) @@ -459,51 +398,6 @@ namespace FBeeService } } - private void SearchNodeStatus() - { - var list = new List() { 0x15 }; - list.AddRange(this._address); - list.Add(0x00); - var command = Command(list.ToArray()); - this.Write(command); - } - - private void SearchNodeType(int i) - { - var list = new List() { 0x16 }; - list.AddRange(this._address); - list.Add(0x00); - var modbusdata = new List { - (byte)i,0x03,0x00,0x0f,0x00,0x01 - }.ToArray(); - var modbus = new List(); - modbus.AddRange(modbusdata); - var crc = Crc16.ComputeChecksum(modbusdata); - modbus.Add(crc[1]); - modbus.Add(crc[0]); - list.AddRange(modbus); - var command = Data(list.ToArray()); - this.Write(command); - } - - private void SearchSwitchStatus(int i, byte cmd) - { - var list = new List() { 0x16 }; - list.AddRange(this._address); - list.Add(0x00); - var modbusdata = new List { - (byte)i, 0x01,0x00,0x00,0x00,cmd - }.ToArray(); - var modbus = new List(); - modbus.AddRange(modbusdata); - var crc = Crc16.ComputeChecksum(modbusdata); - modbus.Add(crc[1]); - modbus.Add(crc[0]); - list.AddRange(modbus); - var command = Data(list.ToArray()); - this.Write(command); - } - private byte[] Command(byte[] data) { var command = new List { 0xfe, 0xa5, 0x00 }; @@ -531,40 +425,14 @@ namespace FBeeService return command.ToArray(); } - private void Command16(byte[] command) + private void Write(TcpClientWrapper clientWrapper, byte[] command) { - var list = new List() { 0x16 }; - list.AddRange(this._address); - list.Add(0x00); - var modbusdata = command; - var modbus = new List(); - modbus.AddRange(modbusdata); - var crc = Crc16.ComputeChecksum(modbusdata); - modbus.Add(crc[1]); - modbus.Add(crc[0]); - list.AddRange(modbus); - var data = Data(list.ToArray()); - this.Write(data); - } - - private void Write(byte[] command) - { - Thread.Sleep(500); - Console.WriteLine($"write:{BitConverter.ToString(command).Replace("-", " ")}"); - this._tcpClient.GetStream().Write(command); - } - - private void Write(TcpClient client, byte[] command) - { - if (client.Connected) - { - Console.WriteLine($"write:{BitConverter.ToString(command).Replace("-", " ")}"); - client.GetStream().Write(command); - } - else + if (!clientWrapper.Client.Connected) { - Console.WriteLine($"can not write:{BitConverter.ToString(command).Replace("-", " ")}"); + this.Connect(clientWrapper); } + Console.WriteLine($"write:{BitConverter.ToString(command).Replace("-", " ")}"); + clientWrapper.Client.GetStream().Write(command); } private NotifyModel CreateModel(string name, string number, string path, string icon) @@ -604,12 +472,15 @@ namespace FBeeService public void Dispose() { - Console.WriteLine("LiChuangService dispose..."); - if (this._tcpClient.Connected) + Console.WriteLine("dispose..."); + this._tokenSource.Cancel(); + foreach (var item in Clients) { - this._tcpClient.Close(); + if (item.Value.Client != null) + { + item.Value.Client.Dispose(); + } } - this._tokenSource.Cancel(); } } } \ No newline at end of file