mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* Refactor messaging Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Rename SubscribeHandler to MessageHandler Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove `Auth` event logs Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update message pubsub APi Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix topics handling Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update CoAP adapter Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update Twins service Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update LoRa adapter Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update OPC UA adapter Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove broker package Package `broker` is conceptually renamed to package `nats`. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update makefile Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Add comment explanation Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix MQTT adapter Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix typo Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Move NATS pub/sub implementation to pubsub pkg Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove an empty line in main methods Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Move messaging-related code to messaging package Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix Twins mocks Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Change Occurred back to Created Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix tranformer test Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix message proto commands Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Replace string literal with constant Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove alias from main method Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Change messaging pubsub alias Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Rename occured to created Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Handle NATS connection in the NATS PubSub Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Rename n to pub/pubSub Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix typos Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
144 lines
3.7 KiB
Go
144 lines
3.7 KiB
Go
package lora
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
"github.com/mainflux/mainflux/messaging"
|
|
)
|
|
|
|
const (
|
|
protocol = "lora"
|
|
thingSuffix = "thing"
|
|
channelSuffix = "channel"
|
|
)
|
|
|
|
var (
|
|
// ErrMalformedMessage indicates malformed LoRa message.
|
|
ErrMalformedMessage = errors.New("malformed message received")
|
|
|
|
// ErrNotFoundDev indicates a non-existent route map for a device EUI.
|
|
ErrNotFoundDev = errors.New("route map not found for this device EUI")
|
|
|
|
// ErrNotFoundApp indicates a non-existent route map for an application ID.
|
|
ErrNotFoundApp = errors.New("route map not found for this application ID")
|
|
)
|
|
|
|
// Service specifies an API that must be fullfiled by the domain service
|
|
// implementation, and all of its decorators (e.g. logging & metrics).
|
|
type Service interface {
|
|
// CreateThing creates thingID:devEUI route-map
|
|
CreateThing(thingID string, devEUI string) error
|
|
|
|
// UpdateThing updates thingID:devEUI route-map
|
|
UpdateThing(thingID string, devEUI string) error
|
|
|
|
// RemoveThing removes thingID:devEUI route-map
|
|
RemoveThing(thingID string) error
|
|
|
|
// CreateChannel creates channelID:appID route-map
|
|
CreateChannel(chanID string, appID string) error
|
|
|
|
// UpdateChannel updates channelID:appID route-map
|
|
UpdateChannel(chanID string, appID string) error
|
|
|
|
// RemoveChannel removes channelID:appID route-map
|
|
RemoveChannel(chanID string) error
|
|
|
|
// Publish forwards messages from the LoRa MQTT broker to Mainflux NATS broker
|
|
Publish(ctx context.Context, token string, msg Message) error
|
|
}
|
|
|
|
var _ Service = (*adapterService)(nil)
|
|
|
|
type adapterService struct {
|
|
publisher messaging.Publisher
|
|
thingsRM RouteMapRepository
|
|
channelsRM RouteMapRepository
|
|
}
|
|
|
|
// New instantiates the LoRa adapter implementation.
|
|
func New(publisher messaging.Publisher, thingsRM, channelsRM RouteMapRepository) Service {
|
|
return &adapterService{
|
|
publisher: publisher,
|
|
thingsRM: thingsRM,
|
|
channelsRM: channelsRM,
|
|
}
|
|
}
|
|
|
|
// Publish forwards messages from Lora MQTT broker to Mainflux NATS broker
|
|
func (as *adapterService) Publish(ctx context.Context, token string, m Message) error {
|
|
// Get route map of lora application
|
|
thing, err := as.thingsRM.Get(m.DevEUI)
|
|
if err != nil {
|
|
return ErrNotFoundDev
|
|
}
|
|
|
|
// Get route map of lora application
|
|
channel, err := as.channelsRM.Get(m.ApplicationID)
|
|
if err != nil {
|
|
return ErrNotFoundApp
|
|
}
|
|
|
|
// Use the SenML message decoded on LoRa server application if
|
|
// field Object isn't empty. Otherwise, decode standard field Data.
|
|
var payload []byte
|
|
switch m.Object {
|
|
case nil:
|
|
payload, err = base64.StdEncoding.DecodeString(m.Data)
|
|
if err != nil {
|
|
return ErrMalformedMessage
|
|
}
|
|
default:
|
|
jo, err := json.Marshal(m.Object)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
payload = []byte(jo)
|
|
}
|
|
|
|
created, err := ptypes.TimestampProto(time.Now())
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
// Publish on Mainflux NATS broker
|
|
msg := messaging.Message{
|
|
Publisher: thing,
|
|
Protocol: protocol,
|
|
Channel: channel,
|
|
Payload: payload,
|
|
Created: created,
|
|
}
|
|
|
|
return as.publisher.Publish(msg.Channel, msg)
|
|
}
|
|
|
|
func (as *adapterService) CreateThing(thingID string, devEUI string) error {
|
|
return as.thingsRM.Save(thingID, devEUI)
|
|
}
|
|
|
|
func (as *adapterService) UpdateThing(thingID string, devEUI string) error {
|
|
return as.thingsRM.Save(thingID, devEUI)
|
|
}
|
|
|
|
func (as *adapterService) RemoveThing(thingID string) error {
|
|
return as.thingsRM.Remove(thingID)
|
|
}
|
|
|
|
func (as *adapterService) CreateChannel(chanID string, appID string) error {
|
|
return as.channelsRM.Save(chanID, appID)
|
|
}
|
|
|
|
func (as *adapterService) UpdateChannel(chanID string, appID string) error {
|
|
return as.channelsRM.Save(chanID, appID)
|
|
}
|
|
|
|
func (as *adapterService) RemoveChannel(chanID string) error {
|
|
return as.channelsRM.Remove(chanID)
|
|
}
|