1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Mainflux.mainflux/lora/service.go
Manuel Imperiale fff492bd50
NOISSUE - Create broker package for NATS (#1080)
* NOISSUEE - Create broker package for NATS

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Create funcs to return NATS connection

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* mv os.exit to main

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix Reviews

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix tests and typos

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix CI

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix reviews

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Unify Publisher and Subscriber interfaces

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Rename Nats interface

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* typo

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Mv message.pb.go, messsage.proto and topics.go to broker directory

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix go.mod

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Use mainflux broker for writers and twins services

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix go.mod

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix twins tests

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix make proto

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix message.proto

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix golangcibot

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* regenerate message.pb.go

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix comment

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix comment

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix make proto

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add NATS errors

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
2020-04-01 21:22:13 +02:00

141 lines
3.8 KiB
Go

package lora
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"github.com/mainflux/mainflux/broker"
)
const (
protocol = "lora"
thingSuffix = "thing"
channelSuffix = "channel"
)
var (
// ErrMalformedIdentity indicates malformed identity received (e.g.
// invalid appID or deviceEUI).
ErrMalformedIdentity = errors.New("malformed identity received")
// 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 thing mfx:lora & lora:mfx route-map
CreateThing(string, string) error
// UpdateThing updates thing mfx:lora & lora:mfx route-map
UpdateThing(string, string) error
// RemoveThing removes thing mfx:lora & lora:mfx route-map
RemoveThing(string) error
// CreateChannel creates channel mfx:lora & lora:mfx route-map
CreateChannel(string, string) error
// UpdateChannel updates mfx:lora & lora:mfx route-map
UpdateChannel(string, string) error
// RemoveChannel removes channel mfx:lora & lora:mfx route-map
RemoveChannel(string) error
// Publish forwards messages from the LoRa MQTT broker to Mainflux NATS broker
Publish(context.Context, string, Message) error
}
var _ Service = (*adapterService)(nil)
type adapterService struct {
broker broker.Nats
thingsRM RouteMapRepository
channelsRM RouteMapRepository
}
// New instantiates the LoRa adapter implementation.
func New(broker broker.Nats, thingsRM, channelsRM RouteMapRepository) Service {
return &adapterService{
broker: broker,
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)
}
// Publish on Mainflux NATS broker
msg := broker.Message{
Publisher: thing,
Protocol: protocol,
ContentType: "Content-Type",
Channel: channel,
Payload: payload,
}
return as.broker.Publish(ctx, token, msg)
}
func (as *adapterService) CreateThing(mfxDevID string, loraDevEUI string) error {
return as.thingsRM.Save(mfxDevID, loraDevEUI)
}
func (as *adapterService) UpdateThing(mfxDevID string, loraDevEUI string) error {
return as.thingsRM.Save(mfxDevID, loraDevEUI)
}
func (as *adapterService) RemoveThing(mfxDevID string) error {
return as.thingsRM.Remove(mfxDevID)
}
func (as *adapterService) CreateChannel(mfxChanID string, loraAppID string) error {
return as.channelsRM.Save(mfxChanID, loraAppID)
}
func (as *adapterService) UpdateChannel(mfxChanID string, loraAppID string) error {
return as.channelsRM.Save(mfxChanID, loraAppID)
}
func (as *adapterService) RemoveChannel(mfxChanID string) error {
return as.channelsRM.Remove(mfxChanID)
}