package adaptors import ( "encoding/json" "fmt" "goUpload/consumers/AXYraw" "goUpload/consumers/GZGZM" "goUpload/dbHelper" "goUpload/models" "log" "math" "time" ) // Adaptor_AXY_LastRAW 安心云 kafka iota数据 转换 es设备数据 type Adaptor_AXY_LastRAW struct { AXYraw.Info Redis *dbHelper.RedisHelper } func (the Adaptor_AXY_LastRAW) Transform(topic, rawMsg string) []byte { iotaData := models.IotaData{} json.Unmarshal([]byte(rawMsg), &iotaData) return the.Theme2GzGZM(iotaData) } func (the Adaptor_AXY_LastRAW) Theme2GzGZM(iotaData models.IotaData) (result []byte) { if !iotaData.Data.Success() { return } log.Printf("设备[%s] 数据时间 %s", iotaData.DeviceId, iotaData.TriggerTime) the.GetDeviceInfo(iotaData.DeviceId) return result } func (the Adaptor_AXY_LastRAW) getSensorId(sensorId string) GZGZM.SensorInfo { s := GZGZM.SensorInfo{} //if v, ok := the.SensorInfoMap[sensorId]; ok { // s = v //} return s } func (the Adaptor_AXY_LastRAW) getCodeBytes(sensorCode int16) []byte { bytes := make([]byte, 0) bytes = append(bytes, byte(sensorCode&0xFF), byte(sensorCode>>8), ) return bytes } func (the Adaptor_AXY_LastRAW) getTimeBytes(sensorTime time.Time) []byte { year := int8(sensorTime.Year() - 1900) month := int8(sensorTime.Month()) day := int8(sensorTime.Day()) hour := int8(sensorTime.Hour()) minute := int8(sensorTime.Minute()) millisecond := uint16(sensorTime.Second()*1000 + sensorTime.Nanosecond()/1e6) bytes := make([]byte, 0) bytes = append(bytes, byte(year), byte(month), byte(day), byte(hour), byte(minute), byte(millisecond&0xFF), byte(millisecond>>8), ) return bytes } func (the Adaptor_AXY_LastRAW) getDatasBytes(datas []float32) []byte { bytes := make([]byte, 0) for _, data := range datas { bits := math.Float32bits(data) bytes = append(bytes, byte(bits&0xFF), byte(bits>>8&0xFF), byte(bits>>16&0xFF), byte(bits>>24&0xFF), ) } return bytes } func (the Adaptor_AXY_LastRAW) getPayloadHeader(floatCount int16) []byte { bytes := make([]byte, 0) bytes = append(bytes, //报文类型 0x02, 0x00, //1:上行信息 0x01, //默认,通讯计算机编号 0x00, //命令码 0x01, //报文长度 byte((floatCount*4+9)&0xFF), byte((floatCount*4+9)>>8), ) return bytes } func (the Adaptor_AXY_LastRAW) GetDeviceInfo(deviceId string) []byte { Key_Iota_device := "iota_device" key_Thing_struct := "thing_struct" key_Iota_meta := "iota_meta" k1 := fmt.Sprintf("%s:%s", Key_Iota_device, deviceId) dev := models.IotaDevice{} ts := models.ThingStruct{} devMeta := models.DeviceMeta{} err1 := the.Redis.GetObj(k1, &dev) k2 := fmt.Sprintf("%s:%s", key_Thing_struct, dev.ThingId) err2 := the.Redis.GetObj(k2, &ts) k3 := fmt.Sprintf("%s:%s", key_Iota_meta, dev.DeviceMeta.Id) err3 := the.Redis.GetObj(k3, &devMeta) println(err1, err2, err3) return make([]byte, 0) }