-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity.go
executable file
·123 lines (98 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
package awsshadow
import (
"encoding/json"
"strings"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/logger"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iotdataplane"
)
// log is the default package logger
var log = logger.GetLogger("activity-tibco-rest")
const (
ivThingName = "thingName"
ivOp = "op"
ivDesired = "desired"
ivReported = "reported"
//ivAwsEndpoint = "awsEndpoint"
ovResult = "result"
)
// AwsIoT is an Activity that is used to update an Aws IoT device shadow
// inputs : {method,uri,params}
// outputs: {result}
type AwsIoT struct {
metadata *activity.Metadata
}
// NewActivity creates a new AwsIoT activity
func NewActivity(metadata *activity.Metadata) activity.Activity {
return &AwsIoT{metadata: metadata}
}
// Metadata returns the activity's metadata
func (a *AwsIoT) Metadata() *activity.Metadata {
return a.metadata
}
// Eval implements api.Activity.Eval - Invokes a Aws Iot Shadow Update
func (a *AwsIoT) Eval(context activity.Context) (done bool, err error) {
thingName := context.GetInput(ivThingName).(string)
op := context.GetInput(ivOp).(string)
op = strings.ToLower(op)
sess, err := session.NewSession()
if err != nil {
return false, err
}
idp := iotdataplane.New(sess)
var payload []byte
switch op {
case "update":
req := &ShadowRequest{State: &ShadowState{}}
if context.GetInput(ivDesired) != nil {
desired := context.GetInput(ivDesired).(map[string]string)
req.State.Desired = desired
}
if context.GetInput(ivReported) != nil {
reported := context.GetInput(ivReported).(map[string]string)
req.State.Reported = reported
}
reqJSON, err := json.Marshal(req)
sInput := &iotdataplane.UpdateThingShadowInput{}
sInput.SetThingName(thingName)
sInput.SetPayload(reqJSON)
out, err := idp.UpdateThingShadow(sInput)
if err != nil {
return false, err
}
payload = out.Payload
case "get":
sInput := &iotdataplane.GetThingShadowInput{}
sInput.SetThingName(thingName)
out, err := idp.GetThingShadow(sInput)
if err != nil {
return false, err
}
payload = out.Payload
case "delete":
sInput := &iotdataplane.DeleteThingShadowInput{}
sInput.SetThingName(thingName)
out, err := idp.DeleteThingShadow(sInput)
if err != nil {
return false, err
}
payload = out.Payload
}
var result interface{}
err = json.Unmarshal(payload, &result)
if err != nil {
return false, err
}
context.SetOutput(ovResult, result)
return true, nil
}
// ShadowRequest is a simple structure representing a Aws Shadow Update Request
type ShadowRequest struct {
State *ShadowState `json:"state"`
}
// ShadowState is the state to be updated
type ShadowState struct {
Desired map[string]string `json:"desired,omitempty"`
Reported map[string]string `json:"reported,omitempty"`
}