-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroker.go
70 lines (53 loc) · 1.91 KB
/
broker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2017 Inca Roads LLC. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
// Outbound MQTT support for publishing to AWS IOT MQTT broker
package main
import (
"encoding/json"
"fmt"
ttdata "github.com/Safecast/safecast-go"
MQTT "github.com/eclipse/paho.mqtt.golang"
)
var brokerConnected bool
var brokerMqttClient MQTT.Client
func brokerOutboundPublisher() {
mqttOpts := MQTT.NewClientOptions()
mqttOpts.AddBroker(ServiceConfig.BrokerHost)
mqttOpts.SetUsername(ServiceConfig.BrokerUsername)
mqttOpts.SetPassword(ServiceConfig.BrokerPassword)
mqttOpts.SetAutoReconnect(true)
mqttOpts.SetCleanSession(true)
onMqConnectionLost := func(client MQTT.Client, err error) {
fmt.Printf("\n%s *** MQTT broker connection lost: %s: %v\n\n", LogTime(), ServiceConfig.BrokerHost, err)
}
mqttOpts.SetConnectionLostHandler(onMqConnectionLost)
brokerMqttClient = MQTT.NewClient(mqttOpts)
// Connect to the service
if token := brokerMqttClient.Connect(); token.Wait() && token.Error() != nil {
fmt.Printf("Error connecting to broker: %s\n", token.Error())
} else {
fmt.Printf("Broker: connected\n")
brokerConnected = true
}
}
// Send to anyone/everyone listening on that MQTT topic
func brokerPublish(sd ttdata.SafecastData) {
// Init
if !brokerConnected {
return
}
// We don't publish anything without a captured date, because it confuses too many systems
if sd.CapturedAt == nil || *sd.CapturedAt == "" {
return
}
// Delete the legacy device ID so that it doesn't confuse anyone. It has been superceded
// by the device URN
sd.DeviceID = 0
// Marshal the safecast data to json
scJSON, _ := json.Marshal(sd)
topic := fmt.Sprintf("device/%s", sd.DeviceUID)
if token := brokerMqttClient.Publish(topic, 0, false, scJSON); token.Wait() && token.Error() != nil {
fmt.Printf("broker: %s\n", token.Error())
}
}