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

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

@@ -1,6 +1,6 @@
using System;
namespace DMS.WPF.Events;
namespace DMS.Application.Events;
/// <summary>
/// 设备状态改变事件参数

View File

@@ -1,6 +1,6 @@
using System;
namespace DMS.WPF.Events;
namespace DMS.Application.Events;
/// <summary>
/// MQTT连接状态改变事件参数

View File

@@ -1,6 +1,6 @@
using System;
namespace DMS.WPF.Events;
namespace DMS.Application.Events;
/// <summary>
/// 变量值改变事件参数

View File

@@ -0,0 +1,11 @@
using System;
using DMS.Application.Events;
namespace DMS.Application.Interfaces;
/// <summary>
/// 设备监视服务接口,用于监视设备的状态变化
/// </summary>
public interface IDeviceMonitoringService
{
}

View File

@@ -1,7 +1,7 @@
using System;
using DMS.WPF.Events;
using DMS.Application.Events;
namespace DMS.WPF.Interfaces;
namespace DMS.Application.Interfaces;
/// <summary>
/// 事件服务接口,用于统一管理应用程序中的各种事件
@@ -13,7 +13,7 @@ public interface IEventService
/// <summary>
/// 设备状态改变事件
/// </summary>
event EventHandler<DeviceActiveChangedEventArgs> DeviceStatusChanged;
event EventHandler<DeviceActiveChangedEventArgs> OnDeviceActiveChanged;
/// <summary>
/// 触发设备状态改变事件

View File

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

View File

@@ -20,6 +20,7 @@ using DMS.WPF.Logging;
using DMS.WPF.Services;
using DMS.WPF.ViewModels;
using DMS.WPF.ViewModels.Dialogs;
using DMS.WPF.ViewModels.Items;
using DMS.WPF.Views;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@@ -44,14 +45,8 @@ public partial class App : System.Windows.Application
public App()
{
Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
ConfigureServices(services);
})
.ConfigureLogging(loggingBuilder =>
{
ConfigureLogging(loggingBuilder);
})
.ConfigureServices((context, services) => { ConfigureServices(services); })
.ConfigureLogging(loggingBuilder => { ConfigureLogging(loggingBuilder); })
.Build();
Services = Host.Services;
_logger = Host.Services.GetRequiredService<ILogger<App>>();
@@ -67,10 +62,8 @@ public partial class App : System.Windows.Application
try
{
Host.Services.GetRequiredService<GrowlNotificationService>();
// 初始化设备监控服务
Host.Services.GetRequiredService<DeviceMonitoringService>();
Host.Services.GetRequiredService<IDeviceMonitoringService>();
DeviceItemViewModel.EventService = Host.Services.GetRequiredService<IEventService>();
// 初始化数据处理链
var dataProcessingService = Host.Services.GetRequiredService<IDataProcessingService>();
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<CheckValueChangedProcessor>());
@@ -161,7 +154,8 @@ public partial class App : System.Windows.Application
{
// 最终解决方案根据异常信息的建议设置此标记以忽略重复的Profile加载。
// 注意:此属性位于 Internal() 方法下。
cfg.Internal().AllowAdditiveTypeMapCreation = true;
cfg.Internal()
.AllowAdditiveTypeMapCreation = true;
cfg.AddProfile(new DMS.Application.Profiles.MappingProfile());
cfg.AddProfile(new DMS.Infrastructure.Profiles.MappingProfile());
@@ -183,7 +177,8 @@ public partial class App : System.Windows.Application
services.AddSingleton<IDataProcessingService, DataProcessingService>();
services.AddHostedService(provider => (DataProcessingService)provider.GetRequiredService<IDataProcessingService>());
services.AddHostedService(provider =>
(DataProcessingService)provider.GetRequiredService<IDataProcessingService>());
services.AddSingleton<CheckValueChangedProcessor>();
services.AddSingleton<LoggingProcessor>();
services.AddSingleton<UpdateDbVariableProcessor>();
@@ -235,6 +230,7 @@ public partial class App : System.Windows.Application
services.AddSingleton<IDataLoaderService, DataLoaderService>();
services.AddSingleton<INlogAppService, NlogAppService>();
services.AddSingleton<IDeviceMonitoringService, DeviceMonitoringService>();
// 注册MQTT服务管理器
services.AddSingleton<IMqttServiceManager, MqttServiceManager>();
@@ -245,10 +241,7 @@ public partial class App : System.Windows.Application
services.AddSingleton<IMqttAppService, MqttAppService>();
// 注册事件服务
services.AddSingleton<IEventService, EventService>();
// 注册设备监控服务
services.AddSingleton<DeviceMonitoringService>();
services.AddSingleton<DMS.Application.Interfaces.IEventService, DMS.Application.Services.EventService>();
// 注册新的数据服务
services.AddSingleton<IDeviceDataService, DeviceDataService>();
@@ -261,30 +254,7 @@ public partial class App : System.Windows.Application
services.AddSingleton<IDataStorageService, DataStorageService>();
// 注册主数据服务
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 =>
// new DataServices(
// provider.GetRequiredService<IMapper>(),
// provider.GetRequiredService<IAppDataCenterService>(),
// provider.GetRequiredService<IMqttAppService>()
// )
// );
services.AddSingleton<IWPFDataService, WPFDataService>();
// 注册视图模型
@@ -332,7 +302,8 @@ public partial class App : System.Windows.Application
private void ConfigureLogging(ILoggingBuilder loggingBuilder)
{
LogManager.Setup().LoadConfigurationFromFile("Configurations/nlog.config");
LogManager.Setup()
.LoadConfigurationFromFile("Configurations/nlog.config");
loggingBuilder.ClearProviders();
loggingBuilder.SetMinimumLevel(LogLevel.Trace);
// loggingBuilder.addn; // 添加NLog作为日志提供者

View File

@@ -1,4 +1,4 @@
using DMS.WPF.Interfaces;
using DMS.Application.Interfaces;
namespace DMS.WPF.Interfaces;
@@ -36,9 +36,4 @@ public interface IWPFDataService
/// </summary>
ILogDataService LogDataService { get; }
/// <summary>
/// 事件服务。
/// </summary>
IEventService EventService { get; }
}

View File

@@ -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;
}
}

View File

@@ -45,11 +45,6 @@ public class WPFDataService : IWPFDataService
/// </summary>
public ILogDataService LogDataService { get; }
/// <summary>
/// 事件服务。
/// </summary>
public IEventService EventService { get; }
/// <summary>
/// WPFDataService 构造函数。
/// </summary>
@@ -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;
}
}

View File

@@ -64,9 +64,6 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
_deviceAppService = deviceAppService;
_notificationService = notificationService;
Devices = _dataStorageService.Devices;
// 设置DeviceItemViewModel的静态服务引用
DeviceItemViewModel.EventService = wpfDataService.EventService;
}

View File

@@ -2,8 +2,9 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Application.Events;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
using DMS.WPF.Events;
using DMS.WPF.Interfaces;
namespace DMS.WPF.ViewModels.Items;