@@ -3,19 +3,12 @@ package cmd
3
3
import (
4
4
"bytes"
5
5
"io/ioutil"
6
- "os"
7
- "os/signal"
8
- "syscall"
9
- "time"
10
6
11
7
log "github.com/sirupsen/logrus"
12
8
"github.com/spf13/cobra"
13
9
"github.com/spf13/viper"
14
10
15
- "github.com/brocaar/lora-gateway-bridge/internal/backend/mqttpubsub"
16
11
"github.com/brocaar/lora-gateway-bridge/internal/config"
17
- "github.com/brocaar/lora-gateway-bridge/internal/gateway"
18
- "github.com/brocaar/lorawan"
19
12
)
20
13
21
14
var cfgFile string // config file
@@ -25,7 +18,7 @@ var rootCmd = &cobra.Command{
25
18
Use : "lora-gateway-bridge" ,
26
19
Short : "abstracts the packet_forwarder protocol into JSON over MQTT" ,
27
20
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
29
22
> source & copyright information: https://github.com/brocaar/lora-gateway-bridge` ,
30
23
RunE : run ,
31
24
}
@@ -83,6 +76,7 @@ func init() {
83
76
viper .SetDefault ("backend.mqtt.downlink_topic_template" , "gateway/{{ .MAC }}/tx" )
84
77
viper .SetDefault ("backend.mqtt.stats_topic_template" , "gateway/{{ .MAC }}/stats" )
85
78
viper .SetDefault ("backend.mqtt.ack_topic_template" , "gateway/{{ .MAC }}/ack" )
79
+ viper .SetDefault ("backend.mqtt.config_topic_template" , "gateway/{{ .MAC }}/config" )
86
80
viper .SetDefault ("backend.mqtt.server" , "tcp://127.0.0.1:1883" )
87
81
viper .SetDefault ("backend.mqtt.clean_session" , true )
88
82
@@ -98,80 +92,6 @@ func Execute(v string) {
98
92
}
99
93
}
100
94
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
-
175
95
func initConfig () {
176
96
if cfgFile != "" {
177
97
b , err := ioutil .ReadFile (cfgFile )
@@ -200,4 +120,10 @@ func initConfig() {
200
120
if err := viper .Unmarshal (& config .C ); err != nil {
201
121
log .WithError (err ).Fatal ("unmarshal config error" )
202
122
}
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
+ }
203
129
}
0 commit comments