From fd6c19067f918f47920a790a54d98f84e7e48fc0 Mon Sep 17 00:00:00 2001 From: tq <1916474859@qq,com> Date: Tue, 16 Jul 2024 17:52:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=85=E7=94=B5=E6=9C=BA=E6=95=85=E9=9A=9C?= =?UTF-8?q?=E5=85=A5=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service/Charger/Handler/UpAlarmHandler.cs | 115 ++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/Service/Charger/Handler/UpAlarmHandler.cs b/Service/Charger/Handler/UpAlarmHandler.cs index b210823..998168c 100644 --- a/Service/Charger/Handler/UpAlarmHandler.cs +++ b/Service/Charger/Handler/UpAlarmHandler.cs @@ -8,6 +8,9 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Common.Const; +using Entity.DbModel.Station; +using Repository.Station; namespace Service.Charger.Handler { @@ -16,12 +19,124 @@ namespace Service.Charger.Handler public class UpAlarmHandler : SimpleChannelInboundHandler, IBaseHandler { private static readonly ILog Log = LogManager.GetLogger(typeof(UpAlarmHandler)); + + public EquipAlarmRecordRepository EquipAlarmRecordRepository { get; set; } + + public EquipAlarmDefineRepository EquipAlarmDefineRepository { get; set; } + + public EquipAlarmProcessRecordRepository EquipAlarmProcessRecordRepository { get; set; } protected override void ChannelRead0(IChannelHandlerContext ctx, UpAlarm msg) { if (ClientMgr.TryGetClient(ctx.Channel, out string sn, out var client)) { Log.Info($"receive {msg} from {sn}"); + if (msg != null && msg.BatteryFaultCode > 4000) + { + SaveAlarmInfo(new List { msg.BatteryFaultCode.ToString() }, sn); + } + } + } + + private void SaveAlarmInfo(List lstAlarm, string sn) + { + string charger = "charger"; + int typeCode = (int)EquipmentType.Charger; + + if (lstAlarm.Count > 0) + { + #region 有报警比较两边差异,新出现的报警就添加,消失的报警就处理并记录 + + var lstEquipAlarmRecord = + EquipAlarmRecordRepository.QueryListByClause(e => e.EquipTypeCode == typeCode).ToList(); + var sqllstAlarm = lstEquipAlarmRecord.Select(obj => obj.ErrorCode).ToList(); + + // 找出实时报警中存在但数据库中不存在的元素 + List uniqueToList1 = lstAlarm.Except(sqllstAlarm).ToList(); + + // 找出数据库中存在但实时报警中不存在的元素 + List uniqueToList2 = sqllstAlarm.Except(lstAlarm).ToList(); + + if (uniqueToList1.Count > 0) + { + //这里要添加新的报警数据 + foreach (var errorCode in uniqueToList1) + { + EquipAlarmDefine? alarmDefine = + EquipAlarmDefineRepository.SelectByEquipCodeAndErrorCode(typeCode, charger, + errorCode); + if (alarmDefine != null) + { + EquipAlarmRecord record = new EquipAlarmRecord() + { + EquipTypeCode = alarmDefine.EquipTypeCode, + EquipCode = sn, + ErrorCode = alarmDefine.ErrorCode, + ErrorLevel = alarmDefine.ErrorLevel, + ErrorMsg = alarmDefine.ErrorMsg, + ProcessMethod = alarmDefine.ProcessMethod, + StartTime = DateTime.Now + }; + EquipAlarmRecordRepository.Insert(record); + } + } + } + + if (uniqueToList2.Count > 0) + { + //这些是要清除实时报警,并且处理记录的。 + // 使用LINQ找出ErrorCode在uniqueToList2中的EquipAlarmRecord对象 + List filteredObjectList = lstEquipAlarmRecord + .Where(obj => uniqueToList2.Contains(obj.ErrorCode)) + .ToList(); + foreach (var VARIABLE in filteredObjectList) + { + EquipAlarmProcessRecord EquipAlarmProcessRecord = new EquipAlarmProcessRecord(); + EquipAlarmProcessRecord.EquipTypeCode = VARIABLE.EquipTypeCode; + EquipAlarmProcessRecord.EquipCode = VARIABLE.EquipCode; + EquipAlarmProcessRecord.ErrorCode = VARIABLE.ErrorCode; + EquipAlarmProcessRecord.ErrorLevel = VARIABLE.ErrorLevel; + EquipAlarmProcessRecord.ErrorMsg = VARIABLE.ErrorMsg; + EquipAlarmProcessRecord.ProcessMethod = VARIABLE.ProcessMethod; + EquipAlarmProcessRecord.StartTime = VARIABLE.StartTime; + EquipAlarmProcessRecord.ProcessTime = DateTime.Now; + + EquipAlarmProcessRecordRepository.Insert(EquipAlarmProcessRecord); + } + + EquipAlarmRecordRepository.Delete(filteredObjectList); + } + + #endregion + } + else + { + #region 没报警把已处理记录更新并删除实时报警 + + var lstEquipAlarmRecord = + EquipAlarmRecordRepository.QueryListByClause(e => e.EquipTypeCode == typeCode).ToList(); + + if (lstEquipAlarmRecord.Count > 0) + { + foreach (var VARIABLE in lstEquipAlarmRecord) + { + EquipAlarmProcessRecord EquipAlarmProcessRecord = new EquipAlarmProcessRecord(); + EquipAlarmProcessRecord.EquipTypeCode = VARIABLE.EquipTypeCode; + EquipAlarmProcessRecord.EquipCode = VARIABLE.EquipCode; + EquipAlarmProcessRecord.ErrorCode = VARIABLE.ErrorCode; + EquipAlarmProcessRecord.ErrorLevel = VARIABLE.ErrorLevel; + EquipAlarmProcessRecord.ErrorMsg = VARIABLE.ErrorMsg; + EquipAlarmProcessRecord.ProcessMethod = VARIABLE.ProcessMethod; + EquipAlarmProcessRecord.StartTime = VARIABLE.StartTime; + EquipAlarmProcessRecord.ProcessTime = DateTime.Now; + + EquipAlarmProcessRecordRepository.Insert(EquipAlarmProcessRecord); + } + + EquipAlarmRecordRepository.Delete(e => e.EquipTypeCode == typeCode); + } + + #endregion } } }