-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt.go
165 lines (140 loc) · 4.26 KB
/
mqtt.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package mqttGather
import (
"crypto/tls"
"fmt"
"log"
"net/url"
"time"
MQTT "github.com/eclipse/paho.mqtt.golang"
)
type Mqtt struct {
Broker string
Topic string
TelemetryTopic string
ClientId string
cfg *RunConfig
db DB
client MQTT.Client
statsChannel chan DBAStats
}
func (m *Mqtt) Disconnect() error {
m.db.Close()
m.client.Disconnect(1000)
close(m.statsChannel)
return nil
}
func retrieveClientId(topic string) string {
// /opennoise/c4:dd:57:66:95:60/dba_stats
// producer := msg.Topic()[11 : 11+17]
if len(topic) < 28 {
log.Printf("E: invalid topic: %s", topic)
return fmt.Sprintf("?:%s", topic)
}
return topic[11 : 11+17]
}
func (m *Mqtt) msgHandler(c MQTT.Client, msg MQTT.Message) {
// /opennoise/c4:dd:57:66:95:60/dba_stats
producer := retrieveClientId(msg.Topic())
csv := string(msg.Payload())
stats, err := DBAStatsFromString(csv, producer)
if err != nil {
log.Printf("E: could not parse %s : %v", csv, err)
} else {
log.Printf("D: recv %s : %s", msg.Topic(), csv)
if _, err := m.db.SaveNow(stats); err != nil {
log.Printf("E: could not save %s (raw:%s) : %v", stats, csv, err)
if err := m.connectDB(); err != nil {
log.Printf("E: could not reconnect to db (%v)", err)
}
}
m.statsChannel <- *stats
}
}
func (m *Mqtt) msgHandlerTelemetry(c MQTT.Client, msg MQTT.Message) {
// /opennoise/c4:dd:57:66:95:60/telemetry
producer := retrieveClientId(msg.Topic())
payload := string(msg.Payload())
telemetry, err := TelemetryFromPayload(payload, producer)
if err != nil {
log.Printf("E: could not parse %s : %v", payload, err)
} else {
log.Printf("D: recv %s : %s", msg.Topic(), payload)
if _, err := m.db.SaveTelemetryNow(telemetry); err != nil {
log.Printf("E: could not save %s (raw:%s) : %v", telemetry, payload, err)
if err := m.connectDB(); err != nil {
log.Printf("E: could not reconnect to db (%v)", err)
}
}
}
}
func (m *Mqtt) connectDB() error {
if m.db != nil {
log.Printf("D: closing existing connection")
m.db.Close()
}
if db, err := NewDatabase(m.cfg.SqlLiteConnect); err != nil {
return err
} else {
m.db = db
}
return nil
}
func NewMQTT(cfg *RunConfig) (*Mqtt, error) {
mqtt := Mqtt{
Broker: cfg.Host,
Topic: cfg.Topic,
TelemetryTopic: cfg.TelemetryTopic,
ClientId: cfg.ClientId,
cfg: cfg,
}
if err := mqtt.connectDB(); err != nil {
log.Printf("E: could not connect to DB: %v", err)
return nil, err
}
mqtt.statsChannel = make(chan DBAStats)
opts := MQTT.NewClientOptions()
opts.AddBroker(mqtt.Broker)
opts.SetClientID(mqtt.ClientId)
opts.SetConnectRetryInterval(10 * time.Second)
opts.SetConnectionAttemptHandler(func(u *url.URL, cfg *tls.Config) *tls.Config {
log.Printf("D: connection attempt: %v", u)
return cfg // why!?
})
opts.SetConnectionLostHandler(func(c MQTT.Client, err error) {
log.Printf("E: connection lost: %v", err)
})
opts.SetDefaultPublishHandler(func(client MQTT.Client, msg MQTT.Message) {
log.Printf("I: unexpected message on topic: %s : %s", msg.Topic(), string(msg.Payload()))
// TODO log to db
})
opts.SetOnConnectHandler(func(c MQTT.Client) {
opts := c.OptionsReader()
log.Printf("D: connect: %v", opts.ClientID())
token := mqtt.client.Subscribe(mqtt.Topic, byte(0), mqtt.msgHandler)
// Stats Topic
if token.Wait() && token.Error() != nil {
log.Printf("E: subscribtion failed: %s (%v)", mqtt.Topic, token.Error())
// TODO figure out what to do here: sit under a tree crying and waiting to die?
} else {
log.Printf("D: subscribed to: %s", mqtt.Topic)
}
// Telemetry Topic
token = mqtt.client.Subscribe(mqtt.TelemetryTopic, byte(0), mqtt.msgHandlerTelemetry)
if token.Wait() && token.Error() != nil {
log.Printf("E: subscription failed: %s (%v)", mqtt.TelemetryTopic, token.Error())
} else {
log.Printf("D: subscribed to: %s", mqtt.TelemetryTopic)
}
})
opts.SetReconnectingHandler(func(c MQTT.Client, o *MQTT.ClientOptions) {
opts := c.OptionsReader()
time.Sleep(2 * time.Second) // don't just hammer away at poor server.
log.Printf("I: reconnecting: %v", opts.ClientID())
})
mqtt.client = MQTT.NewClient(opts)
token := mqtt.client.Connect()
if token.Wait() && token.Error() != nil {
return nil, token.Error()
}
return &mqtt, nil
}