-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathactivity.go
188 lines (154 loc) · 4.02 KB
/
activity.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package mqtt
import (
"strconv"
"strings"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data/metadata"
"github.com/project-flogo/core/support/log"
"github.com/project-flogo/core/support/ssl"
)
var activityMd = activity.ToMetadata(&Settings{}, &Input{}, &Output{})
func init() {
_ = activity.Register(&Activity{}, New)
}
// TokenType is a type of token
type TokenType int
const (
// Literal is a literal token type
Literal TokenType = iota
// Substitution is a parameter substitution
Substitution
)
// Token is a MQTT topic token
type Token struct {
TokenType TokenType
Token string
}
// Topic is a parsed topic
type Topic []Token
// ParseTopic parses the topic
func ParseTopic(topic string) Topic {
var parsed Topic
parts, index := strings.Split(topic, "/"), 0
for _, part := range parts {
if strings.HasPrefix(part, ":") {
token := strings.TrimPrefix(part, ":")
if token == "" {
token = strconv.Itoa(index)
index++
}
parsed = append(parsed, Token{
TokenType: Substitution,
Token: token,
})
} else {
parsed = append(parsed, Token{
TokenType: Literal,
Token: part,
})
}
}
return parsed
}
// String generates a string for the topic with params
func (t Topic) String(params map[string]string) string {
output := strings.Builder{}
for i, token := range t {
if i > 0 {
output.WriteString("/")
}
switch token.TokenType {
case Literal:
output.WriteString(token.Token)
case Substitution:
if value, ok := params[token.Token]; ok {
output.WriteString(value)
} else {
output.WriteString(":")
output.WriteString(token.Token)
}
}
}
return output.String()
}
func New(ctx activity.InitContext) (activity.Activity, error) {
settings := &Settings{}
err := metadata.MapToStruct(ctx.Settings(), settings, true)
if err != nil {
return nil, err
}
options := initClientOption(ctx.Logger(), settings)
if strings.HasPrefix(settings.Broker, "ssl") {
cfg := &ssl.Config{}
if len(settings.SSLConfig) != 0 {
err := cfg.FromMap(settings.SSLConfig)
if err != nil {
return nil, err
}
if _, set := settings.SSLConfig["skipVerify"]; !set {
cfg.SkipVerify = true
}
if _, set := settings.SSLConfig["useSystemCert"]; !set {
cfg.UseSystemCert = true
}
} else {
//using ssl but not configured, use defaults
cfg.SkipVerify = true
cfg.UseSystemCert = true
}
tlsConfig, err := ssl.NewClientTLSConfig(cfg)
if err != nil {
return nil, err
}
options.SetTLSConfig(tlsConfig)
}
mqttClient := mqtt.NewClient(options)
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
return nil, token.Error()
}
act := &Activity{
client: mqttClient,
settings: settings,
topic: ParseTopic(settings.Topic),
}
return act, nil
}
type Activity struct {
settings *Settings
client mqtt.Client
topic Topic
}
func (a *Activity) Metadata() *activity.Metadata {
return activityMd
}
func (a *Activity) Eval(ctx activity.Context) (done bool, err error) {
input := &Input{}
err = ctx.GetInputObject(input)
if err != nil {
return true, err
}
topic := a.settings.Topic
if params := input.TopicParams; len(params) > 0 {
topic = a.topic.String(params)
}
if token := a.client.Publish(topic, byte(a.settings.Qos), a.settings.Retain, input.Message); token.Wait() && token.Error() != nil {
ctx.Logger().Debugf("Error in publishing: %v", err)
return true, token.Error()
}
ctx.Logger().Debugf("Published Message: %v", input.Message)
return true, nil
}
func initClientOption(logger log.Logger, settings *Settings) *mqtt.ClientOptions {
opts := mqtt.NewClientOptions()
opts.AddBroker(settings.Broker)
opts.SetClientID(settings.Id)
opts.SetUsername(settings.Username)
opts.SetPassword(settings.Password)
opts.SetCleanSession(settings.CleanSession)
if settings.Store != "" && settings.Store != ":memory:" {
logger.Debugf("Using file store: %s", settings.Store)
opts.SetStore(mqtt.NewFileStore(settings.Store))
}
return opts
}