From b5bc0d6eb2d2e55ea830a1b286252b9754cbf8e9 Mon Sep 17 00:00:00 2001
From: hutongqing <hutongqing@hnkhzn.com>
Date: 星期三, 21 八月 2024 11:21:04 +0800
Subject: [PATCH] 更新主从表添加、修改、删除时对从表的对应操作

---
 WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs |  122 ++++++++++++++++++++++++++--------------
 1 files changed, 80 insertions(+), 42 deletions(-)

diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs
index 8c7eea6..4fe7441 100644
--- a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs
+++ b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs
@@ -23,6 +23,7 @@
 using System;
 using System.Collections;
 using System.Collections.Generic;
+using System.ComponentModel;
 using System.Linq;
 using System.Net;
 using System.Reflection;
@@ -35,6 +36,7 @@
     /// <summary>
     /// 瑗块棬瀛怱7閫氳绫�
     /// </summary>
+    [Description("瑗块棬瀛怱7")]
     public class SiemensS7 : BaseCommunicator, IDisposable
     {
         #region Private Member
@@ -108,11 +110,20 @@
         /// <exception cref="CommunicationException">鑷畾涔夐�氳寮傚父绫�</exception>
         private object? GetContent<T>(OperateResult<T> operateResult, string address)
         {
-            if (!operateResult.IsSuccess)
+            try
             {
-                throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadFailedException, typeof(T).Name, address, operateResult.Message), CommunicationErrorType.ReadFailed);
+                if (!operateResult.IsSuccess)
+                {
+                    throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadFailedException, typeof(T).Name, address, operateResult.Message), CommunicationErrorType.ReadFailed);
+                }
+                return operateResult.Content;
             }
-            return operateResult.Content;
+            catch (Exception ex)
+            {
+                LogNet.WriteException(Name, $"銆恵Name}銆慞LC璇诲彇寮傚父锛屽湴鍧�锛氥�恵address}銆戯紝閿欒淇℃伅锛氥�恵ex.Message}銆�", ex);
+                throw new CommunicationException(ex.Message, CommunicationErrorType.ReadException, innerException: ex);
+            }
+
         }
 
         /// <summary>
@@ -126,28 +137,37 @@
         /// <exception cref="CommunicationException"></exception>
         private bool GetResult<T>(OperateResult operateResult, string address, T value) where T : notnull
         {
-            if (!operateResult.IsSuccess)
+            try
             {
-                throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, value, operateResult.Message), CommunicationErrorType.WriteFailed);
-            }
-            else
-            {
-                object? obj = null;
-                for (int i = 0; i < 5; i++)
+                if (!operateResult.IsSuccess)
                 {
-                    T readValue = Read<T>(address);
-                    obj = readValue;
-                    if (readValue.Equals(value))
-                    {
-                        return true;
-                    }
-                    else
-                    {
-                        Write(address, value);
-                    }
+                    throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, value, operateResult.Message), CommunicationErrorType.WriteFailed);
                 }
-                throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadWriteDifferentException, typeof(T).Name, address, value, obj), CommunicationErrorType.WriteFailed);
+                else
+                {
+                    object? obj = null;
+                    for (int i = 0; i < 5; i++)
+                    {
+                        T readValue = Read<T>(address);
+                        obj = readValue;
+                        if (readValue.Equals(value))
+                        {
+                            return true;
+                        }
+                        else
+                        {
+                            Write(address, value);
+                        }
+                    }
+                    throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadWriteDifferentException, typeof(T).Name, address, value, obj), CommunicationErrorType.WriteFailed);
+                }
             }
+            catch(Exception ex)
+            {
+                LogNet.WriteException(Name, $"銆恵Name}銆慞LC鍐欏叆寮傚父锛屽湴鍧�锛氥�恵address}銆戯紝鍐欏叆鐨勬暟鎹細銆恵value}銆戯紝閿欒淇℃伅锛氥�恵ex.Message}銆�", ex);
+                throw new CommunicationException(ex.Message, CommunicationErrorType.WriteFailed, innerException: ex);
+            }
+            
         }
 
         /// <summary>
@@ -194,7 +214,7 @@
             catch (Exception ex)
             {
                 //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫�
-                throw new CommunicationException($"璇诲彇鏁版嵁寮傚父,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex);
+                throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex);
             }
         }
 
@@ -610,44 +630,62 @@
         #region ReadCustomer
         public override T ReadCustomer<T>(string address)
         {
-            return plc.ReadCustomer<T>(address).Content;
+            try
+            {
+                return plc.ReadCustomer<T>(address).Content;
+            }
+            catch(Exception ex)
+            {
+                LogNet.WriteException(Name, $"銆恵Name}銆慞LC璇诲彇寮傚父锛屽湴鍧�锛氥�恵address}銆戯紝閿欒淇℃伅锛氥�恵ex.Message}銆�", ex);
+                throw new CommunicationException(ex.Message, CommunicationErrorType.ReadException, innerException: ex);
+            }
+           
         }
         #endregion
 
         #region WriteCustomer
         public override bool WriteCustomer<T>(string address, T value)
         {
-            OperateResult operateResult = plc.WriteCustomer(address, value);
-            if (operateResult.IsSuccess)
+            try
             {
-                for (int i = 0; i < 5; i++)
+                OperateResult operateResult = plc.WriteCustomer(address, value);
+                if (operateResult.IsSuccess)
                 {
-                    T readValue = ReadCustomer<T>(address);
-                    PropertyInfo[] propertyInfos = typeof(T).GetProperties();
-                    foreach (var item in propertyInfos)
+                    for (int i = 0; i < 5; i++)
                     {
-                        object writeValueItem = item.GetValue(value);
-                        if (writeValueItem != null)
+                        T readValue = ReadCustomer<T>(address);
+                        PropertyInfo[] propertyInfos = typeof(T).GetProperties();
+                        foreach (var item in propertyInfos)
                         {
-                            object readValueItem = item.GetValue(readValue);
-                            if (writeValueItem.Equals(readValueItem))
+                            object writeValueItem = item.GetValue(value);
+                            if (writeValueItem != null)
                             {
-                                break;
+                                object readValueItem = item.GetValue(readValue);
+                                if (writeValueItem.Equals(readValueItem))
+                                {
+                                    break;
+                                }
+                                else
+                                {
+                                    plc.WriteCustomer(address, value);
+                                }
+                                throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadWriteDifferentException, typeof(T).Name, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue)), CommunicationErrorType.WriteFailed);
                             }
-                            else
-                            {
-                                plc.WriteCustomer(address, value);
-                            }
-                            throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadWriteDifferentException, typeof(T).Name, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue)), CommunicationErrorType.WriteFailed);
                         }
                     }
                 }
+                else
+                {
+                    throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, JsonConvert.SerializeObject(value), operateResult.Message), CommunicationErrorType.WriteFailed);
+                }
+                return operateResult.IsSuccess;
             }
-            else
+            catch(Exception ex)
             {
-                throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, JsonConvert.SerializeObject(value), operateResult.Message), CommunicationErrorType.WriteFailed);
+                LogNet.WriteException(Name, $"銆恵Name}銆慞LC鍐欏叆寮傚父锛屽湴鍧�锛氥�恵address}銆戯紝鍐欏叆鐨勬暟鎹細銆恵JsonConvert.SerializeObject(value)}銆戯紝閿欒淇℃伅锛氥�恵ex.Message}銆�", ex);
+                throw new CommunicationException(ex.Message, CommunicationErrorType.WriteFailed, innerException: ex);
             }
-            return operateResult.IsSuccess;
+            
         }
         #endregion
 

--
Gitblit v1.9.3