blob: e676a14e772bd1009d17451f46b91078617c279b (
plain)
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
|
// SPDX-FileCopyrightText: 2020 Ethel Morgan
//
// SPDX-License-Identifier: MIT
// Package catbus is a convenience wrapper around MQTT for use with Catbus.
package catbus
import mqtt "github.com/eclipse/paho.mqtt.golang"
type (
Message = mqtt.Message
MessageHandler = func(*Client, Message)
Client struct {
mqtt mqtt.Client
}
ClientOptions struct {
DisconnectHandler func(*Client, error)
ConnectHandler func(*Client)
}
)
const (
atMostOnce byte = iota
atLeastOnce
exactlyOnce
)
const (
Retain = true
)
func NewClient(brokerURI string, options ClientOptions) *Client {
opts := mqtt.NewClientOptions()
opts.AddBroker(brokerURI)
opts.SetAutoReconnect(true)
opts.SetConnectionLostHandler(func(c mqtt.Client, err error) {
if options.DisconnectHandler != nil {
options.DisconnectHandler(&Client{c}, err)
}
})
opts.SetOnConnectHandler(func(c mqtt.Client) {
if options.ConnectHandler != nil {
options.ConnectHandler(&Client{c})
}
})
return &Client{mqtt.NewClient(opts)}
}
// Subscribe subscribes to a Catbus MQTT topic.
func (c *Client) Subscribe(topic string, f MessageHandler) error {
return c.mqtt.Subscribe(topic, atLeastOnce, func(c mqtt.Client, msg mqtt.Message) {
f(&Client{c}, msg)
}).Error()
}
// Connect connects to the Catbus MQTT broker and blocks forever.
func (c *Client) Connect() error {
if err := c.mqtt.Connect().Error(); err != nil {
return err
}
select {}
}
|