唐召明
authored
about a year ago
1
2
using HHECS.BllModel ;
using HHECS.DAQHandle ;
唐召明
authored
about a year ago
3
using HHECS.DAQHandle.Common.Utils ;
李璐瑶
authored
about a year ago
4
using HHECS.DAQHandle.Dto ;
唐召明
authored
about a year ago
5
using HHECS.DAQHandle.EquipmentHandle ;
唐召明
authored
about a year ago
6
7
using HHECS.DAQShared.Common.Enums ;
using HHECS.DAQShared.Models ;
唐召明
authored
about a year ago
8
using LinqKit ;
唐召明
authored
about a year ago
9
10
11
using System.Configuration ;
using System.Diagnostics ;
唐召明
authored
about a year ago
12
internal class Program
唐召明
authored
about a year ago
13
{
唐召明
authored
about a year ago
14
private static DateTime LastUploadTime = DateTime . MinValue ;
唐召明
authored
about a year ago
15
private static void Main ( string [] args )
唐召明
authored
about a year ago
16
{
唐召明
authored
about a year ago
17
try
李璐瑶
authored
about a year ago
18
{
唐召明
authored
about a year ago
19
20
var appConfigResult = LoadAppConfig ();
if (! appConfigResult . Success )
李璐瑶
authored
about a year ago
21
{
唐召明
authored
about a year ago
22
SystemLog . PrintError ( $ "加载配置文件信息失败:{appConfigResult.Msg}" );
唐召明
authored
about a year ago
23
return ;
李璐瑶
authored
about a year ago
24
}
唐召明
authored
about a year ago
25
var appConfig = appConfigResult . Data ;
唐召明
authored
about a year ago
26
唐召明
authored
about a year ago
27
28
var result = ValidationConfig ( appConfig );
if (! result . Success )
李璐瑶
authored
about a year ago
29
{
唐召明
authored
about a year ago
30
SystemLog . PrintError ( $ "配置信息校验失败:{result.Msg}" );
唐召明
authored
about a year ago
31
return ;
李璐瑶
authored
about a year ago
32
}
唐召明
authored
about a year ago
33
唐召明
authored
about a year ago
34
35
var connectionString = ConfigurationManager . ConnectionStrings [ appConfig . IsProductionEnvironment ? "Production" : "Development" ]. ConnectionString ;
GlobalVar . SetConnectionString ( connectionString );
唐召明
authored
about a year ago
36
UpdateClientStatus ();
唐召明
authored
about a year ago
37
Startup ( appConfig );
唐召明
authored
about a year ago
38
39
40
}
catch ( Exception ex )
{
唐召明
authored
about a year ago
41
SystemLog . PrintWarn ( $ "程序启动出现异常:{ex.Message}" );
唐召明
authored
about a year ago
42
43
}
}
李璐瑶
authored
about a year ago
44
唐召明
authored
about a year ago
45
private static void Startup ( AppConfig config )
唐召明
authored
about a year ago
46
{
唐召明
authored
about a year ago
47
var equipments = new List < EquipmentExtend >();
唐召明
authored
about a year ago
48
var equipmentTypes = new List < EquipmentTypeExtend >();
唐召明
authored
about a year ago
49
var equipmentTypeCodes = config . EquipmentTypeCodes ;
唐召明
authored
about a year ago
50
SystemLog . PrintInfo ( $ "正在初始化数据..." );
唐召明
authored
about a year ago
51
while ( true )
唐召明
authored
about a year ago
52
{
唐召明
authored
about a year ago
53
try
唐召明
authored
about a year ago
54
{
唐召明
authored
about a year ago
55
56
var result = InitialData ( equipmentTypeCodes , ref equipments , ref equipmentTypes );
if (! result . Success )
李璐瑶
authored
about a year ago
57
{
唐召明
authored
about a year ago
58
59
SystemLog . PrintError ( $ "初始化数据失败:{result.Msg}" );
continue ;
李璐瑶
authored
about a year ago
60
}
唐召明
authored
about a year ago
61
62
63
64
65
66
if ( equipmentTypes . Count == 0 )
{
SystemLog . PrintError ( $ "获取设备类型[{string.Join(',', equipmentTypeCodes)}]数据失败,数据为空!" );
continue ;
}
Stopwatch stopwatch = Stopwatch . StartNew ();
唐召明
authored
about a year ago
67
68
69
70
71
var freeSql = GlobalVar . FreeSql ;
// 获取所有设备记录表名称, FreeSql 的分表查询,时间跨天后不能获取到新表,这里使用自定义方式实现
var tableNames = freeSql . Select < object >(). WithSql ( "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name LIKE 'daq_equipment_data_record%'" ). OrderBy ( x => "table_name" ). ToList < string >();
唐召明
authored
about a year ago
72
var tasks = new List < Task >();
唐召明
authored
about a year ago
73
foreach ( var currentEquipmentType in equipmentTypes )
唐召明
authored
about a year ago
74
{
唐召明
authored
about a year ago
75
76
77
tasks . Add ( Task . Run (() =>
{
Stopwatch stopwatch = Stopwatch . StartNew ();
唐召明
authored
about a year ago
78
79
80
81
82
83
84
85
_ = Enum . TryParse < EquipmentTypeConst >( currentEquipmentType . Code , out var equipmentTypeCode );
var parameter = new AnalysisParameter
{
Equipments = equipments . Where ( x => x . EquipmentTypeId == currentEquipmentType . Id ). ToList (),
EquipmentType = currentEquipmentType ,
AppConfig = config ,
EquipmentTotalAlarmCode = equipmentTypeCode switch
{
唐召明
authored
12 months ago
86
87
88
89
90
91
EquipmentTypeConst . SingleForkSRM or
EquipmentTypeConst . SingleForkSRMV2 => SRMProps . TotalError . ToString (),
EquipmentTypeConst . DoubleForkSRM or
EquipmentTypeConst . DoubleForkSRMV2 => SRMProps . TotalError . ToString (),
EquipmentTypeConst . SingleForkSSRM or
EquipmentTypeConst . SingleForkSSRMV2 => SRMProps . TotalError . ToString (),
唐召明
authored
about a year ago
92
93
EquipmentTypeConst . SingleForkSSRMV132 => SRMProps . TotalError . ToString (),
EquipmentTypeConst . WeldRobot => RobotProp . Alarm . ToString (),
唐召明
authored
about a year ago
94
EquipmentTypeConst . WeldRobotV2 => RobotV2Prop . Alarm . ToString (),
唐召明
authored
12 months ago
95
EquipmentTypeConst . AGVForklift => AGVForkliftProp . Error . ToString (),
唐召明
authored
about a year ago
96
EquipmentTypeConst . RGVStation => RGVStationProps . Fault . ToString (),
唐召明
authored
about a year ago
97
98
EquipmentTypeConst . StationMonitor => StationProps . StationError . ToString (),
EquipmentTypeConst . Hoist => HoistProps . HoistStatus . ToString (),
唐召明
authored
about a year ago
99
EquipmentTypeConst . CNC => CNCProp . RedLight . ToString (),
唐召明
authored
about a year ago
100
EquipmentTypeConst . SingleForkRGV => SingleForkRGVProp . TotalError . ToString (),
唐召明
authored
about a year ago
101
102
103
_ => string . Empty ,
}
};
唐召明
authored
about a year ago
104
105
IAnalysis handle = equipmentTypeCode switch
{
唐召明
authored
12 months ago
106
107
108
109
110
111
EquipmentTypeConst . SingleForkSRM or
EquipmentTypeConst . SingleForkSRMV2 => new SingleForkSRMAnalysis ( parameter ),
EquipmentTypeConst . DoubleForkSRM or
EquipmentTypeConst . DoubleForkSRMV2 => new DoubleForkSRMAnalysis ( parameter ),
EquipmentTypeConst . SingleForkSSRM or
EquipmentTypeConst . SingleForkSSRMV2 => new SingleForkSSRMAnalysis ( parameter ),
唐召明
authored
about a year ago
112
113
EquipmentTypeConst . SingleForkSSRMV132 => new SingleForkSSRMV132Analysis ( parameter ),
EquipmentTypeConst . WeldRobot => new WeldRobotAnalysis ( parameter ),
唐召明
authored
about a year ago
114
EquipmentTypeConst . WeldRobotV2 => new WeldRobotV2Analysis ( parameter ),
唐召明
authored
12 months ago
115
EquipmentTypeConst . AGVForklift => new AGVForkliftAnalysis ( parameter ),
唐召明
authored
about a year ago
116
EquipmentTypeConst . RGVStation => new RGVStationAnalysis ( parameter ),
唐召明
authored
about a year ago
117
EquipmentTypeConst . SingleForkRGV => new SingleForkRGVAnalysis ( parameter ),
唐召明
authored
about a year ago
118
119
EquipmentTypeConst . StationMonitor => new StationMonitorAnalysis ( parameter ),
EquipmentTypeConst . Hoist => new HoistAnalysis ( parameter ),
唐召明
authored
about a year ago
120
EquipmentTypeConst . CNC => new CNCAnalysis ( parameter ),
唐召明
authored
about a year ago
121
122
123
124
125
_ => null
};
// 未知类型,则跳过
if ( handle is null )
{
唐召明
authored
about a year ago
126
SystemLog . PrintWarn ( $ "未实现设备类型[{currentEquipmentType.Code}]对应的处理方法" );
唐召明
authored
about a year ago
127
128
return ;
}
李璐瑶
authored
about a year ago
129
try
唐召明
authored
about a year ago
130
{
唐召明
authored
about a year ago
131
132
133
134
135
136
137
var limit = config . ProcessingDataVolume ;
if ( equipmentTypeCode == EquipmentTypeConst . StationMonitor )
{
// 站台数据量比较大,目前不处理站台状态和报警,所以每次多取一些数据
limit *= 5 ;
}
唐召明
authored
about a year ago
138
using var equipmentDataRecordRepository = freeSql . GetRepository < EquipmentDataRecord >();
唐召明
authored
about a year ago
139
var equipmentCodes = equipments . Where ( x => x . EquipmentTypeId == currentEquipmentType . Id ). Select ( x => x . Code ). ToList ();
唐召明
authored
about a year ago
140
唐召明
authored
about a year ago
141
142
// 缓存有数据的表名称集合
var cacheTableNames = new List < string >();
唐召明
authored
about a year ago
143
144
145
146
147
148
149
150
151
var equipmentDataRecords = new List < EquipmentDataRecord >();
foreach ( var tableName in tableNames )
{
// 达到设定数量时停止往后查询
if ( equipmentDataRecords . Count == limit )
{
break ;
}
唐召明
authored
about a year ago
152
153
154
155
156
157
158
159
var filter = PredicateBuilder . New < EquipmentDataRecord >( true );
filter = filter . And ( x => equipmentCodes . Contains ( x . EquipmentCode ));
filter = filter . And ( x => ! x . IsHandle );
if ( config . Version != 0 )
{
filter = filter . And ( x => x . Version == config . Version );
}
唐召明
authored
about a year ago
160
161
162
// 本次查询数量
var currentLimit = limit - equipmentDataRecords . Count ;
var temps = freeSql . Queryable < EquipmentDataRecord >(). AsTable (( type , oldName ) => tableName )
唐召明
authored
about a year ago
163
. Where ( filter )
唐召明
authored
about a year ago
164
. OrderBy ( x => x . Timestamp ). Take ( currentLimit ). ToList ();
唐召明
authored
about a year ago
165
166
167
168
169
if ( temps . Count > 0 )
{
equipmentDataRecords . AddRange ( temps );
cacheTableNames . Add ( tableName );
}
唐召明
authored
about a year ago
170
171
}
唐召明
authored
about a year ago
172
173
174
175
176
if ( equipmentDataRecords . Count == 0 )
{
SystemLog . PrintInfo ( $ "设备类型[{currentEquipmentType.Code}]记录数据为空,跳过解析,耗时:{stopwatch.ElapsedMilliseconds}ms" );
return ;
}
李璐瑶
authored
about a year ago
177
178
179
180
var result = handle . Execute ( equipmentDataRecords );
if (! result . Success )
{
唐召明
authored
about a year ago
181
SystemLog . PrintError ( $ "设备类型[{currentEquipmentType.Code}]解析失败,{result.Msg},数量{equipmentDataRecords.Count},耗时:{stopwatch.ElapsedMilliseconds}ms" );
李璐瑶
authored
about a year ago
182
183
return ;
}
唐召明
authored
about a year ago
184
唐召明
authored
about a year ago
185
// 删除数据
唐召明
authored
about a year ago
186
var ids = equipmentDataRecords . Select ( x => x . Id ). ToList ();
唐召明
authored
about a year ago
187
188
189
190
foreach ( var tableName in cacheTableNames )
{
freeSql . Delete < EquipmentDataRecord >(). AsTable ( tableName ). Where ( x => ids . Contains ( x . Id )). CommandTimeout ( 300 ). ExecuteAffrows ();
}
唐召明
authored
about a year ago
191
唐召明
authored
about a year ago
192
SystemLog . PrintSuccess ( $ "设备类型[{currentEquipmentType.Code}]解析完成,数量{equipmentDataRecords.Count},耗时:{stopwatch.ElapsedMilliseconds}ms" );
李璐瑶
authored
about a year ago
193
194
195
}
catch ( Exception ex )
{
唐召明
authored
about a year ago
196
SystemLog . PrintError ( $ "[{handle.GetType().Name}]线程数据解析出现异常:{ex.Message}" );
唐召明
authored
about a year ago
197
}
唐召明
authored
about a year ago
198
}));
唐召明
authored
about a year ago
199
}
唐召明
authored
about a year ago
200
Task . WaitAll ( tasks . ToArray ());
唐召明
authored
about a year ago
201
Thread . Sleep ( 1000 );
唐召明
authored
about a year ago
202
203
const string tempString = "——————————————————" ;
Console . WriteLine ( $ "{tempString}[总耗时:{stopwatch.Elapsed}]{tempString}" );
唐召明
authored
about a year ago
204
205
206
}
catch ( Exception ex )
{
唐召明
authored
about a year ago
207
SystemLog . PrintError ( $ "程序异常:{ex.Message}" );
唐召明
authored
about a year ago
208
}
唐召明
authored
about a year ago
209
210
}
}
唐召明
authored
about a year ago
211
李璐瑶
authored
about a year ago
212
/// < summary >
唐召明
authored
about a year ago
213
/// 加载配置文件信息
李璐瑶
authored
about a year ago
214
/// </ summary >
唐召明
authored
about a year ago
215
216
/// < returns ></ returns >
private static BllResult < AppConfig > LoadAppConfig ()
李璐瑶
authored
about a year ago
217
{
唐召明
authored
about a year ago
218
219
220
221
222
223
224
225
try
{
_ = int . TryParse ( ConfigurationManager . AppSettings [ "DataTimeOut" ], out int dataTimeOutValue );
_ = int . TryParse ( ConfigurationManager . AppSettings [ "ProcessingDataVolume" ], out int processingDataVolumeValue );
_ = int . TryParse ( ConfigurationManager . AppSettings [ "DataRetentionDays" ], out int dataRetentionDaysValue );
_ = int . TryParse ( ConfigurationManager . AppSettings [ "Version" ], out var version );
_ = bool . TryParse ( ConfigurationManager . AppSettings [ "IsProductionEnvironment" ], out var isProductionEnvironmentValue );
_ = Guid . TryParse (( ConfigurationManager . AppSettings [ "ClientId" ]), out var clientIdValue );
唐召明
authored
about a year ago
226
227
var equipmentTypeCodes = ConfigurationManager . AppSettings [ "EquipmentType" ];
var warehouseCode = ConfigurationManager . AppSettings [ "WarehouseCode" ];
李璐瑶
authored
about a year ago
228
唐召明
authored
about a year ago
229
230
231
232
if ( processingDataVolumeValue <= 0 )
{
processingDataVolumeValue = 100 ;
}
李璐瑶
authored
about a year ago
233
唐召明
authored
about a year ago
234
if ( isProductionEnvironmentValue )
李璐瑶
authored
about a year ago
235
{
唐召明
authored
about a year ago
236
237
238
239
240
241
242
243
244
Console . Title = "IOT数据处理端" ;
}
else
{
Console . Title = "IOT数据处理端(测试版)" ;
}
var appConfig = new AppConfig
{
ClientId = clientIdValue ,
唐召明
authored
about a year ago
245
WarehouseCode = warehouseCode ,
唐召明
authored
about a year ago
246
247
248
DataTimeOut = dataTimeOutValue ,
IsProductionEnvironment = isProductionEnvironmentValue ,
ProcessingDataVolume = processingDataVolumeValue ,
唐召明
authored
about a year ago
249
EquipmentTypeCodes = equipmentTypeCodes ?. Split ( ',' ). Where ( x => ! string . IsNullOrWhiteSpace ( x )). ToList () ?? new List < string >(),
唐召明
authored
about a year ago
250
251
252
253
254
255
256
257
258
Version = version
};
return BllResultFactory . Success ( appConfig );
}
catch ( Exception ex )
{
return BllResultFactory . Error < AppConfig >( ex . Message );
}
李璐瑶
authored
about a year ago
259
260
261
}
/// < summary >
唐召明
authored
about a year ago
262
/// 配置信息校验
李璐瑶
authored
about a year ago
263
/// </ summary >
唐召明
authored
about a year ago
264
/// < param name = "appConfig" ></ param >
李璐瑶
authored
about a year ago
265
/// < returns ></ returns >
唐召明
authored
about a year ago
266
private static BllResult ValidationConfig ( AppConfig appConfig )
李璐瑶
authored
about a year ago
267
{
唐召明
authored
about a year ago
268
269
270
271
272
273
try
{
if ( string . IsNullOrWhiteSpace ( appConfig . WarehouseCode ))
{
return BllResultFactory . Error ( $ "仓库编号[{nameof(appConfig.WarehouseCode)}]未配置!" );
}
李璐瑶
authored
about a year ago
274
唐召明
authored
about a year ago
275
276
if ( appConfig . EquipmentTypeCodes == null || appConfig . EquipmentTypeCodes . Count == 0 )
{
唐召明
authored
about a year ago
277
SystemLog . PrintWarn ( $ "设备类型[{nameof(appConfig.EquipmentTypeCodes)}]未配置,将默认匹配所有设备" );
唐召明
authored
about a year ago
278
}
李璐瑶
authored
about a year ago
279
唐召明
authored
about a year ago
280
281
if ( appConfig . Version == 0 )
{
唐召明
authored
about a year ago
282
SystemLog . PrintWarn ( $ "程序版本[{nameof(appConfig.Version)}]未配置,将匹配所有版本设备记录" );
唐召明
authored
about a year ago
283
284
285
286
}
return BllResultFactory . Success ();
}
catch ( Exception ex )
李璐瑶
authored
about a year ago
287
{
唐召明
authored
about a year ago
288
return BllResultFactory . Error ( ex . Message );
李璐瑶
authored
about a year ago
289
290
}
}
李璐瑶
authored
about a year ago
291
唐召明
authored
about a year ago
292
293
294
295
296
297
/// < summary >
/// 初始化数据
/// </ summary >
/// < param name = "equipmentTypeCodes" > 设备类型编号 </ param >
/// < param name = "equipments" > 设备集合 </ param >
/// < param name = "equipmentTypes" > 设备类型集合 </ param >
唐召明
authored
about a year ago
298
299
300
/// < param name = "timeSpanHours" > 强制刷新超时时间,默认 1 小时 </ param >
/// < remarks > 在设备或设备属性数据总数发生变化或距离上次加载时间超过一小时,则刷新数据 </ remarks >
private static BllResult InitialData ( List < string > equipmentTypeCodes , ref List < EquipmentExtend > equipments , ref List < EquipmentTypeExtend > equipmentTypes , double timeSpanHours = 1 )
李璐瑶
authored
about a year ago
301
302
303
{
try
{
唐召明
authored
about a year ago
304
using var freeSql = GlobalVar . FreeSql ;
唐召明
authored
about a year ago
305
306
307
308
var equipmentRepository = freeSql . GetRepository < EquipmentExtend >();
var equipmentPropRepository = freeSql . GetRepository < EquipmentPropExtend >();
var equipmentTypeRepository = freeSql . GetRepository < EquipmentTypeExtend >();
唐召明
authored
about a year ago
309
if (( DateTime . Now . AddHours (- timeSpanHours ) > LastUploadTime ) || equipmentTypes . Count == 0 )
唐召明
authored
about a year ago
310
{
唐召明
authored
about a year ago
311
312
313
314
315
316
var filter = PredicateBuilder . New < EquipmentTypeExtend >( true );
if ( equipmentTypeCodes . Count > 0 )
{
filter = filter . And ( x => equipmentTypeCodes . Contains ( x . Code ));
}
equipmentTypes = equipmentTypeRepository . Where ( filter ). IncludeMany ( x => x . EquipmentTypePropTemplates ). ToList ();
唐召明
authored
about a year ago
317
318
}
var equipmentTypeIds = equipmentTypes . Select ( x => x . Id ). ToList ();
唐召明
authored
about a year ago
319
var equipmentIds = equipmentRepository . Where ( x => equipmentTypeIds . Contains ( x . EquipmentTypeId )). ToList ( x => x . Id );
唐召明
authored
about a year ago
320
var equipmentTotal = equipmentIds . Count ;
唐召明
authored
about a year ago
321
var equipmentPropTotal = equipmentPropRepository . Where ( x => equipmentIds . Contains ( x . EquipmentId )). Count ();
唐召明
authored
about a year ago
322
if (( DateTime . Now . AddHours (- timeSpanHours ) > LastUploadTime ) || equipments . Count != equipmentTotal || equipments . SelectMany ( x => x . EquipmentProps ). Count () != equipmentPropTotal )
唐召明
authored
about a year ago
323
{
唐召明
authored
about a year ago
324
equipments = equipmentRepository . Where ( x => equipmentTypeIds . Contains ( x . EquipmentTypeId )). IncludeMany ( x => x . EquipmentProps ). ToList ();
唐召明
authored
about a year ago
325
326
327
328
329
330
foreach ( var item in equipments )
{
var equipmentType = equipmentTypes . Find ( x => x . Id == item . EquipmentTypeId );
item . EquipmentType = equipmentType ;
item . EquipmentProps . ForEach ( x => x . EquipmentTypePropTemplate = equipmentType . EquipmentTypePropTemplates . Find ( t => t . Id == x . EquipmentTypePropTemplateId ));
}
唐召明
authored
about a year ago
331
LastUploadTime = DateTime . Now ;
唐召明
authored
about a year ago
332
333
}
return BllResultFactory . Success ();
李璐瑶
authored
about a year ago
334
335
336
}
catch ( Exception ex )
{
唐召明
authored
about a year ago
337
return BllResultFactory . Error ( ex . Message );
李璐瑶
authored
about a year ago
338
339
}
}
唐召明
authored
about a year ago
340
唐召明
authored
about a year ago
341
342
343
/// < summary >
/// 更新客户端状态
/// </ summary >
唐召明
authored
about a year ago
344
345
private static void UpdateClientStatus ()
{
唐召明
authored
about a year ago
346
Task . Run ( async () =>
唐召明
authored
about a year ago
347
{
李璐瑶
authored
about a year ago
348
while ( true )
唐召明
authored
about a year ago
349
{
李璐瑶
authored
about a year ago
350
try
唐召明
authored
about a year ago
351
{
唐召明
authored
about a year ago
352
await Task . Delay ( 5000 );
唐召明
authored
about a year ago
353
using var clientStatusRepository = GlobalVar . FreeSql . GetRepository < ClientStatus >();
李璐瑶
authored
about a year ago
354
355
356
// 更新客户端状态
_ = Guid . TryParse ( ConfigurationManager . AppSettings [ "ClientId" ], out var clientId );
if ( clientId != Guid . Empty )
唐召明
authored
about a year ago
357
{
唐召明
authored
about a year ago
358
var client = clientStatusRepository . Where ( x => x . ClientKeys == clientId ). First ();
李璐瑶
authored
about a year ago
359
360
if ( client != null )
{
唐召明
authored
about a year ago
361
clientStatusRepository . Attach ( client );
李璐瑶
authored
about a year ago
362
client . LastSeenDate = DateTime . Now ;
唐召明
authored
about a year ago
363
clientStatusRepository . Update ( client );
李璐瑶
authored
about a year ago
364
}
唐召明
authored
about a year ago
365
366
}
}
李璐瑶
authored
about a year ago
367
368
369
370
catch ( Exception ex )
{
SystemLog . PrintError ( $ "客户端状态更新线程异常:{ex.Message}" );
}
唐召明
authored
about a year ago
371
}
李璐瑶
authored
about a year ago
372
});
唐召明
authored
about a year ago
373
}
唐召明
authored
about a year ago
374
375
/// < summary >
唐召明
authored
about a year ago
376
/// 修复数据设备报警和设备状态
唐召明
authored
about a year ago
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/// </ summary >
private static void FixData ()
{
var freeSql = GlobalVar . FreeSql ;
using var equipmentAlarmRecordRepository = freeSql . GetRepository < EquipmentAlarmRecord >();
using var equipmentStatusRecordHistoryRepository = freeSql . GetRepository < EquipmentStatusRecordHistory >();
// 历史报警
var alarmStartTime = equipmentAlarmRecordRepository . Where ( x => true ). OrderBy ( x => x . CreateTime ). First ( x => x . CreateTime ). Date ;
var alarmTotalDays = ( DateTime . Today - alarmStartTime ). Days ;
var allAlarmRecords = equipmentAlarmRecordRepository . Where ( x => x . UpdateTime < DateTime . Today ). OrderBy ( x => x . CreateTime ). ToList ();
for ( int i = 0 ; i < alarmTotalDays ; i ++)
{
var tempDate1 = alarmStartTime . AddDays ( i );
var tempDate2 = alarmStartTime . AddDays ( i + 1 );
var records = allAlarmRecords . Where ( x => x . CreateTime . Date == tempDate1 && x . UpdateTime . Date != tempDate1 ). ToList ();
if ( records . Count > 0 )
{
Console . WriteLine ( $ "[报警]{tempDate1.ToShortDateString()}:{records.Count}条" );
}
foreach ( var item in records )
{
var days = ( item . UpdateTime . Date - item . CreateTime . Date ). Days ;
var addTemps = new List < EquipmentAlarmRecord >();
for ( int j = 1 ; j <= days ; j ++)
{
var tempDate3 = tempDate1 . AddDays ( j );
var tempDate4 = tempDate3 . Date == item . UpdateTime . Date ? item . UpdateTime : tempDate3 . AddDays ( 1 ). AddSeconds (- 1 );
var addTemp = new EquipmentAlarmRecord
{
EquipmentCode = item . EquipmentCode ,
EquipmentName = item . EquipmentName ,
EquipmentPropCode = item . EquipmentPropCode ,
EquipmentPropName = item . EquipmentPropName ,
AlarmMessage = item . AlarmMessage ,
EquipmentTypeCode = item . EquipmentTypeCode ,
FactoryCode = item . FactoryCode ,
ProjectCode = item . ProjectCode ,
HandleTime = item . HandleTime ,
IsEnd = item . IsEnd ,
IsHandled = item . IsHandled ,
Remark = item . Remark ,
ErrorDuration = ( tempDate4 - tempDate3 ). TotalSeconds ,
CreateTime = tempDate3 ,
UpdateTime = tempDate4
};
if ( addTemp . ErrorDuration > 0 )
{
addTemps . Add ( addTemp );
}
};
equipmentAlarmRecordRepository . Attach ( item );
item . UpdateTime = tempDate2 . AddSeconds (- 1 );
item . ErrorDuration = ( item . UpdateTime - item . CreateTime ). TotalSeconds ;
equipmentAlarmRecordRepository . Update ( item );
equipmentAlarmRecordRepository . Insert ( addTemps );
}
}
// 历史状态
var statusStartTime = equipmentStatusRecordHistoryRepository . Where ( x => true ). OrderBy ( x => x . CreateTime ). First ( x => x . CreateTime ). Date ;
var statusTotalDays = ( DateTime . Today - statusStartTime ). Days ;
var allStatusRecords = equipmentStatusRecordHistoryRepository . Where ( x => x . UpdateTime < DateTime . Today ). OrderBy ( x => x . CreateTime ). ToList ();
for ( int i = 0 ; i < statusTotalDays ; i ++)
{
var tempDate1 = alarmStartTime . AddDays ( i );
var tempDate2 = alarmStartTime . AddDays ( i + 1 );
var records = allStatusRecords . Where ( x => x . CreateTime . Date == tempDate1 && x . UpdateTime . Date != tempDate1 ). ToList ();
if ( records . Count > 0 )
{
Console . WriteLine ( $ "[状态]{tempDate1.ToShortDateString()}:{records.Count}条" );
}
foreach ( var item in records )
{
var days = ( item . UpdateTime . Date - item . CreateTime . Date ). Days ;
var addTemps = new List < EquipmentStatusRecordHistory >();
for ( int j = 1 ; j <= days ; j ++)
{
var tempDate3 = tempDate1 . AddDays ( j );
var tempDate4 = tempDate3 . Date == item . UpdateTime . Date ? item . UpdateTime : tempDate3 . AddDays ( 1 ). AddSeconds (- 1 );
var addTemp = new EquipmentStatusRecordHistory
{
EquipmentCode = item . EquipmentCode ,
EquipmentName = item . EquipmentName ,
EquipmentTypeCode = item . EquipmentTypeCode ,
FactoryCode = item . FactoryCode ,
ProjectCode = item . ProjectCode ,
HandleTime = item . HandleTime ,
IsEnd = item . IsEnd ,
Remark = item . Remark ,
Status = item . Status ,
StatusDuration = ( tempDate4 - tempDate3 ). TotalSeconds ,
CreateTime = tempDate3 ,
UpdateTime = tempDate4
};
if ( addTemp . StatusDuration > 0 )
{
addTemps . Add ( addTemp );
}
};
equipmentStatusRecordHistoryRepository . Attach ( item );
item . UpdateTime = tempDate2 . AddSeconds (- 1 );
item . StatusDuration = ( item . UpdateTime - item . CreateTime ). TotalSeconds ;
equipmentStatusRecordHistoryRepository . Update ( item );
equipmentStatusRecordHistoryRepository . Insert ( addTemps );
}
}
}
唐召明
authored
about a year ago
487
}