221 lines
6.4 KiB
Go
221 lines
6.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"strconv"
|
|
"syscall"
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
r "github.com/go-redis/redis"
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/logger"
|
|
"github.com/mainflux/mainflux/lora"
|
|
"github.com/mainflux/mainflux/lora/api"
|
|
pub "github.com/mainflux/mainflux/lora/nats"
|
|
mqttBroker "github.com/mainflux/mainflux/lora/paho"
|
|
|
|
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
|
|
"github.com/mainflux/mainflux/lora/redis"
|
|
nats "github.com/nats-io/go-nats"
|
|
stdprometheus "github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
const (
|
|
defHTTPPort = "8180"
|
|
defLoraMsgURL = "tcp://localhost:1883"
|
|
defNatsURL = nats.DefaultURL
|
|
defLogLevel = "error"
|
|
defESURL = "localhost:6379"
|
|
defESPass = ""
|
|
defESDB = "0"
|
|
defInstanceName = "lora"
|
|
defRouteMapURL = "localhost:6379"
|
|
defRouteMapPass = ""
|
|
defRouteMapDB = "0"
|
|
|
|
envHTTPPort = "MF_LORA_ADAPTER_HTTP_PORT"
|
|
envLoraMsgURL = "MF_LORA_ADAPTER_MESSAGES_URL"
|
|
envNatsURL = "MF_NATS_URL"
|
|
envLogLevel = "MF_LORA_ADAPTER_LOG_LEVEL"
|
|
envESURL = "MF_THINGS_ES_URL"
|
|
envESPass = "MF_THINGS_ES_PASS"
|
|
envESDB = "MF_THINGS_ES_DB"
|
|
envInstanceName = "MF_LORA_ADAPTER_INSTANCE_NAME"
|
|
envRouteMapURL = "MF_LORA_ADAPTER_ROUTEMAP_URL"
|
|
envRouteMapPass = "MF_LORA_ADAPTER_ROUTEMAP_PASS"
|
|
envRouteMapDB = "MF_LORA_ADAPTER_ROUTEMAP_DB"
|
|
|
|
loraServerTopic = "application/+/device/+/rx"
|
|
|
|
thingsRMPrefix = "thing"
|
|
channelsRMPrefix = "channel"
|
|
)
|
|
|
|
type config struct {
|
|
httpPort string
|
|
loraMsgURL string
|
|
natsURL string
|
|
logLevel string
|
|
esURL string
|
|
esPass string
|
|
esDB string
|
|
instanceName string
|
|
routeMapURL string
|
|
routeMapPass string
|
|
routeMapDB string
|
|
}
|
|
|
|
func main() {
|
|
cfg := loadConfig()
|
|
|
|
logger, err := logger.New(os.Stdout, cfg.logLevel)
|
|
if err != nil {
|
|
log.Fatalf(err.Error())
|
|
}
|
|
|
|
natsConn := connectToNATS(cfg.natsURL, logger)
|
|
defer natsConn.Close()
|
|
|
|
rmConn := connectToRedis(cfg.routeMapURL, cfg.routeMapPass, cfg.routeMapDB, logger)
|
|
defer rmConn.Close()
|
|
|
|
esConn := connectToRedis(cfg.esURL, cfg.esPass, cfg.esDB, logger)
|
|
defer esConn.Close()
|
|
|
|
publisher := pub.NewMessagePublisher(natsConn)
|
|
|
|
thingRM := newRouteMapRepositoy(rmConn, thingsRMPrefix, logger)
|
|
chanRM := newRouteMapRepositoy(rmConn, channelsRMPrefix, logger)
|
|
|
|
mqttConn := connectToMQTTBroker(cfg.loraMsgURL, logger)
|
|
|
|
svc := lora.New(publisher, thingRM, chanRM)
|
|
svc = api.LoggingMiddleware(svc, logger)
|
|
svc = api.MetricsMiddleware(
|
|
svc,
|
|
kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
|
|
Namespace: "lora_adapter",
|
|
Subsystem: "api",
|
|
Name: "request_count",
|
|
Help: "Number of requests received.",
|
|
}, []string{"method"}),
|
|
kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
|
|
Namespace: "lora_adapter",
|
|
Subsystem: "api",
|
|
Name: "request_latency_microseconds",
|
|
Help: "Total duration of requests in microseconds.",
|
|
}, []string{"method"}),
|
|
)
|
|
|
|
go subscribeToLoRaBroker(svc, mqttConn, logger)
|
|
go subscribeToThingsES(svc, esConn, cfg.instanceName, logger)
|
|
|
|
errs := make(chan error, 2)
|
|
|
|
go startHTTPServer(cfg, logger, errs)
|
|
|
|
go func() {
|
|
c := make(chan os.Signal)
|
|
signal.Notify(c, syscall.SIGINT)
|
|
errs <- fmt.Errorf("%s", <-c)
|
|
}()
|
|
|
|
err = <-errs
|
|
logger.Error(fmt.Sprintf("LoRa adapter terminated: %s", err))
|
|
}
|
|
|
|
func loadConfig() config {
|
|
return config{
|
|
httpPort: mainflux.Env(envHTTPPort, defHTTPPort),
|
|
loraMsgURL: mainflux.Env(envLoraMsgURL, defLoraMsgURL),
|
|
natsURL: mainflux.Env(envNatsURL, defNatsURL),
|
|
logLevel: mainflux.Env(envLogLevel, defLogLevel),
|
|
esURL: mainflux.Env(envESURL, defESURL),
|
|
esPass: mainflux.Env(envESPass, defESPass),
|
|
esDB: mainflux.Env(envESDB, defESDB),
|
|
instanceName: mainflux.Env(envInstanceName, defInstanceName),
|
|
routeMapURL: mainflux.Env(envRouteMapURL, defRouteMapURL),
|
|
routeMapPass: mainflux.Env(envRouteMapPass, defRouteMapPass),
|
|
routeMapDB: mainflux.Env(envRouteMapDB, defRouteMapDB),
|
|
}
|
|
}
|
|
|
|
func connectToNATS(url string, logger logger.Logger) *nats.Conn {
|
|
conn, err := nats.Connect(url)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Failed to connect to NATS: %s", err))
|
|
os.Exit(1)
|
|
}
|
|
|
|
logger.Info("Connected to NATS")
|
|
return conn
|
|
}
|
|
|
|
func connectToMQTTBroker(loraURL string, logger logger.Logger) mqtt.Client {
|
|
opts := mqtt.NewClientOptions()
|
|
opts.AddBroker(loraURL)
|
|
opts.SetUsername("")
|
|
opts.SetPassword("")
|
|
opts.SetOnConnectHandler(func(c mqtt.Client) {
|
|
logger.Info("Connected to Lora MQTT broker")
|
|
})
|
|
opts.SetConnectionLostHandler(func(c mqtt.Client, err error) {
|
|
logger.Error(fmt.Sprintf("MQTT connection lost: %s", err.Error()))
|
|
os.Exit(1)
|
|
})
|
|
|
|
client := mqtt.NewClient(opts)
|
|
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
|
logger.Error(fmt.Sprintf("Failed to connect to Lora MQTT broker: %s", token.Error()))
|
|
os.Exit(1)
|
|
}
|
|
|
|
return client
|
|
}
|
|
|
|
func connectToRedis(redisURL, redisPass, redisDB string, logger logger.Logger) *r.Client {
|
|
db, err := strconv.Atoi(redisDB)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Failed to connect to redis: %s", err))
|
|
os.Exit(1)
|
|
}
|
|
|
|
return r.NewClient(&r.Options{
|
|
Addr: redisURL,
|
|
Password: redisPass,
|
|
DB: db,
|
|
})
|
|
}
|
|
|
|
func subscribeToLoRaBroker(svc lora.Service, mc mqtt.Client, logger logger.Logger) {
|
|
mqttBroker := mqttBroker.NewBroker(svc, mc, logger)
|
|
logger.Info("Subscribed to Lora MQTT broker")
|
|
if err := mqttBroker.Subscribe(loraServerTopic); err != nil {
|
|
logger.Error(fmt.Sprintf("Failed to subscribe to Lora MQTT broker: %s", err))
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func subscribeToThingsES(svc lora.Service, client *r.Client, consumer string, logger logger.Logger) {
|
|
eventStore := redis.NewEventStore(svc, client, consumer, logger)
|
|
logger.Info("Subscribed to Redis Event Store")
|
|
if err := eventStore.Subscribe("mainflux.things"); err != nil {
|
|
logger.Warn(fmt.Sprintf("Lora-adapter service failed to subscribe to event sourcing: %s", err))
|
|
}
|
|
}
|
|
|
|
func newRouteMapRepositoy(client *r.Client, prefix string, logger logger.Logger) lora.RouteMapRepository {
|
|
logger.Info("Connected to Redis Route map")
|
|
return redis.NewRouteMapRepository(client, prefix)
|
|
}
|
|
|
|
func startHTTPServer(cfg config, logger logger.Logger, errs chan error) {
|
|
p := fmt.Sprintf(":%s", cfg.httpPort)
|
|
logger.Info(fmt.Sprintf("Lora-adapter service started, exposed port %s", cfg.httpPort))
|
|
errs <- http.ListenAndServe(p, api.MakeHandler())
|
|
}
|