dataRT/data/mongo/alarm.go

114 lines
2.8 KiB
Go
Raw Normal View History

2025-09-19 16:17:46 +08:00
package mongo
import (
"encoding/json"
"github.com/google/uuid"
)
const (
_ = iota
almCodeCommmExcept // 通信异常
almCodeADFault // AD故障
almCodePPSExcept // 同步秒脉冲异常
2025-11-06 21:09:50 +08:00
almCodeReserve1 // 备用
2025-09-19 16:17:46 +08:00
almCodeUnitInit // 单元初始化
almCodeReadParamErr // 读参数错
2025-11-06 21:09:50 +08:00
almCodeReserve2 // 备用
2025-09-19 16:17:46 +08:00
almCodeStartSample // 启动采样-内部转换信号
almCodeOverSample // 秒内采样点数过量
almCodeUnderSample // 秒内采样点数欠量
)
type Alarm struct {
DriverName string `bson:"driver_name" json:"driver_name"`
DeviceNo string `bson:"device_no" json:"device_no"`
AlarmCode int `bson:"alarm_code" json:"alarm_code"`
AlarmTime int64 `bson:"alarm_time" json:"alarm_time"` // ms
2025-10-23 18:02:29 +08:00
AlarmStatus int `bson:"alarm_status" json:"alarm_status"` // 0 "复位", 1 "动作/产生/告警"
2025-09-19 16:17:46 +08:00
}
2025-11-06 21:09:50 +08:00
var almCode2Name = []string{
2025-09-19 16:17:46 +08:00
almCodeCommmExcept: "通信异常",
almCodeADFault: "AD故障",
almCodePPSExcept: "同步秒脉冲异常",
2025-11-06 21:09:50 +08:00
almCodeReserve1: "备用",
2025-09-19 16:17:46 +08:00
almCodeUnitInit: "单元初始化",
almCodeReadParamErr: "读参数错",
2025-11-06 21:09:50 +08:00
almCodeReserve2: "备用",
2025-09-19 16:17:46 +08:00
almCodeStartSample: "启动采样-内部转换信号",
almCodeOverSample: "秒内采样点数过量",
almCodeUnderSample: "秒内采样点数欠量",
}
func (a *Alarm) GetName() string {
return almCode2Name[a.AlarmCode]
}
2025-11-06 21:09:50 +08:00
func (a *Alarm) GetType() int {
switch a.AlarmCode {
case almCodeReserve1, almCodeReserve2, almCodeUnitInit, almCodeStartSample:
return genEventType(0, 0)
case almCodeOverSample, almCodeUnderSample:
return genEventType(0, 1)
case almCodeCommmExcept, almCodeADFault, almCodePPSExcept, almCodeReadParamErr:
return genEventType(0, 2)
}
return -1
}
func (a *Alarm) GetPriority() int {
switch a.AlarmCode {
case almCodeReserve1, almCodeReserve2, almCodeUnitInit, almCodeStartSample:
return 1
case almCodeOverSample, almCodeUnderSample:
return 4
case almCodeCommmExcept, almCodeADFault, almCodePPSExcept, almCodeReadParamErr:
return 7
}
return -1
}
2025-11-20 20:58:51 +08:00
func GenEvent(alarm []byte, ip string) (*Event, error) {
a, err := UnmarshallToAlarm(alarm)
if err != nil {
return nil, err
}
return a.ConvertToEvent(ip)
}
func (a *Alarm) ConvertToEvent(ip string) (*Event, error) {
2025-09-19 16:17:46 +08:00
e := new(Event)
2025-11-20 20:58:51 +08:00
uid, err := uuid.NewV7()
if err != nil {
return nil, err
}
2025-09-19 16:17:46 +08:00
if a != nil {
e.Event = a.GetName()
2025-11-20 20:58:51 +08:00
e.EventUUID = uid.String()
2025-11-06 21:09:50 +08:00
e.Type = a.GetType()
e.Priority = a.GetPriority()
2025-10-23 18:02:29 +08:00
e.Status = EventStatusHappen
2025-11-06 21:09:50 +08:00
e.Timestamp = a.AlarmTime
2025-09-19 16:17:46 +08:00
e.From = "station"
e.Operations = append(e.Operations, &operation{
2025-11-20 20:58:51 +08:00
Action: EventActionHappened,
2025-09-19 16:17:46 +08:00
OP: ip,
2025-11-06 21:09:50 +08:00
TS: a.AlarmTime,
2025-09-19 16:17:46 +08:00
})
e.Alarm = a
}
2025-11-20 20:58:51 +08:00
return e, nil
2025-09-19 16:17:46 +08:00
}
func UnmarshallToAlarm(data []byte) (*Alarm, error) {
alm := new(Alarm)
err := json.Unmarshal(data, alm)
return alm, err
}