diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index 41a8334..ec46eb5 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -68,6 +68,9 @@ public partial class App : System.Windows.Application { Host.Services.GetRequiredService(); + // 初始化设备监控服务 + Host.Services.GetRequiredService(); + // 初始化数据处理链 var dataProcessingService = Host.Services.GetRequiredService(); dataProcessingService.AddProcessor(Host.Services.GetRequiredService()); @@ -241,6 +244,12 @@ public partial class App : System.Windows.Application // 注册WPF中的服务 services.AddSingleton(); + // 注册事件服务 + services.AddSingleton(); + + // 注册设备监控服务 + services.AddSingleton(); + // 注册新的数据服务 services.AddSingleton(); services.AddSingleton(); @@ -252,7 +261,18 @@ public partial class App : System.Windows.Application services.AddSingleton(); // 注册主数据服务 - services.AddSingleton(); + services.AddSingleton(provider => + new WPFDataService( + provider.GetRequiredService(), + provider.GetRequiredService(), + provider.GetRequiredService(), + provider.GetRequiredService(), + provider.GetRequiredService(), + provider.GetRequiredService(), + provider.GetRequiredService(), + provider.GetRequiredService(), + provider.GetRequiredService() + )); // 保留原DataServices以保证现有代码兼容性(可选,建议逐步移除) // services.AddSingleton(provider => diff --git a/DMS.WPF/Events/DeviceActiveChangedEventArgs.cs b/DMS.WPF/Events/DeviceActiveChangedEventArgs.cs new file mode 100644 index 0000000..8bd1fa8 --- /dev/null +++ b/DMS.WPF/Events/DeviceActiveChangedEventArgs.cs @@ -0,0 +1,50 @@ +using System; + +namespace DMS.WPF.Events; + +/// +/// 设备状态改变事件参数 +/// +public class DeviceActiveChangedEventArgs : EventArgs +{ + /// + /// 设备ID + /// + public int DeviceId { get; } + + /// + /// 设备名称 + /// + public string DeviceName { get; } + + /// + /// 旧状态 + /// + public bool OldStatus { get; } + + /// + /// 新状态 + /// + public bool NewStatus { get; } + + /// + /// 状态改变时间 + /// + public DateTime ChangeTime { get; } + + /// + /// 初始化DeviceStatusChangedEventArgs类的新实例 + /// + /// 设备ID + /// 设备名称 + /// 旧状态 + /// 新状态 + public DeviceActiveChangedEventArgs(int deviceId, string deviceName, bool oldStatus, bool newStatus) + { + DeviceId = deviceId; + DeviceName = deviceName; + OldStatus = oldStatus; + NewStatus = newStatus; + ChangeTime = DateTime.Now; + } +} \ No newline at end of file diff --git a/DMS.WPF/Events/MqttConnectionChangedEventArgs.cs b/DMS.WPF/Events/MqttConnectionChangedEventArgs.cs new file mode 100644 index 0000000..50f5206 --- /dev/null +++ b/DMS.WPF/Events/MqttConnectionChangedEventArgs.cs @@ -0,0 +1,50 @@ +using System; + +namespace DMS.WPF.Events; + +/// +/// MQTT连接状态改变事件参数 +/// +public class MqttConnectionChangedEventArgs : EventArgs +{ + /// + /// MQTT服务器ID + /// + public int MqttServerId { get; } + + /// + /// MQTT服务器名称 + /// + public string MqttServerName { get; } + + /// + /// 旧连接状态 + /// + public bool OldConnectionStatus { get; } + + /// + /// 新连接状态 + /// + public bool NewConnectionStatus { get; } + + /// + /// 状态改变时间 + /// + public DateTime ChangeTime { get; } + + /// + /// 初始化MqttConnectionChangedEventArgs类的新实例 + /// + /// MQTT服务器ID + /// MQTT服务器名称 + /// 旧连接状态 + /// 新连接状态 + public MqttConnectionChangedEventArgs(int mqttServerId, string mqttServerName, bool oldStatus, bool newStatus) + { + MqttServerId = mqttServerId; + MqttServerName = mqttServerName; + OldConnectionStatus = oldStatus; + NewConnectionStatus = newStatus; + ChangeTime = DateTime.Now; + } +} \ No newline at end of file diff --git a/DMS.WPF/Events/VariableValueChangedEventArgs.cs b/DMS.WPF/Events/VariableValueChangedEventArgs.cs new file mode 100644 index 0000000..7c69e50 --- /dev/null +++ b/DMS.WPF/Events/VariableValueChangedEventArgs.cs @@ -0,0 +1,50 @@ +using System; + +namespace DMS.WPF.Events; + +/// +/// 变量值改变事件参数 +/// +public class VariableValueChangedEventArgs : EventArgs +{ + /// + /// 变量ID + /// + public int VariableId { get; } + + /// + /// 变量名称 + /// + public string VariableName { get; } + + /// + /// 旧值 + /// + public string OldValue { get; } + + /// + /// 新值 + /// + public string NewValue { get; } + + /// + /// 值改变时间 + /// + public DateTime ChangeTime { get; } + + /// + /// 初始化VariableValueChangedEventArgs类的新实例 + /// + /// 变量ID + /// 变量名称 + /// 旧值 + /// 新值 + public VariableValueChangedEventArgs(int variableId, string variableName, string oldValue, string newValue) + { + VariableId = variableId; + VariableName = variableName; + OldValue = oldValue; + NewValue = newValue; + ChangeTime = DateTime.Now; + } +} \ No newline at end of file diff --git a/DMS.WPF/Interfaces/IEventService.cs b/DMS.WPF/Interfaces/IEventService.cs new file mode 100644 index 0000000..05967a1 --- /dev/null +++ b/DMS.WPF/Interfaces/IEventService.cs @@ -0,0 +1,58 @@ +using System; +using DMS.WPF.Events; + +namespace DMS.WPF.Interfaces; + +/// +/// 事件服务接口,用于统一管理应用程序中的各种事件 +/// +public interface IEventService +{ + #region 设备事件 + + /// + /// 设备状态改变事件 + /// + event EventHandler DeviceStatusChanged; + + /// + /// 触发设备状态改变事件 + /// + /// 事件发送者 + /// 设备状态改变事件参数 + void RaiseDeviceStatusChanged(object sender, DeviceActiveChangedEventArgs e); + + #endregion + + #region 变量事件 + + /// + /// 变量值改变事件 + /// + event EventHandler VariableValueChanged; + + /// + /// 触发变量值改变事件 + /// + /// 事件发送者 + /// 变量值改变事件参数 + void RaiseVariableValueChanged(object sender, VariableValueChangedEventArgs e); + + #endregion + + #region MQTT事件 + + /// + /// MQTT连接状态改变事件 + /// + event EventHandler MqttConnectionChanged; + + /// + /// 触发MQTT连接状态改变事件 + /// + /// 事件发送者 + /// MQTT连接状态改变事件参数 + void RaiseMqttConnectionChanged(object sender, MqttConnectionChangedEventArgs e); + + #endregion +} \ No newline at end of file diff --git a/DMS.WPF/Interfaces/IWPFDataService.cs b/DMS.WPF/Interfaces/IWPFDataService.cs index 8bcc29b..0073b8f 100644 --- a/DMS.WPF/Interfaces/IWPFDataService.cs +++ b/DMS.WPF/Interfaces/IWPFDataService.cs @@ -1,3 +1,5 @@ +using DMS.WPF.Interfaces; + namespace DMS.WPF.Interfaces; /// @@ -33,5 +35,10 @@ public interface IWPFDataService /// 日志数据服务。 /// ILogDataService LogDataService { get; } + + /// + /// 事件服务。 + /// + IEventService EventService { get; } } \ No newline at end of file diff --git a/DMS.WPF/Services/DeviceMonitoringService.cs b/DMS.WPF/Services/DeviceMonitoringService.cs new file mode 100644 index 0000000..19b7740 --- /dev/null +++ b/DMS.WPF/Services/DeviceMonitoringService.cs @@ -0,0 +1,74 @@ +using System; +using DMS.WPF.Events; +using DMS.WPF.Interfaces; +using Microsoft.Extensions.Logging; + +namespace DMS.WPF.Services; + +/// +/// 设备监控服务,用于监听设备状态改变事件并进行相应处理 +/// +public class DeviceMonitoringService +{ + private readonly ILogger _logger; + private readonly IEventService _eventService; + private readonly INotificationService _notificationService; + + /// + /// 初始化DeviceMonitoringService类的新实例 + /// + /// 日志服务 + /// 事件服务 + /// 通知服务 + public DeviceMonitoringService( + ILogger logger, + IEventService eventService, + INotificationService notificationService) + { + _logger = logger; + _eventService = eventService; + _notificationService = notificationService; + + // 订阅设备状态改变事件 + _eventService.DeviceStatusChanged += OnDeviceStatusChanged; + } + + /// + /// 处理设备状态改变事件 + /// + /// 事件发送者 + /// 设备状态改变事件参数 + private void OnDeviceStatusChanged(object sender, DeviceActiveChangedEventArgs e) + { + try + { + // 记录设备状态改变日志 + _logger.LogInformation($"设备 {e.DeviceName}(ID: {e.DeviceId}) 状态改变: {e.OldStatus} -> {e.NewStatus}"); + + // 根据设备状态改变发送通知 + string message = e.NewStatus + ? $"设备 {e.DeviceName} 已启动" + : $"设备 {e.DeviceName} 已停止"; + + _notificationService.ShowInfo(message); + + // 在这里可以添加更多处理逻辑,例如: + // 1. 更新数据库中的设备状态历史记录 + // 2. 发送邮件或短信通知 + // 3. 触发其他相关操作 + } + catch (Exception ex) + { + _logger.LogError(ex, $"处理设备 {e.DeviceName} 状态改变事件时发生错误"); + } + } + + /// + /// 释放资源 + /// + public void Dispose() + { + // 取消订阅事件 + _eventService.DeviceStatusChanged -= OnDeviceStatusChanged; + } +} \ No newline at end of file diff --git a/DMS.WPF/Services/EventService.cs b/DMS.WPF/Services/EventService.cs new file mode 100644 index 0000000..cc6a0e6 --- /dev/null +++ b/DMS.WPF/Services/EventService.cs @@ -0,0 +1,68 @@ +using System; +using DMS.WPF.Events; +using DMS.WPF.Interfaces; + +namespace DMS.WPF.Services; + +/// +/// 事件服务实现类,用于统一管理应用程序中的各种事件 +/// +public class EventService : IEventService +{ + #region 设备事件 + + /// + /// 设备状态改变事件 + /// + public event EventHandler DeviceStatusChanged; + + /// + /// 触发设备状态改变事件 + /// + /// 事件发送者 + /// 设备状态改变事件参数 + public void RaiseDeviceStatusChanged(object sender, DeviceActiveChangedEventArgs e) + { + DeviceStatusChanged?.Invoke(sender, e); + } + + #endregion + + #region 变量事件 + + /// + /// 变量值改变事件 + /// + public event EventHandler VariableValueChanged; + + /// + /// 触发变量值改变事件 + /// + /// 事件发送者 + /// 变量值改变事件参数 + public void RaiseVariableValueChanged(object sender, VariableValueChangedEventArgs e) + { + VariableValueChanged?.Invoke(sender, e); + } + + #endregion + + #region MQTT事件 + + /// + /// MQTT连接状态改变事件 + /// + public event EventHandler MqttConnectionChanged; + + /// + /// 触发MQTT连接状态改变事件 + /// + /// 事件发送者 + /// MQTT连接状态改变事件参数 + public void RaiseMqttConnectionChanged(object sender, MqttConnectionChangedEventArgs e) + { + MqttConnectionChanged?.Invoke(sender, e); + } + + #endregion +} \ No newline at end of file diff --git a/DMS.WPF/Services/WPFDataService.cs b/DMS.WPF/Services/WPFDataService.cs index b7dd67c..ba6c025 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 IEventService EventService { get; } /// /// WPFDataService 构造函数。 @@ -55,7 +60,9 @@ public class WPFDataService : IWPFDataService IVariableDataService variableDataService, IMenuDataService menuDataService, IMqttDataService mqttDataService, - ILogDataService logDataService, IVariableTableDataService variableTableDataService) + ILogDataService logDataService, + IVariableTableDataService variableTableDataService, + IEventService eventService) { _mapper = mapper; _appDataCenterService = appDataCenterService; @@ -65,6 +72,6 @@ public class WPFDataService : IWPFDataService MqttDataService = mqttDataService; LogDataService = logDataService; VariableTableDataService = variableTableDataService; - + EventService = eventService; } } diff --git a/DMS.WPF/ViewModels/DevicesViewModel.cs b/DMS.WPF/ViewModels/DevicesViewModel.cs index a8371c4..bc6d54a 100644 --- a/DMS.WPF/ViewModels/DevicesViewModel.cs +++ b/DMS.WPF/ViewModels/DevicesViewModel.cs @@ -44,9 +44,13 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable /// /// 初始化 类的新实例。 /// - /// 日志记录器。 + /// 对象映射器。 + /// 数据存储服务。 /// 对话框服务。 + /// 导航服务。 /// 主数据服务。 + /// 设备应用服务。 + /// 通知服务。 public DevicesViewModel(IMapper mapper,IDataStorageService dataStorageService, IDialogService dialogService, INavigationService navigationService, IWPFDataService wpfDataService, IDeviceAppService deviceAppService, @@ -60,6 +64,9 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable _deviceAppService = deviceAppService; _notificationService = notificationService; Devices = _dataStorageService.Devices; + + // 设置DeviceItemViewModel的静态服务引用 + DeviceItemViewModel.EventService = wpfDataService.EventService; } @@ -204,5 +211,15 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable public async Task OnNavigatedToAsync(MenuItemViewModel menu) { + + + } + + private void OnDeviceIsActiveChanged(object? sender, bool isActive) + { + if (sender is DeviceItemViewModel deviceItemViewModel) + { + + } } } \ No newline at end of file diff --git a/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs b/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs index 6d19cdd..5a32d3c 100644 --- a/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs @@ -3,6 +3,8 @@ using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; using DMS.Core.Enums; +using DMS.WPF.Events; +using DMS.WPF.Interfaces; namespace DMS.WPF.ViewModels.Items; @@ -12,6 +14,9 @@ namespace DMS.WPF.ViewModels.Items; /// public partial class DeviceItemViewModel : ObservableObject { + // 用于访问事件服务的静态属性 + public static IEventService EventService { get; set; } + public int Id { get; set; } [ObservableProperty] @@ -72,6 +77,19 @@ public partial class DeviceItemViewModel : ObservableObject } } + /// + /// 当IsActive属性改变时调用,用于发布设备状态改变事件 + /// + partial void OnIsActiveChanged(bool oldValue, bool newValue) + { + // 只有当设备ID有效且事件服务已初始化时才发布事件 + if (Id > 0 && EventService != null) + { + // 发布设备状态改变事件 + EventService.RaiseDeviceStatusChanged(this, new DeviceActiveChangedEventArgs(Id, Name, oldValue, newValue)); + } + } + public ObservableCollection VariableTables { get; set; } = new(); }