Blame view

HHECS.DAQHandle/Program.cs 4.5 KB
1
using HHECS.DAQHandle.Common.Enums;
唐召明 authored
2
using HHECS.DAQHandle.Common.Utils;
3
using HHECS.DAQHandle.DataAccess;
唐召明 authored
4
using HHECS.DAQHandle.EquipmentHandle;
5
using HHECS.EquipmentModel;
唐召明 authored
6
7
8
using System.Configuration;
using System.Diagnostics;
唐召明 authored
9
internal class Program
唐召明 authored
10
{
唐召明 authored
11
    /// <summary>
唐召明 authored
12
    /// 单次处理的数据量
唐召明 authored
13
14
    /// </summary>
    private const int _limit = 100;
唐召明 authored
15
唐召明 authored
16
    private static void Main(string[] args)
唐召明 authored
17
    {
唐召明 authored
18
        try
李璐瑶 authored
19
        {
唐召明 authored
20
21
            var warehouseCode = ConfigurationManager.AppSettings["WarehouseCode"];
            if (string.IsNullOrWhiteSpace(warehouseCode))
李璐瑶 authored
22
            {
唐召明 authored
23
24
                SystemLog.PrintWarn("仓库编号未配置!");
                return;
李璐瑶 authored
25
            }
唐召明 authored
26
27
28

            var equipmentTypeCodes = ConfigurationManager.AppSettings["EquipmentType"].Split(',').ToList();
            if (equipmentTypeCodes == null || equipmentTypeCodes.Count == 0)
李璐瑶 authored
29
            {
唐召明 authored
30
31
                SystemLog.PrintWarn("设备类型未配置!");
                return;
李璐瑶 authored
32
33
            }
34
35
            using var context = new DataContext();
            var equipmentTypes = context.EquipmentType.Where(x => equipmentTypeCodes.Contains(x.Code)).IncludeMany(x => x.EquipmentTypePropTemplates).ToList();
唐召明 authored
36
37
38
39
40
41
42
            Startup(equipmentTypes);
        }
        catch (Exception ex)
        {
            SystemLog.PrintWarn($"程序启动出现异常:{ex.Message}");
        }
    }
43
44
    private static void Startup(IEnumerable<EquipmentType> equipmentTypes)
唐召明 authored
45
46
    {
        while (true)
唐召明 authored
47
        {
唐召明 authored
48
            try
唐召明 authored
49
            {
唐召明 authored
50
51
                var tasks = new List<Task>();
                foreach (var item in equipmentTypes)
唐召明 authored
52
                {
唐召明 authored
53
54
55
56
57
58
                    tasks.Add(Task.Run(() =>
                    {
                        Stopwatch stopwatch = Stopwatch.StartNew();
                        _ = Enum.TryParse<EquipmentTypeConst>(item.Code, out var equipmentTypeCode);
                        IAnalysis handle = equipmentTypeCode switch
                        {
59
60
61
62
63
64
                            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),
唐召明 authored
65
66
67
68
69
70
71
72
                            _ => null
                        };
                        //未知类型,则跳过
                        if (handle is null)
                        {
                            SystemLog.PrintWarn($"未实现设备类型[{item.Code}]对应的处理方法");
                            return;
                        }
73
                        using var context = new DataContext();
唐召明 authored
74
75
                        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();
唐召明 authored
76
77
78
79
80
81
                        if (equipmentDataRecords.Count == 0)
                        {
                            SystemLog.PrintInfo($"设备类型[{item.Code}]队列数据为空,跳过解析");
                            return;
                        }
82
                        var result = handle.Execute(equipmentDataRecords);
唐召明 authored
83
                        stopwatch.Stop();
84
                        if (!result.Success)
唐召明 authored
85
86
87
88
                        {
                            SystemLog.PrintError($"设备类型[{item.Code}]解析失败,{result.Msg},数量{equipmentDataRecords.Count},耗时{stopwatch.ElapsedMilliseconds}ms");
                            return;
                        }
唐召明 authored
89
90
                        SystemLog.PrintInfo($"设备类型[{item.Code}]解析完成,数量{equipmentDataRecords.Count},耗时{stopwatch.ElapsedMilliseconds}ms");
                    }));
唐召明 authored
91
                }
唐召明 authored
92
93
94
95
96
97
98
                Task.WaitAll(tasks.ToArray());
                Console.WriteLine($"————————————————————————————————————————————————————————");
            }
            catch (Exception ex)
            {
                SystemLog.PrintError($"程序异常:{ex.Message}");
            }
唐召明 authored
99
100
        }
    }
唐召明 authored
101
}