-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoIoTWorkInterruptTrackerLambda.go
161 lines (144 loc) · 4.36 KB
/
GoIoTWorkInterruptTrackerLambda.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
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
var (
ddbClient *dynamodb.DynamoDB
ddbInputVars ddbVars
)
/*************************************
Sample Event Data to configure structs for 1-click
{
"deviceEvent": {
"buttonClicked": {
"clickType": "SINGLE",
"reportedTime": "2018-05-04T23:26:33.747Z"
}
},
"deviceInfo": {
"attributes": {
"key3": "value3",
"key1": "value1",
"key4": "value4"
},
"type": "button",
"deviceId": " G030PMXXXXXXXXXX ",
"remainingLife": 5.00
},
"placementInfo": {
"projectName": "test",
"placementName": "myPlacement",
"attributes": {
"location": "Seattle",
"equipment": "printer"
},
"devices": {
"myButton": " G030PMXXXXXXXXXX "
}
}
}
*************************************/
type ddbVars struct {
region string
table string
tableKey string
}
type iotButtonEvent struct {
DeviceInfo iotButtonDeviceInfo `json:"deviceInfo"`
DeviceEvent iotButtonDeviceEvent `json:"deviceEvent"`
}
type iotButtonDeviceEvent struct {
ButtonClicked iotButtonClicked `json:"buttonClicked"`
}
type iotButtonClicked struct {
ClickType string `json:"clickType"`
ReportedTime string `json:"reportedTime"`
}
type iotButtonDeviceInfo struct {
SerialNumber string `json:"deviceId"`
RemainingLife float64 `json:"remainingLife"`
}
func handleRequest(req iotButtonEvent) (resultJSON map[string]*dynamodb.AttributeValue, err error) {
inputTime := time.Now().UTC().Unix()
serial := req.DeviceInfo.SerialNumber
clickType := req.DeviceEvent.ButtonClicked.ClickType
timestamp := req.DeviceEvent.ButtonClicked.ReportedTime
batteryLife := req.DeviceInfo.RemainingLife
ddbTablePrimaryKeyValue := strings.Join([]string{serial, strconv.FormatInt(inputTime, 10)}, "_")
prettyJSON, err := json.MarshalIndent(req, "", " ")
if err != nil {
fmt.Printf("Could not unmarshal json. Error:\n%v", err)
}
fmt.Printf("Request details:\n%s", string(prettyJSON))
input := &dynamodb.PutItemInput{
Item: map[string]*dynamodb.AttributeValue{
ddbInputVars.tableKey: {
S: aws.String(ddbTablePrimaryKeyValue),
},
"clickType": {
S: aws.String(clickType),
},
"reportedTime": {
S: aws.String(timestamp),
},
"remainingLife": {
N: aws.String(strconv.FormatFloat(batteryLife, 'f', -1, 64)),
},
"deviceId": {
S: aws.String(serial),
},
},
ReturnConsumedCapacity: aws.String("TOTAL"),
TableName: aws.String(ddbInputVars.table),
}
fmt.Printf("DDB input: \n%v", input)
result, err := ddbClient.PutItem(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case dynamodb.ErrCodeConditionalCheckFailedException:
fmt.Println(dynamodb.ErrCodeConditionalCheckFailedException, aerr.Error())
case dynamodb.ErrCodeProvisionedThroughputExceededException:
fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
case dynamodb.ErrCodeResourceNotFoundException:
fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
case dynamodb.ErrCodeItemCollectionSizeLimitExceededException:
fmt.Println(dynamodb.ErrCodeItemCollectionSizeLimitExceededException, aerr.Error())
case dynamodb.ErrCodeTransactionConflictException:
fmt.Println(dynamodb.ErrCodeTransactionConflictException, aerr.Error())
case dynamodb.ErrCodeRequestLimitExceeded:
fmt.Println(dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
case dynamodb.ErrCodeInternalServerError:
fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
}
fmt.Printf("%v", result)
return result.Attributes, err
}
func main() {
ddbInputVars = ddbVars{
region: os.Getenv("AWS_REGION"),
table: os.Getenv("DDB_TABLE"),
tableKey: os.Getenv("DDB_TABLE_KEY"),
}
session := session.Must(session.NewSession())
ddbClient = dynamodb.New(session, aws.NewConfig().WithRegion(ddbInputVars.region))
lambda.Start(handleRequest)
}