添加了事件服务,并完成了设备状态改变后写入数据库

This commit is contained in:
2025-09-12 14:59:32 +08:00
parent cb739f4cb9
commit 6796a06325
13 changed files with 113 additions and 164 deletions

View File

@@ -0,0 +1,68 @@
using System;
using DMS.Application.Events;
using DMS.Application.Interfaces;
namespace DMS.Application.Services;
/// <summary>
/// 事件服务实现类,用于统一管理应用程序中的各种事件
/// </summary>
public class EventService : IEventService
{
#region
/// <summary>
/// 设备状态改变事件
/// </summary>
public event EventHandler<DeviceActiveChangedEventArgs> OnDeviceActiveChanged;
/// <summary>
/// 触发设备状态改变事件
/// </summary>
/// <param name="sender">事件发送者</param>
/// <param name="e">设备状态改变事件参数</param>
public void RaiseDeviceStatusChanged(object sender, DeviceActiveChangedEventArgs e)
{
OnDeviceActiveChanged?.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
}

View File

@@ -0,0 +1,55 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using DMS.Application.Events;
using DMS.Application.Interfaces;
using DMS.Core.Models;
using Microsoft.Extensions.Logging;
namespace DMS.Application.Services;
/// <summary>
/// 设备监视服务实现类,用于监视设备的状态变化
/// </summary>
public class DeviceMonitoringService : IDeviceMonitoringService, IDisposable
{
private readonly ILogger<DeviceMonitoringService> _logger;
private readonly IEventService _eventService;
private readonly IAppDataStorageService _appDataStorageService;
private readonly IAppDataCenterService _appDataCenterService;
/// <summary>
/// 初始化DeviceMonitoringService类的新实例
/// </summary>
/// <param name="logger">日志记录器</param>
/// <param name="deviceAppService">设备应用服务</param>
public DeviceMonitoringService(ILogger<DeviceMonitoringService> logger, IEventService eventService,
IAppDataStorageService appDataStorageService,
IAppDataCenterService appDataCenterService)
{
_logger = logger;
_eventService = eventService;
_appDataStorageService = appDataStorageService;
_appDataCenterService = appDataCenterService;
_eventService.OnDeviceActiveChanged += OnDeviceActiveChanged;
}
private void OnDeviceActiveChanged(object? sender, DeviceActiveChangedEventArgs e)
{
if (_appDataStorageService.Devices.TryGetValue(e.DeviceId, out var device))
{
if (device.IsActive != e.NewStatus)
{
device.IsActive = e.NewStatus;
_appDataCenterService.DeviceManagementService.UpdateDeviceAsync(device);
}
}
}
public void Dispose()
{
_eventService.OnDeviceActiveChanged -= OnDeviceActiveChanged;
}
}