87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
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
|
|
AlarmStatus int `bson:"alarm_status" json:"alarm_status"` // 0 "复位", 1 "动作/产生/告警"
|
|
}
|
|
|
|
var almCode2Name = []string{
|
|
almCodeCommmExcept: "通信异常",
|
|
almCodeADFault: "AD故障",
|
|
almCodePPSExcept: "同步秒脉冲异常",
|
|
almCodeBackup: "备用",
|
|
almCodeUnitInit: "单元初始化",
|
|
almCodeReadParamErr: "读参数错",
|
|
almCodeReserve: "备用",
|
|
almCodeStartSample: "启动采样-内部转换信号",
|
|
almCodeOverSample: "秒内采样点数过量",
|
|
almCodeUnderSample: "秒内采样点数欠量",
|
|
}
|
|
|
|
func (a *Alarm) GetName() string {
|
|
return almCode2Name[a.AlarmCode]
|
|
}
|
|
|
|
func (a *Alarm) GetType() int {
|
|
switch a.AlarmCode {
|
|
case almCodeBackup, almCodeReserve, 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) ConvertToEvent(ip string) *Event {
|
|
e := new(Event)
|
|
if a != nil {
|
|
e.Event = a.GetName()
|
|
e.EventUUID = uuid.NewString()
|
|
e.Type = a.GetType()
|
|
e.Priority = 5
|
|
e.Status = EventStatusHappen
|
|
e.Timestamp = a.AlarmTime
|
|
e.From = "station"
|
|
e.Operations = append(e.Operations, &operation{
|
|
Action: EventActionHappened, // TODO
|
|
OP: ip,
|
|
TS: a.AlarmTime,
|
|
})
|
|
e.Alarm = a
|
|
}
|
|
|
|
return e
|
|
}
|
|
|
|
func UnmarshallToAlarm(data []byte) (*Alarm, error) {
|
|
alm := new(Alarm)
|
|
err := json.Unmarshal(data, alm)
|
|
return alm, err
|
|
}
|