package adaptors import ( "encoding/json" "goInOut/consumers/HBJCAS" "goInOut/consumers/HBJCAS/protoFiles_hb" "google.golang.org/protobuf/proto" "log" "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 := HBJCAS.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 HBJCAS.EsThemeAggDateHistogram) (result []byte) { buckets := esAggs.Aggregations.GroupSensor.Buckets if len(buckets) == 0 { log.Printf("[s=%d,f=%d] ,es agg数据数量==0", structId, factorId) 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) } } v, _ := json.Marshal(complexData) log.Printf("[s:%d,f:%d] 特征数据=> %s", structId, factorId, v) result, _ = proto.Marshal(complexData) return result } func (the Adaptor_AXYES_HBGL) getMonitorTypeByFactorId(factorId int) protoFiles_hb.MonitoryType { //桥墩倾斜 15 裂缝18 支座位移20 桥面振动28 switch factorId { case 15: return protoFiles_hb.MonitoryType_INC case 18: return protoFiles_hb.MonitoryType_CRK case 20: return protoFiles_hb.MonitoryType_AND case 28: return protoFiles_hb.MonitoryType_VIB default: log.Printf("factorId=%d,无匹配的MonitorType", factorId) return protoFiles_hb.MonitoryType_CMM } } func (the Adaptor_AXYES_HBGL) EsAgg2StatisticData(factorId int, monitorCode int64, dateBucket HBJCAS.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 rootMeanSquareX := math.Sqrt(dateBucket.X.SumOfSquares / float64(dateBucket.X.Count)) maxAbsoluteValueY := max(math.Abs(dateBucket.Y.Max), math.Abs(dateBucket.Y.Min)) avgValueY := dateBucket.Y.Avg 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, }, } switch factorId { case 15: //倾角 dataDefinitionStatisticData.StatisticData.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), }} case 18: //裂缝监测 dataDefinitionStatisticData.StatisticData.DataBody = &protoFiles_hb.StatisticData_Crk{Crk: &protoFiles_hb.CRKStatistic{ MaxAbsoluteValue: float32(maxAbsoluteValueX), AvgValue: float32(avgValueX), RootMeanSquare: float32(rootMeanSquareX), TotalAbsoluteValue: float32(dateBucket.X.Max - dateBucket.X.Min), }} case 20: //支座位移 dataDefinitionStatisticData.StatisticData.DataBody = &protoFiles_hb.StatisticData_Dis{Dis: &protoFiles_hb.DISStatistic{ MaxAbsoluteValue: float32(maxAbsoluteValueX), AvgValue: float32(avgValueX), RootMeanSquare: float32(rootMeanSquareX), TotalAbsoluteValue: float32(dateBucket.X.Max - dateBucket.X.Min), }} case 28: //振动 dataDefinitionStatisticData.StatisticData.DataBody = &protoFiles_hb.StatisticData_Vib{Vib: &protoFiles_hb.VIBStatistic{ MaxAbsoluteValue: float32(maxAbsoluteValueX), RootMeanSquare: 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 }