-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt.go
45 lines (39 loc) · 1.08 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
package wbgong
import "log"
var (
funcNewPahoMQTTClient func(string, string) MQTTClient
)
// MQTTMessageHandler is a handler of MQTTMessages
type MQTTMessageHandler func(message MQTTMessage)
// MQTTMessage represents mqtt message
type MQTTMessage struct {
Topic string
Payload string
QoS byte
Retained bool
}
// MQTTClient is a mqtt client interface
type MQTTClient interface {
WaitForRetained(callback func())
Start()
Stop()
Publish(message MQTTMessage)
Subscribe(callback MQTTMessageHandler, topics ...string)
Unsubscribe(topics ...string)
}
// NewPahoMQTTClient returns new Paho mqtt client
func NewPahoMQTTClient(server, clientID string) MQTTClient {
if funcNewPahoMQTTClient != nil {
return funcNewPahoMQTTClient(server, clientID)
}
funcSym, errSym := plug.Lookup("NewPahoMQTTClient")
if errSym != nil {
log.Fatalf("Error in lookup symbol: %s", errSym)
}
var okResolve bool
funcNewPahoMQTTClient, okResolve = funcSym.(func(string, string) MQTTClient)
if !okResolve {
log.Fatal("Wrong sign on resolving func")
}
return funcNewPahoMQTTClient(server, clientID)
}