feat: 优化设备状态逻辑

This commit is contained in:
Diego
2025-06-19 11:41:43 +08:00
parent 59241b8faa
commit 10eecac19b
10 changed files with 29 additions and 39 deletions

View File

@@ -1,8 +1,8 @@
<Project>
<PropertyGroup>
<PluginVersion>10.8.8</PluginVersion>
<ProPluginVersion>10.8.8</ProPluginVersion>
<PluginVersion>10.8.9</PluginVersion>
<ProPluginVersion>10.8.9</ProPluginVersion>
<AuthenticationVersion>2.8.0</AuthenticationVersion>
<SourceGeneratorVersion>10.8.2</SourceGeneratorVersion>
<NET8Version>8.0.17</NET8Version>

View File

@@ -213,7 +213,19 @@ public abstract class CollectBase : DriverBase, IRpcDriver
{
if (IsConnected())
{
CurrentDevice.SetDeviceStatus(TimerX.Now);
if (CurrentDevice.DeviceStatus == DeviceStatusEnum.OffLine)
{
if (IdVariableRuntimes.Any(a => a.Value.IsOnline))
CurrentDevice.SetDeviceStatus(TimerX.Now, false);
}
else
{
CurrentDevice.SetDeviceStatus(TimerX.Now);
}
}
else if (IsStarted)
{
CurrentDevice.SetDeviceStatus(TimerX.Now, true);
}
}
@@ -258,7 +270,7 @@ public abstract class CollectBase : DriverBase, IRpcDriver
// 方法调用成功时记录日志并增加成功计数器
if (LogMessage?.LogLevel <= TouchSocket.Core.LogLevel.Trace)
LogMessage?.Trace(string.Format("{0} - Execute method [{1}] - Succeeded {2}", DeviceName, readVariableMethods.MethodInfo.Name, readResult.Content?.ToSystemTextJsonString()));
CurrentDevice.SetDeviceStatus(TimerX.Now, false);
CurrentDevice.SetDeviceStatus(TimerX.Now, null);
}
else
{
@@ -281,7 +293,7 @@ public abstract class CollectBase : DriverBase, IRpcDriver
}
readVariableMethods.LastErrorMessage = readResult.ErrorMessage;
CurrentDevice.SetDeviceStatus(TimerX.Now, false);
CurrentDevice.SetDeviceStatus(TimerX.Now, null);
}
return;
@@ -329,7 +341,7 @@ public abstract class CollectBase : DriverBase, IRpcDriver
// 读取成功时记录日志并增加成功计数器
if (LogMessage?.LogLevel <= TouchSocket.Core.LogLevel.Trace)
LogMessage?.Trace(string.Format("{0} - Collection [{1} - {2}] data succeeded {3}", DeviceName, variableSourceRead?.RegisterAddress, variableSourceRead?.Length, readResult.Content?.ToHexString(' ')));
CurrentDevice.SetDeviceStatus(TimerX.Now, false);
CurrentDevice.SetDeviceStatus(TimerX.Now, null);
}
else
{
@@ -352,7 +364,7 @@ public abstract class CollectBase : DriverBase, IRpcDriver
}
variableSourceRead.LastErrorMessage = readResult.ErrorMessage;
CurrentDevice.SetDeviceStatus(TimerX.Now, true, readResult.ErrorMessage);
CurrentDevice.SetDeviceStatus(TimerX.Now, null, readResult.ErrorMessage);
var time = DateTime.Now;
variableSourceRead.VariableRuntimes.ForEach(a => a.SetValue(null, time, isOnline: false));
}

View File

@@ -100,7 +100,7 @@ public abstract class CollectFoundationBase : CollectBase
LogMessage?.LogWarning(exception, string.Format(AppResource.CollectFail, DeviceName, item?.RegisterAddress, item?.Length, exception.Message));
}
item.LastErrorMessage = exception.Message;
CurrentDevice.SetDeviceStatus(TimerX.Now, true, exception.Message);
CurrentDevice.SetDeviceStatus(TimerX.Now, null, exception.Message);
var time = DateTime.Now;
item.VariableRuntimes.ForEach(a => a.SetValue(null, time, isOnline: false));
}
@@ -112,7 +112,7 @@ public abstract class CollectFoundationBase : CollectBase
LogMessage?.LogWarning(exception, string.Format(AppResource.MethodFail, DeviceName, item.MethodInfo.Name, exception.Message));
}
item.LastErrorMessage = exception.Message;
CurrentDevice.SetDeviceStatus(TimerX.Now, true, exception.Message);
CurrentDevice.SetDeviceStatus(TimerX.Now, null, exception.Message);
var time = DateTime.Now;
item.Variable.SetValue(null, time, isOnline: false);
}

View File

@@ -282,7 +282,7 @@ public abstract class DriverBase : DisposableObject, IDriver
}
// 设置设备状态为当前时间
CurrentDevice.SetDeviceStatus(TimerX.Now);
CurrentDevice.SetDeviceStatus(TimerX.Now, false);
}
catch (Exception ex)
{

View File

@@ -153,14 +153,8 @@ public class ModbusSlave : BusinessBase
protected override async Task ProtectedExecuteAsync(object? state, CancellationToken cancellationToken)
{
//获取设备连接状态
if (IsConnected())
if (!IsConnected())
{
//更新设备活动时间
CurrentDevice.SetDeviceStatus(TimerX.Now, false);
}
else
{
CurrentDevice.SetDeviceStatus(TimerX.Now, true);
try
{
if (cancellationToken.IsCancellationRequested)

View File

@@ -287,17 +287,6 @@ public partial class MqttCollect : CollectBase
await Task.Delay(10000, cancellationToken).ConfigureAwait(false);
//return;
}
//获取设备连接状态
if (IsConnected())
{
//更新设备活动时间
CurrentDevice.SetDeviceStatus(TimerX.Now, false);
}
else
{
CurrentDevice.SetDeviceStatus(TimerX.Now, true);
}
}

View File

@@ -14,6 +14,7 @@ using ThingsGateway.Foundation.OpcDa;
using ThingsGateway.Foundation.OpcDa.Da;
using ThingsGateway.Gateway.Application;
using ThingsGateway.NewLife.Json.Extension;
using ThingsGateway.NewLife.Threading;
using TouchSocket.Core;

View File

@@ -157,14 +157,8 @@ public partial class OpcUaServer : BusinessBase
{
try
{
if (IsConnected())
if (!IsConnected())
{
//更新设备活动时间
CurrentDevice.SetDeviceStatus(TimerX.Now, false);
}
else
{
CurrentDevice.SetDeviceStatus(TimerX.Now, true);
try
{
await Task.Delay(3000, cancellationToken).ConfigureAwait(false);

View File

@@ -261,9 +261,9 @@ public partial class OpcUaImportVariable
await ToastService.Warning(OpcUaPropertyLocalizer["NoVariablesAvailable"], OpcUaPropertyLocalizer["NoVariablesAvailable"]);
return;
}
await App.RootServices.GetRequiredService<IChannelRuntimeService>().SaveChannelAsync(data.Item1, ItemChangedType.Add, false);
await App.RootServices.GetRequiredService<IDeviceRuntimeService>().SaveDeviceAsync(data.Item2, ItemChangedType.Add, false);
await App.RootServices.GetRequiredService<IVariableRuntimeService>().BatchSaveVariableAsync(data.Item3.ToList(), ItemChangedType.Add, false, default);
await App.RootServices.GetRequiredService<IChannelRuntimeService>().SaveChannelAsync(data.Item1, ItemChangedType.Add, true);
await App.RootServices.GetRequiredService<IDeviceRuntimeService>().SaveDeviceAsync(data.Item2, ItemChangedType.Add, true);
await App.RootServices.GetRequiredService<IVariableRuntimeService>().BatchSaveVariableAsync(data.Item3.ToList(), ItemChangedType.Add, true, default);
await ToastService.Default();
}
catch (Exception ex)

View File

@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>10.8.8</Version>
<Version>10.8.9</Version>
</PropertyGroup>
<ItemGroup>