添加了事件服务,并完成了设备状态改变后写入数据库
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
|
||||
namespace DMS.WPF.Events;
|
||||
namespace DMS.Application.Events;
|
||||
|
||||
/// <summary>
|
||||
/// 设备状态改变事件参数
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
|
||||
namespace DMS.WPF.Events;
|
||||
namespace DMS.Application.Events;
|
||||
|
||||
/// <summary>
|
||||
/// MQTT连接状态改变事件参数
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
|
||||
namespace DMS.WPF.Events;
|
||||
namespace DMS.Application.Events;
|
||||
|
||||
/// <summary>
|
||||
/// 变量值改变事件参数
|
||||
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
|
||||
{
|
||||
}
|
||||
@@ -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>
|
||||
/// 触发设备状态改变事件
|
||||
@@ -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
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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,15 +45,9 @@ public partial class App : System.Windows.Application
|
||||
public App()
|
||||
{
|
||||
Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
|
||||
.ConfigureServices((context, services) =>
|
||||
{
|
||||
ConfigureServices(services);
|
||||
})
|
||||
.ConfigureLogging(loggingBuilder =>
|
||||
{
|
||||
ConfigureLogging(loggingBuilder);
|
||||
})
|
||||
.Build();
|
||||
.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>());
|
||||
@@ -84,7 +77,7 @@ public partial class App : System.Windows.Application
|
||||
var notificationService = Host.Services.GetRequiredService<INotificationService>();
|
||||
notificationService.ShowError($"加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}", exception);
|
||||
}
|
||||
|
||||
|
||||
var splashWindow = Host.Services.GetRequiredService<SplashWindow>();
|
||||
splashWindow.Show();
|
||||
}
|
||||
@@ -155,13 +148,14 @@ public partial class App : System.Windows.Application
|
||||
services.AddSingleton<ILoggerFactory, NLogLoggerFactory>();
|
||||
services.AddSingleton<GrowlNotificationService>();
|
||||
services.AddSingleton<INotificationService, NotificationService>();
|
||||
|
||||
|
||||
// 注册核心服务
|
||||
services.AddAutoMapper(cfg =>
|
||||
{
|
||||
// 最终解决方案:根据异常信息的建议,设置此标记以忽略重复的Profile加载。
|
||||
// 注意:此属性位于 Internal() 方法下。
|
||||
cfg.Internal().AllowAdditiveTypeMapCreation = true;
|
||||
cfg.Internal()
|
||||
.AllowAdditiveTypeMapCreation = true;
|
||||
|
||||
cfg.AddProfile(new DMS.Application.Profiles.MappingProfile());
|
||||
cfg.AddProfile(new DMS.Infrastructure.Profiles.MappingProfile());
|
||||
@@ -180,16 +174,17 @@ public partial class App : System.Windows.Application
|
||||
services.AddSingleton<IChannelBus, ChannelBus>();
|
||||
services.AddSingleton<IMessenger, Messenger>();
|
||||
services.AddHostedService<OptimizedS7BackgroundService>();
|
||||
|
||||
|
||||
|
||||
|
||||
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>();
|
||||
services.AddSingleton<HistoryProcessor>();
|
||||
services.AddSingleton<MqttPublishProcessor>();
|
||||
|
||||
|
||||
// 注册Core中的仓库
|
||||
services.AddSingleton<AppSettings>();
|
||||
services.AddSingleton<SqlSugarDbContext>(_ =>
|
||||
@@ -197,7 +192,7 @@ public partial class App : System.Windows.Application
|
||||
var appSettings = new AppSettings { Database = { Database = "dms_test" } };
|
||||
return new SqlSugarDbContext(appSettings);
|
||||
});
|
||||
|
||||
|
||||
services.AddSingleton<IInitializeRepository, InitializeRepository>();
|
||||
services.AddSingleton<IDeviceRepository, DeviceRepository>();
|
||||
services.AddSingleton<IVariableTableRepository, VariableTableRepository>();
|
||||
@@ -214,14 +209,14 @@ public partial class App : System.Windows.Application
|
||||
services.AddTransient<IOpcUaService, OpcUaService>();
|
||||
services.AddTransient<IMqttService, MqttService>();
|
||||
services.AddTransient<IMqttServiceFactory, MqttServiceFactory>();
|
||||
|
||||
|
||||
// 注册App服务\r\n
|
||||
services.AddSingleton<IInitializeService, InitializeService>();
|
||||
services.AddSingleton<IDeviceAppService, DeviceAppService>();
|
||||
services.AddSingleton<IVariableAppService, VariableAppService>();
|
||||
services.AddSingleton<IHistoryAppService, HistoryAppService>();
|
||||
services.AddSingleton<IVariableTableAppService, VariableTableAppService>();
|
||||
services.AddSingleton<IMenuService, MenuService>();
|
||||
services.AddSingleton<IMenuService, MenuService>();
|
||||
services.AddSingleton<IAppDataCenterService, AppDataCenterService>();
|
||||
services.AddSingleton<INavigationService, NavigationService>();
|
||||
services.AddSingleton<IDialogService, DialogService>();
|
||||
@@ -232,24 +227,22 @@ public partial class App : System.Windows.Application
|
||||
services.AddSingleton<IMenuManagementService, MenuManagementService>();
|
||||
services.AddSingleton<IMqttManagementService, MqttManagementService>();
|
||||
services.AddSingleton<ILogManagementService, LogManagementService>();
|
||||
|
||||
|
||||
services.AddSingleton<IDataLoaderService, DataLoaderService>();
|
||||
services.AddSingleton<INlogAppService, NlogAppService>();
|
||||
|
||||
services.AddSingleton<IDeviceMonitoringService, DeviceMonitoringService>();
|
||||
|
||||
// 注册MQTT服务管理器
|
||||
services.AddSingleton<IMqttServiceManager, MqttServiceManager>();
|
||||
services.AddSingleton<IMqttAliasAppService, MqttAliasAppService>();
|
||||
services.AddHostedService<MqttBackgroundService>();
|
||||
|
||||
|
||||
// 注册WPF中的服务
|
||||
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>();
|
||||
services.AddSingleton<IVariableDataService, VariableDataService>();
|
||||
@@ -259,34 +252,11 @@ public partial class App : System.Windows.Application
|
||||
services.AddSingleton<ILogDataService, LogDataService>();
|
||||
services.AddSingleton<IDataEventService, DataEventService>();
|
||||
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>();
|
||||
|
||||
|
||||
|
||||
|
||||
// 注册视图模型
|
||||
services.AddSingleton<SplashViewModel>();
|
||||
services.AddSingleton<MainViewModel>();
|
||||
@@ -301,7 +271,7 @@ public partial class App : System.Windows.Application
|
||||
services.AddSingleton<LogHistoryViewModel>();
|
||||
services.AddScoped<MqttServerDetailViewModel>();
|
||||
services.AddSingleton<VariableHistoryViewModel>();
|
||||
|
||||
|
||||
// 注册对话框视图模型
|
||||
services.AddTransient<DeviceDialogViewModel>();
|
||||
services.AddTransient<ConfirmDialogViewModel>();
|
||||
@@ -316,7 +286,7 @@ public partial class App : System.Windows.Application
|
||||
services.AddTransient<MqttAliasBatchEditDialogViewModel>();
|
||||
services.AddTransient<HistorySettingsDialogViewModel>();
|
||||
services.AddTransient<AlarmSettingsDialogViewModel>();
|
||||
|
||||
|
||||
// 注册View视图
|
||||
services.AddSingleton<SplashWindow>();
|
||||
services.AddSingleton<SettingView>();
|
||||
@@ -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作为日志提供者
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using DMS.WPF.Interfaces;
|
||||
using DMS.Application.Interfaces;
|
||||
|
||||
namespace DMS.WPF.Interfaces;
|
||||
|
||||
@@ -35,10 +35,5 @@ public interface IWPFDataService
|
||||
/// 日志数据服务。
|
||||
/// </summary>
|
||||
ILogDataService LogDataService { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 事件服务。
|
||||
/// </summary>
|
||||
IEventService EventService { get; }
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,9 +64,6 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
|
||||
_deviceAppService = deviceAppService;
|
||||
_notificationService = notificationService;
|
||||
Devices = _dataStorageService.Devices;
|
||||
|
||||
// 设置DeviceItemViewModel的静态服务引用
|
||||
DeviceItemViewModel.EventService = wpfDataService.EventService;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -83,7 +84,7 @@ public partial class DeviceItemViewModel : ObservableObject
|
||||
partial void OnIsActiveChanged(bool oldValue, bool newValue)
|
||||
{
|
||||
// 只有当设备ID有效且事件服务已初始化时才发布事件
|
||||
if (Id > 0 && EventService != null)
|
||||
if (Id > 0 && EventService != null )
|
||||
{
|
||||
// 发布设备状态改变事件
|
||||
EventService.RaiseDeviceStatusChanged(this, new DeviceActiveChangedEventArgs(Id, Name, oldValue, newValue));
|
||||
|
||||
Reference in New Issue
Block a user