2018-12-04 20:08:30 +01:00
|
|
|
package lora
|
|
|
|
|
|
|
|
import (
|
2019-07-18 15:01:09 +02:00
|
|
|
"context"
|
2018-12-04 20:08:30 +01:00
|
|
|
"encoding/base64"
|
2019-02-22 15:30:37 +01:00
|
|
|
"encoding/json"
|
2018-12-04 20:08:30 +01:00
|
|
|
"errors"
|
2020-04-13 15:05:05 +02:00
|
|
|
"time"
|
2018-12-04 20:08:30 +01:00
|
|
|
|
2020-06-03 15:16:19 +02:00
|
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
2018-12-04 20:08:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
2018-12-05 15:15:15 +01:00
|
|
|
ErrNotFoundDev = errors.New("route map not found for this device EUI")
|
2018-12-04 20:08:30 +01:00
|
|
|
|
|
|
|
// ErrNotFoundApp indicates a non-existent route map for an application ID.
|
2018-12-05 15:15:15 +01:00
|
|
|
ErrNotFoundApp = errors.New("route map not found for this application ID")
|
2018-12-04 20:08:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2020-04-05 13:15:47 +02:00
|
|
|
// CreateThing creates thingID:devEUI route-map
|
|
|
|
CreateThing(thingID string, devEUI string) error
|
2018-12-04 20:08:30 +01:00
|
|
|
|
2020-04-05 13:15:47 +02:00
|
|
|
// UpdateThing updates thingID:devEUI route-map
|
|
|
|
UpdateThing(thingID string, devEUI string) error
|
2018-12-04 20:08:30 +01:00
|
|
|
|
2020-04-05 13:15:47 +02:00
|
|
|
// RemoveThing removes thingID:devEUI route-map
|
|
|
|
RemoveThing(thingID string) error
|
2018-12-04 20:08:30 +01:00
|
|
|
|
2020-04-05 13:15:47 +02:00
|
|
|
// CreateChannel creates channelID:appID route-map
|
|
|
|
CreateChannel(chanID string, appID string) error
|
2018-12-04 20:08:30 +01:00
|
|
|
|
2020-04-05 13:15:47 +02:00
|
|
|
// UpdateChannel updates channelID:appID route-map
|
|
|
|
UpdateChannel(chanID string, appID string) error
|
2018-12-04 20:08:30 +01:00
|
|
|
|
2020-04-05 13:15:47 +02:00
|
|
|
// RemoveChannel removes channelID:appID route-map
|
|
|
|
RemoveChannel(chanID string) error
|
2018-12-04 20:08:30 +01:00
|
|
|
|
|
|
|
// Publish forwards messages from the LoRa MQTT broker to Mainflux NATS broker
|
2020-04-05 13:15:47 +02:00
|
|
|
Publish(ctx context.Context, token string, msg Message) error
|
2018-12-04 20:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Service = (*adapterService)(nil)
|
|
|
|
|
|
|
|
type adapterService struct {
|
2020-04-28 11:02:35 +02:00
|
|
|
publisher messaging.Publisher
|
2018-12-04 20:08:30 +01:00
|
|
|
thingsRM RouteMapRepository
|
|
|
|
channelsRM RouteMapRepository
|
|
|
|
}
|
|
|
|
|
|
|
|
// New instantiates the LoRa adapter implementation.
|
2020-04-28 11:02:35 +02:00
|
|
|
func New(publisher messaging.Publisher, thingsRM, channelsRM RouteMapRepository) Service {
|
2018-12-04 20:08:30 +01:00
|
|
|
return &adapterService{
|
2020-04-28 11:02:35 +02:00
|
|
|
publisher: publisher,
|
2018-12-04 20:08:30 +01:00
|
|
|
thingsRM: thingsRM,
|
|
|
|
channelsRM: channelsRM,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish forwards messages from Lora MQTT broker to Mainflux NATS broker
|
2019-07-18 15:01:09 +02:00
|
|
|
func (as *adapterService) Publish(ctx context.Context, token string, m Message) error {
|
2018-12-04 20:08:30 +01:00
|
|
|
// Get route map of lora application
|
2018-12-05 13:09:25 +01:00
|
|
|
thing, err := as.thingsRM.Get(m.DevEUI)
|
2018-12-04 20:08:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return ErrNotFoundDev
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get route map of lora application
|
2018-12-05 13:09:25 +01:00
|
|
|
channel, err := as.channelsRM.Get(m.ApplicationID)
|
2018-12-04 20:08:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return ErrNotFoundApp
|
|
|
|
}
|
|
|
|
|
2019-02-22 09:31:44 +01:00
|
|
|
// 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 {
|
2019-02-22 15:30:37 +01:00
|
|
|
case nil:
|
2019-02-22 09:31:44 +01:00
|
|
|
payload, err = base64.StdEncoding.DecodeString(m.Data)
|
|
|
|
if err != nil {
|
|
|
|
return ErrMalformedMessage
|
|
|
|
}
|
|
|
|
default:
|
2019-02-22 15:30:37 +01:00
|
|
|
jo, err := json.Marshal(m.Object)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
payload = []byte(jo)
|
2018-12-04 20:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Publish on Mainflux NATS broker
|
2020-04-28 11:02:35 +02:00
|
|
|
msg := messaging.Message{
|
2020-04-21 19:33:11 +02:00
|
|
|
Publisher: thing,
|
|
|
|
Protocol: protocol,
|
|
|
|
Channel: channel,
|
|
|
|
Payload: payload,
|
2020-05-04 13:14:06 +02:00
|
|
|
Created: time.Now().UnixNano(),
|
2018-12-04 20:08:30 +01:00
|
|
|
}
|
|
|
|
|
2020-04-28 11:02:35 +02:00
|
|
|
return as.publisher.Publish(msg.Channel, msg)
|
2018-12-04 20:08:30 +01:00
|
|
|
}
|
|
|
|
|
2020-04-05 13:15:47 +02:00
|
|
|
func (as *adapterService) CreateThing(thingID string, devEUI string) error {
|
|
|
|
return as.thingsRM.Save(thingID, devEUI)
|
2018-12-04 20:08:30 +01:00
|
|
|
}
|
|
|
|
|
2020-04-05 13:15:47 +02:00
|
|
|
func (as *adapterService) UpdateThing(thingID string, devEUI string) error {
|
|
|
|
return as.thingsRM.Save(thingID, devEUI)
|
2018-12-04 20:08:30 +01:00
|
|
|
}
|
|
|
|
|
2020-04-05 13:15:47 +02:00
|
|
|
func (as *adapterService) RemoveThing(thingID string) error {
|
|
|
|
return as.thingsRM.Remove(thingID)
|
2018-12-04 20:08:30 +01:00
|
|
|
}
|
|
|
|
|
2020-04-05 13:15:47 +02:00
|
|
|
func (as *adapterService) CreateChannel(chanID string, appID string) error {
|
|
|
|
return as.channelsRM.Save(chanID, appID)
|
2018-12-04 20:08:30 +01:00
|
|
|
}
|
|
|
|
|
2020-04-05 13:15:47 +02:00
|
|
|
func (as *adapterService) UpdateChannel(chanID string, appID string) error {
|
|
|
|
return as.channelsRM.Save(chanID, appID)
|
2018-12-04 20:08:30 +01:00
|
|
|
}
|
|
|
|
|
2020-04-05 13:15:47 +02:00
|
|
|
func (as *adapterService) RemoveChannel(chanID string) error {
|
|
|
|
return as.channelsRM.Remove(chanID)
|
2018-12-04 20:08:30 +01:00
|
|
|
}
|