iotgateway/Plugins/Drivers/OPC.UaClient/DeviceUaClient.cs

165 lines
4.7 KiB
C#

using Opc.Ua;
using OpcUaHelper;
using PluginInterface;
using Microsoft.Extensions.Logging;
namespace OPC.UaClient
{
[DriverSupported("OPCUaClient")]
[DriverInfo("OPCUaClient", "V1.0.0", "Copyright IoTGateway.net 20230220")]
public class DeviceUaClient : IDriver
{
private OpcUaClientHelper? _opcUaClient;
public ILogger _logger { get; set; }
private readonly string _device;
#region
[ConfigParameter("设备Id")] public string DeviceId { get; set; }
[ConfigParameter("uri")]
public string Uri { get; set; } = "opc.tcp://localhost:62541/Quickstarts/ReferenceServer";
[ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 3000;
[ConfigParameter("最小通讯周期ms")] public uint MinPeriod { get; set; } = 3000;
#endregion
#region
/// <summary>
/// 反射构造函数
/// </summary>
/// <param name="device"></param>
/// <param name="logger"></param>
public DeviceUaClient(string device, ILogger logger)
{
_device = device;
_logger = logger;
_logger.LogInformation($"Device:[{_device}],Create()");
}
/// <summary>
/// 连接状态
/// </summary>
public bool IsConnected => _opcUaClient is { Connected: true };
/// <summary>
/// 连接
/// </summary>
/// <returns></returns>
public bool Connect()
{
try
{
_logger.LogInformation($"Device:[{_device}],Connect()");
_opcUaClient = new OpcUaClientHelper();
_opcUaClient.ConnectServer(Uri).Wait(Timeout);
}
catch (Exception ex)
{
_logger.LogInformation($"Device:[{_device}],Connect()");
return false;
}
return IsConnected;
}
/// <summary>
/// 断开
/// </summary>
/// <returns></returns>
public bool Close()
{
try
{
_logger.LogInformation($"Device:[{_device}],Close()");
_opcUaClient?.Disconnect();
return !IsConnected;
}
catch (Exception ex)
{
_logger.LogError(ex, $"Device:[{_device}],Close(),Error");
return false;
}
}
/// <summary>
/// 释放
/// </summary>
public void Dispose()
{
try
{
_logger.LogInformation($"Device:[{_device}],Dispose()");
_opcUaClient = null;
// Suppress finalization.
GC.SuppressFinalize(this);
}
catch (Exception ex)
{
_logger.LogError(ex, $"Device:[{_device}],Dispose(),Error");
}
}
#endregion
[Method("读OPCUa", description: "读OPCUa节点")]
public DriverReturnValueModel ReadNode(DriverAddressIoArgModel ioArg)
{
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
if (IsConnected)
{
try
{
var dataValue = _opcUaClient?.ReadNode(new NodeId(ioArg.Address));
if (DataValue.IsGood(dataValue))
ret.Value = dataValue?.Value;
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = $"读取失败,{ex.Message}";
}
}
else
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = "连接失败";
}
return ret;
}
[Method("测试方法", description: "测试方法,返回当前时间")]
public DriverReturnValueModel Read(DriverAddressIoArgModel ioArg)
{
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
if (IsConnected)
ret.Value = DateTime.Now;
else
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = "连接失败";
}
return ret;
}
public async Task<RpcResponse> WriteAsync(string requestId, string method, DriverAddressIoArgModel ioArg)
{
RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" };
await Task.CompletedTask;
return rpcResponse;
}
}
}