ソースを参照

opcua 修改判断错误问题

Shun 2 年 前
コミット
1ac2b3a5b8

+ 1 - 1
src/YSAI.DAQ/YSAI.DAQ.sln

@@ -91,7 +91,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YSAI.Tool", "YSAI.Tool\YSAI
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YSAI.Test.All", "YSAI.Test.All\YSAI.Test.All.csproj", "{1CFDF4C1-53F7-4A32-B83D-4B99AFC95A8D}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YSAI.Pack.Param", "YSAI.Pack.Param\YSAI.Pack.Param.csproj", "{36696247-658B-445E-9C05-A815B73F248A}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YSAI.Pack.Param", "YSAI.Pack.Param\YSAI.Pack.Param.csproj", "{36696247-658B-445E-9C05-A815B73F248A}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution

+ 1 - 1
src/YSAI.DAQ/YSAI.Opc/YSAI.Opc.csproj

@@ -5,7 +5,7 @@
     <ImplicitUsings>enable</ImplicitUsings>
     <Nullable>enable</Nullable>
 	  <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
-	  <Version>1.0.0.22</Version>
+	  <Version>1.0.0.23</Version>
 	  <Authors>Shun</Authors>
 	  <Company>YSAI</Company>
 	  <Product>SCADA</Product>

+ 40 - 29
src/YSAI.DAQ/YSAI.Opc/ua/client/OpcUaClientOperate.cs

@@ -792,21 +792,27 @@ namespace YSAI.Opc.ua.client
                                 //添加监控项
                                 allSubscriptions[Tag].AddItems(monitoredItems);
                                 //添加订阅
-                                clientSession?.AddSubscription(allSubscriptions[Tag]);
-                                //在服务器上创建订阅并添加所有监视项
-                                if (!allSubscriptions[Tag].CreateAsync().Wait(basics.Timeout))
+                                if (clientSession.AddSubscription(allSubscriptions[Tag]))
                                 {
-                                    //// 将任何更改应用于订阅项
-                                    //if (!allSubscriptions[Tag].ApplyChangesAsync().Wait(basics.Timeout))
-                                    //{
-                                    //    allSubscriptions?.Remove(Tag, out _);
-                                    //    return Break("AddSubscribe", false, "将任何更改应用于订阅项超时");
-                                    //}
-                                //}
-                                //else
-                                //{
-                                    allSubscriptions?.Remove(Tag, out _);
-                                    return Break("AddSubscribe", false, "在服务器上创建订阅并添加所有监视项超时");
+                                    //在服务器上创建订阅并添加所有监视项
+                                    if (allSubscriptions[Tag].CreateAsync().Wait(basics.Timeout))
+                                    {
+                                        // 将任何更改应用于订阅项
+                                        if (!allSubscriptions[Tag].ApplyChangesAsync().Wait(basics.Timeout))
+                                        {
+                                            allSubscriptions?.Remove(Tag, out _);
+                                            return Break("AddSubscribe", false, "将任何更改应用于订阅项超时");
+                                        }
+                                    }
+                                    else
+                                    {
+                                        allSubscriptions?.Remove(Tag, out _);
+                                        return Break("AddSubscribe", false, "在服务器上创建订阅并添加所有监视项超时");
+                                    }
+                                }
+                                else
+                                {
+                                    return Break("AddSubscribe", false, "会话添加订阅失败");
                                 }
                             }
                         }
@@ -849,23 +855,28 @@ namespace YSAI.Opc.ua.client
                             //把此订阅添加到集合,方便后续移除订阅(当存在此键则更新值,不存在则添加)
                             allSubscriptions?.AddOrUpdate(Tag, subscription, (k, v) => subscription);
                             //添加订阅
-                            clientSession?.AddSubscription(allSubscriptions[Tag]);
-                            //在服务器上创建订阅并添加所有监视项
-                            if (!allSubscriptions[Tag].CreateAsync().Wait(basics.Timeout))
+                            if (clientSession.AddSubscription(allSubscriptions[Tag]))
+                            {
+                                //在服务器上创建订阅并添加所有监视项
+                                if (allSubscriptions[Tag].CreateAsync().Wait(basics.Timeout))
+                                {
+                                    // 将任何更改应用于订阅项
+                                    if (!allSubscriptions[Tag].ApplyChangesAsync().Wait(basics.Timeout))
+                                    {
+                                        allSubscriptions?.Remove(Tag, out _);
+                                        return Break("AddSubscribe", false, "将任何更改应用于订阅项超时");
+                                    }
+                                }
+                                else
+                                {
+                                    allSubscriptions?.Remove(Tag, out _);
+                                    return Break("AddSubscribe", false, "在服务器上创建订阅并添加所有监视项超时");
+                                }
+                            }
+                            else
                             {
-                            //    // 将任何更改应用于订阅项
-                            //    if (!allSubscriptions[Tag].ApplyChangesAsync().Wait(basics.Timeout))
-                            //    {
-                            //        allSubscriptions?.Remove(Tag, out _);
-                            //        return Break("AddSubscribe", false, "将任何更改应用于订阅项超时");
-                            //    }
-                            //}
-                            //else
-                            //{
-                                allSubscriptions?.Remove(Tag, out _);
-                                return Break("AddSubscribe", false, "在服务器上创建订阅并添加所有监视项超时");
+                                return Break("AddSubscribe", false, "会话添加订阅失败");
                             }
-
                         }
                         return Break("AddSubscribe", true);
                     }

+ 67 - 4
src/YSAI.DAQ/YSAI.Test.All/Program.cs

@@ -1,7 +1,70 @@
-
-
-
-Console.WriteLine(  );
+using YSAI.Core.data;
+using YSAI.Opc.ua.client;
+using YSAI.Unility;
+
+
+//点位地址
+Address address = new Address();
+address.SN = Guid.NewGuid().ToString();
+address.CreationTime = DateTime.Now.ToLocalTime();
+address.AddressArray = new List<AddressDetails> {
+    new AddressDetails()
+    {
+        AddressName = "ns=2;s=YT6022_VALUE_SJ2147_P_CUR_9",
+        AddressDataType = YSAI.Core.@enum.DataType.Float
+    },
+    new AddressDetails()
+    {
+         AddressName = "ns=2;s=YT6022_VALUE_SJ2147_W_P3",
+        AddressDataType = YSAI.Core.@enum.DataType.Float
+    }
+};
+
+
+
+OpcUaClientOperate opcUaClientOperate = OpcUaClientOperate.Instance(new OpcUaClientData.Basics
+{
+    CustomName = Guid.NewGuid().ToString(),
+    Password = "ysai",
+    ServerUrl = "opc.tcp://127.0.0.1:8866/Opc.Ua.Service",
+    UserName = "ysai",
+    SN = Guid.NewGuid().ToString()
+});
+opcUaClientOperate.OnEvent += delegate (object? sender, EventResult e) { OpcUaClientOperate_OnEvent(sender, e, "a"); };
+Console.WriteLine(opcUaClientOperate.OnAsync().Result.ToJson().JsonFormatting());
+Console.WriteLine(opcUaClientOperate.AddSubscribe(new System.Collections.Concurrent.ConcurrentDictionary<string, Address>() { ["ysai"] = address }).ToJson().JsonFormatting());
+
+
+
+OpcUaClientOperate opcUaClientOperate_2 = OpcUaClientOperate.Instance(new OpcUaClientData.Basics
+{
+    CustomName = Guid.NewGuid().ToString(),
+    Password = "ysai",
+    ServerUrl = "opc.tcp://127.0.0.1:8866/Opc.Ua.Service",
+    UserName = "ysai",
+    SN = Guid.NewGuid().ToString()
+});
+opcUaClientOperate_2.OnEvent += delegate (object? sender, EventResult e) { OpcUaClientOperate_OnEvent(sender, e, "b"); };
+Console.WriteLine(opcUaClientOperate_2.OnAsync().Result.ToJson().JsonFormatting());
+Console.WriteLine(opcUaClientOperate_2.AddSubscribe(new System.Collections.Concurrent.ConcurrentDictionary<string, Address>() { ["ysai"] = address }).ToJson().JsonFormatting());
+
+void OpcUaClientOperate_OnEvent(object? sender, EventResult e, string name)
+{
+    if (name.Equals("a"))
+    {
+        Console.ForegroundColor = ConsoleColor.Green;
+    }
+    else
+    {
+        Console.ForegroundColor = ConsoleColor.Red;
+    }
+    Console.WriteLine(e.ToJson().JsonFormatting());
+}
+
+while (true)
+{
+    Console.ReadKey();
+}
 
 //TcpClientOperate tcpClientOperate = TcpClientOperate.Instance(new TcpClientData.Basics
 //{

+ 4 - 0
src/YSAI.DAQ/YSAI.Test.All/YSAI.Test.All.csproj

@@ -12,4 +12,8 @@
     <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.7.0" />
   </ItemGroup>
 
+  <ItemGroup>
+    <ProjectReference Include="..\YSAI.Opc\YSAI.Opc.csproj" />
+  </ItemGroup>
+
 </Project>

+ 61 - 1
src/YSAI.DAQ/YSAI.Test.Console/Program.cs

@@ -1,4 +1,64 @@
-
+using YSAI.Core.data;
+using YSAI.Core.@enum;
+using YSAI.Opc.ua.client;
+using YSAI.Unility;
+
+
+//点位地址
+Address address = new Address();
+address.SN = Guid.NewGuid().ToString();
+address.CreationTime = DateTime.Now.ToLocalTime();
+address.AddressArray = new List<AddressDetails> {
+    new AddressDetails()
+    {
+        AddressName = "ns=2;s=YT6022_VALUE_SJ2147_P_CUR_9",
+        AddressDataType = YSAI.Core.@enum.DataType.Float
+    },
+    new AddressDetails()
+    {
+         AddressName = "ns=2;s=YT6022_VALUE_SJ2147_W_P3",
+        AddressDataType = YSAI.Core.@enum.DataType.Float
+    }
+};
+
+
+
+OpcUaClientOperate opcUaClientOperate = OpcUaClientOperate.Instance(new OpcUaClientData.Basics
+{
+    CustomName = Guid.NewGuid().ToString(),
+    Password = "ysai",
+    ServerUrl = "opc.tcp://127.0.0.1:8866/Opc.Ua.Service",
+    UserName = "ysai",
+    SN = Guid.NewGuid().ToString()
+});
+opcUaClientOperate.OnEvent += delegate (object? sender, EventResult e) { OpcUaClientOperate_OnEvent(sender, e, "▲▲▲"); };
+Console.WriteLine(opcUaClientOperate.OnAsync().Result.ToJson().JsonFormatting());
+Console.WriteLine(opcUaClientOperate.AddSubscribe(new System.Collections.Concurrent.ConcurrentDictionary<string, Address>() { ["ysai"]= address }).ToJson().JsonFormatting());
+
+
+
+OpcUaClientOperate opcUaClientOperate2 = OpcUaClientOperate.Instance(new OpcUaClientData.Basics
+{
+    CustomName = Guid.NewGuid().ToString(),
+    Password = "ysai",
+    ServerUrl = "opc.tcp://127.0.0.1:8866/Opc.Ua.Service",
+    UserName = "ysai",
+    SN = Guid.NewGuid().ToString()
+});
+opcUaClientOperate2.OnEvent += delegate (object? sender, EventResult e) { OpcUaClientOperate_OnEvent(sender, e, "■■■"); };
+Console.WriteLine(opcUaClientOperate2.OnAsync().Result.ToJson().JsonFormatting());
+Console.WriteLine(opcUaClientOperate.AddSubscribe(new System.Collections.Concurrent.ConcurrentDictionary<string, Address>() { ["ysai"] = address }).ToJson().JsonFormatting());
+
+void OpcUaClientOperate_OnEvent(object? sender, EventResult e,string name)
+{
+    Console.WriteLine(name);
+    Console.WriteLine(e.ToJson().JsonFormatting());
+}
+
+while (true)
+{
+    Console.ReadKey();
+}
 
 ////采集
 //using (类 operate = new 类(new 类数据.Basics