Program.cs 2.77 KB
using HHECS.DAQHandle;
using HHECS.DAQHandle.Common.Utils;
using HHECS.DAQHandle.EquipmentHandle;
using System.Configuration;
using System.Diagnostics;

var warehouseCode = ConfigurationManager.AppSettings["WarehouseCode"];
if (string.IsNullOrWhiteSpace(warehouseCode))
{
    SystemLog.PrintWarn("仓库编号未配置!");
    return;
}

var equipmentTypes = ConfigurationManager.AppSettings["EquipmentType"].Split(',')
    .Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => Convert.ToInt32(x)).ToList();
if (equipmentTypes == null || equipmentTypes.Count == 0)
{
    SystemLog.PrintWarn("设备类型未配置!");
    return;
}

var context = DbContextFactory.CreateDbContext();

while (true)
{
    try
    {
        var equipmentInfos = context.Equipment.Where(x => equipmentTypes.Contains(x.EquipmentTypeId)).Include(x => x.EquipmentType).ToList(x => new
        {
            EquipmentTypeCode = x.EquipmentType.Code,
            EquipmentCode = x.Code
        });
        var allEquipmentDataRecords = context.EquipmentDataRecord.Where(x => equipmentInfos.Any(y => y.EquipmentCode == x.EquipmentCode) && !x.IsHandle).ToList();

        var tasks = new List<Task>();
        foreach (var item in equipmentInfos.GroupBy(x => x.EquipmentTypeCode))
        {
            tasks.Add(Task.Run(() =>
            {
                Stopwatch stopwatch = Stopwatch.StartNew();
                IAnalysis? handle = item.Key switch
                {
                    "Stacker" => new SingleSRMAnalysis(),
                    "B1" => new DoubleSRMAnalysis(),
                    "C1" => new AGVAnalysis(),
                    "D1" => new RobotAnalysis(),
                    _ => null
                };
                //未知类型,则跳过
                if (handle is null)
                {
                    SystemLog.PrintWarn($"未实现设备类型[{item.Key}]对应的处理方法");
                    return;
                }

                var equipmentSNs = item.Select(x => x.EquipmentCode).ToList();
                var equipmentDataRecords = allEquipmentDataRecords.Where(x => equipmentInfos.Select(x => x.EquipmentCode).Contains(x.EquipmentCode)).ToList();

                handle.Execute(equipmentDataRecords);
                stopwatch.Stop();
                SystemLog.PrintInfo($"设备类型[{item.Key}]解析完成,数量{equipmentDataRecords.Count},耗时{stopwatch.ElapsedMilliseconds}ms");
            }));
        }
        Task.WaitAll(tasks.ToArray());
        await Task.Delay(100);
        Console.WriteLine($"————————————————————————————————————————————————————————");
    }
    catch (Exception ex)
    {
        SystemLog.PrintError($"程序异常:{ex.Message}");
    }
}