EquipmentStatusUpdateBackgroundService.cs
5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using HHECS.DAQShared.Common.Enums;
using HHECS.DAQShared.Common.Utils;
using HHECS.DAQShared.Dto;
using HHECS.DAQShared.Models;
namespace HHECS.DAQServer.Services
{
public class EquipmentStatusUpdateBackgroundService : BackgroundService
{
private readonly IFreeSql _freeSql;
private readonly DataCacheService _dataCacheService;
private readonly CommonService _commonService;
private readonly ILogger<EquipmentStatusUpdateBackgroundService> _logger;
private readonly int _limit = 1000;
public EquipmentStatusUpdateBackgroundService(IFreeSql freeSql, DataCacheService dataCacheService,
CommonService commonService, ILogger<EquipmentStatusUpdateBackgroundService> logger)
{
_freeSql = freeSql;
_dataCacheService = dataCacheService;
_commonService = commonService;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
//每10秒执行一次
await Task.Delay(10000, stoppingToken);
using var equipmentStatusRecordRepository = _freeSql.GetRepository<EquipmentStatusRecord>();
var temps = _dataCacheService.EquipmentStatusDictionary.Where(x => _commonService.TimestampConvertToLocalDateTime(x.Value.Timestamp) >= DateTime.Now.AddMinutes(-5)).ToList();
//按照设定的数量进行分组
var groups = new List<List<KeyValuePair<string, EquipmentDataDto>>>();
for (int i = 0; i < temps.Count; i += _limit)
{
var group = temps.Skip(i).Take(_limit).ToList();
groups.Add(group);
}
//待更新数据
var waitUpdateItem = new List<EquipmentStatusRecord>();
foreach (var group in groups)
{
var allEquipmentSN = group.Select(x => x.Key).ToList();
var equipmentStatusItems = equipmentStatusRecordRepository.Where(x => allEquipmentSN.Contains(x.EquipmentCode)).ToList();
var equipments = _freeSql.Queryable<EquipmentExtend>().Where(x => allEquipmentSN.Contains(x.Code)).ToList(x => new EquipmentExtend
{
Id = x.Id,
Code = x.Code,
EquipmentTypeId = x.EquipmentTypeId,
});
var equipmentTypeIds = equipments.Select(x => x.EquipmentTypeId).Distinct().ToList();
var equipmentTypes = _freeSql.Queryable<EquipmentTypeExtend>().Where(x => equipmentTypeIds.Contains(x.Id)).ToList(x => new EquipmentTypeExtend
{
Id = x.Id,
Code = x.Code,
});
foreach (var item in equipmentStatusItems)
{
var temp = group.Find(x => x.Key == item.EquipmentCode).Value;
if (item.UpdateTime < _commonService.TimestampConvertToLocalDateTime(temp.Timestamp))
{
var currentEquipment = equipments.Find(x => x.Code == item.EquipmentCode);
var currentEquipmentType = equipmentTypes.Find(x => x.Id == currentEquipment.EquipmentTypeId);
_ = Enum.TryParse<EquipmentTypeConst>(currentEquipmentType.Code, out var equipmetTypeEnum);
var currentEquipmentProps = _freeSql.Queryable<EquipmentPropExtend>().Where(x => x.EquipmentId == currentEquipment.Id).ToList(x => new EquipmentPropExtend
{
Id = x.Id,
EquipmentTypePropTemplateCode = x.EquipmentTypePropTemplateCode,
Address = x.Address,
});
var statusResult = EquipmentStatusHelper.GetEquipmentStatus(equipmetTypeEnum, currentEquipmentProps, temp.Reported);
if (!statusResult.Success)
{
continue;
}
var currentStatus = statusResult.Data.ToString();
equipmentStatusRecordRepository.Attach(item);
//状态有变更
if (item.Status != currentStatus || item.CreateTime.Date != DateTime.Now.Date)
{
item.CreateTime = DateTime.Now;
}
item.Status = currentStatus;
item.UpdateTime = _commonService.TimestampConvertToLocalDateTime(temp.Timestamp);
item.StatusDuration = 0;
item.HandleTime = DateTime.Now;
waitUpdateItem.Add(item);
}
}
}
//分批更新
for (int i = 0; i < waitUpdateItem.Count; i += _limit)
{
var temp2s = waitUpdateItem.Skip(i).Take(_limit).ToList();
equipmentStatusRecordRepository.Update(temp2s);
}
}
catch (Exception ex)
{
_logger.LogError($"[{nameof(EquipmentStatusUpdateBackgroundService)}]线程异常:{ex.Message}");
}
}
}
}
}