refactor:删除了DeviceDto,VariableDto,VariableTableDto,改为使用DMS.Core中的实体
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Events;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Application.Models;
|
||||
@@ -99,7 +98,7 @@ namespace DMS.Infrastructure.Services.OpcUa
|
||||
/// <summary>
|
||||
/// 处理设备添加事件
|
||||
/// </summary>
|
||||
private void HandleDeviceAdded(DeviceDto device)
|
||||
private void HandleDeviceAdded(DMS.Core.Models.Device device)
|
||||
{
|
||||
if (device == null)
|
||||
{
|
||||
@@ -151,7 +150,7 @@ namespace DMS.Infrastructure.Services.OpcUa
|
||||
/// <summary>
|
||||
/// 处理设备更新事件
|
||||
/// </summary>
|
||||
private void HandleDeviceUpdated(DeviceDto device)
|
||||
private void HandleDeviceUpdated(DMS.Core.Models.Device device)
|
||||
{
|
||||
if (device == null)
|
||||
{
|
||||
@@ -303,7 +302,7 @@ namespace DMS.Infrastructure.Services.OpcUa
|
||||
/// <summary>
|
||||
/// 添加设备到监控列表
|
||||
/// </summary>
|
||||
public void AddDevice(DeviceDto device)
|
||||
public void AddDevice(DMS.Core.Models.Device device)
|
||||
{
|
||||
if (device == null)
|
||||
throw new ArgumentNullException(nameof(device));
|
||||
@@ -318,7 +317,7 @@ namespace DMS.Infrastructure.Services.OpcUa
|
||||
{
|
||||
Device = device,
|
||||
OpcUaService = new OpcUaService(),
|
||||
Variables = new ConcurrentDictionary<string, VariableDto>(),
|
||||
Variables = new ConcurrentDictionary<string, Variable>(),
|
||||
IsConnected = false
|
||||
};
|
||||
|
||||
@@ -341,7 +340,7 @@ namespace DMS.Infrastructure.Services.OpcUa
|
||||
/// <summary>
|
||||
/// 更新设备变量
|
||||
/// </summary>
|
||||
public void UpdateVariables(int deviceId, List<VariableDto> variables)
|
||||
public void UpdateVariables(int deviceId, List<Variable> variables)
|
||||
{
|
||||
if (_deviceContexts.TryGetValue(deviceId, out var context))
|
||||
{
|
||||
@@ -679,9 +678,9 @@ namespace DMS.Infrastructure.Services.OpcUa
|
||||
case VariablePropertyType.OpcUaNodeId:
|
||||
case VariablePropertyType.OpcUaUpdateType:
|
||||
case VariablePropertyType.PollingInterval:
|
||||
if (context.Variables.TryGetValue(e.Variable.OpcUaNodeId, out var variableDto))
|
||||
if (context.Variables.TryGetValue(e.Variable.OpcUaNodeId, out var variable))
|
||||
{
|
||||
if (variableDto.IsActive)
|
||||
if (variable.IsActive)
|
||||
{
|
||||
context.OpcUaService.UnsubscribeFromNode(e.Variable.OpcUaNodeId);
|
||||
context.OpcUaService.SubscribeToNode(
|
||||
@@ -744,9 +743,9 @@ namespace DMS.Infrastructure.Services.OpcUa
|
||||
/// </summary>
|
||||
public class DeviceContext
|
||||
{
|
||||
public DeviceDto Device { get; set; }
|
||||
public DMS.Core.Models.Device Device { get; set; }
|
||||
public OpcUaService OpcUaService { get; set; }
|
||||
public ConcurrentDictionary<string, VariableDto> Variables { get; set; }
|
||||
public ConcurrentDictionary<string, Variable> Variables { get; set; }
|
||||
public bool IsConnected { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ using DMS.Application.DTOs;
|
||||
using DMS.Application.Events;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Infrastructure.Interfaces.Services;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -113,7 +114,7 @@ namespace DMS.Infrastructure.Services.OpcUa
|
||||
var variables = device.VariableTables?
|
||||
.SelectMany(vt => vt.Variables)
|
||||
.Where(v => v.IsActive && v.Protocol == ProtocolType.OpcUa)
|
||||
.ToList() ?? new List<VariableDto>();
|
||||
.ToList() ?? new List<Variable>();
|
||||
|
||||
_opcUaServiceManager.UpdateVariables(device.Id, variables);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ using DMS.Application.Interfaces;
|
||||
using DMS.Application.Models;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Core.Events;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Infrastructure.Interfaces.Services;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -30,7 +31,7 @@ public class OptimizedS7BackgroundService : BackgroundService
|
||||
private readonly int _s7PollOnceSleepTimeMs = 50;
|
||||
|
||||
// 存储每个设备的变量按轮询间隔分组
|
||||
private readonly ConcurrentDictionary<int, Dictionary<int, List<VariableDto>>> _variablesByPollingInterval = new();
|
||||
private readonly ConcurrentDictionary<int, Dictionary<int, List<Variable>>> _variablesByPollingInterval = new();
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -132,7 +133,7 @@ public class OptimizedS7BackgroundService : BackgroundService
|
||||
_s7ServiceManager.AddDevice(s7Device);
|
||||
|
||||
// 查找设备中所有要轮询的变量
|
||||
var variables = new List<VariableDto>();
|
||||
var variables = new List<Variable>();
|
||||
|
||||
foreach (var variableTable in s7Device.VariableTables)
|
||||
{
|
||||
@@ -203,7 +204,7 @@ public class OptimizedS7BackgroundService : BackgroundService
|
||||
/// <summary>
|
||||
/// 轮询设备的变量
|
||||
/// </summary>
|
||||
private async Task PollVariablesForDeviceAsync(S7DeviceContext context, List<VariableDto> variables,
|
||||
private async Task PollVariablesForDeviceAsync(S7DeviceContext context, List<Variable> variables,
|
||||
CancellationToken stoppingToken)
|
||||
{
|
||||
if (!_appDataStorageService.Devices.TryGetValue(context.Device.Id, out var device))
|
||||
|
||||
@@ -246,7 +246,7 @@ namespace DMS.Infrastructure.Services.S7
|
||||
variable.UpdatedAt = DateTime.Now;
|
||||
|
||||
// 创建VariableDto对象
|
||||
var variableDto = new VariableDto
|
||||
var variableDto = new Variable
|
||||
{
|
||||
Id = variable.Id,
|
||||
Name = variable.Name,
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Events;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Core.Events;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Infrastructure.Interfaces.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NPOI.HSSF.Record;
|
||||
@@ -85,7 +85,7 @@ namespace DMS.Infrastructure.Services.S7
|
||||
/// <summary>
|
||||
/// 添加设备到监控列表
|
||||
/// </summary>
|
||||
public void AddDevice(DeviceDto device)
|
||||
public void AddDevice(DMS.Core.Models.Device device)
|
||||
{
|
||||
if (device == null)
|
||||
throw new ArgumentNullException(nameof(device));
|
||||
@@ -100,7 +100,7 @@ namespace DMS.Infrastructure.Services.S7
|
||||
{
|
||||
Device = device,
|
||||
S7Service = _s7ServiceFactory.CreateService(),
|
||||
Variables = new ConcurrentDictionary<string, VariableDto>(),
|
||||
Variables = new ConcurrentDictionary<string, Variable>(),
|
||||
IsConnected = false
|
||||
};
|
||||
|
||||
@@ -123,7 +123,7 @@ namespace DMS.Infrastructure.Services.S7
|
||||
/// <summary>
|
||||
/// 更新设备变量
|
||||
/// </summary>
|
||||
public void UpdateVariables(int deviceId, List<VariableDto> variables)
|
||||
public void UpdateVariables(int deviceId, List<Variable> variables)
|
||||
{
|
||||
if (_deviceContexts.TryGetValue(deviceId, out var context))
|
||||
{
|
||||
@@ -467,9 +467,9 @@ namespace DMS.Infrastructure.Services.S7
|
||||
/// </summary>
|
||||
public class S7DeviceContext
|
||||
{
|
||||
public DeviceDto Device { get; set; }
|
||||
public DMS.Core.Models.Device Device { get; set; }
|
||||
public IS7Service S7Service { get; set; }
|
||||
public ConcurrentDictionary<string, VariableDto> Variables { get; set; }
|
||||
public ConcurrentDictionary<string, Variable> Variables { get; set; }
|
||||
public bool IsConnected { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user