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

99 lines
2.7 KiB

package adaptors
import (
"encoding/json"
"fmt"
"goUpload/consumers/AXYraw"
"goUpload/dbHelper"
"goUpload/models"
"log"
"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) *models.EsRaw {
iotaData := models.IotaData{}
json.Unmarshal([]byte(rawMsg), &iotaData)
return the.raw2es(iotaData)
}
func (the Adaptor_AXY_LastRAW) raw2es(iotaData models.IotaData) *models.EsRaw {
if !iotaData.Data.Success() {
return nil
}
log.Printf("设备[%s] 数据时间 %s", iotaData.DeviceId, iotaData.TriggerTime)
deviceInfo := the.GetDeviceInfo(iotaData.DeviceId)
dataType := ""
if _dataType, ok := iotaData.Data.Data["_data_type"]; ok {
if v, ok := _dataType.(string); ok {
dataType = v
}
}
devdata := &models.DeviceData{
DeviceId: iotaData.DeviceId,
Name: deviceInfo.Name,
ThingId: iotaData.ThingId,
StructId: deviceInfo.Structure.Id,
AcqTime: iotaData.TriggerTime,
RealTime: iotaData.RealTime,
ErrCode: 0,
Raw: iotaData.Data.Data,
DeviceInfo: deviceInfo,
DimensionId: iotaData.DimensionId,
DataType: dataType,
}
EsRaws := toEsRaw(devdata)
return EsRaws
}
func (the Adaptor_AXY_LastRAW) GetDeviceInfo(deviceId string) models.DeviceInfo {
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{}
thingStruct := 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, &thingStruct)
k3 := fmt.Sprintf("%s:%s", key_Iota_meta, dev.DeviceMeta.Id)
err3 := the.Redis.GetObj(k3, &devMeta)
if err1 != nil || err2 != nil || err3 != nil {
log.Printf("redis读取异常,err1=%s, err2=%s, err3=%s", err1, err2, err3)
}
s := models.Structure{
ThingId: thingStruct.ThingId,
Id: thingStruct.Id,
Name: thingStruct.Name,
OrgId: thingStruct.OrgId,
}
return models.DeviceInfo{
Id: deviceId,
Name: dev.Name,
Structure: s,
DeviceMeta: devMeta,
}
}
func toEsRaw(deviceData *models.DeviceData) *models.EsRaw {
dataOutMeta := deviceData.DeviceInfo.DeviceMeta.GetOutputProps()
createNativeRaw := models.EsRaw{
StructId: deviceData.StructId,
IotaDeviceName: deviceData.Name,
Data: deviceData.Raw,
CollectTime: deviceData.AcqTime,
Meta: dataOutMeta,
IotaDevice: deviceData.DeviceId,
CreateTime: time.Now(),
}
return &createNativeRaw
}