Skip to content

Commit 68ed758

Browse files
committed
Make it possible to disable state retained flag.
1 parent 08dc3c9 commit 68ed758

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,22 @@ marshaler="{{ .Integration.Marshaler }}"
223223
224224
# State topic template.
225225
#
226-
# States are sent by the gateway as retained MQTT messages so that the last
227-
# message will be stored by the MQTT broker. When set to a blank string, this
228-
# feature will be disabled. This feature is only supported when using the
229-
# generic authentication type.
226+
# States are sent by the gateway as retained MQTT messages (by default)
227+
# so that the last message will be stored by the MQTT broker. When set to
228+
# a blank string, this feature will be disabled. This feature is only
229+
# supported when using the generic authentication type.
230230
state_topic_template="{{ .Integration.MQTT.StateTopicTemplate }}"
231231
232232
# Command topic template.
233233
command_topic_template="{{ .Integration.MQTT.CommandTopicTemplate }}"
234234
235+
# State retained.
236+
#
237+
# By default this value is set to true and states are published as retained
238+
# MQTT messages. Setting this to false means that states will not be retained
239+
# by the MQTT broker.
240+
state_retained={{ .Integration.MQTT.StateRetained }}
241+
235242
# Keep alive will set the amount of time (in seconds) that the client should
236243
# wait before sending a PING request to the broker. This will allow the client
237244
# to know that a connection has not been lost with the server.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func init() {
5959
viper.SetDefault("integration.mqtt.event_topic_template", "gateway/{{ .GatewayID }}/event/{{ .EventType }}")
6060
viper.SetDefault("integration.mqtt.state_topic_template", "gateway/{{ .GatewayID }}/state/{{ .StateType }}")
6161
viper.SetDefault("integration.mqtt.command_topic_template", "gateway/{{ .GatewayID }}/command/#")
62+
viper.SetDefault("integration.mqtt.state_retained", true)
6263
viper.SetDefault("integration.mqtt.keep_alive", 30*time.Second)
6364
viper.SetDefault("integration.mqtt.max_reconnect_interval", time.Minute)
6465

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type Config struct {
5959
EventTopicTemplate string `mapstructure:"event_topic_template"`
6060
CommandTopicTemplate string `mapstructure:"command_topic_template"`
6161
StateTopicTemplate string `mapstructure:"state_topic_template"`
62+
StateRetained bool `mapstructure:"state_retained"`
6263
KeepAlive time.Duration `mapstructure:"keep_alive"`
6364
MaxReconnectInterval time.Duration `mapstructure:"max_reconnect_interval"`
6465
TerminateOnConnectError bool `mapstructure:"terminate_on_connect_error"`

internal/integration/mqtt/backend.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Backend struct {
4040
gatewaysSubscribedMux sync.Mutex
4141
gatewaysSubscribed map[lorawan.EUI64]struct{}
4242
terminateOnConnectError bool
43+
stateRetained bool
4344

4445
qos uint8
4546
eventTopicTemplate *template.Template
@@ -60,6 +61,7 @@ func NewBackend(conf config.Config) (*Backend, error) {
6061
clientOpts: paho.NewClientOptions(),
6162
gateways: make(map[lorawan.EUI64]struct{}),
6263
gatewaysSubscribed: make(map[lorawan.EUI64]struct{}),
64+
stateRetained: conf.Integration.MQTT.StateRetained,
6365
}
6466

6567
switch conf.Integration.MQTT.Auth.Type {
@@ -349,7 +351,7 @@ func (b *Backend) PublishState(gatewayID lorawan.EUI64, state string, v proto.Me
349351
"state": state,
350352
"gateway_id": gatewayID,
351353
}).Info("integration/mqtt: publishing state")
352-
if token := b.conn.Publish(topic.String(), b.qos, true, bytes); token.Wait() && token.Error() != nil {
354+
if token := b.conn.Publish(topic.String(), b.qos, b.stateRetained, bytes); token.Wait() && token.Error() != nil {
353355
return token.Error()
354356
}
355357
return nil

internal/integration/mqtt/backend_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func (ts *MQTTBackendTestSuite) SetupSuite() {
5757
conf.Integration.MQTT.EventTopicTemplate = "gateway/{{ .GatewayID }}/event/{{ .EventType }}"
5858
conf.Integration.MQTT.StateTopicTemplate = "gateway/{{ .GatewayID }}/state/{{ .StateType }}"
5959
conf.Integration.MQTT.CommandTopicTemplate = "gateway/{{ .GatewayID }}/command/#"
60+
conf.Integration.MQTT.StateRetained = true
6061
conf.Integration.MQTT.Auth.Type = "generic"
6162
conf.Integration.MQTT.Auth.Generic.Servers = []string{server}
6263
conf.Integration.MQTT.Auth.Generic.Username = username

0 commit comments

Comments
 (0)