优化OPCUA驱动

This commit is contained in:
iioter 2022-08-10 16:53:36 +08:00
parent 4076f65e85
commit 065bd63fa3

View File

@ -1,38 +1,28 @@
using PluginInterface; using PluginInterface;
using System;
using System.Collections;
using System.IO;
using Opc.Ua; using Opc.Ua;
using Opc.Ua.Client;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Opc.Ua.Configuration;
using OpcUaHelper; using OpcUaHelper;
namespace DriverOPCUaClient namespace DriverOPCUaClient
{ {
[DriverSupported("OPC UA")] [DriverSupported("OPC UA")]
[DriverInfoAttribute("OPCUaClient", "V1.0.0", "Copyright IoTGateway© 2021-12-19")] [DriverInfo("OPCUaClient", "V1.0.0", "Copyright IoTGateway© 2021-12-19")]
public class OPCUaClient : IDriver public class OPCUaClient : IDriver
{ {
OpcUaClientHelper opcUaClient = null; private OpcUaClientHelper? opcUaClient;
public ILogger _logger { get; set; } public ILogger _logger { get; set; }
private readonly string _device; private readonly string _device;
#region #region
[ConfigParameter("设备Id")] [ConfigParameter("设备Id")] public string DeviceId { get; set; }
public Guid DeviceId { get; set; }
[ConfigParameter("uri")] [ConfigParameter("uri")]
public string Uri { get; set; } = "opc.tcp://localhost:62541/Quickstarts/ReferenceServer"; public string Uri { get; set; } = "opc.tcp://localhost:62541/Quickstarts/ReferenceServer";
[ConfigParameter("超时时间ms")] [ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 3000;
public int Timeout { get; set; } = 3000;
[ConfigParameter("最小通讯周期ms")] [ConfigParameter("最小通讯周期ms")] public uint MinPeriod { get; set; } = 3000;
public uint MinPeriod { get; set; } = 3000;
#endregion #endregion
@ -47,11 +37,7 @@ namespace DriverOPCUaClient
public bool IsConnected public bool IsConnected
{ {
get get { return opcUaClient != null && opcUaClient.Connected; }
{
return opcUaClient != null && opcUaClient.Connected;
}
} }
public bool Connect() public bool Connect()
@ -65,6 +51,7 @@ namespace DriverOPCUaClient
{ {
return false; return false;
} }
return IsConnected; return IsConnected;
} }
@ -77,7 +64,6 @@ namespace DriverOPCUaClient
} }
catch (Exception) catch (Exception)
{ {
return false; return false;
} }
} }
@ -90,7 +76,6 @@ namespace DriverOPCUaClient
} }
catch (Exception) catch (Exception)
{ {
} }
} }
@ -104,9 +89,9 @@ namespace DriverOPCUaClient
{ {
try try
{ {
var dataValue = opcUaClient.ReadNode(new NodeId(ioarg.Address)); var dataValue = opcUaClient?.ReadNode(new NodeId(ioarg.Address));
if (DataValue.IsGood(dataValue)) if (DataValue.IsGood(dataValue))
ret.Value = dataValue.Value; ret.Value = dataValue?.Value;
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -119,6 +104,7 @@ namespace DriverOPCUaClient
ret.StatusType = VaribaleStatusTypeEnum.Bad; ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = "连接失败"; ret.Message = "连接失败";
} }
return ret; return ret;
} }
@ -134,10 +120,11 @@ namespace DriverOPCUaClient
ret.StatusType = VaribaleStatusTypeEnum.Bad; ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = "连接失败"; ret.Message = "连接失败";
} }
return ret; return ret;
} }
public async Task<RpcResponse> WriteAsync(string RequestId, string Method, DriverAddressIoArgModel Ioarg) public async Task<RpcResponse> WriteAsync(string requestId, string method, DriverAddressIoArgModel ioarg)
{ {
RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" }; RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" };
return rpcResponse; return rpcResponse;