数据 输入输出 处理
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.

129 lines
4.0 KiB

package adaptors
import (
"encoding/json"
"github.com/labstack/gommon/log"
"goInOut/consumers/HBJCAS/protoFiles_hb"
"goInOut/models"
"google.golang.org/protobuf/proto"
"math"
"strconv"
"time"
)
// Adaptor_AXYES_HBGL 统一采集软件数据 转换 湘潭健康监测平台
type Adaptor_AXYES_HBGL struct {
//传感器code转换信息
PointInfo map[int64]map[int64]int64
StructInfo map[int64]int64
//一些必要信息
Info map[string]string
}
func (the Adaptor_AXYES_HBGL) Transform(structId int64, factorId int, rawMsg string) []NeedPush {
esAggDateHistogram := models.EsThemeAggDateHistogram{}
var needPush []NeedPush
err := json.Unmarshal([]byte(rawMsg), &esAggDateHistogram)
if err != nil {
return nil
}
Payload := the.EsAggTopToHBJCAS(structId, factorId, esAggDateHistogram)
if len(Payload) == 0 {
return needPush
}
needPush = append(needPush, NeedPush{
Payload: Payload,
})
return needPush
}
func (the Adaptor_AXYES_HBGL) EsAggTopToHBJCAS(structId int64, factorId int, esAggs models.EsThemeAggDateHistogram) (result []byte) {
buckets := esAggs.Aggregations.GroupSensor.Buckets
if len(buckets) == 0 {
log.Info("es agg数据数量==0")
return
}
//设施唯一编码(省平台)
uniqueCode := the.getUniqueCode(structId)
if uniqueCode == 0 {
log.Printf("structId=%d,无匹配省平台uniqueCode", structId)
return
}
//数据汇总
complexData := &protoFiles_hb.ComplexData{}
for _, sensorBucket := range buckets {
sensorId := sensorBucket.Key
monitorCode := int64(0)
for _, dateBucket := range sensorBucket.GroupDate.Buckets {
if _, ok := the.PointInfo[structId]; !ok {
continue
}
if _, ok := the.PointInfo[structId][sensorId]; !ok {
continue
}
monitorCode = the.PointInfo[structId][sensorId]
dataDefinition := &protoFiles_hb.DataDefinition{
DataType: protoFiles_hb.DataType_STATISTICS,
UniqueCode: strconv.FormatInt(uniqueCode, 10), //乃积沟大桥
DataBody: the.EsAgg2StatisticData(factorId, monitorCode, dateBucket),
}
complexData.SensorData = append(complexData.SensorData, dataDefinition)
}
}
result, _ = proto.Marshal(complexData)
return result
}
func (the Adaptor_AXYES_HBGL) getMonitorTypeByFactorId(factorId int) protoFiles_hb.MonitoryType {
//桥墩倾斜 15 支座位移20 桥面振动28
switch factorId {
case 15:
return protoFiles_hb.MonitoryType_INC
case 20:
return protoFiles_hb.MonitoryType_AND
case 28:
return protoFiles_hb.MonitoryType_VIB
default:
return protoFiles_hb.MonitoryType_CMM
}
}
func (the Adaptor_AXYES_HBGL) EsAgg2StatisticData(factorId int, monitorCode int64, dateBucket models.BucketsXY) *protoFiles_hb.DataDefinition_StatisticData {
Atime := dateBucket.KeyAsString.Add(-8 * time.Hour).UnixMilli()
maxAbsoluteValueX := max(math.Abs(dateBucket.X.Max), math.Abs(dateBucket.X.Min))
avgValueX := dateBucket.X.Avg
maxAbsoluteValueY := max(math.Abs(dateBucket.Y.Max), math.Abs(dateBucket.Y.Min))
avgValueY := dateBucket.Y.Avg
rootMeanSquareX := math.Sqrt(dateBucket.X.SumOfSquares / float64(dateBucket.X.Count))
rootMeanSquareY := math.Sqrt(dateBucket.Y.SumOfSquares / float64(dateBucket.Y.Count))
monitoryType := the.getMonitorTypeByFactorId(factorId)
dataDefinitionStatisticData := &protoFiles_hb.DataDefinition_StatisticData{
StatisticData: &protoFiles_hb.StatisticData{
MonitorType: monitoryType,
MonitorCode: monitorCode, //测点唯一编码
EventTime: Atime,
Interval: 60 * 1000,
DataBody: &protoFiles_hb.StatisticData_Inc{Inc: &protoFiles_hb.INCStatistic{
MaxAbsoluteValueX: float32(maxAbsoluteValueX),
AvgValueX: float32(avgValueX),
RootMeanSquareX: float32(rootMeanSquareX),
MaxAbsoluteValueY: float32(maxAbsoluteValueY),
AvgValueY: float32(avgValueY),
RootMeanSquareY: float32(rootMeanSquareY),
}},
},
}
return dataDefinitionStatisticData
}
func (the Adaptor_AXYES_HBGL) getUniqueCode(structId int64) (uniqueCode int64) {
if v, ok := the.StructInfo[structId]; ok {
uniqueCode = v
}
return uniqueCode
}