-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathactivity.go
145 lines (109 loc) · 2.81 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
package coap
import (
"errors"
"fmt"
"math/rand"
"net/url"
"github.com/dustin/go-coap"
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data/metadata"
)
func init() {
_ = activity.Register(&Activity{}, New)
}
var activityMd = activity.ToMetadata(&Settings{}, &Input{}, &Output{})
// New creates a new CoAP activity
func New(ctx activity.InitContext) (activity.Activity, error) {
s := &Settings{}
err := metadata.MapToStruct(ctx.Settings(), s, true)
if err != nil {
return nil, err
}
coapURI, err := url.Parse(s.URI)
if err != nil {
return nil, err
}
scheme := coapURI.Scheme
if scheme != "coap" {
return nil, errors.New("URI scheme must be 'coap'")
}
msgType := coap.Confirmable
if s.MessageType != "" {
msgType = toCoapMsgType(s.MessageType)
}
return &Activity{settings: s, msgType: msgType, methodCode: toCoapMethodCode(s.Method), coapURI: coapURI}, nil
}
// Activity is an Activity that is used to send a CoAP message
// inputs : {method,type,payload,messageId}
// outputs: {result}
type Activity struct {
settings *Settings
coapURI *url.URL
msgType coap.COAPType
methodCode coap.COAPCode
}
func (act *Activity) Metadata() *activity.Metadata {
return activityMd
}
//todo enhance CoAP client code
// Eval implements api.Activity.Eval - Invokes a REST Operation
func (act *Activity) Eval(ctx activity.Context) (done bool, err error) {
log := ctx.Logger()
input := &Input{}
req := coap.Message{
Type: act.msgType,
Code: act.methodCode,
}
req.SetPathString(act.coapURI.Path)
for k, v := range act.settings.Options {
op, val := toOption(k, v)
req.SetOption(op, val)
}
err = ctx.GetInputObject(input)
if err != nil {
return true, err
}
if input.MessageId == 0 {
req.MessageID = uint16(rand.Intn(10))
} else {
req.MessageID = uint16(input.MessageId)
}
if input.Payload != "" {
req.Payload = []byte(input.Payload)
}
if input.QueryParams != nil {
qp := url.Values{}
for key, value := range input.QueryParams {
qp.Set(key, value)
}
queryStr := qp.Encode()
req.SetOption(coap.URIQuery, queryStr)
log.Debugf("CoAP Message: [%s] %s?%s\n", act.settings.Method, act.coapURI.Path, queryStr)
} else {
log.Debugf("CoAP Message: [%s] %s\n", act.settings.Method, act.coapURI.Path)
}
c, err := coap.Dial("udp", act.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, err
}
if rv != nil {
if rv.Code > 100 {
return false, fmt.Errorf("CoAP Error: %s ", rv.Code.String())
}
if rv.Payload != nil {
log.Tracef("Response payload: %s", rv.Payload)
out := &Output{}
out.Response = string(rv.Payload)
err := ctx.SetOutputObject(out)
if err != nil {
return false, err
}
}
}
return true, nil
}