Program.cs
4.56 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
using HHECS.DAQHandle.Common.Enums;
using HHECS.DAQHandle.Common.Utils;
using HHECS.DAQHandle.DataAccess;
using HHECS.DAQHandle.EquipmentHandle;
using HHECS.EquipmentModel;
using System.Configuration;
using System.Diagnostics;
internal class Program
{
/// <summary>
/// 单次处理的数据量
/// </summary>
private const int _limit = 100;
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;
}
using var context = new DataContext();
var equipmentTypes = context.EquipmentType.Where(x => equipmentTypeCodes.Contains(x.Code)).IncludeMany(x => x.EquipmentTypePropTemplates).ToList();
Startup(equipmentTypes);
}
catch (Exception ex)
{
SystemLog.PrintWarn($"程序启动出现异常:{ex.Message}");
}
}
private static void Startup(IEnumerable<EquipmentType> equipmentTypes)
{
while (true)
{
try
{
var tasks = new List<Task>();
foreach (var item in equipmentTypes)
{
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),
_ => null
};
//未知类型,则跳过
if (handle is null)
{
SystemLog.PrintWarn($"未实现设备类型[{item.Code}]对应的处理方法");
return;
}
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");
}));
}
Task.WaitAll(tasks.ToArray());
Console.WriteLine($"————————————————————————————————————————————————————————");
}
catch (Exception ex)
{
SystemLog.PrintError($"程序异常:{ex.Message}");
}
}
}
}