Files
ThingsGateway/framework/Plugin/ThingsGateway.Plugin.OPCUA/Page/OPCUAClientPage.razor.cs
2023-10-18 13:09:25 +08:00

86 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#region copyright
//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
// 使用文档https://diego2098.gitee.io/thingsgateway-docs/
// QQ群605534569
//------------------------------------------------------------------------------
#endregion
using ThingsGateway.Foundation.Adapter.OPCUA;
namespace ThingsGateway.Foundation.Demo;
/// <summary>
/// OPCUAClientPage
/// </summary>
public partial class OPCUAClientPage
{
/// <summary>
/// 日志输出
/// </summary>
public Action<LogLevel, object, string, Exception> LogAction;
/// <summary>
/// OPC
/// </summary>
public ThingsGateway.Foundation.Adapter.OPCUA.OPCUAClient OPC;
private readonly OPCNode node = new();
/// <summary>
/// <inheritdoc/>
/// </summary>
public void Dispose()
{
OPC.OpcStatusChange -= OPC_OpcStatusChange;
OPC.SafeDispose();
}
/// <inheritdoc/>
protected override void OnInitialized()
{
OPC = new ThingsGateway.Foundation.Adapter.OPCUA.OPCUAClient();
OPC.OpcStatusChange += OPC_OpcStatusChange;
base.OnInitialized();
}
private void OPC_OpcStatusChange(object sender, OpcUaStatusEventArgs e)
{
if (e.LogLevel > 0)
LogAction?.Invoke((LogLevel)e.LogLevel, null, e.Text, null);
}
private async Task ConnectAsync()
{
try
{
OPC.Disconnect();
await GetOPCClient().ConnectAsync();
}
catch (Exception ex)
{
LogAction?.Invoke(LogLevel.Error, null, null, ex);
}
}
private void DisConnect()
{
try
{
OPC.Disconnect();
}
catch (Exception ex)
{
LogAction?.Invoke(LogLevel.Error, null, null, ex);
}
}
private ThingsGateway.Foundation.Adapter.OPCUA.OPCUAClient GetOPCClient()
{
OPC.OPCNode = node;
return OPC;
}
}