Skip to content

Commit ab7647a

Browse files
authored
Merge pull request #89 from operable/nmohoric/mqtt-config-fixes
mqtt config fixes
2 parents 21861d5 + 09c1dc0 commit ab7647a

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

relay/bus/mqtt.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ func (mqc *MQTTConnection) Connect(options ConnectionOptions) error {
2929
compressed := snappy.Encode(nil, []byte(options.OnDisconnect.Body))
3030
mqttOpts.SetWill(options.OnDisconnect.Topic, string(compressed), 1, false)
3131
}
32+
if options.EventsHandler != nil && options.AutoReconnect == true {
33+
mqttOpts.OnConnect = func(c *mqtt.Client) {
34+
mqc.conn = c
35+
mqc.options.EventsHandler(mqc, ConnectedEvent)
36+
}
37+
}
3238
mqc.backoff = NewBackoff()
3339
mqc.conn = mqtt.NewClient(mqttOpts)
3440
for {
@@ -41,7 +47,7 @@ func (mqc *MQTTConnection) Connect(options ConnectionOptions) error {
4147
}
4248
}
4349
mqc.options = options
44-
if mqc.options.EventsHandler != nil {
50+
if mqc.options.EventsHandler != nil && mqc.options.AutoReconnect != true {
4551
mqc.options.EventsHandler(mqc, ConnectedEvent)
4652
}
4753
return nil
@@ -77,7 +83,7 @@ func (mqc *MQTTConnection) Subscribe(topic string, handler SubscriptionHandler)
7783
return token.Error()
7884
}
7985

80-
func (mqc *MQTTConnection) disconnected(cilent *mqtt.Client, err error) {
86+
func (mqc *MQTTConnection) disconnected(client *mqtt.Client, err error) {
8187
log.Errorf("MQTT connection failed: %s.", err)
8288
for {
8389
if token := mqc.conn.Connect(); token.Wait() && token.Error() != nil {

relay/relay.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,10 @@ func (r *cogRelay) handleBusEvents(conn bus.Connection, event bus.Event) {
147147
}
148148
if r.catalog.Len() > 0 {
149149
r.catalog.Reconnected()
150+
} else {
151+
log.Info("Loading bundle catalog.")
152+
r.requestBundles()
150153
}
151-
log.Info("Loading bundle catalog.")
152-
r.requestBundles()
153154
}
154155
}
155156

0 commit comments

Comments
 (0)