From 63788a1fd4809859af165e0ac46a101317334d85 Mon Sep 17 00:00:00 2001 From: luosheng Date: Sat, 3 Jun 2023 14:51:04 +0800 Subject: [PATCH] OPC function change --- .../Modbus.Net.Opc/AddressFormaterOpc.cs | 10 +--- Modbus.Net/Modbus.Net.Opc/ClientExtend.cs | 11 +++- .../Modbus.Net.Opc/Modbus.Net.Opc.csproj | 5 ++ Modbus.Net/Modbus.Net.Opc/OpcConnector.cs | 58 +++---------------- Modbus.Net/Modbus.Net.Opc/OpcDaConnector.cs | 8 +-- Modbus.Net/Modbus.Net.Opc/OpcDaProtocal.cs | 8 +-- .../Modbus.Net.Opc/OpcDaProtocalLinker.cs | 8 +-- Modbus.Net/Modbus.Net.Opc/OpcMachine.cs | 7 +-- Modbus.Net/Modbus.Net.Opc/OpcProtocol.cs | 24 ++------ Modbus.Net/Modbus.Net.Opc/OpcUaConnector.cs | 8 +-- Modbus.Net/Modbus.Net.Opc/OpcUaProtocol.cs | 8 +-- .../Modbus.Net.Opc/OpcUaProtocolLinker.cs | 8 +-- Modbus.Net/Modbus.Net.Opc/OpcUtility.cs | 31 ++-------- .../Modbus.Net/Machine/AddressCombiner.cs | 3 +- Samples/MachineJob/ConsoleLogProvider.cs | 2 +- 15 files changed, 55 insertions(+), 144 deletions(-) diff --git a/Modbus.Net/Modbus.Net.Opc/AddressFormaterOpc.cs b/Modbus.Net/Modbus.Net.Opc/AddressFormaterOpc.cs index 045f999..2b0e8c6 100644 --- a/Modbus.Net/Modbus.Net.Opc/AddressFormaterOpc.cs +++ b/Modbus.Net/Modbus.Net.Opc/AddressFormaterOpc.cs @@ -14,14 +14,11 @@ namespace Modbus.Net.Opc /// /// 如何通过BaseMachine和AddressUnit构造Opc的标签 /// 调用这个编码器的设备 - /// 每两个标签之间用什么符号隔开,默认为/ public AddressFormaterOpc(Func, AddressUnit, string[]> tagGeter, - BaseMachine machine, - char seperator = '/') + BaseMachine machine) { Machine = machine; TagGeter = tagGeter; - Seperator = seperator; } /// @@ -35,11 +32,6 @@ namespace Modbus.Net.Opc /// protected Func, AddressUnit, string[]> TagGeter { get; set; } - /// - /// 分割符 - /// - public char Seperator { get; protected set; } - /// /// 编码地址 /// diff --git a/Modbus.Net/Modbus.Net.Opc/ClientExtend.cs b/Modbus.Net/Modbus.Net.Opc/ClientExtend.cs index d68c1c2..38c9027 100644 --- a/Modbus.Net/Modbus.Net.Opc/ClientExtend.cs +++ b/Modbus.Net/Modbus.Net.Opc/ClientExtend.cs @@ -3,7 +3,9 @@ using Hylasoft.Opc.Da; using Hylasoft.Opc.Ua; using System; using System.Collections.Generic; +using System.Reflection; using System.Threading.Tasks; +using URL = Opc.URL; namespace Modbus.Net.Opc { @@ -76,6 +78,13 @@ namespace Modbus.Net.Opc /// Url address of Opc UA server public MyDaClient(Uri serverUrl) : base(serverUrl) { + var url = new URL(serverUrl.OriginalString) + { + Scheme = serverUrl.Scheme, + HostName = serverUrl.Host + }; + + typeof(DaClient).GetField("_url", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(this, url); } /// @@ -115,7 +124,7 @@ namespace Modbus.Net.Opc /// /// Tag of a node /// - public string[] Tag { get; set; } + public string Tag { get; set; } /// /// Tag splitter of a node diff --git a/Modbus.Net/Modbus.Net.Opc/Modbus.Net.Opc.csproj b/Modbus.Net/Modbus.Net.Opc/Modbus.Net.Opc.csproj index 93aefd7..4432066 100644 --- a/Modbus.Net/Modbus.Net.Opc/Modbus.Net.Opc.csproj +++ b/Modbus.Net/Modbus.Net.Opc/Modbus.Net.Opc.csproj @@ -23,12 +23,17 @@ MIT README.md snupkg + AnyCPU;x86 bin\Debug\Modbus.Net.Opc.xml + + bin\Debug\Modbus.Net.Opc.xml + + diff --git a/Modbus.Net/Modbus.Net.Opc/OpcConnector.cs b/Modbus.Net/Modbus.Net.Opc/OpcConnector.cs index ac35e32..f7a218b 100644 --- a/Modbus.Net/Modbus.Net.Opc/OpcConnector.cs +++ b/Modbus.Net/Modbus.Net.Opc/OpcConnector.cs @@ -1,9 +1,6 @@ -using Hylasoft.Opc.Common; -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging; using System; -using System.Collections.Generic; using System.Text; -using System.Text.RegularExpressions; using System.Threading.Tasks; namespace Modbus.Net.Opc @@ -25,20 +22,13 @@ namespace Modbus.Net.Opc /// protected IClientExtend Client; - /// - /// 是否开启正则匹配 - /// - protected bool RegexOn { get; set; } - /// /// 构造函数 /// /// 服务端url - /// 是否开启正则匹配 - protected OpcConnector(string host, bool isRegexOn) + protected OpcConnector(string host) { ConnectionToken = host; - RegexOn = isRegexOn; } /// @@ -102,14 +92,11 @@ namespace Modbus.Net.Opc { if (message.IsRead) { - var split = message.Split; var tag = message.Tag; - var rootDirectory = await Client.ExploreFolderAsync(""); - var answerTag = await SearchTag(tag, split, 0, rootDirectory); - if (answerTag != null) + if (tag != null) { - var result = await Client.ReadAsync(answerTag); - logger.LogDebug($"Opc Machine {ConnectionToken} Read Opc tag {answerTag} for value {result.Value}"); + var result = await Client.ReadAsync(tag); + logger.LogInformation($"Opc Machine {ConnectionToken} Read Opc tag {tag} for value {result.Value}"); return new OpcParamOut { Success = true, @@ -125,17 +112,14 @@ namespace Modbus.Net.Opc else { var tag = message.Tag; - var split = message.Split; var value = message.SetValue; - - var rootDirectory = await Client.ExploreFolderAsync(""); - var answerTag = await SearchTag(tag, split, 0, rootDirectory); - if (answerTag != null) + ; + if (tag != null) { try { - await Client.WriteAsync(answerTag, value); - logger.LogDebug($"Opc Machine {ConnectionToken} Write Opc tag {answerTag} for value {value}"); + await Client.WriteAsync(tag, value); + logger.LogInformation($"Opc Machine {ConnectionToken} Write Opc tag {tag} for value {value}"); } catch (Exception e) { @@ -167,30 +151,6 @@ namespace Modbus.Net.Opc } } - /// - /// 搜索标签 - /// - /// 标签 - /// 分隔符 - /// 递归深度(第几级标签) - /// 当前搜索的节点 - /// 搜索到的标签 - private async Task SearchTag(string[] tags, char split, int deep, IEnumerable nodes) - { - foreach (var node in nodes) - { - var currentTag = node.Tag.Substring(node.Tag.LastIndexOf(split) + 1); - if (RegexOn && Regex.IsMatch(currentTag, tags[deep]) || !RegexOn && currentTag == tags[deep]) - { - if (deep == tags.Length - 1) return node.Tag; - var subDirectories = await Client.ExploreFolderAsync(node.Tag); - var answerTag = await SearchTag(tags, split, deep + 1, subDirectories); - if (answerTag != null) return answerTag; - } - } - return null; - } - private bool Connect() { try diff --git a/Modbus.Net/Modbus.Net.Opc/OpcDaConnector.cs b/Modbus.Net/Modbus.Net.Opc/OpcDaConnector.cs index 12fa468..c30600a 100644 --- a/Modbus.Net/Modbus.Net.Opc/OpcDaConnector.cs +++ b/Modbus.Net/Modbus.Net.Opc/OpcDaConnector.cs @@ -17,8 +17,7 @@ namespace Modbus.Net.Opc /// 构造函数 /// /// Opc DA 服务地址 - /// 是否开启正则匹配 - protected OpcDaConnector(string host, bool isRegexOn) : base(host, isRegexOn) + protected OpcDaConnector(string host) : base(host) { Client = new MyDaClient(new Uri(ConnectionToken)); } @@ -27,13 +26,12 @@ namespace Modbus.Net.Opc /// 根据服务地址生成DA单例 /// /// Opc DA 服务地址 - /// 是否开启正则匹配 /// Opc DA 连接器实例 - public static OpcDaConnector Instance(string host, bool isRegexOn) + public static OpcDaConnector Instance(string host) { if (!_instances.ContainsKey(host)) { - var connector = new OpcDaConnector(host, isRegexOn); + var connector = new OpcDaConnector(host); _instances.Add(host, connector); } return _instances[host]; diff --git a/Modbus.Net/Modbus.Net.Opc/OpcDaProtocal.cs b/Modbus.Net/Modbus.Net.Opc/OpcDaProtocal.cs index 330b54f..364534d 100644 --- a/Modbus.Net/Modbus.Net.Opc/OpcDaProtocal.cs +++ b/Modbus.Net/Modbus.Net.Opc/OpcDaProtocal.cs @@ -9,17 +9,13 @@ namespace Modbus.Net.Opc { private readonly string _host; - private readonly bool _isRegexOn; - /// /// 构造函数 /// /// Opc DA服务地址 - /// 是否开启正则匹配 - public OpcDaProtocol(string host, bool isRegexOn) + public OpcDaProtocol(string host) { _host = host; - _isRegexOn = isRegexOn; } @@ -29,7 +25,7 @@ namespace Modbus.Net.Opc /// 是否连接成功 public override async Task ConnectAsync() { - ProtocolLinker = new OpcDaProtocolLinker(_host, _isRegexOn); + ProtocolLinker = new OpcDaProtocolLinker(_host); if (!await ProtocolLinker.ConnectAsync()) return false; return true; diff --git a/Modbus.Net/Modbus.Net.Opc/OpcDaProtocalLinker.cs b/Modbus.Net/Modbus.Net.Opc/OpcDaProtocalLinker.cs index caec5d4..0e0759d 100644 --- a/Modbus.Net/Modbus.Net.Opc/OpcDaProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net.Opc/OpcDaProtocalLinker.cs @@ -8,8 +8,7 @@ /// /// 构造函数 /// - /// 是否开启正则匹配 - public OpcDaProtocolLinker(bool isRegexOn) : this(ConfigurationReader.GetValueDirect("OpcDa", "Host"), isRegexOn) + public OpcDaProtocolLinker() : this(ConfigurationReader.GetValueDirect("OpcDa", "Host")) { } @@ -17,10 +16,9 @@ /// 构造函数 /// /// Opc DA服务地址 - /// 是否开启正则匹配 - public OpcDaProtocolLinker(string host, bool isRegexOn) + public OpcDaProtocolLinker(string host) { - BaseConnector = OpcDaConnector.Instance(host, isRegexOn); + BaseConnector = OpcDaConnector.Instance(host); } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Opc/OpcMachine.cs b/Modbus.Net/Modbus.Net.Opc/OpcMachine.cs index 3769471..17a9fd1 100644 --- a/Modbus.Net/Modbus.Net.Opc/OpcMachine.cs +++ b/Modbus.Net/Modbus.Net.Opc/OpcMachine.cs @@ -16,14 +16,11 @@ namespace Modbus.Net.Opc /// 连接类型 /// 连接地址 /// 需要读写的地址 - /// 开启正则匹配 - public OpcMachine(TKey id, OpcType connectionType, string connectionString, IEnumerable> getAddresses, bool isRegexOn = false) + public OpcMachine(TKey id, OpcType connectionType, string connectionString, IEnumerable> getAddresses) : base(id, getAddresses, true) { - BaseUtility = new OpcUtility(connectionType, connectionString, isRegexOn); + BaseUtility = new OpcUtility(connectionType, connectionString); AddressFormater = new AddressFormaterOpc((machine, unit) => { return new string[] { unit.Area }; }, this); - ((OpcUtility)BaseUtility).GetSeperator += - () => ((AddressFormaterOpc)AddressFormater).Seperator; AddressCombiner = new AddressCombinerSingle(); AddressCombinerSet = new AddressCombinerSingle(); } diff --git a/Modbus.Net/Modbus.Net.Opc/OpcProtocol.cs b/Modbus.Net/Modbus.Net.Opc/OpcProtocol.cs index cea9f7b..9af6609 100644 --- a/Modbus.Net/Modbus.Net.Opc/OpcProtocol.cs +++ b/Modbus.Net/Modbus.Net.Opc/OpcProtocol.cs @@ -26,22 +26,15 @@ /// 构造函数 /// /// 标签 - /// 分隔符 - public ReadRequestOpcInputStruct(string[] tag, char split) + public ReadRequestOpcInputStruct(string tag) { Tag = tag; - Split = split; } /// /// 标签 /// - public string[] Tag { get; } - - /// - /// 分隔符 - /// - public char Split { get; } + public string Tag { get; } } /// @@ -82,7 +75,6 @@ { IsRead = true, Tag = r_message.Tag, - Split = r_message.Split }; } @@ -111,24 +103,17 @@ /// 构造函数 /// /// 标签 - /// 分隔符 /// 写入的数据 - public WriteRequestOpcInputStruct(string[] tag, char split, object setValue) + public WriteRequestOpcInputStruct(string tag, object setValue) { Tag = tag; - Split = split; SetValue = setValue; } /// /// 标签 /// - public string[] Tag { get; } - - /// - /// 分隔符 - /// - public char Split { get; } + public string Tag { get; } /// /// 写入的数据 @@ -174,7 +159,6 @@ { IsRead = false, Tag = r_message.Tag, - Split = r_message.Split, SetValue = r_message.SetValue }; } diff --git a/Modbus.Net/Modbus.Net.Opc/OpcUaConnector.cs b/Modbus.Net/Modbus.Net.Opc/OpcUaConnector.cs index 75ad592..228c495 100644 --- a/Modbus.Net/Modbus.Net.Opc/OpcUaConnector.cs +++ b/Modbus.Net/Modbus.Net.Opc/OpcUaConnector.cs @@ -17,8 +17,7 @@ namespace Modbus.Net.Opc /// 构造函数 /// /// Opc UA 服务地址 - /// 是否开启正则匹配 - protected OpcUaConnector(string host, bool isRegexOn) : base(host, isRegexOn) + protected OpcUaConnector(string host) : base(host) { Client = new MyUaClient(new Uri(ConnectionToken)); } @@ -27,13 +26,12 @@ namespace Modbus.Net.Opc /// 根据地址获取UA连接器单例 /// /// Opc UA服务地址 - /// 是否开启正则匹配 /// Opc UA实例 - public static OpcUaConnector Instance(string host, bool isRegexOn) + public static OpcUaConnector Instance(string host) { if (!_instances.ContainsKey(host)) { - var connector = new OpcUaConnector(host, isRegexOn); + var connector = new OpcUaConnector(host); _instances.Add(host, connector); } return _instances[host]; diff --git a/Modbus.Net/Modbus.Net.Opc/OpcUaProtocol.cs b/Modbus.Net/Modbus.Net.Opc/OpcUaProtocol.cs index 3e9dbac..7dfcefd 100644 --- a/Modbus.Net/Modbus.Net.Opc/OpcUaProtocol.cs +++ b/Modbus.Net/Modbus.Net.Opc/OpcUaProtocol.cs @@ -9,17 +9,13 @@ namespace Modbus.Net.Opc { private readonly string _host; - private readonly bool _isRegexOn; - /// /// 构造函数 /// /// Opc UA服务地址 - /// 是否开启正则匹配 - public OpcUaProtocol(string host, bool isRegexOn) + public OpcUaProtocol(string host) { _host = host; - _isRegexOn = isRegexOn; } /// @@ -28,7 +24,7 @@ namespace Modbus.Net.Opc /// 是否连接成功 public override async Task ConnectAsync() { - ProtocolLinker = new OpcUaProtocolLinker(_host, _isRegexOn); + ProtocolLinker = new OpcUaProtocolLinker(_host); if (!await ProtocolLinker.ConnectAsync()) return false; return true; } diff --git a/Modbus.Net/Modbus.Net.Opc/OpcUaProtocolLinker.cs b/Modbus.Net/Modbus.Net.Opc/OpcUaProtocolLinker.cs index acb1632..188f0d2 100644 --- a/Modbus.Net/Modbus.Net.Opc/OpcUaProtocolLinker.cs +++ b/Modbus.Net/Modbus.Net.Opc/OpcUaProtocolLinker.cs @@ -8,8 +8,7 @@ /// /// 构造函数 /// - /// 是否开启正则匹配 - public OpcUaProtocolLinker(bool isRegexOn) : this(ConfigurationReader.GetValueDirect("OpcUa", "Host"), isRegexOn) + public OpcUaProtocolLinker() : this(ConfigurationReader.GetValueDirect("OpcUa", "Host")) { } @@ -17,10 +16,9 @@ /// 构造函数 /// /// Opc UA服务地址 - /// 是否开启正则匹配 - public OpcUaProtocolLinker(string host, bool isRegexOn) + public OpcUaProtocolLinker(string host) { - BaseConnector = OpcUaConnector.Instance(host, isRegexOn); + BaseConnector = OpcUaConnector.Instance(host); } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Opc/OpcUtility.cs b/Modbus.Net/Modbus.Net.Opc/OpcUtility.cs index 8712cef..ba361d2 100644 --- a/Modbus.Net/Modbus.Net.Opc/OpcUtility.cs +++ b/Modbus.Net/Modbus.Net.Opc/OpcUtility.cs @@ -30,8 +30,6 @@ namespace Modbus.Net.Opc private OpcType _opcType; - private bool IsRegexOn { get; set; } - /// /// 协议类型 /// @@ -46,35 +44,27 @@ namespace Modbus.Net.Opc //Da协议 case OpcType.Da: { - Wrapper = new OpcDaProtocol(ConnectionString, IsRegexOn); + Wrapper = new OpcDaProtocol(ConnectionString); break; } //Ua协议 case OpcType.Ua: { - Wrapper = new OpcUaProtocol(ConnectionString, IsRegexOn); + Wrapper = new OpcUaProtocol(ConnectionString); break; } } } } - /// - /// 获取分隔符 - /// - /// 分隔符 - public delegate char GetSeperatorDelegate(); - /// /// 构造函数 /// /// 连接类型 /// 连接地址 - /// 是否开启正则匹配 - public OpcUtility(int connectionType, string connectionString, bool isRegexOn = false) : base(0, 0) + public OpcUtility(int connectionType, string connectionString) : base(0, 0) { ConnectionString = connectionString; - IsRegexOn = isRegexOn; OpcType = (OpcType)connectionType; AddressTranslator = new AddressTranslatorOpc(); } @@ -84,11 +74,9 @@ namespace Modbus.Net.Opc /// /// 连接类型 /// 连接地址 - /// 是否开启正则匹配 - public OpcUtility(OpcType connectionType, string connectionString, bool isRegexOn = false) : base(0, 0) + public OpcUtility(OpcType connectionType, string connectionString) : base(0, 0) { ConnectionString = connectionString; - IsRegexOn = isRegexOn; OpcType = connectionType; AddressTranslator = new AddressTranslatorOpc(); } @@ -98,11 +86,6 @@ namespace Modbus.Net.Opc /// public override Endian Endian => Endian.BigEndianLsb; - /// - /// 获取分隔符 - /// - public event GetSeperatorDelegate GetSeperator; - /// /// 设置连接方式(Opc忽略该函数) /// @@ -122,8 +105,7 @@ namespace Modbus.Net.Opc { try { - var split = GetSeperator?.Invoke() ?? '/'; - var readRequestOpcInputStruct = new ReadRequestOpcInputStruct(startAddress.Split('\r'), split); + var readRequestOpcInputStruct = new ReadRequestOpcInputStruct(startAddress); var readRequestOpcOutputStruct = await Wrapper.SendReceiveAsync(Wrapper[typeof(ReadRequestOpcProtocol)], @@ -159,9 +141,8 @@ namespace Modbus.Net.Opc { try { - var split = GetSeperator?.Invoke() ?? '/'; var writeRequestOpcInputStruct = - new WriteRequestOpcInputStruct(startAddress.Split('\r'), split, setContents[0]); + new WriteRequestOpcInputStruct(startAddress, setContents[0]); var writeRequestOpcOutputStruct = await Wrapper.SendReceiveAsync(Wrapper[typeof(WriteRequestOpcProtocol)], diff --git a/Modbus.Net/Modbus.Net/Machine/AddressCombiner.cs b/Modbus.Net/Modbus.Net/Machine/AddressCombiner.cs index 849d7d5..179db88 100644 --- a/Modbus.Net/Modbus.Net/Machine/AddressCombiner.cs +++ b/Modbus.Net/Modbus.Net/Machine/AddressCombiner.cs @@ -1,5 +1,4 @@ -using Quartz.Util; -using System; +using System; using System.Collections.Generic; using System.Linq; diff --git a/Samples/MachineJob/ConsoleLogProvider.cs b/Samples/MachineJob/ConsoleLogProvider.cs index c65d043..9288d8b 100644 --- a/Samples/MachineJob/ConsoleLogProvider.cs +++ b/Samples/MachineJob/ConsoleLogProvider.cs @@ -9,7 +9,7 @@ namespace MachineJob { return (level, func, exception, parameters) => { - if (level >= Quartz.Logging.LogLevel.Info && func != null) + if (func != null) { Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] [" + level + "] " + func(), parameters); }