dataRT/data/mongo/alarm.go

74 lines
2.0 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 // 同步秒脉冲异常
almCodeBackup // 备用
almCodeUnitInit // 单元初始化
almCodeReadParamErr // 读参数错
almCodeReserve // 备用
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
}
var almCode2Name = map[int]string{
almCodeCommmExcept: "通信异常",
almCodeADFault: "AD故障",
almCodePPSExcept: "同步秒脉冲异常",
almCodeBackup: "备用",
almCodeUnitInit: "单元初始化",
almCodeReadParamErr: "读参数错",
almCodeReserve: "备用",
almCodeStartSample: "启动采样-内部转换信号",
almCodeOverSample: "秒内采样点数过量",
almCodeUnderSample: "秒内采样点数欠量",
}
func (a *Alarm) GetName() string {
return almCode2Name[a.AlarmCode]
}
func (a *Alarm) ConvertToEvent(ip string) *Event {
e := new(Event)
if a != nil {
e.Event = a.GetName()
e.EventUUID = uuid.NewString()
e.Type = genEventType(0, 2)
e.Priority = 5
2025-10-23 18:02:29 +08:00
e.Status = EventStatusHappen
2025-10-11 14:56:11 +08:00
e.Timestamp = a.AlarmTime / 1e3 // TODO ms ?
2025-09-19 16:17:46 +08:00
e.From = "station"
e.Operations = append(e.Operations, &operation{
2025-10-23 18:02:29 +08:00
Action: EventActionHappened, // TODO
2025-09-19 16:17:46 +08:00
OP: ip,
2025-10-11 14:56:11 +08:00
TS: a.AlarmTime / 1e3,
2025-09-19 16:17:46 +08:00
})
e.Alarm = a
}
return e
}
func UnmarshallToAlarm(data []byte) (*Alarm, error) {
alm := new(Alarm)
err := json.Unmarshal(data, alm)
return alm, err
}