iotgateway/Plugins/Drivers/DriverOPCUaClient/OPCUaClient.cs

133 lines
3.6 KiB
C#
Raw Normal View History

using PluginInterface;
using Opc.Ua;
using Microsoft.Extensions.Logging;
using OpcUaHelper;
namespace DriverOPCUaClient
{
[DriverSupported("OPC UA")]
2022-08-10 08:53:36 +00:00
[DriverInfo("OPCUaClient", "V1.0.0", "Copyright IoTGateway© 2021-12-19")]
public class OPCUaClient : IDriver
{
2022-08-10 08:53:36 +00:00
private OpcUaClientHelper? opcUaClient;
public ILogger _logger { get; set; }
2022-08-08 07:15:09 +00:00
private readonly string _device;
2022-08-10 08:53:36 +00:00
#region
2022-08-10 08:53:36 +00:00
[ConfigParameter("设备Id")] public string DeviceId { get; set; }
[ConfigParameter("uri")]
public string Uri { get; set; } = "opc.tcp://localhost:62541/Quickstarts/ReferenceServer";
2022-08-10 08:53:36 +00:00
[ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 3000;
2022-08-10 08:53:36 +00:00
[ConfigParameter("最小通讯周期ms")] public uint MinPeriod { get; set; } = 3000;
#endregion
2022-08-08 07:15:09 +00:00
public OPCUaClient(string device, ILogger logger)
{
_device = device;
_logger = logger;
2022-08-08 07:15:09 +00:00
_logger.LogInformation($"Device:[{_device}],Create()");
}
public bool IsConnected
{
2022-08-10 08:53:36 +00:00
get { return opcUaClient != null && opcUaClient.Connected; }
}
public bool Connect()
{
try
{
opcUaClient = new OpcUaClientHelper();
opcUaClient.ConnectServer(Uri).Wait(Timeout);
}
catch (Exception)
{
return false;
}
2022-08-10 08:53:36 +00:00
return IsConnected;
}
public bool Close()
{
try
{
opcUaClient?.Disconnect();
return !IsConnected;
}
catch (Exception)
{
return false;
}
}
public void Dispose()
{
try
{
opcUaClient = null;
}
catch (Exception)
{
}
}
[Method("读OPCUa", description: "读OPCUa节点")]
public DriverReturnValueModel ReadNode(DriverAddressIoArgModel ioarg)
{
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
if (IsConnected)
{
try
{
2022-08-10 08:53:36 +00:00
var dataValue = opcUaClient?.ReadNode(new NodeId(ioarg.Address));
if (DataValue.IsGood(dataValue))
2022-08-10 08:53:36 +00:00
ret.Value = dataValue?.Value;
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = $"读取失败,{ex.Message}";
}
}
else
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = "连接失败";
}
2022-08-10 08:53:36 +00:00
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 = "连接失败";
}
2022-08-10 08:53:36 +00:00
return ret;
}
2022-04-13 09:01:24 +00:00
2022-08-10 08:53:36 +00:00
public async Task<RpcResponse> WriteAsync(string requestId, string method, DriverAddressIoArgModel ioarg)
2022-04-13 09:01:24 +00:00
{
RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" };
return rpcResponse;
}
}
2022-08-10 08:53:36 +00:00
}