| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- 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<object, bool> ConnectChangedEvent;
- // public override event Action<object, string> 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<object> 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));
- //}
- }
|