-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt.go
66 lines (58 loc) · 1.54 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
"path"
"strings"
)
type PrefixRouter struct {
client mqtt.Client
prefix string
}
func (r *PrefixRouter) AddRoute(pattern string, handler func (client mqtt.Client, message *ParsedMessage)) {
parse := parseWildcards(pattern)
r.client.AddRoute(path.Join(r.prefix, pattern), func(client mqtt.Client, message mqtt.Message) {
match, plus, rest := parse(strings.TrimPrefix(message.Topic(), r.prefix + "/"))
if !match {
panic(fmt.Sprintf("pattern didnt match: %q", message.Topic()))
}
handler(client, &ParsedMessage{message, plus, rest})
})
}
func (r *PrefixRouter) Subscribe(qos byte) {
r.client.Subscribe(path.Join(r.prefix, "#"), qos, nil)
}
type ParsedMessage struct {
mqtt.Message
plus []string
rest []string
}
func (p *ParsedMessage) Plus(idx int) string {
return p.plus[idx]
}
func (p *ParsedMessage) Rest(idx int) string {
return p.rest[idx]
}
func parseWildcards(pattern string) func (topic string) (bool, []string, []string) {
parts := strings.Split(pattern, "/")
return func(topic string) (bool, []string, []string) {
topicParts := strings.Split(topic, "/")
var plus []string
if len(topicParts) != len(parts) && !(parts[len(parts) - 1] != "#") {
return false, nil, nil
}
for idx := range topicParts {
switch parts[idx] {
case "+":
plus = append(plus, topicParts[idx])
case "#":
return true, plus, topicParts[idx:]
default:
if topicParts[idx] != parts[idx] {
return false, nil, nil
}
}
}
return true, plus, nil
}
}