-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent.go
114 lines (98 loc) · 2.56 KB
/
agent.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
package agent
import (
"errors"
"os"
"os/signal"
"time"
"github.com/eclipse/paho.mqtt.golang"
log "github.com/sirupsen/logrus"
)
// Agent runs an mqtt client
type Agent struct {
client mqtt.Client
clientID string
terminated bool
subscriptions []subscription
}
type subscription struct {
topic string
handler mqtt.MessageHandler
}
// Close agent
func (a *Agent) Close() {
a.client.Disconnect(250)
a.terminated = true
}
// Subscribe to topic
func (a *Agent) Subscribe(topic string, handler mqtt.MessageHandler) (err error) {
token := a.client.Subscribe(topic, 2, handler)
if token.WaitTimeout(2*time.Second) == false {
return errors.New("Subscribe timout")
}
if token.Error() != nil {
return token.Error()
}
log.WithFields(log.Fields{
"topic": topic}).Info("Subscribe")
a.subscriptions = append(a.subscriptions, subscription{topic, handler})
return nil
}
// Publish things
func (a *Agent) Publish(topic string, retain bool, payload string) (err error) {
token := a.client.Publish(topic, 2, retain, payload)
if token.WaitTimeout(2*time.Second) == false {
return errors.New("Publish timout")
}
if token.Error() != nil {
return token.Error()
}
return nil
}
// NewAgent creates an agent
func NewAgent(host string, clientID string) (a *Agent) {
a = &Agent{}
a.clientID = clientID
// mqtt.DEBUG = log.New(os.Stdout, "", 0)
// mqtt.ERROR = log.New(os.Stdout, "", 0)
opts := mqtt.NewClientOptions().AddBroker(host).SetClientID(clientID)
opts.SetKeepAlive(5 * time.Second)
opts.SetPingTimeout(5 * time.Second)
opts.OnConnectionLost = func(c mqtt.Client, err error) {
log.WithField("error", err).Info("Lost connection")
}
opts.OnConnect = func(c mqtt.Client) {
log.Info("Connect")
//Subscribe here, otherwise after connection lost,
//you may not receive any message
for _, s := range a.subscriptions {
if token := c.Subscribe(s.topic, 2, s.handler); token.Wait() && token.Error() != nil {
log.WithField("error", token.Error()).Error("Can't subscribe")
os.Exit(1)
}
log.WithField("topic", s.topic).Info("Resubscribe")
}
}
a.client = mqtt.NewClient(opts)
go func() {
done := make(chan os.Signal)
signal.Notify(done, os.Interrupt)
<-done
log.Info("Shutting down agent")
a.Close()
}()
return a
}
// Connect opens a new connection
func (a *Agent) Connect() (err error) {
token := a.client.Connect()
if token.WaitTimeout(2*time.Second) == false {
return errors.New("Open timeout")
}
if token.Error() != nil {
return token.Error()
}
return
}
func (a *Agent) IsTerminated() bool {
return a.terminated
}