using System; using System.ComponentModel; using System.Threading.Tasks; using Newtonsoft.Json; using TeamAAS.Communication.Attributes; using TeamAAS.Communication.Base; using TeamAAS.Communication.Interfaces; namespace TeamAAS.Communication.Devices { //[Communication("OPC UA", "工业协议", "OPC 统一架构客户端(官方 OPC UA .NET 库)")] //public class OPCCommunication : BindableCommunicationBase //{ // [JsonIgnore] private ISessionClientMethods _session; // [JsonIgnore] private bool _connected; // public override string TypeKey => typeof(OPCCommunication).FullName; // private string _endpointUrl = "opc.tcp://localhost:4840"; // [Category("II.连接配置"), DisplayName("端点 URL"), Description("OPC UA 服务器地址,如 opc.tcp://localhost:4840")] // public override string EndpointUrl // { // get => _endpointUrl; // set => SetProperty(ref _endpointUrl, value); // } // private string _userName = ""; // [Category("II.连接配置"), DisplayName("用户名"), Description("空则匿名")] // public string UserName // { // get => _userName; // set => SetProperty(ref _userName, value); // } // private string _password = ""; // [Category("II.连接配置"), DisplayName("密码")] // [PasswordPropertyText] // public string Password // { // get => _password; // set => SetProperty(ref _password, value); // } // private int _timeout = 5000; // [Category("II.连接配置"), DisplayName("超时(ms)")] // public int Timeout // { // get => _timeout; // set => SetProperty(ref _timeout, value); // } // [JsonIgnore, Browsable(false)] // public override bool IsConnected => _connected; // public override event Action ConnectChangedEvent; // public override event Action DataReceivedEvent; // public OPCCommunication() { } // public override void Connect() // { // try // { // Disconnect(); // if (string.IsNullOrWhiteSpace(EndpointUrl)) // throw new InvalidOperationException("OPC UA 端点 URL 不能为空。"); // var config = new ApplicationConfiguration // { // ApplicationName = "TeamAAS.OpcUaClient", // ApplicationType = ApplicationType.Client, // TransportQuotas = new TransportQuotas { OperationTimeout = Timeout }, // ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 } // }; // var endpointDesc = CoreClientUtils.SelectEndpoint(EndpointUrl, false); // if (endpointDesc == null) // throw new InvalidOperationException("未发现可用的 OPC UA 端点。"); // var endpoint = new ConfiguredEndpoint(null, endpointDesc, EndpointConfiguration.Create(config)); // var factory = (ISessionFactory)Activator.CreateInstance(typeof(DefaultSessionFactory), true); // _session = factory.Create(config, null, endpoint, null, null, null); // IUserIdentity identity; // if (string.IsNullOrWhiteSpace(UserName)) // identity = new UserIdentity(new AnonymousIdentityToken()); // else // identity = new UserIdentity(UserName, Password); // var userIdentityToken = new ExtensionObject(identity.GetIdentityToken()); // _session.ActivateSession(null, null, null, null, userIdentityToken, null, out _, out _, out _); // _connected = true; // ConnectChangedEvent?.Invoke(this, true); // Notify(nameof(IsConnected)); // AppendLog($"已连接 {EndpointUrl}"); // } // catch (Exception ex) // { // Disconnect(); // throw new InvalidOperationException("OPC UA 连接失败: " + ex.Message, ex); // } // } // public override Task ConnectAsync() => Task.Run(() => Connect()); // public override void Disconnect() // { // _connected = false; // try // { // if (_session != null) // _session.CloseSession(null, true); // } // catch { } // _session = null; // ConnectChangedEvent?.Invoke(this, false); // Notify(nameof(IsConnected)); // } // public override void Dispose() => Disconnect(); // private NodeId ParseNodeId(string address) // { // if (string.IsNullOrWhiteSpace(address)) // throw new ArgumentException("NodeId 不能为空。", nameof(address)); // var trimmed = address.Trim(); // if (trimmed.StartsWith("ns=", StringComparison.OrdinalIgnoreCase) || // trimmed.StartsWith("s=", StringComparison.OrdinalIgnoreCase) || // trimmed.StartsWith("i=", StringComparison.OrdinalIgnoreCase)) // { // return new NodeId(trimmed); // } // return new NodeId("s=" + trimmed); // } // public override object ReadValue(string address) // { // if (_session == null || !_connected) // throw new InvalidOperationException("OPC UA 尚未连接。"); // var nodeId = ParseNodeId(address); // var nodesToRead = new ReadValueIdCollection // { // new ReadValueId { NodeId = nodeId, AttributeId = Opc.Ua.Attributes.Value } // }; // _session.Read(null, 0.0, TimestampsToReturn.Neither, nodesToRead, // out var dataValues, out _); // if (dataValues != null && dataValues.Count > 0 && StatusCode.IsGood(dataValues[0].StatusCode)) // return dataValues[0].Value; // throw new InvalidOperationException($"读取节点 {address} 失败"); // } // public override Task ReadValueAsync(string address) => Task.Run(() => ReadValue(address)); // public override void WriteValue(string address, object value) // { // if (_session == null || !_connected) // throw new InvalidOperationException("OPC UA 尚未连接。"); // if (value == null) // throw new ArgumentNullException(nameof(value)); // var nodeId = ParseNodeId(address); // var nodesToWrite = new WriteValueCollection // { // new WriteValue // { // NodeId = nodeId, // AttributeId = Opc.Ua.Attributes.Value, // Value = new DataValue(new Variant(value)) // } // }; // _session.Write(null, nodesToWrite, out var results, out _); // if (results != null && results.Count > 0 && !StatusCode.IsGood(results[0])) // throw new InvalidOperationException($"写入节点 {address} 失败: {results[0]}"); // } // public override Task WriteValueAsync(string address, object value) => Task.Run(() => WriteValue(address, value)); //} }