修复在线设备数量

This commit is contained in:
Zhanghu
2026-01-13 15:37:51 +08:00
parent 5ace3be63f
commit 6c362c0e87
2 changed files with 23 additions and 2 deletions

View File

@@ -342,13 +342,16 @@ public class DashboardController : ControllerBase
{
private readonly DeviceManagementService _deviceManagementService;
private readonly ApplicationService _applicationService;
private readonly ILogger<DashboardController> _logger;
public DashboardController(
DeviceManagementService deviceManagementService,
ApplicationService applicationService)
ApplicationService applicationService,
ILogger<DashboardController> logger)
{
_deviceManagementService = deviceManagementService;
_applicationService = applicationService;
_logger = logger;
}
/// <summary>
@@ -360,10 +363,26 @@ public class DashboardController : ControllerBase
var devices = await _deviceManagementService.GetAllDevicesAsync();
var applications = await _applicationService.GetAllAsync();
// 在线设备1分钟内有活动
var onlineThreshold = DateTime.UtcNow.AddMinutes(-1);
var onlineCount = devices.Data.Count(d => d.LastSeenAt.HasValue && d.LastSeenAt >= onlineThreshold);
// 调试输出
_logger.LogInformation("UTC Now: {Now}", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"));
_logger.LogInformation("Online Threshold: {Threshold}", onlineThreshold.ToString("yyyy-MM-dd HH:mm:ss"));
foreach (var d in devices.Data)
{
_logger.LogInformation("Device {Id}: LastSeenAt={LastSeenAt}, IsOnline={IsOnline}",
d.Id,
d.LastSeenAt?.ToString("yyyy-MM-dd HH:mm:ss") ?? "null",
d.LastSeenAt.HasValue && d.LastSeenAt >= onlineThreshold);
}
_logger.LogInformation("Online Count: {Count}", onlineCount);
var stats = new DashboardStatsDto
{
TotalDevices = devices.Data.Count,
OnlineDevices = devices.Data.Count(d => d.IsEnabled),
OnlineDevices = onlineCount,
TotalApplications = applications.Total
};