-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (52 loc) · 1.66 KB
/
index.js
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
const AWS = require('aws-sdk')
const uuidv1 = require('uuid/v1')
const DYNAMODB_TABLE_NAME = 'lasttimei_events'
/**
* Returns a specific action the user has initiated/completed based off of
* a buttons serial number
*
* @param serialNumber String Serial number provided by the iot button
* @return String Event associated to the button.
*/
const getAction = (serialNumber) => {
switch (serialNumber) {
case 'G030MD027383CRCB':
return 'ACTION_WASHED_KIDS_BED_SHEETS'
default:
return null
}
}
/**
* Generate the payload we're going to save into
* dynamo!
*
* @param serialNumber String
* @param action String
* @return JSON
*/
const getPayload = (serialNumber, action) => ({
Item: {
"Id": { S: uuidv1() }, // For true randomness use v4.
"SerialNumber": { S: serialNumber },
"ActionTaken": { S: action },
"TimeStamp": { N: Date.now().toString() }
},
TableName: DYNAMODB_TABLE_NAME
})
exports.handler = (event, context, callback) => {
const payload = event
// Fetch the data
// Note specific actions are triggered by specific iot buttons.
// Each button has a unique DSN (serial number). I'm using that
// to associate a button to an action.
const serialNumber = payload.serialNumber
const action = getAction(serialNumber)
if (!action) return callback(`serial number, ${serialNumber}, not registered!`)
// Generate the payload to save into Dynamo!
const dbSavePayload = getPayload(serialNumber, action)
console.log("dbPayload", dbSavePayload)
const DynamoDB = new AWS.DynamoDB()
return DynamoDB.putItem(dbSavePayload).promise()
.then((resp) => callback(null))
.catch(error => callback(error.message))
}