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

* NOISSUE - Add mProxy support (#1017) * Add mproxy Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> * Fix docker and add EMQ compose Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> * Fix EMQX name Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> * Add nats, auth and es Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> * Removed unucessary vendoring Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> * Update vendoring Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> * Fix mproxy interface implementation Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> NOISSUE - Aligned Event interface method signatures with new spec (#1025) * Aligned Event interface method signatures with new spec Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> * Updated deps Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> NOISSUE - Update mproxy dependency (#1038) Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> Update Vendor with new mProxy (#1043) Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> Twins merge conflict reverted Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> Twins merge conflict reverted Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> Twins fixed nats import Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> Update deps Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> * Resolved GolangCI remarks Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> Resolved GolangCI remarks Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> Resolved GolangCI remarks Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> * Fixed Event interface Unsubscribe() typo Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> * Update vendors Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Upgrade CI script Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> Co-authored-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package writers
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
"github.com/mainflux/mainflux"
|
|
log "github.com/mainflux/mainflux/logger"
|
|
"github.com/mainflux/mainflux/transformers"
|
|
"github.com/mainflux/mainflux/transformers/senml"
|
|
nats "github.com/nats-io/nats.go"
|
|
)
|
|
|
|
type consumer struct {
|
|
nc *nats.Conn
|
|
channels map[string]bool
|
|
repo MessageRepository
|
|
transformer transformers.Transformer
|
|
logger log.Logger
|
|
}
|
|
|
|
// Start method starts consuming messages received from NATS.
|
|
// This method transforms messages to SenML format before
|
|
// using MessageRepository to store them.
|
|
func Start(nc *nats.Conn, repo MessageRepository, transformer transformers.Transformer, queue string, channels map[string]bool, logger log.Logger) error {
|
|
c := consumer{
|
|
nc: nc,
|
|
channels: channels,
|
|
repo: repo,
|
|
transformer: transformer,
|
|
logger: logger,
|
|
}
|
|
|
|
_, err := nc.QueueSubscribe(mainflux.InputChannels, queue, c.consume)
|
|
return err
|
|
}
|
|
|
|
func (c *consumer) consume(m *nats.Msg) {
|
|
var msg mainflux.Message
|
|
if err := proto.Unmarshal(m.Data, &msg); err != nil {
|
|
c.logger.Warn(fmt.Sprintf("Failed to unmarshal received message: %s", err))
|
|
return
|
|
}
|
|
|
|
t, err := c.transformer.Transform(msg)
|
|
if err != nil {
|
|
c.logger.Warn(fmt.Sprintf("Failed to tranform received message: %s", err))
|
|
return
|
|
}
|
|
norm, ok := t.([]senml.Message)
|
|
if !ok {
|
|
c.logger.Warn("Invalid message format from the Transformer output.")
|
|
return
|
|
}
|
|
var msgs []senml.Message
|
|
for _, v := range norm {
|
|
if c.channelExists(v.Channel) {
|
|
msgs = append(msgs, v)
|
|
}
|
|
}
|
|
|
|
if err := c.repo.Save(msgs...); err != nil {
|
|
c.logger.Warn(fmt.Sprintf("Failed to save message: %s", err))
|
|
return
|
|
}
|
|
}
|
|
|
|
func (c *consumer) channelExists(channel string) bool {
|
|
if _, ok := c.channels["*"]; ok {
|
|
return true
|
|
}
|
|
|
|
_, found := c.channels[channel]
|
|
return found
|
|
}
|