将pollLevel属性名改为pollingInterval
This commit is contained in:
@@ -260,7 +260,7 @@ public class ExcelService : IExcelService
|
||||
|
||||
variable.OpcUaNodeId = "";
|
||||
variable.Protocol = ProtocolType.S7;
|
||||
variable.PollLevel = 30000; // ThirtySeconds
|
||||
variable.PollingInterval = 30000; // ThirtySeconds
|
||||
variableDatas.Add(variable);
|
||||
}
|
||||
|
||||
|
||||
@@ -277,15 +277,15 @@ public class OpcUaBackgroundService : BackgroundService
|
||||
|
||||
if (_opcUaVariablesByDeviceId.TryGetValue(device.Id, out var opcUaVariables))
|
||||
{
|
||||
var variableGroup = opcUaVariables.GroupBy(variable => variable.PollLevel);
|
||||
var variableGroup = opcUaVariables.GroupBy(variable => variable.PollingInterval);
|
||||
foreach (var vGroup in variableGroup)
|
||||
{
|
||||
var pollLevel = vGroup.Key;
|
||||
var pollingInterval = vGroup.Key;
|
||||
var opcUaNodes
|
||||
= vGroup.Select(variableDto => new OpcUaNode() { NodeId = variableDto.OpcUaNodeId })
|
||||
.ToList();
|
||||
|
||||
PollingIntervals.TryGetValue(pollLevel, out var interval);
|
||||
PollingIntervals.TryGetValue(pollingInterval, out var interval);
|
||||
opcUaService.SubscribeToNode(opcUaNodes,HandleDataChanged,10000,1000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,22 +215,22 @@ namespace DMS.Infrastructure.Services
|
||||
_logger.LogInformation("正在为设备 {DeviceName} 设置订阅,变量数: {VariableCount}",
|
||||
context.Device.Name, context.Variables.Count);
|
||||
|
||||
// 按PollLevel对变量进行分组
|
||||
var variablesByPollLevel = context.Variables.Values
|
||||
.GroupBy(v => v.PollLevel)
|
||||
// 按PollingInterval对变量进行分组
|
||||
var variablesByPollingInterval = context.Variables.Values
|
||||
.GroupBy(v => v.PollingInterval)
|
||||
.ToDictionary(g => g.Key, g => g.ToList());
|
||||
|
||||
// 为每个PollLevel组设置单独的订阅
|
||||
foreach (var group in variablesByPollLevel)
|
||||
// 为每个PollingInterval组设置单独的订阅
|
||||
foreach (var group in variablesByPollingInterval)
|
||||
{
|
||||
int pollLevel = group.Key;
|
||||
int pollingInterval = group.Key;
|
||||
var variables = group.Value;
|
||||
|
||||
_logger.LogInformation("为设备 {DeviceName} 设置PollLevel {PollLevel} 的订阅,变量数: {VariableCount}",
|
||||
context.Device.Name, pollLevel, variables.Count);
|
||||
_logger.LogInformation("为设备 {DeviceName} 设置PollingInterval {PollingInterval} 的订阅,变量数: {VariableCount}",
|
||||
context.Device.Name, pollingInterval, variables.Count);
|
||||
|
||||
// 根据PollLevel计算发布间隔和采样间隔(毫秒)
|
||||
var publishingInterval = GetPublishingIntervalFromPollLevel(pollLevel);
|
||||
// 根据PollingInterval计算发布间隔和采样间隔(毫秒)
|
||||
var publishingInterval = GetPublishingIntervalFromPollLevel(pollingInterval);
|
||||
// var samplingInterval = GetSamplingIntervalFromPollLevel(pollLevel);
|
||||
|
||||
var opcUaNodes = variables
|
||||
@@ -251,12 +251,12 @@ namespace DMS.Infrastructure.Services
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据PollLevel获取发布间隔(毫秒)
|
||||
/// 根据PollingInterval获取发布间隔(毫秒)
|
||||
/// </summary>
|
||||
private int GetPublishingIntervalFromPollLevel(int pollLevel)
|
||||
private int GetPublishingIntervalFromPollLevel(int pollingInterval)
|
||||
{
|
||||
// 根据轮询级别值映射到发布间隔
|
||||
return pollLevel switch
|
||||
// 根据轮询间隔值映射到发布间隔
|
||||
return pollingInterval switch
|
||||
{
|
||||
100 => 100, // HundredMilliseconds -> 100ms发布间隔
|
||||
500 => 500, // FiveHundredMilliseconds -> 500ms发布间隔
|
||||
|
||||
@@ -29,8 +29,8 @@ public class OptimizedS7BackgroundService : BackgroundService
|
||||
// S7轮询一遍后的等待时间
|
||||
private readonly int _s7PollOnceSleepTimeMs = 50;
|
||||
|
||||
// 存储每个设备的变量按轮询级别分组
|
||||
private readonly ConcurrentDictionary<int, Dictionary<int, List<VariableDto>>> _variablesByPollLevel = new();
|
||||
// 存储每个设备的变量按轮询间隔分组
|
||||
private readonly ConcurrentDictionary<int, Dictionary<int, List<VariableDto>>> _variablesByPollingInterval = new();
|
||||
|
||||
// 模拟 PollingIntervals,实际应用中可能从配置或数据库加载
|
||||
private static readonly Dictionary<int, TimeSpan> PollingIntervals = new Dictionary<int, TimeSpan>
|
||||
@@ -105,7 +105,7 @@ public class OptimizedS7BackgroundService : BackgroundService
|
||||
// 持续轮询,直到取消请求或需要重新加载
|
||||
while (!stoppingToken.IsCancellationRequested && _reloadSemaphore.CurrentCount == 0)
|
||||
{
|
||||
await PollS7VariablesByPollLevelAsync(stoppingToken);
|
||||
await PollS7VariablesByPollingIntervalAsync(stoppingToken);
|
||||
await Task.Delay(_s7PollOnceSleepTimeMs, stoppingToken);
|
||||
}
|
||||
}
|
||||
@@ -131,7 +131,7 @@ public class OptimizedS7BackgroundService : BackgroundService
|
||||
{
|
||||
try
|
||||
{
|
||||
_variablesByPollLevel.Clear();
|
||||
_variablesByPollingInterval.Clear();
|
||||
_logger.LogInformation("开始加载S7变量....");
|
||||
|
||||
var s7Devices = _dataCenterService
|
||||
@@ -149,12 +149,12 @@ public class OptimizedS7BackgroundService : BackgroundService
|
||||
|
||||
_s7ServiceManager.UpdateVariables(s7Device.Id, variables);
|
||||
|
||||
// 按轮询级别分组变量
|
||||
var variablesByPollLevel = variables
|
||||
.GroupBy(v => v.PollLevel)
|
||||
// 按轮询间隔分组变量
|
||||
var variablesByPollingInterval = variables
|
||||
.GroupBy(v => v.PollingInterval)
|
||||
.ToDictionary(g => g.Key, g => g.ToList());
|
||||
|
||||
_variablesByPollLevel.AddOrUpdate(s7Device.Id, variablesByPollLevel, (key, oldValue) => variablesByPollLevel);
|
||||
_variablesByPollingInterval.AddOrUpdate(s7Device.Id, variablesByPollingInterval, (key, oldValue) => variablesByPollingInterval);
|
||||
}
|
||||
|
||||
_logger.LogInformation($"S7 变量加载成功,共加载S7设备:{s7Devices.Count}个");
|
||||
@@ -184,28 +184,28 @@ public class OptimizedS7BackgroundService : BackgroundService
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按轮询级别轮询S7变量
|
||||
/// 按轮询间隔轮询S7变量
|
||||
/// </summary>
|
||||
private async Task PollS7VariablesByPollLevelAsync(CancellationToken stoppingToken)
|
||||
private async Task PollS7VariablesByPollingIntervalAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var pollTasks = new List<Task>();
|
||||
|
||||
// 为每个设备创建轮询任务
|
||||
foreach (var deviceEntry in _variablesByPollLevel)
|
||||
foreach (var deviceEntry in _variablesByPollingInterval)
|
||||
{
|
||||
var deviceId = deviceEntry.Key;
|
||||
var variablesByPollLevel = deviceEntry.Value;
|
||||
var variablesByPollingInterval = deviceEntry.Value;
|
||||
|
||||
// 为每个轮询级别创建轮询任务
|
||||
foreach (var pollLevelEntry in variablesByPollLevel)
|
||||
// 为每个轮询间隔创建轮询任务
|
||||
foreach (var pollingIntervalEntry in variablesByPollingInterval)
|
||||
{
|
||||
var pollLevel = pollLevelEntry.Key;
|
||||
var variables = pollLevelEntry.Value;
|
||||
var pollingInterval = pollingIntervalEntry.Key;
|
||||
var variables = pollingIntervalEntry.Value;
|
||||
|
||||
// 检查是否达到轮询时间
|
||||
if (ShouldPollVariables(variables, pollLevel))
|
||||
if (ShouldPollVariables(variables, pollingInterval))
|
||||
{
|
||||
pollTasks.Add(PollVariablesForDeviceAsync(deviceId, variables, stoppingToken));
|
||||
}
|
||||
@@ -216,16 +216,16 @@ public class OptimizedS7BackgroundService : BackgroundService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"按轮询级别轮询S7变量时发生错误:{ex.Message}");
|
||||
_logger.LogError(ex, $"按轮询间隔轮询S7变量时发生错误:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否应该轮询变量
|
||||
/// </summary>
|
||||
private bool ShouldPollVariables(List<VariableDto> variables, int pollLevel)
|
||||
private bool ShouldPollVariables(List<VariableDto> variables, int pollingInterval)
|
||||
{
|
||||
if (!PollingIntervals.TryGetValue(pollLevel, out var interval))
|
||||
if (!PollingIntervals.TryGetValue(pollingInterval, out var interval))
|
||||
return false;
|
||||
|
||||
// 检查是否有任何一个变量需要轮询
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace DMS.Infrastructure.Services
|
||||
private readonly ILogger<S7DeviceAgent> _logger;
|
||||
private Plc _plc;
|
||||
private bool _isConnected;
|
||||
private readonly Dictionary<int, List<Variable>> _variablesByPollLevel;
|
||||
private readonly Dictionary<int, List<Variable>> _variablesByPollingInterval;
|
||||
private readonly Dictionary<int, DateTime> _lastPollTimes;
|
||||
|
||||
public S7DeviceAgent(Device device, IChannelBus channelBus, IMessenger messenger, ILogger<S7DeviceAgent> logger)
|
||||
@@ -36,7 +36,7 @@ namespace DMS.Infrastructure.Services
|
||||
_messenger = messenger ?? throw new ArgumentNullException(nameof(messenger));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
|
||||
_variablesByPollLevel = new Dictionary<int, List<Variable>>();
|
||||
_variablesByPollingInterval = new Dictionary<int, List<Variable>>();
|
||||
_lastPollTimes = new Dictionary<int, DateTime>();
|
||||
|
||||
InitializePlc();
|
||||
@@ -125,18 +125,18 @@ namespace DMS.Infrastructure.Services
|
||||
public void UpdateVariables(List<Variable> variables)
|
||||
{
|
||||
// 清空现有的变量分组
|
||||
_variablesByPollLevel.Clear();
|
||||
_variablesByPollingInterval.Clear();
|
||||
_lastPollTimes.Clear();
|
||||
|
||||
// 按轮询级别分组变量
|
||||
// 按轮询间隔分组变量
|
||||
foreach (var variable in variables)
|
||||
{
|
||||
if (!_variablesByPollLevel.ContainsKey(variable.PollLevel))
|
||||
if (!_variablesByPollingInterval.ContainsKey(variable.PollingInterval))
|
||||
{
|
||||
_variablesByPollLevel[variable.PollLevel] = new List<Variable>();
|
||||
_lastPollTimes[variable.PollLevel] = DateTime.MinValue;
|
||||
_variablesByPollingInterval[variable.PollingInterval] = new List<Variable>();
|
||||
_lastPollTimes[variable.PollingInterval] = DateTime.MinValue;
|
||||
}
|
||||
_variablesByPollLevel[variable.PollLevel].Add(variable);
|
||||
_variablesByPollingInterval[variable.PollingInterval].Add(variable);
|
||||
}
|
||||
|
||||
_logger.LogInformation($"S7DeviceAgent: 更新设备 {_deviceConfig.Name} 的变量配置,共 {variables.Count} 个变量");
|
||||
@@ -155,17 +155,17 @@ namespace DMS.Infrastructure.Services
|
||||
|
||||
try
|
||||
{
|
||||
// 按轮询级别依次轮询
|
||||
foreach (var kvp in _variablesByPollLevel)
|
||||
// 按轮询间隔依次轮询
|
||||
foreach (var kvp in _variablesByPollingInterval)
|
||||
{
|
||||
var pollLevel = kvp.Key;
|
||||
var pollingInterval = kvp.Key;
|
||||
var variables = kvp.Value;
|
||||
|
||||
// 检查是否到了轮询时间
|
||||
if (ShouldPoll(pollLevel))
|
||||
if (ShouldPoll(pollingInterval))
|
||||
{
|
||||
await PollVariablesByLevelAsync(variables, pollLevel);
|
||||
_lastPollTimes[pollLevel] = DateTime.Now;
|
||||
await PollVariablesByLevelAsync(variables, pollingInterval);
|
||||
_lastPollTimes[pollingInterval] = DateTime.Now;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,23 +175,22 @@ namespace DMS.Infrastructure.Services
|
||||
}
|
||||
}
|
||||
|
||||
private bool ShouldPoll(int pollLevel)
|
||||
private bool ShouldPoll(int PollingInterval)
|
||||
{
|
||||
// 获取轮询间隔
|
||||
var interval = GetPollingInterval(pollLevel);
|
||||
var interval = GetPollingInterval(PollingInterval);
|
||||
|
||||
// 检查是否到了轮询时间
|
||||
if (_lastPollTimes.TryGetValue(pollLevel, out var lastPollTime))
|
||||
if (_lastPollTimes.TryGetValue(PollingInterval, out var lastPollTime))
|
||||
{
|
||||
return DateTime.Now - lastPollTime >= interval;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private TimeSpan GetPollingInterval(int pollLevel)
|
||||
private TimeSpan GetPollingInterval(int pollingInterval)
|
||||
{
|
||||
return pollLevel switch
|
||||
return pollingInterval switch
|
||||
{
|
||||
10 => TimeSpan.FromMilliseconds(10), // TenMilliseconds
|
||||
100 => TimeSpan.FromMilliseconds(100), // HundredMilliseconds
|
||||
@@ -210,7 +209,7 @@ namespace DMS.Infrastructure.Services
|
||||
};
|
||||
}
|
||||
|
||||
private async Task PollVariablesByLevelAsync(List<Variable> variables, int pollLevel)
|
||||
private async Task PollVariablesByLevelAsync(List<Variable> variables, int pollingInterval)
|
||||
{
|
||||
// 批量读取变量
|
||||
var dataItems = new List<DataItem>();
|
||||
@@ -259,7 +258,7 @@ namespace DMS.Infrastructure.Services
|
||||
S7Address = variable.S7Address,
|
||||
DataValue = variable.DataValue,
|
||||
DisplayValue = variable.DisplayValue,
|
||||
PollLevel = variable.PollLevel,
|
||||
PollingInterval = variable.PollingInterval,
|
||||
Protocol = variable.Protocol,
|
||||
UpdatedAt = variable.UpdatedAt
|
||||
// 可以根据需要添加其他属性
|
||||
@@ -274,7 +273,7 @@ namespace DMS.Infrastructure.Services
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogDebug($"S7DeviceAgent: 设备 {_deviceConfig.Name} 完成 {pollLevel} 级别轮询,共处理 {dataItems.Count} 个变量");
|
||||
_logger.LogDebug($"S7DeviceAgent: 设备 {_deviceConfig.Name} 完成 {pollingInterval} 毫秒轮询间隔,共处理 {dataItems.Count} 个变量");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user