Program.cs 23.2 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 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
using HHECS.BllModel;
using HHECS.DAQHandle;
using HHECS.DAQHandle.Common.Utils;
using HHECS.DAQHandle.Dto;
using HHECS.DAQHandle.EquipmentHandle;
using HHECS.DAQShared.Common.Enums;
using HHECS.DAQShared.Models;
using LinqKit;
using System.Configuration;
using System.Diagnostics;

internal class Program
{
    private static DateTime LastUploadTime = DateTime.MinValue;
    private static void Main(string[] args)
    {
        try
        {
            var appConfigResult = LoadAppConfig();
            if (!appConfigResult.Success)
            {
                SystemLog.PrintError($"加载配置文件信息失败:{appConfigResult.Msg}");
                return;
            }
            var appConfig = appConfigResult.Data;

            var result = ValidationConfig(appConfig);
            if (!result.Success)
            {
                SystemLog.PrintError($"配置信息校验失败:{result.Msg}");
                return;
            }

            var connectionString = ConfigurationManager.ConnectionStrings[appConfig.IsProductionEnvironment ? "Production" : "Development"].ConnectionString;
            GlobalVar.SetConnectionString(connectionString);
            UpdateClientStatus();
            Startup(appConfig);
        }
        catch (Exception ex)
        {
            SystemLog.PrintWarn($"程序启动出现异常:{ex.Message}");
        }
    }

    private static void Startup(AppConfig config)
    {
        var equipments = new List<EquipmentExtend>();
        var equipmentTypes = new List<EquipmentTypeExtend>();
        var equipmentTypeCodes = config.EquipmentTypeCodes;
        SystemLog.PrintInfo($"正在初始化数据...");
        while (true)
        {
            try
            {
                var result = InitialData(equipmentTypeCodes, ref equipments, ref equipmentTypes);
                if (!result.Success)
                {
                    SystemLog.PrintError($"初始化数据失败:{result.Msg}");
                    continue;
                }
                if (equipmentTypes.Count == 0)
                {
                    SystemLog.PrintError($"获取设备类型[{string.Join(',', equipmentTypeCodes)}]数据失败,数据为空!");
                    continue;
                }
                Stopwatch stopwatch = Stopwatch.StartNew();
                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>();

                var tasks = new List<Task>();
                foreach (var currentEquipmentType in equipmentTypes)
                {
                    tasks.Add(Task.Run(() =>
                    {
                        Stopwatch stopwatch = Stopwatch.StartNew();
                        _ = 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
                            {
                                EquipmentTypeConst.SingleForkSRM => SRMProps.TotalError.ToString(),
                                EquipmentTypeConst.DoubleForkSRM => SRMProps.TotalError.ToString(),
                                EquipmentTypeConst.SingleForkSSRM => SRMProps.TotalError.ToString(),
                                EquipmentTypeConst.SingleForkSSRMV132 => SRMProps.TotalError.ToString(),
                                EquipmentTypeConst.WeldRobot => RobotProp.Alarm.ToString(),
                                EquipmentTypeConst.AGVForklift => AGVProp.Error.ToString(),
                                EquipmentTypeConst.RGVStation => RGVStationProps.Fault.ToString(),
                                EquipmentTypeConst.StationMonitor => StationProps.StationError.ToString(),
                                EquipmentTypeConst.Hoist => HoistProps.HoistStatus.ToString(),
                                EquipmentTypeConst.CNC => CNCProp.RedLight.ToString(),
                                _ => string.Empty,
                            }
                        };
                        IAnalysis handle = equipmentTypeCode switch
                        {
                            EquipmentTypeConst.SingleForkSRM => new SingleForkSRMAnalysis(parameter),
                            EquipmentTypeConst.DoubleForkSRM => new DoubleForkSRMAnalysis(parameter),
                            EquipmentTypeConst.SingleForkSSRM => new SingleForkSSRMAnalysis(parameter),
                            EquipmentTypeConst.SingleForkSSRMV132 => new SingleForkSSRMV132Analysis(parameter),
                            EquipmentTypeConst.WeldRobot => new WeldRobotAnalysis(parameter),
                            EquipmentTypeConst.AGVForklift => new AGVAnalysis(parameter),
                            EquipmentTypeConst.RGVStation => new RGVStationAnalysis(parameter),
                            EquipmentTypeConst.StationMonitor => new StationMonitorAnalysis(parameter),
                            EquipmentTypeConst.Hoist => new HoistAnalysis(parameter),
                            EquipmentTypeConst.CNC => new CNCAnalysis(parameter),
                            _ => null
                        };
                        //未知类型,则跳过
                        if (handle is null)
                        {
                            SystemLog.PrintWarn($"未实现设备类型[{currentEquipmentType.Code}]对应的处理方法");
                            return;
                        }
                        try
                        {
                            var limit = config.ProcessingDataVolume;
                            if (equipmentTypeCode == EquipmentTypeConst.StationMonitor)
                            {
                                //站台数据量比较大,目前不处理站台状态和报警,所以每次多取一些数据
                                limit *= 5;
                            }

                            using var equipmentDataRecordRepository = freeSql.GetRepository<EquipmentDataRecord>();
                            var equipmentCodes = equipments.Where(x => x.EquipmentTypeId == currentEquipmentType.Id).Select(x => x.Code).ToList();

                            //缓存有数据的表名称集合
                            var cacheTableNames = new List<string>();
                            var equipmentDataRecords = new List<EquipmentDataRecord>();
                            foreach (var tableName in tableNames)
                            {
                                //达到设定数量时停止往后查询
                                if (equipmentDataRecords.Count == limit)
                                {
                                    break;
                                }

                                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);
                                }

                                //本次查询数量
                                var currentLimit = limit - equipmentDataRecords.Count;
                                var temps = freeSql.Queryable<EquipmentDataRecord>().AsTable((type, oldName) => tableName)
                                 .Where(filter)
                                 .OrderBy(x => x.Timestamp).Take(currentLimit).ToList();
                                if (temps.Count > 0)
                                {
                                    equipmentDataRecords.AddRange(temps);
                                    cacheTableNames.Add(tableName);
                                }
                            }

                            if (equipmentDataRecords.Count == 0)
                            {
                                SystemLog.PrintInfo($"设备类型[{currentEquipmentType.Code}]记录数据为空,跳过解析,耗时:{stopwatch.ElapsedMilliseconds}ms");
                                return;
                            }

                            var result = handle.Execute(equipmentDataRecords);
                            if (!result.Success)
                            {
                                SystemLog.PrintError($"设备类型[{currentEquipmentType.Code}]解析失败,{result.Msg},数量{equipmentDataRecords.Count},耗时:{stopwatch.ElapsedMilliseconds}ms");
                                return;
                            }

                            //删除数据
                            var ids = equipmentDataRecords.Select(x => x.Id).ToList();
                            foreach (var tableName in cacheTableNames)
                            {
                                freeSql.Delete<EquipmentDataRecord>().AsTable(tableName).Where(x => ids.Contains(x.Id)).CommandTimeout(300).ExecuteAffrows();
                            }

                            SystemLog.PrintSuccess($"设备类型[{currentEquipmentType.Code}]解析完成,数量{equipmentDataRecords.Count},耗时:{stopwatch.ElapsedMilliseconds}ms");
                        }
                        catch (Exception ex)
                        {
                            SystemLog.PrintError($"[{handle.GetType().Name}]线程数据解析出现异常:{ex.Message}");
                        }
                    }));
                }
                Task.WaitAll(tasks.ToArray());
                Thread.Sleep(1000);
                const string tempString = "——————————————————";
                Console.WriteLine($"{tempString}[总耗时:{stopwatch.Elapsed}]{tempString}");
            }
            catch (Exception ex)
            {
                SystemLog.PrintError($"程序异常:{ex.Message}");
            }
        }
    }

    /// <summary>
    /// 加载配置文件信息
    /// </summary>
    /// <returns></returns>
    private static BllResult<AppConfig> LoadAppConfig()
    {
        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);
            var equipmentTypeCodes = ConfigurationManager.AppSettings["EquipmentType"];
            var warehouseCode = ConfigurationManager.AppSettings["WarehouseCode"];

            if (processingDataVolumeValue <= 0)
            {
                processingDataVolumeValue = 100;
            }

            if (isProductionEnvironmentValue)
            {
                Console.Title = "IOT数据处理端";
            }
            else
            {
                Console.Title = "IOT数据处理端(测试版)";
            }
            var appConfig = new AppConfig
            {
                ClientId = clientIdValue,
                WarehouseCode = warehouseCode,
                DataTimeOut = dataTimeOutValue,
                IsProductionEnvironment = isProductionEnvironmentValue,
                ProcessingDataVolume = processingDataVolumeValue,
                EquipmentTypeCodes = equipmentTypeCodes?.Split(',').Where(x => !string.IsNullOrWhiteSpace(x)).ToList() ?? new List<string>(),
                Version = version
            };
            return BllResultFactory.Success(appConfig);
        }

        catch (Exception ex)
        {
            return BllResultFactory.Error<AppConfig>(ex.Message);
        }
    }

    /// <summary>
    /// 配置信息校验
    /// </summary>
    /// <param name="appConfig"></param>
    /// <returns></returns>
    private static BllResult ValidationConfig(AppConfig appConfig)
    {
        try
        {
            if (string.IsNullOrWhiteSpace(appConfig.WarehouseCode))
            {
                return BllResultFactory.Error($"仓库编号[{nameof(appConfig.WarehouseCode)}]未配置!");
            }

            if (appConfig.EquipmentTypeCodes == null || appConfig.EquipmentTypeCodes.Count == 0)
            {
                SystemLog.PrintWarn($"设备类型[{nameof(appConfig.EquipmentTypeCodes)}]未配置,将默认匹配所有设备");
            }

            if (appConfig.Version == 0)
            {
                SystemLog.PrintWarn($"程序版本[{nameof(appConfig.Version)}]未配置,将匹配所有版本设备记录");
            }
            return BllResultFactory.Success();
        }
        catch (Exception ex)
        {
            return BllResultFactory.Error(ex.Message);
        }
    }

    /// <summary>
    /// 初始化数据
    /// </summary>
    /// <param name="equipmentTypeCodes">设备类型编号</param>
    /// <param name="equipments">设备集合</param>
    /// <param name="equipmentTypes">设备类型集合</param>
    /// <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)
    {
        try
        {
            using var freeSql = GlobalVar.FreeSql;
            var equipmentRepository = freeSql.GetRepository<EquipmentExtend>();
            var equipmentPropRepository = freeSql.GetRepository<EquipmentPropExtend>();
            var equipmentTypeRepository = freeSql.GetRepository<EquipmentTypeExtend>();

            if ((DateTime.Now.AddHours(-timeSpanHours) > LastUploadTime) || equipmentTypes.Count == 0)
            {
                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();
            }
            var equipmentTypeIds = equipmentTypes.Select(x => x.Id).ToList();
            var equipmentIds = equipmentRepository.Where(x => equipmentTypeIds.Contains(x.EquipmentTypeId)).ToList(x => x.Id);
            var equipmentTotal = equipmentIds.Count;
            var equipmentPropTotal = equipmentPropRepository.Where(x => equipmentIds.Contains(x.EquipmentId)).Count();
            if ((DateTime.Now.AddHours(-timeSpanHours) > LastUploadTime) || equipments.Count != equipmentTotal || equipments.SelectMany(x => x.EquipmentProps).Count() != equipmentPropTotal)
            {
                equipments = equipmentRepository.Where(x => equipmentTypeIds.Contains(x.EquipmentTypeId)).IncludeMany(x => x.EquipmentProps).ToList();
                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));
                }
                LastUploadTime = DateTime.Now;
            }
            return BllResultFactory.Success();
        }
        catch (Exception ex)
        {
            return BllResultFactory.Error(ex.Message);
        }
    }

    /// <summary>
    /// 更新客户端状态
    /// </summary>
    private static void UpdateClientStatus()
    {
        Task.Run(async () =>
        {
            while (true)
            {
                try
                {
                    await Task.Delay(5000);
                    using var clientStatusRepository = GlobalVar.FreeSql.GetRepository<ClientStatus>();
                    //更新客户端状态
                    _ = Guid.TryParse(ConfigurationManager.AppSettings["ClientId"], out var clientId);
                    if (clientId != Guid.Empty)
                    {
                        var client = clientStatusRepository.Where(x => x.ClientKeys == clientId).First();
                        if (client != null)
                        {
                            clientStatusRepository.Attach(client);
                            client.LastSeenDate = DateTime.Now;
                            clientStatusRepository.Update(client);
                        }
                    }
                }
                catch (Exception ex)
                {
                    SystemLog.PrintError($"客户端状态更新线程异常:{ex.Message}");
                }
            }
        });
    }

    /// <summary>
    /// 修复数据设备报警和设备状态
    /// </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);
            }
        }
    }
}