mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-04 22:17:59 +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>
63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package subscriber
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
"github.com/mainflux/mainflux"
|
|
log "github.com/mainflux/mainflux/logger"
|
|
"github.com/mainflux/mainflux/twins"
|
|
nats "github.com/nats-io/nats.go"
|
|
)
|
|
|
|
const (
|
|
queue = "twins"
|
|
input = "channel.>"
|
|
)
|
|
|
|
// Subscriber is used to intercept messages and save corresponding twin states
|
|
type Subscriber struct {
|
|
natsClient *nats.Conn
|
|
logger log.Logger
|
|
svc twins.Service
|
|
channelID string
|
|
}
|
|
|
|
// NewSubscriber instances Subscriber strucure and subscribes to appropriate NATS topic
|
|
func NewSubscriber(nc *nats.Conn, chID string, svc twins.Service, logger log.Logger) *Subscriber {
|
|
s := Subscriber{
|
|
natsClient: nc,
|
|
logger: logger,
|
|
svc: svc,
|
|
channelID: chID,
|
|
}
|
|
|
|
if _, err := s.natsClient.QueueSubscribe(input, queue, s.handleMsg); err != nil {
|
|
logger.Error(fmt.Sprintf("Failed to subscribe to NATS: %s", err))
|
|
os.Exit(1)
|
|
}
|
|
|
|
return &s
|
|
}
|
|
|
|
func (s *Subscriber) handleMsg(m *nats.Msg) {
|
|
var msg mainflux.Message
|
|
if err := proto.Unmarshal(m.Data, &msg); err != nil {
|
|
s.logger.Warn(fmt.Sprintf("Unmarshalling failed: %s", err))
|
|
return
|
|
}
|
|
|
|
if msg.Channel == s.channelID {
|
|
return
|
|
}
|
|
|
|
if err := s.svc.SaveStates(&msg); err != nil {
|
|
s.logger.Error(fmt.Sprintf("State save failed: %s", err))
|
|
return
|
|
}
|
|
}
|