diff --git a/DMS.Application/Interfaces/IAppDataCenterService.cs b/DMS.Application/Interfaces/IAppDataCenterService.cs
index 020a29c..f8ae948 100644
--- a/DMS.Application/Interfaces/IAppDataCenterService.cs
+++ b/DMS.Application/Interfaces/IAppDataCenterService.cs
@@ -2,6 +2,7 @@ using System.Collections.Concurrent;
using DMS.Application.DTOs;
using DMS.Application.Interfaces.Management;
using DMS.Application.Services;
+using DMS.Application.Services.Management;
using DMS.Core.Models;
namespace DMS.Application.Interfaces;
@@ -17,5 +18,6 @@ public interface IAppDataCenterService
IVariableManagementService VariableManagementService { get; set; }
IVariableTableManagementService VariableTableManagementService { get; set; }
IDeviceManagementService DeviceManagementService { get; set; }
+ ITriggerManagementService TriggerManagementService { get; set; } // 添加触发器管理服务
IDataLoaderService DataLoaderService { get; set; }
}
\ No newline at end of file
diff --git a/DMS.Application/Services/AppDataCenterService.cs b/DMS.Application/Services/AppDataCenterService.cs
index 0954604..4ac2b60 100644
--- a/DMS.Application/Services/AppDataCenterService.cs
+++ b/DMS.Application/Services/AppDataCenterService.cs
@@ -12,6 +12,7 @@ using System;
using System.Linq;
using DMS.Application.Events;
using DMS.Application.Interfaces.Management;
+using DMS.Application.Services.Management;
using DMS.Core.Events;
namespace DMS.Application.Services;
@@ -77,7 +78,8 @@ public class AppDataCenterService : IAppDataCenterService
IVariableManagementService variableManagementService,
IMenuManagementService menuManagementService,
IMqttManagementService mqttManagementService,
- ILogManagementService logManagementService
+ ILogManagementService logManagementService,
+ ITriggerManagementService triggerManagementService // 添加触发器管理服务
)
{
_repositoryManager = repositoryManager;
@@ -91,6 +93,7 @@ public class AppDataCenterService : IAppDataCenterService
MenuManagementService = menuManagementService;
MqttManagementService = mqttManagementService;
LogManagementService = logManagementService;
+ TriggerManagementService = triggerManagementService; // 初始化触发器管理服务
}
public ILogManagementService LogManagementService { get; set; }
@@ -105,5 +108,7 @@ public class AppDataCenterService : IAppDataCenterService
public IDeviceManagementService DeviceManagementService { get; set; }
+ public ITriggerManagementService TriggerManagementService { get; set; } // 添加触发器管理服务
+
public IDataLoaderService DataLoaderService { get; set; }
}
\ No newline at end of file
diff --git a/DMS.Application/Services/DataLoaderService.cs b/DMS.Application/Services/DataLoaderService.cs
index 709ffec..6c934a2 100644
--- a/DMS.Application/Services/DataLoaderService.cs
+++ b/DMS.Application/Services/DataLoaderService.cs
@@ -5,6 +5,7 @@ using DMS.Core.Interfaces;
using System.Collections.Concurrent;
using DMS.Application.Events;
using DMS.Application.Interfaces.Database;
+using DMS.Application.Services.Management;
using DMS.Core.Models;
using DMS.Core.Models.Triggers;
using DMS.Application.Services.Triggers;
diff --git a/DMS.Application/Services/Triggers/ITriggerManagementService.cs b/DMS.Application/Services/Management/ITriggerManagementService.cs
similarity index 94%
rename from DMS.Application/Services/Triggers/ITriggerManagementService.cs
rename to DMS.Application/Services/Management/ITriggerManagementService.cs
index 00f9ce5..4ababf4 100644
--- a/DMS.Application/Services/Triggers/ITriggerManagementService.cs
+++ b/DMS.Application/Services/Management/ITriggerManagementService.cs
@@ -1,9 +1,6 @@
-using System;
-using System.Collections.Generic;
-using System.Threading.Tasks;
using DMS.Application.DTOs;
-namespace DMS.Application.Services.Triggers
+namespace DMS.Application.Services.Management
{
///
/// 触发器管理服务接口 (负责 CRUD 操作)
diff --git a/DMS.Application/Services/Triggers/Impl/TriggerManagementService.cs b/DMS.Application/Services/Management/TriggerManagementService.cs
similarity index 97%
rename from DMS.Application/Services/Triggers/Impl/TriggerManagementService.cs
rename to DMS.Application/Services/Management/TriggerManagementService.cs
index 15a547c..15cedd1 100644
--- a/DMS.Application/Services/Triggers/Impl/TriggerManagementService.cs
+++ b/DMS.Application/Services/Management/TriggerManagementService.cs
@@ -1,13 +1,10 @@
-using System;
-using System.Collections.Generic;
-using System.Threading.Tasks;
using AutoMapper;
using DMS.Application.DTOs;
using DMS.Application.Services.Triggers;
using DMS.Core.Interfaces;
using DMS.Core.Models.Triggers;
-namespace DMS.Application.Services.Triggers.Impl
+namespace DMS.Application.Services.Management
{
///
/// 触发器管理服务实现
diff --git a/DMS.Application/Services/Triggers/Impl/TriggerEvaluationService.cs b/DMS.Application/Services/Triggers/Impl/TriggerEvaluationService.cs
index 5622700..07a019a 100644
--- a/DMS.Application/Services/Triggers/Impl/TriggerEvaluationService.cs
+++ b/DMS.Application/Services/Triggers/Impl/TriggerEvaluationService.cs
@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DMS.Application.DTOs;
+using DMS.Application.Services.Management;
// 明确指定 Timer 类型,避免歧义
using ThreadingTimer = System.Threading.Timer;
using TimersTimer = System.Timers.Timer;
diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs
index 766c3c0..929c2c4 100644
--- a/DMS.WPF/App.xaml.cs
+++ b/DMS.WPF/App.xaml.cs
@@ -32,7 +32,6 @@ using DMS.WPF.Services;
using DMS.WPF.ViewModels;
using DMS.WPF.ViewModels.Dialogs;
using DMS.WPF.ViewModels.Items;
-using DMS.WPF.ViewModels.Triggers;
using DMS.WPF.Views;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@@ -304,6 +303,7 @@ public partial class App : System.Windows.Application
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
+ services.AddSingleton(); // 注册触发器数据服务
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
diff --git a/DMS.WPF/Interfaces/IDataStorageService.cs b/DMS.WPF/Interfaces/IDataStorageService.cs
index a709820..0e7cece 100644
--- a/DMS.WPF/Interfaces/IDataStorageService.cs
+++ b/DMS.WPF/Interfaces/IDataStorageService.cs
@@ -41,4 +41,9 @@ public interface IDataStorageService
/// 日志列表。
///
ObservableCollection Nlogs { get; set; }
+
+ ///
+ /// 触发器列表。
+ ///
+ ObservableDictionary Triggers { get; set; }
}
\ No newline at end of file
diff --git a/DMS.WPF/Interfaces/ITriggerDataService.cs b/DMS.WPF/Interfaces/ITriggerDataService.cs
new file mode 100644
index 0000000..b179ea5
--- /dev/null
+++ b/DMS.WPF/Interfaces/ITriggerDataService.cs
@@ -0,0 +1,31 @@
+using System.Collections.ObjectModel;
+using DMS.Application.DTOs;
+using DMS.WPF.ViewModels.Items;
+
+namespace DMS.WPF.Interfaces;
+
+///
+/// 触发器数据服务接口。
+///
+public interface ITriggerDataService
+{
+ ///
+ /// 加载所有触发器数据。
+ ///
+ void LoadAllTriggers();
+
+ ///
+ /// 添加触发器。
+ ///
+ Task AddTrigger(TriggerItemViewModel dto);
+
+ ///
+ /// 删除触发器。
+ ///
+ Task DeleteTrigger(TriggerItemViewModel trigger);
+
+ ///
+ /// 更新触发器。
+ ///
+ Task UpdateTrigger(TriggerItemViewModel trigger);
+}
\ No newline at end of file
diff --git a/DMS.WPF/Interfaces/IWPFDataService.cs b/DMS.WPF/Interfaces/IWPFDataService.cs
index 72df67e..b7b8aa7 100644
--- a/DMS.WPF/Interfaces/IWPFDataService.cs
+++ b/DMS.WPF/Interfaces/IWPFDataService.cs
@@ -36,4 +36,8 @@ public interface IWPFDataService
///
ILogDataService LogDataService { get; }
+ ///
+ /// 触发器数据服务。
+ ///
+ ITriggerDataService TriggerDataService { get; }
}
\ No newline at end of file
diff --git a/DMS.WPF/Services/DataStorageService.cs b/DMS.WPF/Services/DataStorageService.cs
index a31315b..a536a84 100644
--- a/DMS.WPF/Services/DataStorageService.cs
+++ b/DMS.WPF/Services/DataStorageService.cs
@@ -45,6 +45,11 @@ public class DataStorageService : IDataStorageService
///
public ObservableCollection Nlogs { get; set; }
+ ///
+ /// 触发器列表。
+ ///
+ public ObservableDictionary Triggers { get; set; }
+
public DataStorageService()
{
Devices=new ObservableDictionary();
@@ -54,6 +59,7 @@ public class DataStorageService : IDataStorageService
Menus=new ObservableCollection();
MenuTrees=new ObservableCollection();
Nlogs=new ObservableCollection();
+ Triggers = new ObservableDictionary();
}
diff --git a/DMS.WPF/Services/DialogService.cs b/DMS.WPF/Services/DialogService.cs
index 5883643..349c886 100644
--- a/DMS.WPF/Services/DialogService.cs
+++ b/DMS.WPF/Services/DialogService.cs
@@ -1,12 +1,6 @@
+using DMS.WPF.Interfaces;
using DMS.WPF.ViewModels.Dialogs;
using DMS.WPF.Views.Dialogs;
-using System;
-using System.Collections.Generic;
-using System.Threading.Tasks;
-using System.Windows;
-using DMS.WPF.Interfaces;
-using DMS.WPF.ViewModels.Triggers;
-using DMS.WPF.Views;
using iNKORE.UI.WPF.Modern.Controls;
namespace DMS.WPF.Services
diff --git a/DMS.WPF/Services/NavigationService.cs b/DMS.WPF/Services/NavigationService.cs
index 52eca4e..206f5f6 100644
--- a/DMS.WPF/Services/NavigationService.cs
+++ b/DMS.WPF/Services/NavigationService.cs
@@ -1,7 +1,6 @@
using DMS.WPF.Interfaces;
using DMS.WPF.ViewModels;
using DMS.WPF.ViewModels.Items;
-using DMS.WPF.ViewModels.Triggers;
using Microsoft.Extensions.DependencyInjection;
namespace DMS.WPF.Services;
diff --git a/DMS.WPF/Services/TriggerDataService.cs b/DMS.WPF/Services/TriggerDataService.cs
new file mode 100644
index 0000000..28f543c
--- /dev/null
+++ b/DMS.WPF/Services/TriggerDataService.cs
@@ -0,0 +1,119 @@
+using System.Windows.Threading;
+using AutoMapper;
+using DMS.Application.DTOs;
+using DMS.Application.Interfaces;
+using DMS.Core.Events;
+using DMS.WPF.Interfaces;
+using DMS.WPF.ViewModels.Items;
+using Opc.Ua;
+
+namespace DMS.WPF.Services;
+
+///
+/// 触发器数据服务类,负责管理触发器相关的数据和操作。
+///
+public class TriggerDataService : ITriggerDataService
+{
+ private readonly IMapper _mapper;
+ private readonly IAppDataCenterService _appDataCenterService;
+ private readonly IAppDataStorageService _appDataStorageService;
+ private readonly IDataStorageService _dataStorageService;
+ private readonly IEventService _eventService;
+ private readonly INotificationService _notificationService;
+ private readonly Dispatcher _uiDispatcher;
+
+ ///
+ /// TriggerDataService类的构造函数。
+ ///
+ /// AutoMapper 实例。
+ /// 数据服务中心实例。
+ /// 应用数据存储服务实例。
+ /// 数据存储服务实例。
+ /// 事件服务实例。
+ /// 通知服务实例。
+ public TriggerDataService(IMapper mapper, IAppDataCenterService appDataCenterService,
+ IAppDataStorageService appDataStorageService, IDataStorageService dataStorageService,
+ IEventService eventService, INotificationService notificationService)
+ {
+ _mapper = mapper;
+ _appDataCenterService = appDataCenterService;
+ _appDataStorageService = appDataStorageService;
+ _dataStorageService = dataStorageService;
+ _eventService = eventService;
+ _notificationService = notificationService;
+ _uiDispatcher = Dispatcher.CurrentDispatcher;
+ }
+
+ ///
+ /// 加载所有触发器数据。
+ ///
+ public void LoadAllTriggers()
+ {
+ foreach (var triggerDto in _appDataStorageService.Triggers.Values)
+ {
+ _dataStorageService.Triggers.Add(triggerDto.Id, _mapper.Map(triggerDto));
+ }
+ }
+
+ ///
+ /// 添加触发器。
+ ///
+ public async Task AddTrigger(TriggerItemViewModel dto)
+ {
+ // 添加null检查
+ if (dto == null)
+ return null;
+
+ var addDto
+ = await _appDataCenterService.TriggerManagementService.CreateTriggerAsync(
+ _mapper.Map(dto));
+
+ // 添加null检查
+ if (addDto == null)
+ {
+ return null;
+ }
+
+ // 给界面添加触发器
+ var addItem = _mapper.Map(addDto);
+ _dataStorageService.Triggers.Add(addDto.Id, addItem);
+
+ return addItem;
+ }
+
+ ///
+ /// 删除触发器。
+ ///
+ public async Task DeleteTrigger(TriggerItemViewModel trigger)
+ {
+ // 从数据库删除触发器数据
+ if (!await _appDataCenterService.TriggerManagementService.DeleteTriggerAsync(trigger.Id))
+ {
+ return false;
+ }
+
+ // 从界面删除触发器
+ _dataStorageService.Triggers.Remove(trigger.Id);
+
+ return true;
+ }
+
+ ///
+ /// 更新触发器。
+ ///
+ public async Task UpdateTrigger(TriggerItemViewModel trigger)
+ {
+ if (!_appDataStorageService.Triggers.TryGetValue(trigger.Id, out var triggerDto))
+ {
+ return false;
+ }
+
+ _mapper.Map(trigger, triggerDto);
+ if (await _appDataCenterService.TriggerManagementService.UpdateTriggerAsync(trigger.Id, triggerDto) != null)
+ {
+ return true;
+ }
+
+ return false;
+ }
+}
\ No newline at end of file
diff --git a/DMS.WPF/Services/WPFDataService.cs b/DMS.WPF/Services/WPFDataService.cs
index 6db65c5..1e066a0 100644
--- a/DMS.WPF/Services/WPFDataService.cs
+++ b/DMS.WPF/Services/WPFDataService.cs
@@ -44,6 +44,11 @@ public class WPFDataService : IWPFDataService
///
public ILogDataService LogDataService { get; }
+ ///
+ /// 触发器数据服务。
+ ///
+ public ITriggerDataService TriggerDataService { get; }
+
///
/// WPFDataService 构造函数。
///
@@ -55,7 +60,8 @@ public class WPFDataService : IWPFDataService
IMenuDataService menuDataService,
IMqttDataService mqttDataService,
ILogDataService logDataService,
- IVariableTableDataService variableTableDataService)
+ IVariableTableDataService variableTableDataService,
+ ITriggerDataService triggerDataService)
{
_mapper = mapper;
_appDataCenterService = appDataCenterService;
@@ -65,5 +71,6 @@ public class WPFDataService : IWPFDataService
MqttDataService = mqttDataService;
LogDataService = logDataService;
VariableTableDataService = variableTableDataService;
+ TriggerDataService = triggerDataService;
}
}
diff --git a/DMS.WPF/ViewModels/Dialogs/TriggerDialogViewModel.cs b/DMS.WPF/ViewModels/Dialogs/TriggerDialogViewModel.cs
index 29cbef3..510db63 100644
--- a/DMS.WPF/ViewModels/Dialogs/TriggerDialogViewModel.cs
+++ b/DMS.WPF/ViewModels/Dialogs/TriggerDialogViewModel.cs
@@ -17,7 +17,7 @@ namespace DMS.WPF.ViewModels.Dialogs
///
/// 触发器编辑器视图模型
///
- public partial class TriggerDialogViewModel : DialogViewModelBase
+ public partial class TriggerDialogViewModel : DialogViewModelBase
{
private readonly IVariableAppService _variableAppService; // To populate variable selection dropdown
private readonly IDialogService _dialogService;
@@ -28,7 +28,7 @@ namespace DMS.WPF.ViewModels.Dialogs
private string _searchText = "";
[ObservableProperty]
- private TriggerDefinitionDto _trigger = new();
+ private TriggerItemViewModel _trigger = new();
[ObservableProperty]
private List _availableVariables = new();
@@ -129,9 +129,9 @@ namespace DMS.WPF.ViewModels.Dialogs
/// 待编辑的触发器 DTO
public async Task OnInitializedAsync(object? parameter)
{
- if (parameter is TriggerDefinitionDto triggerDto)
+ if (parameter is TriggerItemViewModel triggerItemViewModel)
{
- Trigger = triggerDto;
+ Trigger = triggerItemViewModel;
Title = Trigger.Id == default(int) ? "新建触发器" : "编辑触发器";
PrimaryButText = "保存";
@@ -217,7 +217,10 @@ namespace DMS.WPF.ViewModels.Dialogs
}
// 设置选中的变量ID
- Trigger.VariableIds = SelectedVariables.Select(v => v.Id).ToList();
+ foreach (var selectedVariable in SelectedVariables)
+ {
+ Trigger.VariableIds.Add(selectedVariable.Id);
+ }
// Validate condition-specific fields
switch (Trigger.Condition)
diff --git a/DMS.WPF/ViewModels/Items/TriggerItemViewModel.cs b/DMS.WPF/ViewModels/Items/TriggerItemViewModel.cs
new file mode 100644
index 0000000..3bd589a
--- /dev/null
+++ b/DMS.WPF/ViewModels/Items/TriggerItemViewModel.cs
@@ -0,0 +1,96 @@
+using System.Collections.ObjectModel;
+using CommunityToolkit.Mvvm.ComponentModel;
+using DMS.Application.DTOs;
+using DMS.Core.Models.Triggers;
+
+namespace DMS.WPF.ViewModels.Items
+{
+ ///
+ /// 触发器项视图模型
+ ///
+ public partial class TriggerItemViewModel : ObservableObject
+ {
+ ///
+ /// 触发器唯一标识符
+ ///
+ [ObservableProperty]
+ private int _id;
+
+ ///
+ /// 触发器描述
+ ///
+ [ObservableProperty]
+ private string _description = string.Empty;
+
+ ///
+ /// 触发器是否处于激活状态
+ ///
+ [ObservableProperty]
+ private bool _isActive;
+
+ ///
+ /// 触发条件类型
+ ///
+ [ObservableProperty]
+ private ConditionType _condition;
+
+ ///
+ /// 阈值 (用于 GreaterThan, LessThan, EqualTo, NotEqualTo)
+ ///
+ [ObservableProperty]
+ private double? _threshold;
+
+ ///
+ /// 下限 (用于 InRange, OutOfRange)
+ ///
+ [ObservableProperty]
+ private double? _lowerBound;
+
+ ///
+ /// 上限 (用于 InRange, OutOfRange)
+ ///
+ [ObservableProperty]
+ private double? _upperBound;
+
+ ///
+ /// 动作类型
+ ///
+ [ObservableProperty]
+ private ActionType _action;
+
+ ///
+ /// 动作配置 JSON 字符串
+ ///
+ [ObservableProperty]
+ private string _actionConfigurationJson = string.Empty;
+
+ ///
+ /// 抑制持续时间
+ ///
+ [ObservableProperty]
+ private TimeSpan? _suppressionDuration;
+
+ ///
+ /// 上次触发的时间
+ ///
+ [ObservableProperty]
+ private DateTime? _lastTriggeredAt;
+
+ ///
+ /// 创建时间
+ ///
+ [ObservableProperty]
+ private DateTime _createdAt;
+
+ ///
+ /// 最后更新时间
+ ///
+ [ObservableProperty]
+ private DateTime _updatedAt;
+
+ ///
+ /// 关联的变量 ID 列表
+ ///
+ public ObservableCollection VariableIds { get; } = new ObservableCollection();
+ }
+}
\ No newline at end of file
diff --git a/DMS.WPF/ViewModels/TriggersViewModel.cs b/DMS.WPF/ViewModels/TriggersViewModel.cs
index aff7c24..a6f0dc6 100644
--- a/DMS.WPF/ViewModels/TriggersViewModel.cs
+++ b/DMS.WPF/ViewModels/TriggersViewModel.cs
@@ -1,59 +1,47 @@
-using System;
using System.Collections.ObjectModel;
-using System.Linq;
-using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Application.DTOs;
-using DMS.Application.Services.Triggers;
using DMS.WPF.Interfaces;
-using DMS.WPF.Services;
using DMS.WPF.ViewModels.Dialogs;
+using DMS.WPF.ViewModels.Items;
using Microsoft.Extensions.DependencyInjection;
+using ObservableCollections;
-namespace DMS.WPF.ViewModels.Triggers
+namespace DMS.WPF.ViewModels
{
///
/// 触发器管理视图模型
///
public partial class TriggersViewModel : ViewModelBase
{
- private readonly ITriggerManagementService _triggerManagementService;
+ private readonly ITriggerDataService _triggerDataService;
+ private readonly IDataStorageService _dataStorageService;
private readonly IDialogService _dialogService;
private readonly INotificationService _notificationService;
[ObservableProperty]
- private ObservableCollection _triggers = new();
+ private ObservableDictionary _triggers ;
[ObservableProperty]
- private TriggerDefinitionDto? _selectedTrigger;
+ private TriggerItemViewModel? _selectedTrigger;
public TriggersViewModel(
- ITriggerManagementService triggerManagementService,
+ ITriggerDataService triggerDataService,
+ IDataStorageService dataStorageService,
IDialogService dialogService,
INotificationService notificationService)
{
- _triggerManagementService = triggerManagementService ?? throw new ArgumentNullException(nameof(triggerManagementService));
+ _triggerDataService = triggerDataService ?? throw new ArgumentNullException(nameof(triggerDataService));
+ _dataStorageService = dataStorageService ?? throw new ArgumentNullException(nameof(dataStorageService));
_dialogService = dialogService ?? throw new ArgumentNullException(nameof(dialogService));
_notificationService = notificationService ?? throw new ArgumentNullException(nameof(notificationService));
+
+ // 初始化时加载触发器数据
+ Triggers=_dataStorageService.Triggers;
}
- ///
- /// 加载所有触发器
- ///
- [RelayCommand]
- private async Task LoadTriggersAsync()
- {
- try
- {
- var triggerList = await _triggerManagementService.GetAllTriggersAsync();
- Triggers = new ObservableCollection(triggerList);
- }
- catch (Exception ex)
- {
- _notificationService.ShowError($"加载触发器失败: {ex.Message}");
- }
- }
+
///
/// 添加新触发器
@@ -61,14 +49,14 @@ namespace DMS.WPF.ViewModels.Triggers
[RelayCommand]
private async Task AddTriggerAsync()
{
- var newTrigger = new TriggerDefinitionDto
- {
- IsActive = true,
- Condition = Core.Models.Triggers.ConditionType.GreaterThan,
- Action = Core.Models.Triggers.ActionType.SendEmail,
- Description = "新建触发器",
- CreatedAt = DateTime.UtcNow,
- UpdatedAt = DateTime.UtcNow
+ var newTrigger = new TriggerItemViewModel()
+ {
+ IsActive = true,
+ Condition = Core.Models.Triggers.ConditionType.GreaterThan,
+ Action = Core.Models.Triggers.ActionType.SendEmail,
+ Description = "新建触发器",
+ CreatedAt = DateTime.UtcNow,
+ UpdatedAt = DateTime.UtcNow
};
TriggerDialogViewModel viewModel = App.Current.Services.GetRequiredService();
@@ -79,10 +67,18 @@ namespace DMS.WPF.ViewModels.Triggers
{
try
{
- var createdTrigger = await _triggerManagementService.CreateTriggerAsync(result);
- Triggers.Add(createdTrigger);
- SelectedTrigger = createdTrigger;
- _notificationService.ShowSuccess("触发器创建成功");
+ // 使用TriggerDataService添加触发器
+ var createdTrigger = await _triggerDataService.AddTrigger(newTrigger);
+
+ if (createdTrigger != null )
+ {
+ // 触发器已添加到数据存储中,只需更新本地集合
+ _notificationService.ShowSuccess("触发器创建成功");
+ }
+ else
+ {
+ _notificationService.ShowError("触发器创建失败");
+ }
}
catch (Exception ex)
{
@@ -121,6 +117,7 @@ namespace DMS.WPF.ViewModels.Triggers
CreatedAt = SelectedTrigger.CreatedAt,
UpdatedAt = SelectedTrigger.UpdatedAt
};
+
TriggerDialogViewModel viewModel = App.Current.Services.GetRequiredService();
await viewModel.OnInitializedAsync(triggerToEdit);
@@ -129,20 +126,15 @@ namespace DMS.WPF.ViewModels.Triggers
{
try
{
- var updatedTrigger = await _triggerManagementService.UpdateTriggerAsync(result.Id, result);
- if (updatedTrigger != null)
+ // 使用TriggerDataService更新触发器
+ var updatedTrigger = await _triggerDataService.UpdateTrigger(SelectedTrigger);
+ if (updatedTrigger)
{
- var index = Triggers.IndexOf(SelectedTrigger);
- if (index >= 0)
- {
- Triggers[index] = updatedTrigger;
- }
- SelectedTrigger = updatedTrigger;
_notificationService.ShowSuccess("触发器更新成功");
}
else
{
- _notificationService.ShowError("触发器更新失败,未找到对应记录");
+ _notificationService.ShowError("触发器更新失败");
}
}
catch (Exception ex)
@@ -169,11 +161,10 @@ namespace DMS.WPF.ViewModels.Triggers
{
try
{
- var success = await _triggerManagementService.DeleteTriggerAsync(SelectedTrigger.Id);
+ // 使用TriggerDataService删除触发器
+ var success = await _triggerDataService.DeleteTrigger(SelectedTrigger);
if (success)
{
- Triggers.Remove(SelectedTrigger);
- SelectedTrigger = Triggers.FirstOrDefault();
_notificationService.ShowSuccess("触发器删除成功");
}
else
@@ -188,13 +179,5 @@ namespace DMS.WPF.ViewModels.Triggers
}
}
- ///
- /// 视图加载时执行的命令
- ///
- [RelayCommand]
- private async Task OnLoadedAsync()
- {
- await LoadTriggersCommand.ExecuteAsync(null);
- }
}
}
\ No newline at end of file
diff --git a/DMS.WPF/Views/MainView.xaml b/DMS.WPF/Views/MainView.xaml
index 9fd77cb..862576b 100644
--- a/DMS.WPF/Views/MainView.xaml
+++ b/DMS.WPF/Views/MainView.xaml
@@ -12,7 +12,6 @@
xmlns:tb="http://hardcodet.net/taskbar"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:vm="clr-namespace:DMS.WPF.ViewModels"
- xmlns:vmt="clr-namespace:DMS.WPF.ViewModels.Triggers"
Title="设备管理系统"
Width="1080"
Height="800"
@@ -114,7 +113,7 @@
-
+
diff --git a/DMS.WPF/Views/TriggersView.xaml.cs b/DMS.WPF/Views/TriggersView.xaml.cs
index c6a6fc3..501816f 100644
--- a/DMS.WPF/Views/TriggersView.xaml.cs
+++ b/DMS.WPF/Views/TriggersView.xaml.cs
@@ -1,5 +1,5 @@
using System.Windows.Controls;
-using DMS.WPF.ViewModels.Triggers;
+using DMS.WPF.ViewModels;
using Microsoft.Extensions.DependencyInjection;
namespace DMS.WPF.Views