From 181e5608fb0daae39473fae15a01cb90ebb57e90 Mon Sep 17 00:00:00 2001 From: lxw Date: Sat, 26 Oct 2024 16:30:02 +0800 Subject: [PATCH] =?UTF-8?q?led=E8=BF=9E=E6=8E=A5=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service/Led/LedClient.cs | 34 +++++++++++---------- Service/MyTask/UpdateDownBatteryInfoTask.cs | 2 +- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Service/Led/LedClient.cs b/Service/Led/LedClient.cs index 3909228..be60e50 100644 --- a/Service/Led/LedClient.cs +++ b/Service/Led/LedClient.cs @@ -181,8 +181,11 @@ public class LedClient // private static string _ip = "127.0.0.1"; private static int _port = 10000; - private static DateTime LastestSendTime; - private static string LastestKey=InfoEnum.SwapInfo.WelcomeInfo.GetLed(); + + private static string LastestKey=InfoEnum.SwapInfo.IDEI.GetLed(); + + + private static bool HeartBegin = false; private static byte[] Heart = new byte[] { @@ -242,20 +245,30 @@ public class LedClient _socket.NoDelay = true; _socket.Connect(_ip, _port); Log.Info($"end connect {_ip}:{_port} {_socket.Connected}"); - Task.Run(ListenHeart); + + if (_socket.Connected) + { + if (!HeartBegin) + { + HeartBegin = true; + Task.Run(ListenHeart); + } + } + } catch (SocketException ex) { Log.Error($"Failed to connect to {_ip}:{_port}. Reason: {ex.Message}"); _socket?.Close(); _socket = null; + } } private static bool InnerSend(byte[] bytes) { - if (_socket?.Connected ?? false) + if ( _socket?.Connected ?? false ) { try { @@ -295,19 +308,8 @@ public class LedClient int count = 0; if (Dictionary.TryGetValue(key, out var bytes)) { - LastestKey = key; - LastestSendTime=DateTime.Now; - while (!InnerSend(bytes)) - { - Thread.Sleep(100); - count++; - if (count > 10) - { - return false; - } - } - return true; + return InnerSend(bytes); } return false; diff --git a/Service/MyTask/UpdateDownBatteryInfoTask.cs b/Service/MyTask/UpdateDownBatteryInfoTask.cs index 863a23c..9bd0247 100644 --- a/Service/MyTask/UpdateDownBatteryInfoTask.cs +++ b/Service/MyTask/UpdateDownBatteryInfoTask.cs @@ -82,7 +82,7 @@ public class UpdateDownBatteryInfoTask : ITask battery.DownBatterySoc = info.Soc; battery.DownBatterySoe = info.Soe; battery.DownNominalEnergy = info.NominalEnergy; - + Log.Info($"add battery battery ={JsonConvert.SerializeObject(battery)}"); updateDbBattery.Add(battery);