AnalysisBase.cs 29.6 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 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
using HHECS.BllModel;
using HHECS.DAQHandle.Common.Enums;
using HHECS.DAQHandle.DataAccess;
using HHECS.DAQHandle.Dto;
using HHECS.DAQHandle.Models;
using HHECS.EquipmentModel;
using System.Text.Json;

namespace HHECS.DAQHandle.EquipmentHandle
{
    internal abstract class AnalysisBase : IAnalysis
    {
        /// <summary>
        /// 数据库上下文
        /// </summary>
        protected readonly DataContext Context = new DataContext();

        /// <summary>
        /// 当前设备类型
        /// </summary>
        protected readonly EquipmentType CurrentEquipmentType;

        /// <summary>
        /// 程序配置信息
        /// </summary>
        protected readonly AppConfig AppConfig;

        /// <summary>
        /// 当前类型设备数据
        /// </summary>
        protected readonly List<EquipmentExtend> Equipments;

        /// <summary>
        /// 设备总报警属性编码
        /// </summary>
        protected readonly string EquipmentTotalAlarmCode;

        protected AnalysisBase(AnalysisParameter parameter)
        {
            Equipments = parameter.Equipments;
            CurrentEquipmentType = parameter.EquipmentType;
            AppConfig = parameter.AppConfig;
            EquipmentTotalAlarmCode = parameter.EquipmentTotalAlarmCode;
        }

        public virtual BllResult Execute(IEnumerable<EquipmentDataRecord> records)
        {
            try
            {
                foreach (var item in records.GroupBy(x => x.EquipmentCode))
                {
                    var equipmentSN = item.Key;
                    var orderItems = item.OrderBy(x => x.Timestamp).ToList();
                    var lastRecord = orderItems.Last();
                    var equipment = Equipments.Where(x => x.Code == equipmentSN).FirstOrDefault();
                    if (equipment != null)
                    {
                        var result1 = UpdateTagValue(equipment, lastRecord);
                        var result2 = UpdateEquipmentAlarm(equipment, orderItems);
                        var result3 = UpdateEquipmentStatus(equipment, orderItems);
                    }
                }
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"数据解析出现异常:{ex.Message}");
            }
        }

        /// <summary>
        /// 更新设备属性值
        /// </summary>
        /// <param name="equipment">设备</param>
        /// <param name="record">最后一条记录</param>
        /// <returns></returns>
        protected virtual BllResult UpdateTagValue(EquipmentExtend equipment, EquipmentDataRecord record)
        {
            try
            {
                var acceptTime = DateTimeOffset.FromUnixTimeSeconds(record.Timestamp).LocalDateTime;
                var equipmetProps = equipment.EquipmentProps.Where(x => x.Updated < acceptTime).ToList();
                if (equipmetProps.Count == 0)
                {
                    return BllResultFactory.Error($"设备[{equipment.Code}]属性集合为空!");
                }

                var tagResult = JsonConvertToTagList(record.Tags);
                if (!tagResult.Success)
                {
                    return tagResult;
                }
                var tagData = tagResult.Data;

                foreach (var prop in equipmetProps)
                {
                    var tag = tagData.Where(x => x.Tag == prop.Address).FirstOrDefault();
                    if (tag == null)
                    {
                        continue;
                    }

                    Context.EquipmentProp.Attach(prop);
                    prop.Value = tag.Value;
                    prop.Updated = acceptTime;
                    Context.EquipmentProp.Update(prop);
                }
                Context.SaveChanges();
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"更新[{equipment.Code}]设备属性值失败:{ex.Message}");
            }
        }

        /// <summary>
        /// 更新设备报警记录
        /// </summary>
        /// <param name="equipment">设备</param>
        /// <param name="records">记录数据</param>
        /// <returns>一台设备一次仅记录一条主报警信息,报警明细附加在明细字段</returns>
        protected virtual BllResult UpdateEquipmentAlarm(EquipmentExtend equipment, IEnumerable<EquipmentDataRecord> records)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(EquipmentTotalAlarmCode))
                {
                    return BllResultFactory.Error($"设备[{equipment.Code}]总报警编码[{nameof(EquipmentTotalAlarmCode)}]字段不能为空!");
                }

                var alarmRecordCacheTempsResult = AssemblyAlarmData(equipment, records);
                if (!alarmRecordCacheTempsResult.Success)
                {
                    return BllResultFactory.Error(alarmRecordCacheTempsResult.Msg);
                }

                if (alarmRecordCacheTempsResult.Data.Count == 0)
                {
                    return BllResultFactory.Error($"更新设备[{equipment.Code}]报警失败,组装数据结果为空!");
                }

                var alarmRecordCacheTemps = alarmRecordCacheTempsResult.Data;

                var oldAlarmRecord = Context.EquipmentAlarmRecords
                    .Where(x => x.EquipmentCode == equipment.Code && x.EquipmentPropCode == EquipmentTotalAlarmCode)
                    .OrderByDescending(x => x.CreateTime)
                    .First(x => new EquipmentAlarmRecord
                    {
                        Id = x.Id,
                        IsEnd = x.IsEnd,
                        CreateTime = x.CreateTime,
                        UpdateTime = x.UpdateTime
                    });

                var lastCacheTemp = alarmRecordCacheTemps.OrderByDescending(x => x.CreateTime).First();
                //数据库无报警记录
                if (oldAlarmRecord == null)
                {
                    foreach (var item in alarmRecordCacheTemps)
                    {
                        //不是最后一条记录的数据需要结束
                        if (!item.Equals(lastCacheTemp))
                        {
                            item.IsEnd = true;
                        }
                        Context.EquipmentAlarmRecords.Add(item);
                    }
                }
                //数据库有报警记录,则需要校验
                else
                {
                    foreach (var item in alarmRecordCacheTemps)
                    {
                        //对于之前的数据,这里暂时不做处理
                        if (oldAlarmRecord.UpdateTime >= item.UpdateTime)
                        {
                            continue;
                        }

                        //在设定时间以内,且报警明细一致,则更新
                        if (oldAlarmRecord.UpdateTime >= item.CreateTime.AddSeconds(-AppConfig.DataTimeOut) && oldAlarmRecord.Remark == item.Remark)
                        {
                            Context.EquipmentAlarmRecords.Attach(oldAlarmRecord);
                            //不是最后一条记录,或时间超过设定时间,说明已经成为历史报警,需要结束标记
                            if (!item.Equals(lastCacheTemp) || item.UpdateTime < DateTime.Now.AddSeconds(-AppConfig.DataTimeOut))
                            {
                                oldAlarmRecord.IsEnd = true;
                            }
                            else
                            {
                                oldAlarmRecord.IsEnd = false;
                            }
                            oldAlarmRecord.ErrorDuration = (item.UpdateTime - oldAlarmRecord.CreateTime).TotalSeconds;
                            oldAlarmRecord.UpdateTime = item.UpdateTime;
                            Context.EquipmentAlarmRecords.Update(oldAlarmRecord);
                            continue;
                        }

                        //其他情况需要新增记录
                        //不是最后一条记录
                        if (!item.Equals(lastCacheTemp))
                        {
                            item.IsEnd = true;
                        }
                        Context.EquipmentAlarmRecords.Add(item);
                    }
                }

                //结束其他超时报警
                var needEndAlaremRecords = Context.EquipmentAlarmRecords
                    .Where(x => x.EquipmentCode == equipment.Code && x.EquipmentPropCode == EquipmentTotalAlarmCode && !x.IsEnd && x.UpdateTime < DateTime.Now.AddSeconds(-AppConfig.DataTimeOut))
                    .ToList(x => new EquipmentAlarmRecord
                    {
                        Id = x.Id,
                    });
                foreach (var item in needEndAlaremRecords)
                {
                    Context.EquipmentAlarmRecords.Attach(item);
                    item.IsEnd = true;
                    Context.EquipmentAlarmRecords.Update(item);
                }
                Context.SaveChanges();
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }

        /// <summary>
        /// 更新设备状态记录
        /// </summary>
        /// <param name="equipment">设备</param>
        /// <param name="records">记录集合</param>
        /// <returns></returns>
        protected virtual BllResult UpdateEquipmentStatus(EquipmentExtend equipment, IEnumerable<EquipmentDataRecord> records)
        {
            try
            {
                var statusRecordCacheTempsResult = AssemblyStatusData(equipment, records);
                if (!statusRecordCacheTempsResult.Success)
                {
                    return BllResultFactory.Error(statusRecordCacheTempsResult.Msg);
                }

                if (statusRecordCacheTempsResult.Data.Count == 0)
                {
                    return BllResultFactory.Error($"更新[{equipment.Code}]设备状态失败,数据组装集合为空!");
                }

                var statusRecordCacheTemps = statusRecordCacheTempsResult.Data;

                //数据库中的实时状态记录
                var oldStatusRecord = Context.EquipmentStatusRecords
                    .Where(x => x.EquipmentCode == equipment.Code)
                    .OrderByDescending(x => x.CreateTime).First();

                //最后一条历史状态记录
                var oldStatusRecordHistory = Context.EquipmentStatusRecordHistorys.Where(x => x.EquipmentCode == equipment.Code)
                    .OrderByDescending(x => x.CreateTime).First(x => new EquipmentStatusRecord
                    {
                        Id = x.Id,
                        Status = x.Status,
                        CreateTime = x.CreateTime,
                        UpdateTime = x.UpdateTime,
                    });

                //取最后一条数据,用于更新实时状态表数据
                var lastCacheStatusRecord = statusRecordCacheTemps.OrderByDescending(x => x.CreateTime).First();

                //实时记录表无当前设备数据
                if (oldStatusRecord == null)
                {
                    foreach (var item in statusRecordCacheTemps)
                    {
                        //倘若不是最后一条记录,则存入历史表
                        if (!item.Equals(lastCacheStatusRecord))
                        {
                            //存在历史记录
                            if (oldStatusRecordHistory != null)
                            {
                                //在历史记录时间之后,这里暂时不做处理
                                if (oldStatusRecord.UpdateTime <= item.CreateTime && (oldStatusRecord.UpdateTime - item.CreateTime).TotalSeconds <= AppConfig.DataTimeOut)
                                {
                                    continue;
                                }

                                //在历史记录时间之前,这里暂时不做处理
                                if (oldStatusRecord.CreateTime >= item.UpdateTime && (oldStatusRecord.CreateTime - item.UpdateTime).TotalSeconds <= AppConfig.DataTimeOut)
                                {
                                    continue;
                                }
                            }
                            //历史记录为空,新增数据
                            Context.EquipmentStatusRecordHistorys.Add(item);
                            continue;
                        }
                        //是最后一条记录,则新增
                        Context.EquipmentStatusRecords.Add(item);
                    }
                    Context.SaveChanges();
                    return BllResultFactory.Success();
                }

                //实时记录数据不为空的情况
                foreach (var item in statusRecordCacheTemps)
                {
                    //不是最后一条记录,存入历史表
                    if (!item.Equals(lastCacheStatusRecord))
                    {
                        //存在历史记录
                        if (oldStatusRecordHistory != null)
                        {
                            //在历史记录时间之后,这里暂时不做处理
                            if (oldStatusRecord.UpdateTime <= item.CreateTime && (oldStatusRecord.UpdateTime - item.CreateTime).TotalSeconds <= AppConfig.DataTimeOut)
                            {
                                continue;
                            }

                            //在历史记录时间之前,这里暂时不做处理
                            if (oldStatusRecord.CreateTime >= item.UpdateTime && (oldStatusRecord.CreateTime - item.UpdateTime).TotalSeconds <= AppConfig.DataTimeOut)
                            {
                                continue;
                            }
                        }
                        Context.EquipmentStatusRecordHistorys.Add(item);
                        continue;
                    }

                    //是最后一条记录,状态一致
                    if (oldStatusRecord.Status == item.Status)
                    {
                        //在设定时间之内,且在现有记录之后
                        if (oldStatusRecord.UpdateTime <= item.CreateTime && (item.CreateTime - oldStatusRecord.UpdateTime).TotalSeconds <= AppConfig.DataTimeOut)
                        {
                            Context.EquipmentStatusRecords.Attach(oldStatusRecord);
                            oldStatusRecord.StatusDuration = (item.UpdateTime - oldStatusRecord.CreateTime).TotalSeconds;
                            oldStatusRecord.UpdateTime = item.UpdateTime;
                            Context.EquipmentStatusRecords.Update(oldStatusRecord);
                            continue;
                        }

                        //在设定时间之内,且在现有记录之前
                        if (oldStatusRecord.CreateTime >= item.UpdateTime && (oldStatusRecord.CreateTime - item.UpdateTime).TotalSeconds <= AppConfig.DataTimeOut)
                        {
                            Context.EquipmentStatusRecords.Attach(oldStatusRecord);
                            oldStatusRecord.StatusDuration = (oldStatusRecord.UpdateTime - item.CreateTime).TotalSeconds;
                            oldStatusRecord.CreateTime = item.CreateTime;
                            Context.EquipmentStatusRecords.Update(oldStatusRecord);
                            continue;
                        }
                    }
                    //是最后一条记录,状态变更
                    else
                    {
                        //在历史记录时间之后
                        if (oldStatusRecord.UpdateTime < item.CreateTime)
                        {
                            Context.EquipmentStatusRecordHistorys.AddOrUpdate(new EquipmentStatusRecordHistory
                            {
                                Id = oldStatusRecord.Id,
                                EquipmentCode = oldStatusRecord.EquipmentCode,
                                EquipmentName = oldStatusRecord.EquipmentName,
                                EquipmentType = oldStatusRecord.EquipmentType,
                                IsEnd = oldStatusRecord.IsEnd,
                                FactoryCode = oldStatusRecord.FactoryCode,
                                HandleTime = oldStatusRecord.HandleTime,
                                ProjectCode = oldStatusRecord.ProjectCode,
                                Status = oldStatusRecord.Status,
                                StatusDuration = oldStatusRecord.StatusDuration,
                                CreateTime = oldStatusRecord.CreateTime,
                                UpdateTime = oldStatusRecord.UpdateTime,
                                Remark = oldStatusRecord.Remark,
                            });
                            //从实时状态表删除这条记录
                            Context.EquipmentStatusRecords.Remove(oldStatusRecord);
                            //记录新的实时状态
                            Context.EquipmentStatusRecords.Add(item);
                            continue;
                        }

                        //在历史记录时间之前,暂不处理
                        if (oldStatusRecord.CreateTime > item.UpdateTime)
                        {

                        }
                    }
                }
                Context.SaveChanges();
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"更新[{equipment.Code}]设备状态记录失败:{ex.Message}");
            }
        }

        /// <summary>
        /// 组装报警数据
        /// </summary>
        /// <param name="equipment">设备</param>
        /// <param name="records">记录</param>
        /// <returns></returns>
        protected BllResult<List<EquipmentAlarmRecord>> AssemblyAlarmData(EquipmentExtend equipment, IEnumerable<EquipmentDataRecord> records)
        {
            try
            {
                //获取设备总报警编号,仅在设备报总故障时才记录报警
                var totalAlarmProp = equipment.EquipmentProps.Where(x => x.EquipmentTypePropTemplateCode == EquipmentTotalAlarmCode).FirstOrDefault();
                if (totalAlarmProp == null)
                {
                    return BllResultFactory.Error<List<EquipmentAlarmRecord>>($"设备[{equipment.Code}]总报警编码[{EquipmentTotalAlarmCode}]未配置!");
                }

                //当前设备监控地址属性模板
                var monitorPropTemps = equipment.EquipmentProps.Where(x => x.EquipmentTypePropTemplate.PropType == EquipmentPropType.PLCMonitorAddress).OrderBy(x => x.Id).ToList();

                //数据组装
                var temps = new List<EquipmentAlarmRecord>();
                foreach (var item in records.OrderBy(x => x.Timestamp))
                {
                    var currentRecordStatusResult = GetEquipmentStatus(equipment, item);
                    if (!currentRecordStatusResult.Success)
                    {
                        continue;
                    }
                    if (currentRecordStatusResult.Data != EquipmentStatus.Failure)
                    {
                        continue;
                    }

                    var time = DateTimeOffset.FromUnixTimeSeconds(item.Timestamp).LocalDateTime;
                    var tagResult = JsonConvertToTagList(item.Tags);
                    //数据不合法,则直接弃用这条记录
                    if (!tagResult.Success)
                    {
                        continue;
                    }
                    var tagItems = tagResult.Data;

                    var totalAlarmPropTag = tagItems.Where(x => x.Tag == totalAlarmProp.Address).FirstOrDefault();
                    //当前记录无设备总报警节点地址,则跳过
                    if (totalAlarmPropTag == null)
                    {
                        continue;
                    }

                    //设备总报警未报警,则跳过
                    if (totalAlarmProp.EquipmentTypePropTemplate.MonitorCompareValue != totalAlarmPropTag.Value)
                    {
                        continue;
                    }

                    //其他存在报警的文本信息
                    var alarmMsgList = monitorPropTemps.Where(monitorPropTemp =>
                    {
                        //总报警编码在这里需要跳过
                        if (monitorPropTemp.EquipmentTypePropTemplateCode == totalAlarmProp.EquipmentTypePropTemplateCode)
                        {
                            return false;
                        }

                        //校验不通过,则跳过
                        if (!EquipmentAlarmCodeValidation(equipment, monitorPropTemp.EquipmentTypePropTemplateCode).Success)
                        {
                            return false;
                        }

                        var tagItem = tagItems.Where(x => x.Tag == monitorPropTemp.Address).FirstOrDefault();
                        //当前记录无此属性,则跳过
                        if (tagItem == null)
                        {
                            return false;
                        }

                        //值与比较值不相等,则跳过
                        if (monitorPropTemp.EquipmentTypePropTemplate.MonitorCompareValue != tagItem.Value)
                        {
                            return false;
                        }
                        //其他情况,为报警
                        return true;
                    }).OrderBy(x => x.EquipmentTypePropTemplateCode).Select(x => x.EquipmentTypePropTemplate.MonitorFailure).ToList();

                    var alarmMsg = string.Join(";", alarmMsgList);
                    var cacheTemp = temps.Where(x => x.EquipmentPropCode == totalAlarmProp.EquipmentTypePropTemplateCode)
                        .OrderByDescending(x => x.CreateTime).FirstOrDefault();
                    //时间间隔超过设定时间、报警文本有变化,则认为是不连续的数据
                    if (cacheTemp == null || (cacheTemp != null && (cacheTemp.UpdateTime < time.AddSeconds(-AppConfig.DataTimeOut) || cacheTemp.Remark != alarmMsg)))
                    {
                        temps.Add(new EquipmentAlarmRecord
                        {
                            EquipmentCode = equipment.Code,
                            EquipmentName = equipment.Code,
                            EquipmentPropCode = totalAlarmProp.EquipmentTypePropTemplateCode,
                            EquipmentPropName = totalAlarmProp.EquipmentTypePropTemplate.Name,
                            EquipmentTypeCode = CurrentEquipmentType.Code,
                            ErrorDuration = 0,
                            IsEnd = false,
                            Tag = totalAlarmPropTag.Tag,
                            AlarmMessage = totalAlarmProp.EquipmentTypePropTemplate.MonitorFailure,
                            FactoryCode = equipment.FactoryCode,
                            ProjectCode = equipment.ProjectCode,
                            CreateTime = time,
                            UpdateTime = time,
                            Remark = alarmMsg
                        });
                        continue;
                    }

                    cacheTemp.UpdateTime = time;
                    cacheTemp.ErrorDuration = (time - cacheTemp.CreateTime).TotalSeconds;
                }
                return BllResultFactory.Success(temps);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<EquipmentAlarmRecord>>(ex.Message);
            }
        }

        /// <summary>
        /// 组装状态数据
        /// </summary>
        /// <param name="equipment"></param>
        /// <param name="records"></param>
        /// <returns></returns>
        protected BllResult<List<EquipmentStatusRecordHistory>> AssemblyStatusData(EquipmentExtend equipment, IEnumerable<EquipmentDataRecord> records)
        {
            try
            {
                //数据组装(实时状态与历史状态实体之间为父子关系,这里用子类,可以省去手动转换操作)
                var temps = new List<EquipmentStatusRecordHistory>();
                foreach (var record in records.OrderBy(x => x.Timestamp))
                {
                    var time = DateTimeOffset.FromUnixTimeSeconds(record.Timestamp).LocalDateTime;
                    var currentRecordStatusResult = GetEquipmentStatus(equipment, record);
                    if (!currentRecordStatusResult.Success)
                    {
                        continue;
                    }
                    //离线或无数据时,不记录数据
                    if (currentRecordStatusResult.Data == EquipmentStatus.None || currentRecordStatusResult.Data == EquipmentStatus.Offline)
                    {
                        continue;
                    }

                    var currentStatus = currentRecordStatusResult.Data.ToString();
                    var cacheTemp = temps.OrderByDescending(x => x.CreateTime).FirstOrDefault();
                    //缓存集合无数据、数据超过设定时间、状态有变化,则新增数据
                    if (cacheTemp == null || (cacheTemp != null && (cacheTemp.UpdateTime < time.AddSeconds(-AppConfig.DataTimeOut) || cacheTemp.Status != currentStatus)))
                    {
                        temps.Add(new EquipmentStatusRecordHistory
                        {
                            EquipmentCode = equipment.Code,
                            EquipmentName = equipment.Name,
                            EquipmentType = equipment.EquipmentType.Code,
                            FactoryCode = equipment.FactoryCode,
                            ProjectCode = equipment.ProjectCode,
                            Status = currentStatus,
                            HandleTime = DateTime.Now,
                            StatusDuration = 0,
                            IsEnd = false,
                            CreateTime = time,
                            UpdateTime = time,
                        });
                        continue;
                    }
                    cacheTemp.StatusDuration = (time - cacheTemp.CreateTime).TotalSeconds;
                    cacheTemp.UpdateTime = time;
                }
                return BllResultFactory.Success(temps);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<EquipmentStatusRecordHistory>>(ex.Message);
            }
        }

        protected abstract BllResult<EquipmentStatus> GetEquipmentStatus(EquipmentExtend equipment, EquipmentDataRecord record);

        /// <summary>
        /// Json转<see cref="TagItemDto"/>集合
        /// </summary>
        /// <param name="json">Json数据</param>
        /// <returns><see cref="BllResult{T}"/></returns>
        protected BllResult<List<TagItemDto>> JsonConvertToTagList(string json)
        {
            try
            {
                var jsonOption = new JsonSerializerOptions
                {
                    PropertyNameCaseInsensitive = true,
                };
                var tags = JsonSerializer.Deserialize<List<TagItemDto>>(json);
                return BllResultFactory.Success(tags);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<TagItemDto>>($"Json数据转换成Tag集合失败!{ex.Message}");
            }
        }

        /// <summary>
        /// 将Tag集合转换成Dictionary
        /// </summary>
        /// <param name="equipmentProps">设备属性</param>
        /// <param name="tags">Tag集合</param>
        /// <remarks>
        /// <para>key:EquipmentTypePropTemplateCode</para>
        /// <para>value:Tag值</para>
        /// </remarks>
        /// <returns></returns>
        protected BllResult<Dictionary<string, string>> TagListConvertToDictionary(IEnumerable<EquipmentPropExtend> equipmentProps, IEnumerable<TagItemDto> tags)
        {
            try
            {
                var result = new Dictionary<string, string>();
                foreach (var item in equipmentProps.DistinctBy(x => x.EquipmentTypePropTemplateCode))
                {
                    var temp = tags.Where(x => item.Address == x.Tag).FirstOrDefault();
                    if (temp == null)
                    {
                        continue;
                    }
                    result.Add(item.EquipmentTypePropTemplateCode, temp.Value);
                }
                return BllResultFactory.Success(result);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<Dictionary<string, string>>(ex.Message);
            }
        }

        /// <summary>
        /// 设备报警属性编号校验,用于自定义配置需要忽略的报警属性
        /// </summary>
        /// <param name="equipment">设备</param>
        /// <param name="equipmentPropCode">设备报警属性编号</param>
        /// <remarks>校验通过则记录,校验失败则忽略</remarks>
        /// <returns></returns>
        protected virtual BllResult EquipmentAlarmCodeValidation(EquipmentExtend equipment, string equipmentPropCode)
        {
            return BllResultFactory.Success();
        }
    }
}