添加了事件服务,并完成了设备状态改变后写入数据库
This commit is contained in:
50
DMS.Application/Events/DeviceActiveChangedEventArgs.cs
Normal file
50
DMS.Application/Events/DeviceActiveChangedEventArgs.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
|
||||
namespace DMS.Application.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.Application/Events/MqttConnectionChangedEventArgs.cs
Normal file
50
DMS.Application/Events/MqttConnectionChangedEventArgs.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
|
||||
namespace DMS.Application.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.Application/Events/VariableValueChangedEventArgs.cs
Normal file
50
DMS.Application/Events/VariableValueChangedEventArgs.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
|
||||
namespace DMS.Application.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;
|
||||
}
|
||||
}
|
||||
11
DMS.Application/Interfaces/IDeviceMonitoringService.cs
Normal file
11
DMS.Application/Interfaces/IDeviceMonitoringService.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using DMS.Application.Events;
|
||||
|
||||
namespace DMS.Application.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// 设备监视服务接口,用于监视设备的状态变化
|
||||
/// </summary>
|
||||
public interface IDeviceMonitoringService
|
||||
{
|
||||
}
|
||||
58
DMS.Application/Interfaces/IEventService.cs
Normal file
58
DMS.Application/Interfaces/IEventService.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using DMS.Application.Events;
|
||||
|
||||
namespace DMS.Application.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// 事件服务接口,用于统一管理应用程序中的各种事件
|
||||
/// </summary>
|
||||
public interface IEventService
|
||||
{
|
||||
#region 设备事件
|
||||
|
||||
/// <summary>
|
||||
/// 设备状态改变事件
|
||||
/// </summary>
|
||||
event EventHandler<DeviceActiveChangedEventArgs> OnDeviceActiveChanged;
|
||||
|
||||
/// <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
|
||||
}
|
||||
68
DMS.Application/Services/EventService.cs
Normal file
68
DMS.Application/Services/EventService.cs
Normal 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
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user