-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMQTT-Handler.js
More file actions
61 lines (54 loc) · 1.33 KB
/
MQTT-Handler.js
File metadata and controls
61 lines (54 loc) · 1.33 KB
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
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'ap-southeast-1'});
// Create the DynamoDB service object
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
// Set table name
var table = "cypress-psoc";
exports.handler = async (event) => {
console.log(event)
if (event.direction == "0") {
// Remove entry
var params = {
TableName: table,
Key: {
"data": { S: event.data },
"device": { S: event.device },
},
};
// Call DynamoDB to delete the item from the table
try {
var result = await ddb.deleteItem(params).promise();
console.log("Success deleting", result);
} catch(err) {
console.log("Error deleting", err);
console.log(err);
}
} else {
// Insert entry
const time_now = String(Date.now()); // Unix timestamp in milliseconds
var params = {
TableName: table,
Item: {
"data": { S: event.data },
"device": { S: event.device },
"time": { S: time_now },
}
};
// Call DynamoDB to add the item to the table
try {
var result = await ddb.putItem(params).promise();
console.log("Success putting", result);
} catch(err) {
console.log("Error putting", err);
console.log(err);
}
}
// Success response
const response = {
statusCode: 200,
body: JSON.stringify('Completed'),
};
return response;
};