-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumper.go
48 lines (36 loc) · 892 Bytes
/
dumper.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
package main
import (
"fmt"
"os"
"os/signal"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
type Tor struct {
pos int8
dir bool
}
var tor Tor
func mqttHandler(client mqtt.Client, msg mqtt.Message) {
fmt.Println(msg.Topic(), ":", string(msg.Payload()))
}
func main() {
mqttOpts := mqtt.NewClientOptions().AddBroker("tcps://rpi.fritz.box:8883")
mqttOpts.SetClientID("tor-dumper")
mqttOpts.SetDefaultPublishHandler(mqttHandler)
mqttClient := mqtt.NewClient(mqttOpts)
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
mqttClient.Subscribe("tor/#", 2, mqttHandler)
mqttClient.Subscribe("tor2/#", 2, mqttHandler)
quit := make(chan struct{})
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
mqttClient.Disconnect(250)
fmt.Println("[MQTT] Disconnected")
quit <- struct{}{}
}()
<-quit
}