Program.cs
11.1 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using FreeSql;
using HHECS.DAQHandle.Common.Enums;
using HHECS.DAQHandle.Common.Utils;
using HHECS.DAQHandle.DataAccess;
using HHECS.DAQHandle.Dto;
using HHECS.DAQHandle.EquipmentHandle;
using HHECS.DAQHandle.Models;
using HHECS.EquipmentModel;
using System.Configuration;
using System.Diagnostics;
using System.Xml.Linq;
using static FreeSql.Internal.GlobalFilter;
internal class Program
{
/// <summary>
/// 单次处理的数据量
/// </summary>
private static int _limit = 100;
private static long LastEquipmentCount = 0;
private static long LastEquipmentPropTempCount = 0;
private static List<EquipmentType> EquipmentTypes;
//设备缓存数据
public static Dictionary<string, EquipmentInfoDto> EquipmentInfos;
protected static int DataRetentionDays = int.Parse(ConfigurationManager.AppSettings["DataRetentionDays"]);
private static void Main(string[] args)
{
try
{
Console.Title = "IOT数据处理端";
var warehouseCode = ConfigurationManager.AppSettings["WarehouseCode"];
if (string.IsNullOrWhiteSpace(warehouseCode))
{
SystemLog.PrintWarn("仓库编号未配置!");
return;
}
var equipmentTypeCodes = ConfigurationManager.AppSettings["EquipmentType"].Split(',').ToList();
if (equipmentTypeCodes == null || equipmentTypeCodes.Count == 0)
{
SystemLog.PrintWarn("设备类型未配置!");
return;
}
var limit = ConfigurationManager.AppSettings["ProcessingDataVolume"];
if (!int.TryParse(limit, out _limit))
{
_limit = 100;
}
using var context = new DataContext();
EquipmentTypes = context.EquipmentType.Where(x => equipmentTypeCodes.Contains(x.Code)).IncludeMany(x => x.EquipmentTypePropTemplates).ToList();
//刷新模板数据
InitData();
Task.Run(async () =>
{
await Task.Delay(1000 * 60);
while (true)
{
try
{
if (IsDataChange())
{
InitData();
}
DeleteExpireData();
await Task.Delay(1000 * 60);
}
catch (Exception ex)
{
SystemLog.PrintError($"{ex}");
}
}
});
Startup(EquipmentTypes);
}
catch (Exception ex)
{
SystemLog.PrintWarn($"程序启动出现异常:{ex.Message}");
}
}
private static void Startup(IEnumerable<EquipmentType> equipmentTypes)
{
while (true)
{
try
{
var tasks = new List<Task>();
// using var context = new DataContext();
foreach (var item in equipmentTypes)
{
using var context = new DataContext();
var equipmentCodes = context.Equipment.Where(x => x.EquipmentTypeId == item.Id).ToList(x => x.Code);
var equipmentDataRecords = context.EquipmentDataRecord.Where(x => equipmentCodes.Contains(x.EquipmentCode) && !x.IsHandle).OrderBy(x => x.Timestamp).Take(_limit).ToList();
tasks.Add(Task.Run(() =>
{
Stopwatch stopwatch = Stopwatch.StartNew();
_ = Enum.TryParse<EquipmentTypeConst>(item.Code, out var equipmentTypeCode);
IAnalysis handle = equipmentTypeCode switch
{
EquipmentTypeConst.SingleForkSRM => new SingleForkSRMAnalysis(item),
EquipmentTypeConst.DoubleForkSRM => new DoubleForkSRMAnalysis(item),
EquipmentTypeConst.SingleForkSSRM => new SingleForkSSRMAnalysis(item),
EquipmentTypeConst.SingleForkSSRMV132 => new SingleForkSSRMV132Analysis(item),
EquipmentTypeConst.WeldRobot => new WeldRobotAnalysis(item),
EquipmentTypeConst.AGV => new AGVAnalysis(item),
EquipmentTypeConst.RGVStation => new RGVAnalysis(item),
EquipmentTypeConst.StationMonitor => new StationMonitorAnalysis(item),
_ => null
};
//未知类型,则跳过
if (handle is null)
{
SystemLog.PrintWarn($"未实现设备类型[{item.Code}]对应的处理方法");
return;
}
try
{
//using var context = new DataContext();
//var equipmentCodes = context.Equipment.Where(x => x.EquipmentTypeId == item.Id).ToList(x => x.Code);
//var equipmentDataRecords = context.EquipmentDataRecord.Where(x => equipmentCodes.Contains(x.EquipmentCode) && !x.IsHandle).OrderBy(x => x.Timestamp).Take(_limit).ToList();
//if (equipmentDataRecords.Count == 0)
//{
// SystemLog.PrintInfo($"设备类型[{item.Code}]队列数据为空,跳过解析");
// return;
//}
var result = handle.Execute(equipmentDataRecords);
stopwatch.Stop();
if (!result.Success)
{
SystemLog.PrintError($"设备类型[{item.Code}]解析失败,{result.Msg},数量{equipmentDataRecords.Count},耗时{stopwatch.ElapsedMilliseconds}ms");
return;
}
SystemLog.PrintInfo($"设备类型[{item.Code}]解析完成,数量{equipmentDataRecords.Count},耗时{stopwatch.ElapsedMilliseconds}ms");
}
catch (Exception ex)
{
SystemLog.PrintError($"{ex}");
}
}));
}
Task.WaitAll(tasks.ToArray());
Console.WriteLine($"————————————————————————————————————————————————————————");
}
catch (Exception ex)
{
SystemLog.PrintError($"程序异常:{ex}");
}
}
}
/// <summary>
/// 初始化设备数据,并进行缓存
/// </summary>
private static void InitData()
{
SystemLog.PrintInfo("数据发生改变刷新设备数据");
using var context = new DataContext();
var equipments = context.Equipment.Where(x => EquipmentTypes.Any(y => y.Code == x.EquipmentType.Code)).IncludeMany(x => x.EquipmentProps).Include(x => x.EquipmentType).ToList();
var equipmentPropTemps = context.EquipmentTypePropTemplate.Where(x => EquipmentTypes.Any(y => y.Id == x.EquipmentTypeId)).ToList();
EquipmentInfos = equipments.Select(equipment => new EquipmentInfoDto()
{
Id = equipment.Id,
Code = equipment.Code,
Name = equipment.Name,
EquipmentType = equipment.EquipmentType,
//EquipmentPropTemps = x.EquipmentProps.Select(prop => new{prop.Address,Value=eq})
EquipmentPropTemps =
//equipment.EquipmentProps.ToDictionary(prop => prop.Address,
// prop => equipmentPropTemps.Find(temp => temp.Code == prop.EquipmentTypePropTemplateCode)?.MonitorCompareValue)
equipment.EquipmentProps.Select(prop =>
{
var temp = equipmentPropTemps.Find(temp => temp.Code == prop.EquipmentTypePropTemplateCode);
if (temp == null)
{
SystemLog.PrintError($"设备{equipment.Name}的属性{prop.EquipmentTypePropTemplateCode}在模板中不存在");
return (prop.Address, null, null, null, null);
}
return (prop.Address, temp?.MonitorCompareValue, temp?.PropType, temp?.MonitorFailure, temp.Code);
}).ToList(),
}).ToDictionary(x => x.Code, x => x);
}
/// <summary>
/// 判断设备数据或者设备模板数据是否发生改变
/// </summary>
/// <returns></returns>
private static bool IsDataChange()
{
var flag = false;
using var context = new DataContext();
var lastEquipmentCount = context.Equipment.Where(x => true).Count();
var lastEquipmentPropTempCount = context.EquipmentProp.Where(x => true).Count();
if (lastEquipmentCount != LastEquipmentCount || lastEquipmentPropTempCount != LastEquipmentPropTempCount)
{
flag = true;
}
LastEquipmentCount = lastEquipmentCount;
LastEquipmentPropTempCount = lastEquipmentPropTempCount;
return flag;
}
private static void DeleteExpireData()
{
try
{
using var context = new DataContext();
var handleStage = EquipmentDataRecord.SET_ERROR_HANDLE | EquipmentDataRecord.SET_STATUS_HANDLE | EquipmentDataRecord.SET_VAULE_HANDLE;
//删除已完成并且超过配置保留天数的数据
var count = context.EquipmentDataRecord.Remove(x => x.IsHandle && x.HandleStage == handleStage && (DateTime.Now - (DateTime)x.CreateTime).TotalDays > DataRetentionDays);
context.SaveChanges();
SystemLog.PrintInfo($"清除了{count}条已处理数据");
}
catch (Exception ex)
{
SystemLog.PrintInfo($"清除已处理数据失败{ex}");
}
#region
//EquipmentType = equipmentType;
//EquipmentTypePropTemps = equipmentType.EquipmentTypePropTemplates;
//var handleStage = EquipmentDataRecord.SET_ERROR_HANDLE | EquipmentDataRecord.SET_STATUS_HANDLE | EquipmentDataRecord.SET_VAULE_HANDLE;
//var count = Context.EquipmentDataRecord.Where(x => x.IsHandle && x.HandleStage == handleStage).Count();
////保留9000-10000条数据
//if (count < DataRetentionAmount) return;
//var handleData = Context.EquipmentDataRecord.Where(x => x.IsHandle && x.HandleStage == handleStage).OrderBy(x => x.Timestamp).Take(1000).ToList();
//if (handleData.Count > 0)
//{
// try
// {
// Context.EquipmentDataRecord.RemoveRange(handleData);
// Context.SaveChanges();
// SystemLog.PrintInfo($"清除了{handleData.Count}条已处理数据");
// }
// catch (Exception ex)
// {
// SystemLog.PrintInfo($"清除已处理数据失败{ex}");
// }
//}
#endregion
}
}