Skip to content

Commit 1d5af29

Browse files
committed
Implement managed pf configuration.
1 parent c36b99a commit 1d5af29

File tree

16 files changed

+1974
-744
lines changed

16 files changed

+1974
-744
lines changed

Gopkg.lock

Lines changed: 4 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
[[constraint]]
55
name = "github.com/brocaar/loraserver"
6-
version = "0.23.2"
6+
branch = "gw_config"
77

88
[[constraint]]
99
branch = "master"

cmd/lora-gateway-bridge/cmd/configfile.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,41 @@ udp_bind = "{{ .PacketForwarder.UDPBind }}"
3232
skip_crc_check = {{ .PacketForwarder.SkipCRCCheck }}
3333
3434
35+
# # Managed packet-forwarder configuration.
36+
# #
37+
# # By configuring one or multiple managed packet-forwarder sections, the
38+
# # LoRa Gateway Bridge updates the configuration when the backend receives
39+
# # a configuration change, after which it will restart the packet-forwarder.
40+
# [[packet_forwarder.configuration]]
41+
# # Gateway MAC.
42+
# #
43+
# # The LoRa Gateway Bridge will only apply the configuration updates for this
44+
# # gateway MAC.
45+
# mac="0102030405060708"
46+
47+
# # Base configuration file.
48+
# #
49+
# # This file will be used as base-configuration and will not be overwritten on
50+
# # a configuration update. This file needs to exist and contains the base
51+
# # configuration and vendor specific
52+
# base_file="/etc/lora-packet-forwarder/global_conf.json"
53+
54+
# # Output configuration file.
55+
# #
56+
# # This will be the final configuration for the packet-forwarder, containing
57+
# # a merged version of the base configuration + the requested configuration
58+
# # update.
59+
# # Warning: this file will be overwritten on a configuration update!
60+
# output_file="/etc/lora-packet-forwarder/local_conf.json"
61+
62+
# # Restart command.
63+
# #
64+
# # This command is issued by the LoRa Gateway Bridge on a configuration
65+
# # change. Make sure the LoRa Gateway Bridge process has sufficient
66+
# # permissions to execute this command.
67+
# restart_command="/etc/init.d/lora-packet-forwarder restart"
68+
69+
3570
# Configuration for the MQTT backend.
3671
[backend.mqtt]
3772
# MQTT topic templates for the different MQTT topics.
@@ -47,6 +82,7 @@ uplink_topic_template="{{ .Backend.MQTT.UplinkTopicTemplate }}"
4782
downlink_topic_template="{{ .Backend.MQTT.DownlinkTopicTemplate }}"
4883
stats_topic_template="{{ .Backend.MQTT.StatsTopicTemplate }}"
4984
ack_topic_template="{{ .Backend.MQTT.AckTopicTemplate }}"
85+
config_topic_template="{{ .Backend.MQTT.ConfigTopicTemplate }}"
5086
5187
# MQTT server (e.g. scheme://host:port where scheme is tcp, ssl or ws)
5288
server="{{ .Backend.MQTT.Server }}"

cmd/lora-gateway-bridge/cmd/root.go

Lines changed: 8 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,12 @@ package cmd
33
import (
44
"bytes"
55
"io/ioutil"
6-
"os"
7-
"os/signal"
8-
"syscall"
9-
"time"
106

117
log "github.com/sirupsen/logrus"
128
"github.com/spf13/cobra"
139
"github.com/spf13/viper"
1410

15-
"github.com/brocaar/lora-gateway-bridge/internal/backend/mqttpubsub"
1611
"github.com/brocaar/lora-gateway-bridge/internal/config"
17-
"github.com/brocaar/lora-gateway-bridge/internal/gateway"
18-
"github.com/brocaar/lorawan"
1912
)
2013

2114
var cfgFile string // config file
@@ -25,7 +18,7 @@ var rootCmd = &cobra.Command{
2518
Use: "lora-gateway-bridge",
2619
Short: "abstracts the packet_forwarder protocol into JSON over MQTT",
2720
Long: `LoRa Gateway Bridge abstracts the packet_forwarder protocol into JSON over MQTT
28-
> documentation & support: https://docs.loraserver.io/lora-gateway-bridge
21+
> documentation & support: https://www.loraserver.io/lora-gateway-bridge
2922
> source & copyright information: https://github.com/brocaar/lora-gateway-bridge`,
3023
RunE: run,
3124
}
@@ -83,6 +76,7 @@ func init() {
8376
viper.SetDefault("backend.mqtt.downlink_topic_template", "gateway/{{ .MAC }}/tx")
8477
viper.SetDefault("backend.mqtt.stats_topic_template", "gateway/{{ .MAC }}/stats")
8578
viper.SetDefault("backend.mqtt.ack_topic_template", "gateway/{{ .MAC }}/ack")
79+
viper.SetDefault("backend.mqtt.config_topic_template", "gateway/{{ .MAC }}/config")
8680
viper.SetDefault("backend.mqtt.server", "tcp://127.0.0.1:1883")
8781
viper.SetDefault("backend.mqtt.clean_session", true)
8882

@@ -98,80 +92,6 @@ func Execute(v string) {
9892
}
9993
}
10094

101-
func run(cmd *cobra.Command, args []string) error {
102-
log.SetLevel(log.Level(uint8(config.C.General.LogLevel)))
103-
104-
log.WithFields(log.Fields{
105-
"version": version,
106-
"docs": "https://docs.loraserver.io/lora-gateway-bridge/",
107-
}).Info("starting LoRa Gateway Bridge")
108-
109-
var pubsub *mqttpubsub.Backend
110-
for {
111-
var err error
112-
pubsub, err = mqttpubsub.NewBackend(config.C.Backend.MQTT)
113-
if err == nil {
114-
break
115-
}
116-
117-
log.Errorf("could not setup mqtt backend, retry in 2 seconds: %s", err)
118-
time.Sleep(2 * time.Second)
119-
}
120-
defer pubsub.Close()
121-
122-
onNew := func(mac lorawan.EUI64) error {
123-
return pubsub.SubscribeGatewayTX(mac)
124-
}
125-
126-
onDelete := func(mac lorawan.EUI64) error {
127-
return pubsub.UnSubscribeGatewayTX(mac)
128-
}
129-
130-
gw, err := gateway.NewBackend(config.C.PacketForwarder.UDPBind, onNew, onDelete, config.C.PacketForwarder.SkipCRCCheck)
131-
if err != nil {
132-
log.Fatalf("could not setup gateway backend: %s", err)
133-
}
134-
defer gw.Close()
135-
136-
go func() {
137-
for rxPacket := range gw.RXPacketChan() {
138-
if err := pubsub.PublishGatewayRX(rxPacket.RXInfo.MAC, rxPacket); err != nil {
139-
log.Errorf("could not publish RXPacket: %s", err)
140-
}
141-
}
142-
}()
143-
144-
go func() {
145-
for stats := range gw.StatsChan() {
146-
if err := pubsub.PublishGatewayStats(stats.MAC, stats); err != nil {
147-
log.Errorf("could not publish GatewayStatsPacket: %s", err)
148-
}
149-
}
150-
}()
151-
152-
go func() {
153-
for txPacket := range pubsub.TXPacketChan() {
154-
if err := gw.Send(txPacket); err != nil {
155-
log.Errorf("could not send TXPacket: %s", err)
156-
}
157-
}
158-
}()
159-
160-
go func() {
161-
for txAck := range gw.TXAckChan() {
162-
if err := pubsub.PublishGatewayTXAck(txAck.MAC, txAck); err != nil {
163-
log.Errorf("could not publish TXAck: %s", err)
164-
}
165-
}
166-
}()
167-
168-
sigChan := make(chan os.Signal)
169-
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
170-
log.WithField("signal", <-sigChan).Info("signal received")
171-
log.Warning("shutting down server")
172-
return nil
173-
}
174-
17595
func initConfig() {
17696
if cfgFile != "" {
17797
b, err := ioutil.ReadFile(cfgFile)
@@ -200,4 +120,10 @@ func initConfig() {
200120
if err := viper.Unmarshal(&config.C); err != nil {
201121
log.WithError(err).Fatal("unmarshal config error")
202122
}
123+
124+
for i := range config.C.PacketForwarder.Configuration {
125+
if err := config.C.PacketForwarder.Configuration[i].MAC.UnmarshalText([]byte(config.C.PacketForwarder.Configuration[i].MACString)); err != nil {
126+
log.WithError(err).Fatal("unmarshal config error")
127+
}
128+
}
203129
}

0 commit comments

Comments
 (0)