From 2ca90a404fa1ab94eb6374f50c6ddd47a2b7f0e6 Mon Sep 17 00:00:00 2001
From: kazelee <1847801760@qq.com>
Date: 星期四, 31 七月 2025 17:23:57 +0800
Subject: [PATCH] 删除有问题的TCP旧代码, 优化事务处理和ERP数据模型

---
 device/OpcUaHelper.cs |   87 ++++++++++++++-----------------------------
 1 files changed, 29 insertions(+), 58 deletions(-)

diff --git a/device/OpcUaHelper.cs b/device/OpcUaHelper.cs
index 4f8922c..a916ed8 100644
--- a/device/OpcUaHelper.cs
+++ b/device/OpcUaHelper.cs
@@ -4,33 +4,25 @@
 using Opc.Ua.Configuration;
 using HH.WCS.Mobox3.DSZSH.util;
 
-
-namespace HH.WCS.Mobox3.DSZSH.device
-{
-    internal class OpcUaHelper
-    {
+namespace HH.WCS.Mobox3.DSZSH.device {
+    internal class OpcUaHelper {
         private static Opc.Ua.Client.Session session;
 
-        static OpcUaHelper()
-        {
+        static OpcUaHelper() {
             CreateUpcSession();
         }
 
         /// <summary>
         /// 杩炴帴OPC鏈嶅姟
         /// </summary>
-        internal static async void CreateUpcSession()
-        {
-            try
-            {
+        internal static async void CreateUpcSession() {
+            try {
                 // 鍒涘缓涓�釜搴旂敤閰嶇疆瀵硅薄,鐢ㄤ簬璁剧疆搴旂敤鍚嶇О,鍞竴鏍囪瘑,绫诲瀷,璇佷功鍜屽畨鍏ㄧ瓥鐣�-                var config = new ApplicationConfiguration()
-                {
+                var config = new ApplicationConfiguration() {
                     ApplicationName = "MyClient",
                     ApplicationUri = Utils.Format(@"urn:{0}:MyClient", System.Net.Dns.GetHostName()),
                     ApplicationType = ApplicationType.Client,
-                    SecurityConfiguration = new SecurityConfiguration
-                    {
+                    SecurityConfiguration = new SecurityConfiguration {
                         ApplicationCertificate = new CertificateIdentifier { StoreType = @"Directory", StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\MachineDefault", SubjectName = "MyClientSubjectName" },
                         TrustedIssuerCertificates = new CertificateTrustList { StoreType = @"Directory", StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\UA Certificate Authorities" },
                         TrustedPeerCertificates = new CertificateTrustList { StoreType = @"Directory", StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\UA Applications" },
@@ -50,8 +42,7 @@
                 await config.Validate(ApplicationType.Client);
 
                 // 璁剧疆璇佷功楠岃瘉浜嬩欢,鐢ㄤ簬鑷姩鎺ュ彈涓嶅彈淇′换鐨勮瘉涔�-                if (config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
-                {
+                if (config.SecurityConfiguration.AutoAcceptUntrustedCertificates) {
                     config.CertificateValidator.CertificateValidation += (s, e) => { e.Accept = (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted); };
                 }
 
@@ -65,19 +56,16 @@
                 EndpointConfiguration endpointConfiguration = EndpointConfiguration.Create(config);
                 ConfiguredEndpoint endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration);
                 session = await Session.Create(config, endpoint, false, false, "DataCollector", 60000, new UserIdentity(), null);
-                if (session != null && session.Connected)
-                {
+                if (session != null && session.Connected) {
                     Console.WriteLine("The session is connected to the OPC UA server.");
                     LogHelper.Info($"鍒涘缓OPC杩炴帴鎴愬姛", "OPC");
                 }
-                else
-                {
+                else {
                     Console.WriteLine("The session is not connected to the OPC UA server.");
                     LogHelper.Info($"鍒涘缓OPC杩炴帴澶辫触", "OPC");
                 }
             }
-            catch (Exception e)
-            {
+            catch (Exception e) {
                 LogHelper.Info($"杩炴帴OPC鏈嶅姟寮傚父" + e.Message, "OPC");
             }
         }
@@ -86,27 +74,22 @@
         /// 璇诲彇OPC鑺傜偣鏁版嵁
         /// </summary>
         /// <param name="nodeId"></param>
-        internal static object ReadOpcValue(string nodeId)
-        {
-            try
-            {
-                if (session != null && session.Connected)
-                {
+        internal static object ReadOpcValue(string nodeId) {
+            try {
+                if (session != null && session.Connected) {
                     // 璇诲彇鏁版嵁鑺傜偣
                     DataValue item = session.ReadValue(nodeId: nodeId);
                     LogHelper.Info($"OPC璇诲彇:nodeid:{nodeId},value:{item.Value}", "OPC");
                     return item.Value;
                 }
-                else
-                {
+                else {
                     Console.WriteLine("The session is not connected to the OPC UA server.");
                     LogHelper.Info($"OPC杩炴帴澶辫触", "OPC");
                     CreateUpcSession();
                 }
-               
+
             }
-            catch (Exception e)
-            {
+            catch (Exception e) {
 
                 LogHelper.Info($"璇诲彇OPC鏁版嵁寮傚父" + e.Message, "OPC");
             }
@@ -118,15 +101,11 @@
         /// </summary>
         /// <param name="nodeId"></param>
         /// <param name="val"></param>
-        internal static void WriteOpcValue(string nodeId,bool val)
-        {
-            try
-            {
-                if (session != null && session.Connected)
-                {
+        internal static void WriteOpcValue(string nodeId, bool val) {
+            try {
+                if (session != null && session.Connected) {
                     // 鍐欏叆鏁版嵁鍒拌妭鐐�-                    WriteValue writeValue = new WriteValue
-                    {
+                    WriteValue writeValue = new WriteValue {
                         NodeId = new NodeId(nodeId),
                         AttributeId = Attributes.Value,
                         Value = new DataValue(val)
@@ -138,16 +117,14 @@
                     Console.WriteLine($"Write Status for {nodeId}: {results[0]}");
                     LogHelper.Info($"OPC鍐欏叆:nodeid:{nodeId},value:{val}", "OPC");
                 }
-                else
-                {
+                else {
                     Console.WriteLine("The session is not connected to the OPC UA server.");
                     LogHelper.Info($"OPC杩炴帴澶辫触", "OPC");
                     CreateUpcSession();
                 }
 
             }
-            catch (Exception e)
-            {
+            catch (Exception e) {
                 LogHelper.Info($"鍐欏叆OPC鏁版嵁寮傚父" + e.Message, "OPC");
             }
         }
@@ -157,15 +134,11 @@
         /// </summary>
         /// <param name="nodeId"></param>
         /// <param name="val"></param>
-        internal static void WriteOpcValue(string nodeId, int val)
-        {
-            try
-            {
-                if (session != null && session.Connected)
-                {
+        internal static void WriteOpcValue(string nodeId, int val) {
+            try {
+                if (session != null && session.Connected) {
                     // 鍐欏叆鏁版嵁鍒拌妭鐐�-                    WriteValue writeValue = new WriteValue
-                    {
+                    WriteValue writeValue = new WriteValue {
                         NodeId = new NodeId(nodeId),
                         AttributeId = Attributes.Value,
                         Value = new DataValue(val)
@@ -177,16 +150,14 @@
                     Console.WriteLine($"Write Status for {nodeId}: {results[0]}");
                     LogHelper.Info($"OPC鍐欏叆:nodeid:{nodeId},value:{val}", "OPC");
                 }
-                else
-                {
+                else {
                     Console.WriteLine("The session is not connected to the OPC UA server.");
                     LogHelper.Info($"OPC杩炴帴澶辫触", "OPC");
                     CreateUpcSession();
                 }
 
             }
-            catch (Exception e)
-            {
+            catch (Exception e) {
                 LogHelper.Info($"鍐欏叆OPC鏁版嵁寮傚父" + e.Message, "OPC");
             }
         }

--
Gitblit v1.9.1