diff --git a/DMS.Application/Events/VariableTableChangedEventArgs.cs b/DMS.Application/Events/VariableTableChangedEventArgs.cs index 2dbacfd..fd6d2a9 100644 --- a/DMS.Application/Events/VariableTableChangedEventArgs.cs +++ b/DMS.Application/Events/VariableTableChangedEventArgs.cs @@ -18,10 +18,6 @@ namespace DMS.Application.Events /// public VariableTableDto VariableTable { get; } - /// - /// 关联的设备DTO - /// - public DeviceDto Device { get; } /// /// 变更时间 @@ -34,11 +30,10 @@ namespace DMS.Application.Events /// 变更类型 /// 变量表DTO /// 关联的设备DTO - public VariableTableChangedEventArgs(DataChangeType changeType, VariableTableDto variableTable, DeviceDto device) + public VariableTableChangedEventArgs(DataChangeType changeType, VariableTableDto variableTable) { ChangeType = changeType; VariableTable = variableTable; - Device = device; ChangeTime = DateTime.Now; } } diff --git a/DMS.Application/Services/Management/DeviceManagementService.cs b/DMS.Application/Services/Management/DeviceManagementService.cs index 9b8a962..18bf818 100644 --- a/DMS.Application/Services/Management/DeviceManagementService.cs +++ b/DMS.Application/Services/Management/DeviceManagementService.cs @@ -53,6 +53,10 @@ public class DeviceManagementService : IDeviceManagementService { _eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Added, result.Device)); } + if (_appDataStorageService.VariableTables.TryAdd(result.VariableTable.Id, result.VariableTable)) + { + _eventService.RaiseVariableTableChanged(this, new VariableTableChangedEventArgs(DataChangeType.Added, result.VariableTable)); + } } return result; diff --git a/DMS.Application/Services/Management/VariableTableManagementService.cs b/DMS.Application/Services/Management/VariableTableManagementService.cs index 2c66956..aaa5c6e 100644 --- a/DMS.Application/Services/Management/VariableTableManagementService.cs +++ b/DMS.Application/Services/Management/VariableTableManagementService.cs @@ -82,8 +82,7 @@ public class VariableTableManagementService : IVariableTableManagementService { _eventService.RaiseVariableTableChanged(this, new VariableTableChangedEventArgs( DataChangeType.Added, - result.VariableTable, - deviceDto)); + result.VariableTable)); } } @@ -109,8 +108,7 @@ public class VariableTableManagementService : IVariableTableManagementService _appDataStorageService.VariableTables.AddOrUpdate(variableTableDto.Id, variableTableDto, (key, oldValue) => variableTableDto); _eventService.RaiseVariableTableChanged(this, new VariableTableChangedEventArgs( DataChangeType.Updated, - variableTableDto, - deviceDto)); + variableTableDto)); } return result; @@ -139,8 +137,7 @@ public class VariableTableManagementService : IVariableTableManagementService _eventService.RaiseVariableTableChanged(this, new VariableTableChangedEventArgs( DataChangeType.Deleted, - variableTableDto, - deviceDto)); + variableTableDto)); } } diff --git a/DMS.Infrastructure/Repositories/InitializeRepository.cs b/DMS.Infrastructure/Repositories/InitializeRepository.cs index 11297b3..6cdbb09 100644 --- a/DMS.Infrastructure/Repositories/InitializeRepository.cs +++ b/DMS.Infrastructure/Repositories/InitializeRepository.cs @@ -119,54 +119,54 @@ public class InitializeRepository : IInitializeRepository new DbMenu { Id = 1, Header = "主页", Icon = "\uE80F", ParentId = 0, - MenuType = MenuType.MainMenu, TargetViewKey = "HomeView", DisplayOrder = 1 + MenuType = MenuType.MainMenu, TargetViewKey = "HomeViewModel", DisplayOrder = 1 }, new DbMenu { Id = 2, Header = "设备", Icon = "\uE975", ParentId = 0, - MenuType = MenuType.MainMenu, TargetViewKey = "DevicesView", + MenuType = MenuType.MainMenu, TargetViewKey = "DevicesViewModel", DisplayOrder = 2 }, new DbMenu { Id = 3, Header = "数据转换", Icon = "\uF1CB", ParentId = 0, - MenuType = MenuType.MainMenu, TargetViewKey = "DataTransformView", + MenuType = MenuType.MainMenu, TargetViewKey = "DataTransformViewModel", DisplayOrder = 3 }, new DbMenu { Id = 4, Header = "Mqtt服务器", Icon = "\uE753", ParentId = 0, - MenuType = MenuType.MainMenu, TargetViewKey = "MqttsView", + MenuType = MenuType.MainMenu, TargetViewKey = "MqttsViewModel", DisplayOrder = 4 }, new DbMenu { Id = 5, Header = "触发器", Icon = "\uE7BA", ParentId = 0, - MenuType = MenuType.MainMenu, TargetViewKey = "TriggersView", + MenuType = MenuType.MainMenu, TargetViewKey = "TriggersViewModel", DisplayOrder = 5 }, new DbMenu { Id = 6, Header = "日志历史", Icon = "\uE7BA", ParentId = 0, - MenuType = MenuType.MainMenu, TargetViewKey = "LogHistoryView", + MenuType = MenuType.MainMenu, TargetViewKey = "LogHistoryViewModel", DisplayOrder = 6 }, new DbMenu { Id = 7, Header = "邮件管理", Icon = "\uE715", ParentId = 0, - MenuType = MenuType.MainMenu, TargetViewKey = "EmailManagementView", + MenuType = MenuType.MainMenu, TargetViewKey = "EmailManagementViewModel", DisplayOrder = 7 }, new DbMenu { Id = 8, Header = "变量历史", Icon = "\uE81C", ParentId = 0, - MenuType = MenuType.MainMenu, TargetViewKey = "VariableHistoryView", + MenuType = MenuType.MainMenu, TargetViewKey = "VariableHistoryViewModel", DisplayOrder = 8 }, new DbMenu { Id = 9, Header = "设置", Icon = "\uE713", ParentId = 0, - MenuType = MenuType.MainMenu, TargetViewKey = "SettingView", + MenuType = MenuType.MainMenu, TargetViewKey = "SettingViewModel", DisplayOrder = 9 }, new DbMenu diff --git a/DMS.Infrastructure/Services/OpcUa/OpcUaServiceManager.cs b/DMS.Infrastructure/Services/OpcUa/OpcUaServiceManager.cs index 9a14bff..46788bb 100644 --- a/DMS.Infrastructure/Services/OpcUa/OpcUaServiceManager.cs +++ b/DMS.Infrastructure/Services/OpcUa/OpcUaServiceManager.cs @@ -1,5 +1,6 @@ using System.Collections.Concurrent; using System.Diagnostics; +using System.Text; using DMS.Application.DTOs; using DMS.Application.Events; using DMS.Application.Interfaces; @@ -251,38 +252,37 @@ namespace DMS.Infrastructure.Services.OpcUa { _logger.LogInformation("处理批量导入变量事件,共 {Count} 个变量", e.Count); - // 更新相关设备的变量表 var deviceIds = e.Variables.Select(v => v.VariableTable.DeviceId) - .Distinct(); - foreach (var deviceId in deviceIds) - { - // 获取设备的变量表信息 - var variablesForDevice = e.Variables.Where(v => v.VariableTable.DeviceId == deviceId) - .ToList(); - if (variablesForDevice.Any()) - { - // 更新设备上下文中的变量 - if (_deviceContexts.TryGetValue(deviceId, out var context)) - { - // 将新导入的变量添加到设备上下文 - foreach (var variable in variablesForDevice) - { - if (!context.Variables.ContainsKey(variable.OpcUaNodeId)) - { - context.Variables.TryAdd(variable.OpcUaNodeId, variable); - } - } + .Distinct().ToList(); - // 如果设备已连接,则设置订阅 - if (context.IsConnected) + foreach (var deviceId in deviceIds){ + // 获取设备的变量表信息 + var variablesForDevice = e.Variables.Where(v => v.VariableTable.DeviceId == deviceId) + .ToList(); + if (variablesForDevice.Any()) + { + // 更新设备上下文中的变量 + if (_deviceContexts.TryGetValue(deviceId, out var context)) { - await SetupSubscriptionsAsync(context, CancellationToken.None); + // 将新导入的变量添加到设备上下文 + foreach (var variable in variablesForDevice) + { + if (!context.Variables.ContainsKey(variable.OpcUaNodeId)) + { + context.Variables.TryAdd(variable.OpcUaNodeId, variable); + } + } + + // 如果设备已连接,则设置订阅 + if (context.IsConnected) + { + await SetupSubscriptionsAsync(context, CancellationToken.None); + } } } } - } - _logger.LogInformation("批量导入变量事件处理完成,更新了 {DeviceCount} 个设备的变量信息", deviceIds.Count()); + _logger.LogInformation("批量导入变量事件处理完成,更新了 {DeviceCount} 个设备的变量信息", deviceIds.Count()); } catch (Exception ex) { diff --git a/DMS.WPF/ViewModels/DevicesViewModel.cs b/DMS.WPF/ViewModels/DevicesViewModel.cs index 011504f..85bfc39 100644 --- a/DMS.WPF/ViewModels/DevicesViewModel.cs +++ b/DMS.WPF/ViewModels/DevicesViewModel.cs @@ -108,7 +108,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable { Header = device.Name, Icon = SegoeFluentIcons.Devices2.Glyph, - TargetViewKey = "DeviceDetailView" + TargetViewKey = nameof(DeviceDetailViewModel), }; if (device.IsAddDefVarTable)