-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
64 lines (47 loc) · 1.16 KB
/
main.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
package main
import (
"os"
"time"
"github.com/eclipse/paho.mqtt.golang"
"github.com/golang-task/config"
"github.com/golang-task/helper"
"github.com/golang-task/models"
"log"
"flag"
)
func main() {
brokerConn := flag.String("broker", "tcp://localhost:1883", "The broker URI. ex: tcp://10.10.1.1:1883")
flag.Parse()
opts := config.Init(*brokerConn)
// option for system generated logs for Debug
//mqtt.DEBUG = log.New(os.Stdout, "", 0)
//mqtt.ERROR = log.New(os.Stdout, "", 0)
opts.SetKeepAlive(2 * time.Second)
opts.SetPingTimeout(1 * time.Second)
c := mqtt.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
log.Println(token.Error())
os.Exit(1)
}
log.Println("Connection Established \n")
brokerSub := &models.BrokerSub{
Topic: "/readings/temperature",
}
err := helper.Subscribe(brokerSub, c)
if err != nil {
log.Println("Unable to Subscribe token")
return
}
err = helper.GenPeriodicTemp(10* time.Second, c)
if err!= nil {
log.Println(err)
return
}
err = helper.Unsubscribe(brokerSub, c)
if err!= nil {
log.Println(err)
return
}
c.Disconnect(250)
time.Sleep(1 * time.Second)
}