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.
58 lines
1.6 KiB
58 lines
1.6 KiB
package processors
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"gitea.anxinyun.cn/container/common_models"
|
|
"gitea.anxinyun.cn/container/common_utils/dbHelper"
|
|
"log"
|
|
)
|
|
|
|
func NewEsSink() ISink {
|
|
return &EsSink{
|
|
esESHelper: dbHelper.NewESHelper([]string{"http://10.8.30.160:30092"}, "", ""),
|
|
}
|
|
}
|
|
|
|
type EsSink struct {
|
|
esESHelper *dbHelper.ESHelper
|
|
}
|
|
|
|
func (s *EsSink) Process(ctx context.Context, params any) error {
|
|
|
|
log.Printf("es sink 操作")
|
|
|
|
switch params.(type) {
|
|
case *common_models.DeviceData:
|
|
esRawList := s.toEsRaw([]*common_models.DeviceData{params.(*common_models.DeviceData)})
|
|
s.dumpRaw(esRawList)
|
|
case *common_models.ProcessData:
|
|
esRawList := s.toEsRaw([]*common_models.DeviceData{¶ms.(*common_models.ProcessData).DeviceData})
|
|
s.dumpRaw(esRawList)
|
|
default:
|
|
return errors.New("不支持的类型数据")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *EsSink) toEsRaw(deviceDataList []*common_models.DeviceData) []common_models.EsRaw {
|
|
var createNativeRaws []common_models.EsRaw
|
|
for _, deviceData := range deviceDataList {
|
|
dataOutMeta := deviceData.DeviceInfo.DeviceMeta.GetOutputProps()
|
|
createNativeRaws = append(createNativeRaws, common_models.EsRaw{
|
|
StructId: deviceData.StructId,
|
|
IotaDeviceName: deviceData.Name,
|
|
Data: deviceData.Raw,
|
|
CollectTime: deviceData.AcqTime,
|
|
Meta: dataOutMeta,
|
|
IotaDevice: deviceData.DeviceId,
|
|
CreateTime: deviceData.RealTime,
|
|
})
|
|
}
|
|
|
|
return createNativeRaws
|
|
}
|
|
func (s *EsSink) dumpRaw(esRaws []common_models.EsRaw) {
|
|
esIndex := "go_native_raws"
|
|
go s.esESHelper.BulkWriteRaws2Es(esIndex, esRaws)
|
|
}
|
|
|