opcua添加初始化连接错误重试

This commit is contained in:
Kimdiego2098
2023-08-08 18:04:24 +08:00
parent 918ca449a1
commit 5e557ff0bc
5 changed files with 53 additions and 19 deletions

View File

@@ -199,14 +199,12 @@ public partial class DeviceStatusPage : IDisposable
// await MainLayout.StateHasChangedAsync();
// }
//}
bool timerR;
private async Task RunTimerAsync()
{
while (await _periodicTimer.WaitForNextTickAsync())
{
try
{
timerR = true;
{
_collectDeviceGroups = GlobalDeviceData.CollectDevices.Adapt<List<CollectDevice>>()?.Select(a => a.DeviceGroup)?.Where(a => a != null).Distinct()?.ToList() ?? new();
_collectDeviceCores = CollectDeviceHostService?.CollectDeviceCores?.WhereIF(!_collectDeviceGroup.IsNullOrEmpty(), a => a.Device?.DeviceGroup == _collectDeviceGroup).ToList() ?? new();
@@ -226,7 +224,6 @@ public partial class DeviceStatusPage : IDisposable
catch
{
}
timerR = false;
}
}

View File

@@ -117,7 +117,6 @@ public partial class DeviceVariableRunTimePage
}
}
}
bool timerR;
private Task<SqlSugarPagedList<DeviceVariableRunTime>> QueryCallAsync(VariablePageInput input)
@@ -138,13 +137,11 @@ public partial class DeviceVariableRunTimePage
{
try
{
timerR = true;
await InvokeAsync(StateHasChanged);
}
catch
{
}
timerR = false;
}
}

View File

@@ -454,13 +454,24 @@ public class OPCUAClient : DisposableObject
/// </summary>
public async Task ConnectAsync()
{
m_session = await ConnectAsync(OPCNode.OPCUrl);
await ConnectAsync(OPCNode.OPCUrl);
}
/// <summary>
/// 断开连接。
/// </summary>
public void Disconnect()
{
PrivateDisconnect();
// disconnect any existing session.
if (m_session != null)
{
Log.Debug("断开连接");
m_session = null;
}
}
private void PrivateDisconnect()
{
// stop any reconnect operation.
if (m_reConnectHandler != null)
@@ -468,15 +479,7 @@ public class OPCUAClient : DisposableObject
m_reConnectHandler.SafeDispose();
m_reConnectHandler = null;
}
// disconnect any existing session.
if (m_session != null)
{
Log.Debug("断开连接");
m_session.Close(10000);
m_session = null;
}
m_session?.Close(10000);
}
@@ -859,7 +862,7 @@ public class OPCUAClient : DisposableObject
/// <returns>The new session object.</returns>
private async Task<ISession> ConnectAsync(string serverUrl)
{
Disconnect();
PrivateDisconnect();
if (m_configuration == null)
{

View File

@@ -36,6 +36,34 @@ namespace ThingsGateway.OPCUA;
/// </summary>
public class OPCUAClient : CollectBase
{
readonly PeriodicTimer _periodicTimer = new(TimeSpan.FromSeconds(60));
/// <summary>
/// OPCUA客户端
/// </summary>
public OPCUAClient()
{
_ = RunTimerAsync();
}
private async Task RunTimerAsync()
{
while (await _periodicTimer.WaitForNextTickAsync())
{
if (PLC != null && PLC.Session == null)
{
try
{
await PLC.ConnectAsync();
}
catch (Exception ex)
{
LogMessage.Exception(ex);
}
}
}
}
internal CollectDeviceRunTime Device;
internal Foundation.Adapter.OPCUA.OPCUAClient PLC = null;
@@ -73,7 +101,11 @@ public class OPCUAClient : CollectBase
}
/// <inheritdoc/>
public override bool IsConnected() => PLC.Connected;
public override bool IsConnected()
{
return PLC.Connected;
}
/// <inheritdoc/>
public override List<DeviceVariableSourceRead> LoadSourceRead(List<DeviceVariableRunTime> deviceVariables)
@@ -99,7 +131,6 @@ public class OPCUAClient : CollectBase
/// <inheritdoc/>
public override async Task<OperResult<byte[]>> ReadSourceAsync(DeviceVariableSourceRead deviceVariableSourceRead, CancellationToken token)
{
await Task.CompletedTask;
var result = await PLC.ReadJTokenValueAsync(deviceVariableSourceRead.DeviceVariables.Select(a => a.VariableAddress).ToArray(), token);
foreach (var data in result)
{
@@ -163,6 +194,7 @@ public class OPCUAClient : CollectBase
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
_periodicTimer?.Dispose();
if (PLC != null)
{
PLC.DataChangedHandler -= DataChangedHandler;

View File

@@ -33,6 +33,11 @@
OPCUA客户端
</summary>
</member>
<member name="M:ThingsGateway.OPCUA.OPCUAClient.#ctor">
<summary>
OPCUA客户端
</summary>
</member>
<member name="P:ThingsGateway.OPCUA.OPCUAClient.DriverDebugUIType">
<inheritdoc/>
</member>