2025-09-12 14:59:32 +08:00
|
|
|
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))
|
|
|
|
|
{
|
2025-09-12 15:45:14 +08:00
|
|
|
_appDataCenterService.DeviceManagementService.UpdateDeviceAsync(device);
|
2025-09-12 14:59:32 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_eventService.OnDeviceActiveChanged -= OnDeviceActiveChanged;
|
|
|
|
|
}
|
|
|
|
|
}
|