Program.cs 4.5 KB
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
        {
            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}");
            }
        }
    }
}