fix 优化获取设备状态数据
This commit is contained in:
@@ -264,7 +264,7 @@ public class DeviceService : IDeviceService
|
|||||||
/// 获取离线设备
|
/// 获取离线设备
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private async Task<List<DeviceEntity>> GetOfflineDevicesCount(List<DeviceEntity> devices)
|
private async Task<List<long>> GetOfflineDevices(List<DeviceEntity> devices)
|
||||||
{
|
{
|
||||||
var now = DateTime.Now;
|
var now = DateTime.Now;
|
||||||
|
|
||||||
@@ -281,23 +281,35 @@ public class DeviceService : IDeviceService
|
|||||||
})
|
})
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
var onlineDevices = deviceRegistries.Where(x =>
|
var offlineUniqueNos = deviceRegistries
|
||||||
{
|
.Where(x => x.HeartbeatInterval > 0 && x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval) < now)
|
||||||
if (x.HeartbeatInterval <= 0) return true;
|
.Select(x => x.UniqueNo);
|
||||||
return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval) >= now;
|
|
||||||
})
|
|
||||||
.Select(x => new
|
|
||||||
{
|
|
||||||
x.UniqueNo
|
|
||||||
})
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
var onlineUniqueNos = new List<string>();
|
return devices.Where(x => offlineUniqueNos.Contains(x.UniqueNo)).Select(x => x.Id).ToList();
|
||||||
onlineDevices.ForEach(x => onlineUniqueNos.Add(x.UniqueNo));
|
}
|
||||||
|
|
||||||
var offUniqueNos = deviceUniqueNos.Except(onlineUniqueNos).ToList();
|
private async Task<List<long>> GetOnlineDevices(List<DeviceEntity> devices)
|
||||||
var list = devices.Where(x => offUniqueNos.Contains(x.UniqueNo)).ToList();
|
{
|
||||||
return list;
|
var now = DateTime.Now;
|
||||||
|
|
||||||
|
var deviceUniqueNos = devices.Select(x => x.UniqueNo).ToList();
|
||||||
|
|
||||||
|
var deviceRegistries = await _dbContext.Queryable<DeviceRegistryEntity>()
|
||||||
|
.LeftJoin<DeviceEntity>((x, d) => x.DeviceUniqueNo == d.UniqueNo)
|
||||||
|
.Where(x => deviceUniqueNos.Contains(x.DeviceUniqueNo))
|
||||||
|
.Select((x, d) => new
|
||||||
|
{
|
||||||
|
d.UniqueNo,
|
||||||
|
d.HeartbeatInterval,
|
||||||
|
x.LastHeartbeatTime
|
||||||
|
})
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
var offlineUniqueNos = deviceRegistries
|
||||||
|
.Where(x => x.HeartbeatInterval <= 0 || x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval) >= now)
|
||||||
|
.Select(x => x.UniqueNo);
|
||||||
|
|
||||||
|
return devices.Where(x => offlineUniqueNos.Contains(x.UniqueNo)).Select(x => x.Id).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -314,13 +326,9 @@ public class DeviceService : IDeviceService
|
|||||||
? await _dbContext.Queryable<DeviceEntity>().Where(x => x.Type == DeviceTypeEnumType.Attendance).ToListAsync()
|
? await _dbContext.Queryable<DeviceEntity>().Where(x => x.Type == DeviceTypeEnumType.Attendance).ToListAsync()
|
||||||
: await _dbContext.Queryable<DeviceEntity>().Where(x => x.Type == DeviceTypeEnumType.Door).ToListAsync();
|
: await _dbContext.Queryable<DeviceEntity>().Where(x => x.Type == DeviceTypeEnumType.Door).ToListAsync();
|
||||||
|
|
||||||
var offDevices = await GetOfflineDevicesCount(devices);
|
var ids = isOnline
|
||||||
//离线
|
? await GetOnlineDevices(devices)
|
||||||
var ids = offDevices.Select(x => x.Id).ToList();
|
: await GetOfflineDevices(devices);
|
||||||
if (!isOnline)
|
|
||||||
{
|
|
||||||
ids = devices.Select(x => x.Id).Except(ids).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
return await _dbContext.Queryable<DeviceEntity>()
|
return await _dbContext.Queryable<DeviceEntity>()
|
||||||
.LeftJoin<DeviceTemplateEntity>((d, dt) => d.TemplateCode == dt.Code)
|
.LeftJoin<DeviceTemplateEntity>((d, dt) => d.TemplateCode == dt.Code)
|
||||||
|
|||||||
Reference in New Issue
Block a user