初步添加事件服务
This commit is contained in:
@@ -68,6 +68,9 @@ public partial class App : System.Windows.Application
|
||||
{
|
||||
Host.Services.GetRequiredService<GrowlNotificationService>();
|
||||
|
||||
// 初始化设备监控服务
|
||||
Host.Services.GetRequiredService<DeviceMonitoringService>();
|
||||
|
||||
// 初始化数据处理链
|
||||
var dataProcessingService = Host.Services.GetRequiredService<IDataProcessingService>();
|
||||
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<CheckValueChangedProcessor>());
|
||||
@@ -241,6 +244,12 @@ public partial class App : System.Windows.Application
|
||||
// 注册WPF中的服务
|
||||
services.AddSingleton<IMqttAppService, MqttAppService>();
|
||||
|
||||
// 注册事件服务
|
||||
services.AddSingleton<IEventService, EventService>();
|
||||
|
||||
// 注册设备监控服务
|
||||
services.AddSingleton<DeviceMonitoringService>();
|
||||
|
||||
// 注册新的数据服务
|
||||
services.AddSingleton<IDeviceDataService, DeviceDataService>();
|
||||
services.AddSingleton<IVariableDataService, VariableDataService>();
|
||||
@@ -252,7 +261,18 @@ public partial class App : System.Windows.Application
|
||||
services.AddSingleton<IDataStorageService, DataStorageService>();
|
||||
|
||||
// 注册主数据服务
|
||||
services.AddSingleton<IWPFDataService, WPFDataService>();
|
||||
services.AddSingleton<IWPFDataService>(provider =>
|
||||
new WPFDataService(
|
||||
provider.GetRequiredService<IMapper>(),
|
||||
provider.GetRequiredService<IAppDataCenterService>(),
|
||||
provider.GetRequiredService<IDeviceDataService>(),
|
||||
provider.GetRequiredService<IVariableDataService>(),
|
||||
provider.GetRequiredService<IMenuDataService>(),
|
||||
provider.GetRequiredService<IMqttDataService>(),
|
||||
provider.GetRequiredService<ILogDataService>(),
|
||||
provider.GetRequiredService<IVariableTableDataService>(),
|
||||
provider.GetRequiredService<IEventService>()
|
||||
));
|
||||
|
||||
// 保留原DataServices以保证现有代码兼容性(可选,建议逐步移除)
|
||||
// services.AddSingleton<DataServices>(provider =>
|
||||
|
||||
50
DMS.WPF/Events/DeviceActiveChangedEventArgs.cs
Normal file
50
DMS.WPF/Events/DeviceActiveChangedEventArgs.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
|
||||
namespace DMS.WPF.Events;
|
||||
|
||||
/// <summary>
|
||||
/// 设备状态改变事件参数
|
||||
/// </summary>
|
||||
public class DeviceActiveChangedEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备ID
|
||||
/// </summary>
|
||||
public int DeviceId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 设备名称
|
||||
/// </summary>
|
||||
public string DeviceName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 旧状态
|
||||
/// </summary>
|
||||
public bool OldStatus { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 新状态
|
||||
/// </summary>
|
||||
public bool NewStatus { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态改变时间
|
||||
/// </summary>
|
||||
public DateTime ChangeTime { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 初始化DeviceStatusChangedEventArgs类的新实例
|
||||
/// </summary>
|
||||
/// <param name="deviceId">设备ID</param>
|
||||
/// <param name="deviceName">设备名称</param>
|
||||
/// <param name="oldStatus">旧状态</param>
|
||||
/// <param name="newStatus">新状态</param>
|
||||
public DeviceActiveChangedEventArgs(int deviceId, string deviceName, bool oldStatus, bool newStatus)
|
||||
{
|
||||
DeviceId = deviceId;
|
||||
DeviceName = deviceName;
|
||||
OldStatus = oldStatus;
|
||||
NewStatus = newStatus;
|
||||
ChangeTime = DateTime.Now;
|
||||
}
|
||||
}
|
||||
50
DMS.WPF/Events/MqttConnectionChangedEventArgs.cs
Normal file
50
DMS.WPF/Events/MqttConnectionChangedEventArgs.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
|
||||
namespace DMS.WPF.Events;
|
||||
|
||||
/// <summary>
|
||||
/// MQTT连接状态改变事件参数
|
||||
/// </summary>
|
||||
public class MqttConnectionChangedEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// MQTT服务器ID
|
||||
/// </summary>
|
||||
public int MqttServerId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// MQTT服务器名称
|
||||
/// </summary>
|
||||
public string MqttServerName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 旧连接状态
|
||||
/// </summary>
|
||||
public bool OldConnectionStatus { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 新连接状态
|
||||
/// </summary>
|
||||
public bool NewConnectionStatus { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态改变时间
|
||||
/// </summary>
|
||||
public DateTime ChangeTime { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 初始化MqttConnectionChangedEventArgs类的新实例
|
||||
/// </summary>
|
||||
/// <param name="mqttServerId">MQTT服务器ID</param>
|
||||
/// <param name="mqttServerName">MQTT服务器名称</param>
|
||||
/// <param name="oldStatus">旧连接状态</param>
|
||||
/// <param name="newStatus">新连接状态</param>
|
||||
public MqttConnectionChangedEventArgs(int mqttServerId, string mqttServerName, bool oldStatus, bool newStatus)
|
||||
{
|
||||
MqttServerId = mqttServerId;
|
||||
MqttServerName = mqttServerName;
|
||||
OldConnectionStatus = oldStatus;
|
||||
NewConnectionStatus = newStatus;
|
||||
ChangeTime = DateTime.Now;
|
||||
}
|
||||
}
|
||||
50
DMS.WPF/Events/VariableValueChangedEventArgs.cs
Normal file
50
DMS.WPF/Events/VariableValueChangedEventArgs.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
|
||||
namespace DMS.WPF.Events;
|
||||
|
||||
/// <summary>
|
||||
/// 变量值改变事件参数
|
||||
/// </summary>
|
||||
public class VariableValueChangedEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// 变量ID
|
||||
/// </summary>
|
||||
public int VariableId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 变量名称
|
||||
/// </summary>
|
||||
public string VariableName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 旧值
|
||||
/// </summary>
|
||||
public string OldValue { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 新值
|
||||
/// </summary>
|
||||
public string NewValue { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 值改变时间
|
||||
/// </summary>
|
||||
public DateTime ChangeTime { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 初始化VariableValueChangedEventArgs类的新实例
|
||||
/// </summary>
|
||||
/// <param name="variableId">变量ID</param>
|
||||
/// <param name="variableName">变量名称</param>
|
||||
/// <param name="oldValue">旧值</param>
|
||||
/// <param name="newValue">新值</param>
|
||||
public VariableValueChangedEventArgs(int variableId, string variableName, string oldValue, string newValue)
|
||||
{
|
||||
VariableId = variableId;
|
||||
VariableName = variableName;
|
||||
OldValue = oldValue;
|
||||
NewValue = newValue;
|
||||
ChangeTime = DateTime.Now;
|
||||
}
|
||||
}
|
||||
58
DMS.WPF/Interfaces/IEventService.cs
Normal file
58
DMS.WPF/Interfaces/IEventService.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using DMS.WPF.Events;
|
||||
|
||||
namespace DMS.WPF.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// 事件服务接口,用于统一管理应用程序中的各种事件
|
||||
/// </summary>
|
||||
public interface IEventService
|
||||
{
|
||||
#region 设备事件
|
||||
|
||||
/// <summary>
|
||||
/// 设备状态改变事件
|
||||
/// </summary>
|
||||
event EventHandler<DeviceActiveChangedEventArgs> DeviceStatusChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 触发设备状态改变事件
|
||||
/// </summary>
|
||||
/// <param name="sender">事件发送者</param>
|
||||
/// <param name="e">设备状态改变事件参数</param>
|
||||
void RaiseDeviceStatusChanged(object sender, DeviceActiveChangedEventArgs e);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 变量事件
|
||||
|
||||
/// <summary>
|
||||
/// 变量值改变事件
|
||||
/// </summary>
|
||||
event EventHandler<VariableValueChangedEventArgs> VariableValueChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 触发变量值改变事件
|
||||
/// </summary>
|
||||
/// <param name="sender">事件发送者</param>
|
||||
/// <param name="e">变量值改变事件参数</param>
|
||||
void RaiseVariableValueChanged(object sender, VariableValueChangedEventArgs e);
|
||||
|
||||
#endregion
|
||||
|
||||
#region MQTT事件
|
||||
|
||||
/// <summary>
|
||||
/// MQTT连接状态改变事件
|
||||
/// </summary>
|
||||
event EventHandler<MqttConnectionChangedEventArgs> MqttConnectionChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 触发MQTT连接状态改变事件
|
||||
/// </summary>
|
||||
/// <param name="sender">事件发送者</param>
|
||||
/// <param name="e">MQTT连接状态改变事件参数</param>
|
||||
void RaiseMqttConnectionChanged(object sender, MqttConnectionChangedEventArgs e);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
using DMS.WPF.Interfaces;
|
||||
|
||||
namespace DMS.WPF.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
@@ -34,4 +36,9 @@ public interface IWPFDataService
|
||||
/// </summary>
|
||||
ILogDataService LogDataService { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 事件服务。
|
||||
/// </summary>
|
||||
IEventService EventService { get; }
|
||||
|
||||
}
|
||||
74
DMS.WPF/Services/DeviceMonitoringService.cs
Normal file
74
DMS.WPF/Services/DeviceMonitoringService.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using DMS.WPF.Events;
|
||||
using DMS.WPF.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DMS.WPF.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 设备监控服务,用于监听设备状态改变事件并进行相应处理
|
||||
/// </summary>
|
||||
public class DeviceMonitoringService
|
||||
{
|
||||
private readonly ILogger<DeviceMonitoringService> _logger;
|
||||
private readonly IEventService _eventService;
|
||||
private readonly INotificationService _notificationService;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化DeviceMonitoringService类的新实例
|
||||
/// </summary>
|
||||
/// <param name="logger">日志服务</param>
|
||||
/// <param name="eventService">事件服务</param>
|
||||
/// <param name="notificationService">通知服务</param>
|
||||
public DeviceMonitoringService(
|
||||
ILogger<DeviceMonitoringService> logger,
|
||||
IEventService eventService,
|
||||
INotificationService notificationService)
|
||||
{
|
||||
_logger = logger;
|
||||
_eventService = eventService;
|
||||
_notificationService = notificationService;
|
||||
|
||||
// 订阅设备状态改变事件
|
||||
_eventService.DeviceStatusChanged += OnDeviceStatusChanged;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理设备状态改变事件
|
||||
/// </summary>
|
||||
/// <param name="sender">事件发送者</param>
|
||||
/// <param name="e">设备状态改变事件参数</param>
|
||||
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} 状态改变事件时发生错误");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
// 取消订阅事件
|
||||
_eventService.DeviceStatusChanged -= OnDeviceStatusChanged;
|
||||
}
|
||||
}
|
||||
68
DMS.WPF/Services/EventService.cs
Normal file
68
DMS.WPF/Services/EventService.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using DMS.WPF.Events;
|
||||
using DMS.WPF.Interfaces;
|
||||
|
||||
namespace DMS.WPF.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 事件服务实现类,用于统一管理应用程序中的各种事件
|
||||
/// </summary>
|
||||
public class EventService : IEventService
|
||||
{
|
||||
#region 设备事件
|
||||
|
||||
/// <summary>
|
||||
/// 设备状态改变事件
|
||||
/// </summary>
|
||||
public event EventHandler<DeviceActiveChangedEventArgs> DeviceStatusChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 触发设备状态改变事件
|
||||
/// </summary>
|
||||
/// <param name="sender">事件发送者</param>
|
||||
/// <param name="e">设备状态改变事件参数</param>
|
||||
public void RaiseDeviceStatusChanged(object sender, DeviceActiveChangedEventArgs e)
|
||||
{
|
||||
DeviceStatusChanged?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 变量事件
|
||||
|
||||
/// <summary>
|
||||
/// 变量值改变事件
|
||||
/// </summary>
|
||||
public event EventHandler<VariableValueChangedEventArgs> VariableValueChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 触发变量值改变事件
|
||||
/// </summary>
|
||||
/// <param name="sender">事件发送者</param>
|
||||
/// <param name="e">变量值改变事件参数</param>
|
||||
public void RaiseVariableValueChanged(object sender, VariableValueChangedEventArgs e)
|
||||
{
|
||||
VariableValueChanged?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region MQTT事件
|
||||
|
||||
/// <summary>
|
||||
/// MQTT连接状态改变事件
|
||||
/// </summary>
|
||||
public event EventHandler<MqttConnectionChangedEventArgs> MqttConnectionChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 触发MQTT连接状态改变事件
|
||||
/// </summary>
|
||||
/// <param name="sender">事件发送者</param>
|
||||
/// <param name="e">MQTT连接状态改变事件参数</param>
|
||||
public void RaiseMqttConnectionChanged(object sender, MqttConnectionChangedEventArgs e)
|
||||
{
|
||||
MqttConnectionChanged?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -45,6 +45,11 @@ public class WPFDataService : IWPFDataService
|
||||
/// </summary>
|
||||
public ILogDataService LogDataService { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 事件服务。
|
||||
/// </summary>
|
||||
public IEventService EventService { get; }
|
||||
|
||||
/// <summary>
|
||||
/// WPFDataService 构造函数。
|
||||
/// </summary>
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,9 +44,13 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
|
||||
/// <summary>
|
||||
/// 初始化 <see cref="DevicesViewModel"/> 类的新实例。
|
||||
/// </summary>
|
||||
/// <param name="logger">日志记录器。</param>
|
||||
/// <param name="mapper">对象映射器。</param>
|
||||
/// <param name="dataStorageService">数据存储服务。</param>
|
||||
/// <param name="dialogService">对话框服务。</param>
|
||||
/// <param name="navigationService">导航服务。</param>
|
||||
/// <param name="wpfDataService">主数据服务。</param>
|
||||
/// <param name="deviceAppService">设备应用服务。</param>
|
||||
/// <param name="notificationService">通知服务。</param>
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
/// </summary>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当IsActive属性改变时调用,用于发布设备状态改变事件
|
||||
/// </summary>
|
||||
partial void OnIsActiveChanged(bool oldValue, bool newValue)
|
||||
{
|
||||
// 只有当设备ID有效且事件服务已初始化时才发布事件
|
||||
if (Id > 0 && EventService != null)
|
||||
{
|
||||
// 发布设备状态改变事件
|
||||
EventService.RaiseDeviceStatusChanged(this, new DeviceActiveChangedEventArgs(Id, Name, oldValue, newValue));
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollection<VariableTableItemViewModel> VariableTables { get; set; } = new();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user