-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt.go
67 lines (54 loc) · 1.44 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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/spf13/viper"
)
var mq mqtt.Client
func setupMQTT() error {
opts := mqtt.NewClientOptions()
opts.AddBroker(viper.GetString("mqtt.broker"))
opts.SetClientID(viper.GetString("mqtt.client_id"))
opts.SetConnectTimeout(viper.GetDuration("mqtt.timeout"))
opts.SetConnectRetryInterval(time.Second * 5)
opts.SetCleanSession(true)
opts.SetAutoReconnect(true)
opts.SetConnectionLostHandler(func(c mqtt.Client, err error) {
log.Println("MQTT: connection lost:", err.Error())
})
opts.SetOnConnectHandler(func(c mqtt.Client) {
log.Println("MQTT: connected")
})
mq = mqtt.NewClient(opts)
var (
x = mq.Connect()
timedOut = x.WaitTimeout(time.Second)
)
if x.Error() != nil {
return fmt.Errorf("MQTT: could not connect: %v", x.Error())
} else if !timedOut {
return fmt.Errorf("MQTT: connection timeout")
}
mq.Subscribe("kistan/kvitto", 0, handleMessage)
return nil
}
func handleMessage(_ mqtt.Client, msg mqtt.Message) {
if msg.Retained() {
return
}
data := Kvitto{}
data.Raw = msg.Payload()
if err := json.Unmarshal(msg.Payload(), &data); err != nil {
log.Println("MQTT: could not unmarshal payload:", err)
}
points := data.toPoints()
for _, point := range points {
if err := writeCli.WritePoint(context.Background(), point); err != nil {
log.Println("Influx: could not write point:", err)
}
}
}