package mongo import ( "encoding/json" "github.com/google/uuid" ) const ( _ = iota almCodeCommmExcept almCodeADFault almCodePPSExcept almCodeReserve1 almCodeUnitInit almCodeReadParamErr almCodeReserve2 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 AlarmStatus int `bson:"alarm_status" json:"alarm_status"` // 0 "复位", 1 "动作/产生/告警" } var almCode2Name = []string{ almCodeCommmExcept: "通信异常", almCodeADFault: "AD故障", almCodePPSExcept: "同步秒脉冲异常", almCodeReserve1: "备用", almCodeUnitInit: "单元初始化", almCodeReadParamErr: "读参数错", almCodeReserve2: "备用", almCodeStartSample: "启动采样-内部转换信号", almCodeOverSample: "秒内采样点数过量", almCodeUnderSample: "秒内采样点数欠量", } func (a *Alarm) GetName() string { return almCode2Name[a.AlarmCode] } 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 } 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) { e := new(Event) uid, err := uuid.NewV7() if err != nil { return nil, err } if a != nil { e.Event = a.GetName() e.EventUUID = uid.String() e.Type = a.GetType() e.Priority = a.GetPriority() e.Status = EventStatusHappen e.Timestamp = a.AlarmTime e.From = "station" e.Operations = append(e.Operations, &operation{ Action: EventActionHappen, OP: ip, TS: a.AlarmTime, }) e.Alarm = a } return e, nil } func UnmarshallToAlarm(data []byte) (*Alarm, error) { alm := new(Alarm) err := json.Unmarshal(data, alm) return alm, err }