From 2cc4dfef234b47bef364edf798b5051a25f33217 Mon Sep 17 00:00:00 2001
From: hutongqing <hutongqing@hnkhzn.com>
Date: 星期五, 30 八月 2024 10:58:56 +0800
Subject: [PATCH] 1

---
 WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/ConveyorLine/CommonConveyorLine.cs |   47 +++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/ConveyorLine/CommonConveyorLine.cs b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/ConveyorLine/CommonConveyorLine.cs
index ccf896d..0ed332f 100644
--- a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/ConveyorLine/CommonConveyorLine.cs
+++ b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/ConveyorLine/CommonConveyorLine.cs
@@ -30,7 +30,7 @@
 
 namespace WIDESEAWCS_QuartzJob
 {
-    [Description("閫氱敤杈撻�佺嚎")]
+    [Description("杈撻�佺嚎")]
     public class CommonConveyorLine : IConveyorLine
     {
         #region Private Member
@@ -38,22 +38,30 @@
         /// 鍫嗗灈鏈洪�氳瀵硅薄
         /// </summary>
         private readonly BaseCommunicator _communicator;
+
         /// <summary>
         /// 鍫嗗灈鏈哄崗璁俊鎭�
         /// </summary>
         private readonly List<DeviceProDTO> _deviceProDTOs;
+
         /// <summary>
         /// 鍫嗗灈鏈哄崗璁槑缁嗕俊鎭�
         /// </summary>
         private readonly List<DeviceProtocolDetailDTO> _deviceProtocolDetailDTOs;
+
         /// <summary>
         /// 璁惧缂栧彿
         /// </summary>
         public readonly string _deviceCode;
+
         /// <summary>
         /// 璁惧鍚嶇О
         /// </summary>
         public readonly string _deviceName;
+
+        private bool _heartStatr = true;
+
+        private bool _isConnected = true;
         #endregion
 
         #region Public Member
@@ -69,7 +77,7 @@
 
         public bool IsFault => false;
 
-        public bool IsConnected => Communicator.IsConnected;
+        public bool IsConnected => Communicator.IsConnected && _isConnected;
 
         public DeviceStatus Status => DeviceStatus.Offline;
         #endregion
@@ -90,15 +98,40 @@
             _deviceProtocolDetailDTOs = deviceProtocolDetailDTOs;
             _deviceCode = deviceCode;
             _deviceName = deviceName;
+            CheckConnect();
         }
         #endregion
 
         #region Private Method
+        private void CheckConnect()
+        {
+            Task.Run(() =>
+            {
+                while (_heartStatr)
+                {
+                    try
+                    {
+                        DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault();
+                        if (devicePro == null)
+                            _isConnected = false;
+                        else
+                            Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
+                        _isConnected = true;
+                    }
+                    catch (Exception ex)
+                    {
+                        _isConnected = false;
+                    }
+                    Thread.Sleep(500);
+                }
+            });
+        }
         #endregion
 
         #region Public Method
         public TRsult GetValue<TEnum, TRsult>(TEnum value, string deviceChildCode) where TEnum : Enum
         {
+            if (!IsConnected) throw new Exception($"閫氳杩炴帴閿欒锛岃妫�鏌ョ綉缁�");
             DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == value.ToString() && x.DeviceChildCode == deviceChildCode);
             return devicePro == null ? throw new Exception() : (TRsult)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
         }
@@ -110,6 +143,7 @@
 
         public bool SendCommand<T>(T command, string deviceChildCode) where T : IDataTransfer, new()
         {
+            if (!IsConnected) throw new Exception($"閫氳杩炴帴閿欒锛岃妫�鏌ョ綉缁�");
             DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand) && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
             if (devicePro == null)
             {
@@ -124,6 +158,7 @@
 
         public T ReadCustomer<T>(string deviceChildCode) where T : IDataTransfer, new()
         {
+            if (!IsConnected) throw new Exception($"閫氳杩炴帴閿欒锛岃妫�鏌ョ綉缁�");
             DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == "ReadDeviceCommand" && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
 
             if (devicePro == null)
@@ -140,6 +175,7 @@
             where TEnum : Enum
             where TValue : notnull
         {
+            if (!IsConnected) throw new Exception($"閫氳杩炴帴閿欒锛岃妫�鏌ョ綉缁�");
             DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == @enum.ToString() && x.DeviceChildCode == deviceChildCode);
             return devicePro == null ? throw new Exception() : Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value);
         }
@@ -170,6 +206,13 @@
             //todo 閫氳鏈繛鎺ユ椂鎶涘嚭寮傚父
             return false;
         }
+
+        public void Dispose()
+        {
+            _heartStatr = false;
+            _communicator.Dispose();
+            GC.SuppressFinalize(this);
+        }
         #endregion
     }
 }

--
Gitblit v1.9.3