forked from TIBCOSoftware/flogo-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity.go
executable file
·303 lines (237 loc) · 5.96 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package coap
import (
"fmt"
"net/url"
"strconv"
"strings"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/logger"
"github.com/dustin/go-coap"
)
// log is the default package logger
var log = logger.GetLogger("activity-tibco-coap")
const (
methodGET = "GET"
methodPOST = "POST"
methodPUT = "PUT"
methodDELETE = "DELETE"
typeCON = "CONFIRMABLE"
typeNON = "NONCONFIRMABLE"
typeACK = "ACKNOWLEDGEMENT"
typeRST = "RESET"
ivMethod = "method"
ivURI = "uri"
ivQueryParams = "queryParams"
ivType = "type"
ivPayload = "payload"
ivMessageID = "messageId"
ivOptions = "options"
ovResponse = "response"
)
var validMethods = []string{methodGET, methodPOST, methodPUT, methodDELETE}
var validTypes = []string{typeCON, typeNON}
// CoAPActivity is an Activity that is used to send a CoAP message
// inputs : {method,type,payload,messageId}
// outputs: {result}
type CoAPActivity struct {
metadata *activity.Metadata
}
// NewActivity creates a new CoAP activity
func NewActivity(metadata *activity.Metadata) activity.Activity {
return &CoAPActivity{metadata: metadata}
}
// Metadata returns the activity's metadata
func (a *CoAPActivity) Metadata() *activity.Metadata {
return a.metadata
}
//todo enhance CoAP client code
// Eval implements api.Activity.Eval - Invokes a REST Operation
func (a *CoAPActivity) Eval(context activity.Context) (done bool, err error) {
method, ok := getStringValue(context, ivMethod, nil, true)
if !ok {
activity.NewError("Method not specified", "", nil)
}
uri, ok := getStringValue(context, ivURI, nil, false)
if !ok {
activity.NewError("URI not specified", "", nil)
}
msgType, _ := getStringValue(context, ivType, typeNON, true)
payload, hasPayload := getStringValue(context, ivPayload, nil, false)
messageID, _ := getIntValue(context, ivMessageID, 0)
coapURI, err := url.Parse(uri)
if err != nil {
return false, activity.NewError(err.Error(), "", nil)
}
scheme := coapURI.Scheme
if scheme != "coap" {
return false, activity.NewError("URI scheme must be 'coap'", "", nil)
}
req := coap.Message{
Type: toCoapType(msgType),
Code: toCoapCode(method),
MessageID: uint16(messageID),
}
if hasPayload {
req.Payload = []byte(payload)
}
val := context.GetInput(ivOptions)
if val != nil {
options := val.(map[string]string)
for k, v := range options {
op, val := toOption(k, v)
req.SetOption(op, val)
}
}
if context.GetInput(ivQueryParams) != nil {
queryParams := context.GetInput(ivQueryParams).(map[string]string)
qp := url.Values{}
for key, value := range queryParams {
qp.Set(key, value)
}
queryStr := qp.Encode()
req.SetOption(coap.URIQuery, queryStr)
log.Debugf("CoAP Message: [%s] %s?%s\n", method, coapURI.Path, queryStr)
} else {
log.Debugf("CoAP Message: [%s] %s\n", method, coapURI.Path)
}
req.SetPathString(coapURI.Path)
c, err := coap.Dial("udp", coapURI.Host)
if err != nil {
return false, activity.NewError(err.Error(), "", nil)
}
log.Debugf("conn: %v\n", c)
rv, err := c.Send(req)
if err != nil {
return false, activity.NewError(err.Error(), "", nil)
}
if rv != nil {
if rv.Code > 100 {
return false, activity.NewError(fmt.Sprintf("CoAP Error: %s", rv.Code.String()), rv.Code.String(), nil)
}
if rv.Payload != nil {
log.Debugf("Response payload: %s", rv.Payload)
context.SetOutput(ovResponse, string(rv.Payload))
}
}
return true, nil
}
////////////////////////////////////////////////////////////////////////////////////////
// Utils
func toCoapCode(method string) coap.COAPCode {
var code coap.COAPCode
switch method {
case methodGET:
code = coap.GET
case methodPOST:
code = coap.POST
case methodPUT:
code = coap.PUT
case methodDELETE:
code = coap.DELETE
}
return code
}
func toCoapType(typeStr string) coap.COAPType {
var ctype coap.COAPType
switch typeStr {
case typeCON:
ctype = coap.Confirmable
case typeNON:
ctype = coap.NonConfirmable
case typeACK:
ctype = coap.Acknowledgement
case typeRST:
ctype = coap.Reset
}
return ctype
}
func toOption(name string, value string) (coap.OptionID, interface{}) {
var opID coap.OptionID
var val interface{}
val = value
switch name {
case "IFMATCH":
opID = coap.IfMatch
case "URIHOST":
opID = coap.URIHost
case "ETAG":
opID = coap.ETag
//case "IFNONEMATCH":
// opID = coap.IfNoneMatch
case "OBSERVE":
opID = coap.Observe
val, _ = strconv.Atoi(value)
case "URIPORT":
opID = coap.URIPort
val, _ = strconv.Atoi(value)
case "LOCATIONPATH":
opID = coap.LocationPath
case "URIPATH":
opID = coap.URIPath
case "CONTENTFORMAT":
opID = coap.ContentFormat
val, _ = strconv.Atoi(value)
case "MAXAGE":
opID = coap.MaxAge
val, _ = strconv.Atoi(value)
case "URIQUERY":
opID = coap.URIQuery
case "ACCEPT":
opID = coap.IfMatch
val, _ = strconv.Atoi(value)
case "LOCATIONQUERY":
opID = coap.LocationQuery
case "PROXYURI":
opID = coap.ProxyURI
case "PROXYSCHEME":
opID = coap.ProxyScheme
case "SIZE1":
opID = coap.Size1
val, _ = strconv.Atoi(value)
default:
opID = 0
val = nil
}
return opID, val
}
func methodIsValid(method string) bool {
if !stringInList(method, validMethods) {
return false
}
return true
}
func stringInList(str string, list []string) bool {
for _, value := range list {
if value == str {
return true
}
}
return false
}
func getStringValue(context activity.Context, attrName string, defValue interface{}, uc bool) (string, bool) {
val := context.GetInput(attrName)
found := true
if val == nil {
found = false
if defValue == nil {
return "", false
}
val = defValue
}
if uc {
return strings.ToUpper(val.(string)), found
}
return val.(string), found
}
func getIntValue(context activity.Context, attrName string, defValue interface{}) (int, bool) {
val := context.GetInput(attrName)
found := true
if val == nil {
found = false
if defValue == nil {
return 0, false
}
val = defValue
}
return val.(int), found
}