Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 4e08226

Browse files
committed
Resuscribe to topics after successfull reconnection
1 parent 52059e4 commit 4e08226

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func register(config Config, token string) {
8686

8787
// Connect to MQTT and communicate back
8888
fmt.Println("Check successful mqtt connection")
89-
client, err := setupMQTTConnection("certificate.pem", "certificate.key", config.ID, config.URL)
89+
client, err := setupMQTTConnection("certificate.pem", "certificate.key", config.ID, config.URL, nil)
9090
check(err, "ConnectMQTT")
9191

9292
err = registerDevice(client, config.ID)

main.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,20 @@ func (p program) run() {
109109
log.Fatal("NATS server not redy for connections!")
110110
}
111111

112+
// Create global status
113+
status := NewStatus(p.Config.ID, nil)
114+
112115
// Setup MQTT connection
113-
mqttClient, err := setupMQTTConnection("certificate.pem", "certificate.key", p.Config.ID, p.Config.URL)
116+
mqttClient, err := setupMQTTConnection("certificate.pem", "certificate.key", p.Config.ID, p.Config.URL, status)
117+
114118
if err != nil {
115119
// if installing in a chroot the paths may be wrong and the installer may fail.
116120
// Don't report it as an error
117121
os.Exit(0)
118122
}
119123
log.Println("Connected to MQTT")
120124

121-
// Create global status
122-
status := NewStatus(p.Config.ID, mqttClient)
125+
status.mqttClient = mqttClient
123126

124127
if p.listenFile != "" {
125128
go tailAndReport(p.listenFile, status)
@@ -133,11 +136,6 @@ func (p program) run() {
133136
// wipe the thing shadows
134137
mqttClient.Publish("$aws/things/"+p.Config.ID+"/shadow/delete", 1, false, "")
135138

136-
// Subscribe to topics endpoint
137-
mqttClient.Subscribe("$aws/things/"+p.Config.ID+"/status/post", 1, StatusCB(status))
138-
mqttClient.Subscribe("$aws/things/"+p.Config.ID+"/upload/post", 1, UploadCB(status))
139-
mqttClient.Subscribe("$aws/things/"+p.Config.ID+"/sketch/post", 1, SketchCB(status))
140-
141139
sketchFolder, err := GetSketchFolder()
142140
// Export LD_LIBRARY_PATH to local lib subfolder
143141
// This way any external library can be safely copied there and the sketch should run anyway
@@ -163,6 +161,16 @@ func (p program) run() {
163161
select {}
164162
}
165163

164+
func subscribeTopics(mqttClient mqtt.Client, id string, status *Status) {
165+
// Subscribe to topics endpoint
166+
if status == nil {
167+
return
168+
}
169+
mqttClient.Subscribe("$aws/things/"+id+"/status/post", 1, StatusCB(status))
170+
mqttClient.Subscribe("$aws/things/"+id+"/upload/post", 1, UploadCB(status))
171+
mqttClient.Subscribe("$aws/things/"+id+"/sketch/post", 1, SketchCB(status))
172+
}
173+
166174
func addFileToSketchDB(file os.FileInfo, status *Status) *SketchStatus {
167175
id, err := GetSketchIDFromDB(file.Name())
168176
if err != nil {
@@ -245,7 +253,7 @@ func check(err error, context string) {
245253
}
246254

247255
// setupMQTTConnection establish a connection with aws iot
248-
func setupMQTTConnection(cert, key, id, url string) (mqtt.Client, error) {
256+
func setupMQTTConnection(cert, key, id, url string, status *Status) (mqtt.Client, error) {
249257
// Read certificate
250258
cer, err := tls.LoadX509KeyPair(cert, key)
251259
if err != nil {
@@ -257,7 +265,12 @@ func setupMQTTConnection(cert, key, id, url string) (mqtt.Client, error) {
257265
// KeepAlive option is 30 seconds by default
258266
opts := mqtt.NewClientOptions() // This line is different, we use the constructor function instead of creating the instance ourselves.
259267
opts.SetClientID(id)
260-
opts.SetMaxReconnectInterval(1 * time.Second)
268+
opts.SetMaxReconnectInterval(20 * time.Second)
269+
opts.SetConnectTimeout(0)
270+
opts.SetAutoReconnect(true)
271+
opts.SetOnConnectHandler(func(c mqtt.Client) {
272+
subscribeTopics(c, id, status)
273+
})
261274
opts.SetTLSConfig(&tls.Config{
262275
Certificates: []tls.Certificate{cer},
263276
ServerName: url,

0 commit comments

Comments
 (0)