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 生命周期 /// /// 反射构造函数 /// /// /// public DeviceUaClient(string device, ILogger logger) { _device = device; _logger = logger; _logger.LogInformation($"Device:[{_device}],Create()"); } /// /// 连接状态 /// public bool IsConnected => _opcUaClient is { Connected: true }; /// /// 连接 /// /// 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; } /// /// 断开 /// /// 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; } } /// /// 释放 /// 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 WriteAsync(string requestId, string method, DriverAddressIoArgModel ioArg) { RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" }; await Task.CompletedTask; return rpcResponse; } } }