添加了事件服务,并完成了设备状态改变后写入数据库
This commit is contained in:
@@ -1,74 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -44,11 +44,6 @@ public class WPFDataService : IWPFDataService
|
||||
/// 日志数据服务。
|
||||
/// </summary>
|
||||
public ILogDataService LogDataService { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 事件服务。
|
||||
/// </summary>
|
||||
public IEventService EventService { get; }
|
||||
|
||||
/// <summary>
|
||||
/// WPFDataService 构造函数。
|
||||
@@ -61,8 +56,7 @@ public class WPFDataService : IWPFDataService
|
||||
IMenuDataService menuDataService,
|
||||
IMqttDataService mqttDataService,
|
||||
ILogDataService logDataService,
|
||||
IVariableTableDataService variableTableDataService,
|
||||
IEventService eventService)
|
||||
IVariableTableDataService variableTableDataService)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_appDataCenterService = appDataCenterService;
|
||||
@@ -72,6 +66,5 @@ public class WPFDataService : IWPFDataService
|
||||
MqttDataService = mqttDataService;
|
||||
LogDataService = logDataService;
|
||||
VariableTableDataService = variableTableDataService;
|
||||
EventService = eventService;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user