From 1d2ef80451bf2c99a03204c077590520acd533af Mon Sep 17 00:00:00 2001 From: lucas Date: Fri, 29 Aug 2025 09:25:24 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E5=8E=86=E5=8F=B2=E5=91=8A=E8=AD=A6?= =?UTF-8?q?=20=E7=BA=AA=E5=BD=95=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consumers/consumerAxySkAlarm.go | 49 ++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/consumers/consumerAxySkAlarm.go b/consumers/consumerAxySkAlarm.go index 7175c72..3c60bd7 100644 --- a/consumers/consumerAxySkAlarm.go +++ b/consumers/consumerAxySkAlarm.go @@ -110,13 +110,13 @@ func (the *consumerAxySkAlarm) monitorInitial() error { for taskName, cron := range the.Info.Monitor { switch taskName { case "cron": - the.monitor.RegisterTask(cron, the.updateTriggerConfig) + //the.monitor.RegisterTask(cron, the.updateTriggerConfig) default: log.Printf("定时任务[%s],cron=[%s] 无匹配", taskName, cron) } } //测试用 - //the.updateTriggerConfig() + the.updateTriggerConfig() return nil } func (the *consumerAxySkAlarm) updateTriggerConfig() { @@ -192,7 +192,7 @@ func (the *consumerAxySkAlarm) judgeSK() string { log.Printf("测点[%s]本次已经触发过[%s],不再重复触发", sid, v) continue } - isAlarm, level, detail := the.isRuleAlarm(trigger, stationAlarmInfo) + isAlarm, level, detail, alarmTime := the.isRuleAlarm(trigger, stationAlarmInfo) println(isAlarm, level, detail) var alarmInfoTemplate *models.EsAlarm if stationAlarmInfo.Alarm3007 != nil { @@ -205,9 +205,6 @@ func (the *consumerAxySkAlarm) judgeSK() string { conditionStr := fmt.Sprintf("st:%d,f:%d,level:%d", trigger.StructId, trigger.FactorId, level) onceTriggerStationAlarmMap[sid] = conditionStr - //纪录历史告警 - - payload, now := the.skAlarmInfo(alarmInfoTemplate, level, detail) //判断历史有没有 if v, ok := the.historyStationAlarmMap[sid]; ok { if v.AlarmLevel > level { //低等级告警过滤 @@ -215,32 +212,34 @@ func (the *consumerAxySkAlarm) judgeSK() string { continue } - if !v.Time.After(now) { + if !v.Time.After(alarmTime) { log.Printf("测点[%s]本次触发时刻[%s] 对比历史有效时刻[%s] 非新", sid, - v.Time.Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05")) + v.Time.Format("2006-01-02 15:04:05"), alarmTime.Format("2006-01-02 15:04:05")) continue } - } + //纪录历史告警 + payload := the.skAlarmInfo(alarmInfoTemplate, level, detail, alarmTime) + the.historyStationAlarmMap[sid] = AXY_SK.HistoryAlarm{ SourceId: sid, Condition: conditionStr, AlarmLevel: level, - Time: now, + Time: alarmTime, } the.InKafka.Publish(the.Info.IoConfig.In.Kafka.AlarmTopic, payload) } else { payload := the.skAlarmElimination(alarmInfoTemplate, level, detail) the.InKafka.Publish(the.Info.IoConfig.In.Kafka.AlarmTopic, payload) + delete(the.historyStationAlarmMap, sid) } } } return "" } -func (the *consumerAxySkAlarm) skAlarmInfo(alarmInfoTemplate *models.EsAlarm, level int, detail string) ([]byte, time.Time) { - now := time.Now() +func (the *consumerAxySkAlarm) skAlarmInfo(alarmInfoTemplate *models.EsAlarm, level int, detail string, alarmTime time.Time) []byte { alarmMsg := models.KafkaAlarm{ MessageMode: "AlarmGeneration", StructureId: alarmInfoTemplate.StructureId, @@ -250,14 +249,14 @@ func (the *consumerAxySkAlarm) skAlarmInfo(alarmInfoTemplate *models.EsAlarm, le AlarmTypeCode: "3077", AlarmCode: fmt.Sprintf("3077000%d", level), Content: detail, - Time: now.Format("2006-01-02T15:04:05+0800"), + Time: alarmTime.Format("2006-01-02T15:04:05+0800"), SourceTypeId: 2, // 0:DTU, 1:传感器, 2:测点 Sponsor: "goInOut_axySkAlarm", Extras: nil, SubDevices: nil, } payload, _ := json.Marshal(alarmMsg) - return payload, now + return payload } func (the *consumerAxySkAlarm) skAlarmElimination(alarmInfoTemplate *models.EsAlarm, level int, detail string) []byte { alarmMsg := models.KafkaAlarm{ @@ -279,9 +278,10 @@ func (the *consumerAxySkAlarm) skAlarmElimination(alarmInfoTemplate *models.EsAl return payload } -func (the *consumerAxySkAlarm) isRuleAlarm(trigger AXY_SK.AlarmTrigger, stationAlarm AXY_SK.StationAlarmGroup) (bool, int, string) { +func (the *consumerAxySkAlarm) isRuleAlarm(trigger AXY_SK.AlarmTrigger, stationAlarm AXY_SK.StationAlarmGroup) (bool, int, string, time.Time) { level := 0 detail := "" + dt := time.Time{} //3007和3008都要有 if trigger.Rule == 0 { isAlarm := true @@ -296,6 +296,9 @@ func (the *consumerAxySkAlarm) isRuleAlarm(trigger AXY_SK.AlarmTrigger, stationA if len(detail) > 0 { detail += "且 " } + if stationAlarm.Alarm3007.EndTime.After(dt) { + dt = stationAlarm.Alarm3007.EndTime + } detail += stationAlarm.Alarm3007.Detail } @@ -310,7 +313,9 @@ func (the *consumerAxySkAlarm) isRuleAlarm(trigger AXY_SK.AlarmTrigger, stationA detail += "且 " } detail += stationAlarm.Alarm3008.Detail - + if stationAlarm.Alarm3008.EndTime.After(dt) { + dt = stationAlarm.Alarm3008.EndTime + } } } @@ -318,7 +323,7 @@ func (the *consumerAxySkAlarm) isRuleAlarm(trigger AXY_SK.AlarmTrigger, stationA if isAlarm { level = trigger.AlarmLevel } - return isAlarm, level, detail + return isAlarm, level, detail, dt } //3007和3008 任何一个 @@ -331,11 +336,17 @@ func (the *consumerAxySkAlarm) isRuleAlarm(trigger AXY_SK.AlarmTrigger, stationA if stationAlarm.Alarm3007.CurrentLevel > trigger.AlarmLevel { isAlarm = true detail += stationAlarm.Alarm3007.Detail + if stationAlarm.Alarm3007.EndTime.After(dt) { + dt = stationAlarm.Alarm3007.EndTime + } } case 1: if stationAlarm.Alarm3008.CurrentLevel > trigger.AlarmLevel { isAlarm = true detail += stationAlarm.Alarm3008.Detail + if stationAlarm.Alarm3008.EndTime.After(dt) { + dt = stationAlarm.Alarm3008.EndTime + } } } @@ -344,9 +355,9 @@ func (the *consumerAxySkAlarm) isRuleAlarm(trigger AXY_SK.AlarmTrigger, stationA break } } - return isAlarm, level, detail + return isAlarm, level, detail, dt } - return false, level, detail + return false, level, detail, dt } func (the *consumerAxySkAlarm) getEsAlarmTriggerQueryStr(structId int) string {