mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-12 19:29:30 +08:00

* MF-1254 - Create universal JSON writer (#1260) Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add JSON support to Readers Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix Influx Reader tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix messages format query Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix Postgres reader Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix Cassandra Readers and writers Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix Mongo reader Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Extract utility method to the JSON transformer Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix Influx and Postgres count Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Update JSON transformer Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix Influxdb Reader total count Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Refactor init.go for Cassandra writer Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Create a Payload type Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add comments for defaults Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix variable declarations Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Replace interface{} with a new type Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Don't set channel just to overwrite it later Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix range search Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Rename Messages field Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Co-authored-by: Manuel Imperiale <manuel.imperiale@gmail.com>
83 lines
2.1 KiB
Go
83 lines
2.1 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package writers
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
"github.com/mainflux/mainflux/logger"
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
|
pubsub "github.com/mainflux/mainflux/pkg/messaging/nats"
|
|
"github.com/mainflux/mainflux/pkg/transformers"
|
|
)
|
|
|
|
var (
|
|
errOpenConfFile = errors.New("unable to open configuration file")
|
|
errParseConfFile = errors.New("unable to parse configuration file")
|
|
errMessageConversion = errors.New("error conversing transformed messages")
|
|
)
|
|
|
|
type consumer struct {
|
|
repo MessageRepository
|
|
transformer transformers.Transformer
|
|
logger logger.Logger
|
|
}
|
|
|
|
// Start method starts consuming messages received from NATS.
|
|
// This method transforms messages to SenML format before
|
|
// using MessageRepository to store them.
|
|
func Start(sub messaging.Subscriber, repo MessageRepository, transformer transformers.Transformer, subjectsCfgPath string, logger logger.Logger) error {
|
|
c := consumer{
|
|
repo: repo,
|
|
transformer: transformer,
|
|
logger: logger,
|
|
}
|
|
|
|
subjects, err := loadSubjectsConfig(subjectsCfgPath)
|
|
if err != nil {
|
|
logger.Warn(fmt.Sprintf("Failed to load subjects: %s", err))
|
|
}
|
|
|
|
for _, subject := range subjects {
|
|
if err := sub.Subscribe(subject, c.handler); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *consumer) handler(msg messaging.Message) error {
|
|
t, err := c.transformer.Transform(msg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.repo.Save(t)
|
|
}
|
|
|
|
type filterConfig struct {
|
|
Filter []string `toml:"filter"`
|
|
}
|
|
|
|
type subjectsConfig struct {
|
|
Subjects filterConfig `toml:"subjects"`
|
|
}
|
|
|
|
func loadSubjectsConfig(subjectsConfigPath string) ([]string, error) {
|
|
data, err := ioutil.ReadFile(subjectsConfigPath)
|
|
if err != nil {
|
|
return []string{pubsub.SubjectAllChannels}, errors.Wrap(errOpenConfFile, err)
|
|
}
|
|
|
|
var subjectsCfg subjectsConfig
|
|
if err := toml.Unmarshal(data, &subjectsCfg); err != nil {
|
|
return []string{pubsub.SubjectAllChannels}, errors.Wrap(errParseConfFile, err)
|
|
}
|
|
|
|
return subjectsCfg.Subjects.Filter, nil
|
|
}
|