数据 输入输出 处理
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.3 KiB

package adaptors
import (
"encoding/json"
"fmt"
"goInOut/consumers/AlarmCombination"
"goInOut/models"
"log"
"strconv"
"strings"
"time"
)
// Adaptor_ZWY_AlarmCombin 知物云的告警数据 组合统计 触发后 发到 kafka
type Adaptor_ZWY_AlarmCombin struct {
//一些必要信息
Info map[string]string
}
func (the Adaptor_ZWY_AlarmCombin) Transform(config AlarmCombination.CombinationInfo, rawMsg string) []NeedPush {
log.Printf("解析数据")
configItems := config.ConfigItems
esAggData := AlarmCombination.EsAggAlarm{}
esAggData.Aggregations.GroupBySensor.Buckets = []AlarmCombination.BucketsSensorDataCount{}
var needPush []NeedPush
err := json.Unmarshal([]byte(rawMsg), &esAggData)
if err != nil {
log.Printf("解析 es proxy 数据异常: %s", err.Error())
return nil
}
pointsCount := 0
for _, item := range configItems {
pointsCount += len(item.StationIds)
}
esAggPointsCount := len(esAggData.Aggregations.GroupBySensor.Buckets)
if esAggPointsCount < pointsCount {
log.Printf("es 聚集查询告警数=%d < 配置测点数 %d", esAggPointsCount, pointsCount)
return nil
}
msg := fmt.Sprintf("组合告警[%s]生效,存在%d个不同测点的告警", config.Name, esAggPointsCount)
log.Println(msg)
prefix := "zh-"
sourceId := prefix + strconv.Itoa(config.Id)
alarmMsg := models.KafkaAlarm{
MessageMode: "AlarmGeneration",
StructureId: config.StructId,
StructureName: config.StructName,
SourceId: sourceId,
SourceName: config.Name,
AlarmTypeCode: "8003",
AlarmCode: "80030001",
Content: msg,
Time: time.Now().Format("2006-01-02T15:04:05+0800"),
SourceTypeId: 4, // 0:DTU, 1:传感器, 2:测点
Sponsor: "goInOut_zhgj",
Extras: nil,
SubDevices: nil,
}
Payload, _ := json.Marshal(alarmMsg)
needPush = append(needPush, NeedPush{
Payload: Payload,
})
return needPush
}
func (the Adaptor_ZWY_AlarmCombin) GetPointCodeFromLabel(label string) int64 {
//解析label {13010600001}
pointUniqueCode := int64(0)
if len(label) > 2 {
newLabel := strings.TrimLeft(label, "{")
str := strings.TrimRight(newLabel, "}")
codeInt64, err := strconv.ParseInt(str, 10, 64)
if err != nil {
log.Printf("测点标签转换异常[%s]", label)
}
pointUniqueCode = codeInt64
}
return pointUniqueCode
}