Skip to content

Instantly share code, notes, and snippets.

@hokuma
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hokuma/d3e3932cbaf75067064f to your computer and use it in GitHub Desktop.
Save hokuma/d3e3932cbaf75067064f to your computer and use it in GitHub Desktop.
温度を取得してdynamodbに保存する
var fs = require('fs');
var Aws = require('aws-sdk');
var deviceId = 'YOUR_DEVICE_ID';
var sensorPath = '/sys/bus/w1/devices/' + deviceId + '/w1_slave';
var data = fs.readFileSync(sensorPath, {'encoding': 'ascii'});
var temp_line = data.split("\n")[1];
var matches = temp_line.match(/t=(\d+)/);
var temp;
if(matches) {
temp = matches[1];
}
var dynamodb = new Aws.DynamoDB({
region: 'ap-northeast-1'
});
var date = new Date();
var dateStr = "" + date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
var timeStr = "" + date.getHours() + ":" + date.getMinutes();
var param = {
TableName: 'temp_indicators',
Key: {
"measured_date": {"S": dateStr}
}
};
var promise = new Promise(function(success, reject) {
dynamodb.getItem(param, function(err, res) {
if(err) {
reject(err);
} else {
success(res);
}
});
});
promise.then(
function(res) {
var temps;
if(res.Item) {
temps = res.Item.temps.L;
} else {
temps = [];
}
temps.push({
'M': {
'measured_at': {'S': timeStr},
'temp': {'N': temp.toString()}
}
});
var param = {
TableName: 'temp_indicators',
Item: {
'measured_date': {'S': dateStr},
'temps': {'L': temps}
}
};
dynamodb.putItem(param, function(err, data) {
if(err) {
console.log(err, err.stack);
}
});
},
function(err) {
console.log(err, err.stack);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment