-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_dynamodb.js
48 lines (41 loc) · 1.1 KB
/
write_dynamodb.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
'use strict';
const qs = require("querystring");
var date = new Date();
var AWS = require("aws-sdk");
var dynamo = new AWS.DynamoDB.DocumentClient();
exports.handler = (event, context, callback) => {
// パラメータチェック
if (!event.data) {
context.fail('macAddress is not specified');
}
// uuidを生成
var uuid = createUuid();
// 更新内容をセット
var item = {
"DeviceId": event.data.deviceId,
"MeasureTimestamp": parseInt(event.data.measureTimestamp),
"Weight": parseInt(event.data.weight),
"LogId": uuid
};
var param = {
TableName: 'Tabegram',
Item: item
};
dynamo.put(param, function(err, data) {
if (err) {
context.fail(err);
} else {
context.succeed(item);
}
});
};
/**
* UUID(ランダム文字列)の生成
* @return string UUID
*/
function createUuid() {
var S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4() +S4());
}