| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package mqtt |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"encoding/json" |
| 10 |
"fmt" |
| 11 |
"net" |
| 12 |
|
| 13 |
"github.com/eclipse/paho.golang/paho" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
type TopicData struct { |
| 18 |
Topic string |
| 19 |
Message []byte |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
type MQTT struct { |
| 24 |
client *paho.Client |
| 25 |
connectProperties *paho.Connect |
| 26 |
qos byte |
| 27 |
incoming chan *TopicData |
| 28 |
} |
| 29 |
|
| 30 |
func (m MQTT) Incoming() <-chan *TopicData { |
| 31 |
return m.incoming |
| 32 |
} |
| 33 |
|
| 34 |
func (m MQTT) Close() error { |
| 35 |
close(m.incoming) |
| 36 |
return m.client.Disconnect(&paho.Disconnect{ReasonCode: 0}) |
| 37 |
} |
| 38 |
|
| 39 |
func (m MQTT) register(topic string) error { |
| 40 |
|
| 41 |
m.client.Router.RegisterHandler(topic, func(p *paho.Publish) { |
| 42 |
m.incoming <- &TopicData{Topic: topic, Message: p.Payload} |
| 43 |
|
| 44 |
}) |
| 45 |
|
| 46 |
_, err := m.client.Subscribe(context.Background(), &paho.Subscribe{ |
| 47 |
|
| 48 |
|
| 49 |
Subscriptions: map[string]paho.SubscribeOptions{ |
| 50 |
topic: {QoS: m.qos, NoLocal: true}, |
| 51 |
}, |
| 52 |
}, |
| 53 |
) |
| 54 |
return err |
| 55 |
} |
| 56 |
|
| 57 |
func (m MQTT) Subscribe(topics ...string) error { |
| 58 |
for _, t := range topics { |
| 59 |
if err := m.register(t); err != nil { |
| 60 |
return err |
| 61 |
} |
| 62 |
} |
| 63 |
return nil |
| 64 |
} |
| 65 |
|
| 66 |
func (m MQTT) Publish(topic string, message interface{}) error { |
| 67 |
b, err := json.Marshal(message) |
| 68 |
if err != nil { |
| 69 |
return err |
| 70 |
} |
| 71 |
props := &paho.PublishProperties{} |
| 72 |
pb := &paho.Publish{ |
| 73 |
Topic: topic, |
| 74 |
QoS: m.qos, |
| 75 |
Payload: b, |
| 76 |
Properties: props, |
| 77 |
} |
| 78 |
_, err = m.client.Publish(context.Background(), pb) |
| 79 |
return err |
| 80 |
} |
| 81 |
|
| 82 |
func (m MQTT) Connect() error { |
| 83 |
ca, err := m.client.Connect(context.Background(), m.connectProperties) |
| 84 |
if err != nil { |
| 85 |
return err |
| 86 |
} |
| 87 |
if ca.ReasonCode != 0 { |
| 88 |
return fmt.Errorf( |
| 89 |
"failed to connect to %s : %d - %s", |
| 90 |
m.client.Conn.RemoteAddr().String(), |
| 91 |
ca.ReasonCode, |
| 92 |
ca.Properties.ReasonString, |
| 93 |
) |
| 94 |
} |
| 95 |
return nil |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
type Configuration struct { |
| 100 |
ClientID string |
| 101 |
Username string |
| 102 |
Password string |
| 103 |
CleanStart bool |
| 104 |
SessionExpiry uint64 |
| 105 |
QOS byte |
| 106 |
KeepAlive uint16 |
| 107 |
Inflight uint |
| 108 |
} |
| 109 |
|
| 110 |
func New(conn net.Conn, |
| 111 |
cfg Configuration, |
| 112 |
) (*MQTT, error) { |
| 113 |
|
| 114 |
c := paho.NewClient(paho.ClientConfig{ |
| 115 |
Router: paho.NewStandardRouter(), |
| 116 |
Conn: conn, |
| 117 |
}) |
| 118 |
|
| 119 |
cp := &paho.Connect{ |
| 120 |
KeepAlive: cfg.KeepAlive, |
| 121 |
ClientID: cfg.ClientID, |
| 122 |
CleanStart: cfg.CleanStart, |
| 123 |
Username: cfg.Username, |
| 124 |
Password: []byte(cfg.Password), |
| 125 |
} |
| 126 |
if cfg.Username != "" { |
| 127 |
cp.UsernameFlag = true |
| 128 |
} |
| 129 |
if cfg.Password != "" { |
| 130 |
cp.PasswordFlag = true |
| 131 |
} |
| 132 |
|
| 133 |
return &MQTT{ |
| 134 |
client: c, |
| 135 |
connectProperties: cp, |
| 136 |
qos: cfg.QOS, |
| 137 |
incoming: make(chan *TopicData, cfg.Inflight), |
| 138 |
}, nil |
| 139 |
} |