【发布时间】:2016-03-24 02:59:49
【问题描述】:
我正在使用 Particle Electron 和 AWS 创建一个气象站。我已设法将返回的数据发送到 DynamoDB 表“天气”,该表包含具有以下模式的所有天气数据(包含示例值):
Item{13}
deviceId: 540056000a51343334363138 (String) (Primary Partition Key)
tm: 1458754711 (Number) (Primary Sort Key)
batSoC: 89 (String)
batV: 4.01 (String)
hum: 27.9 (String)
lat: 41.2083 (String)
lon: -73.3439 (String)
pres: 968.4 (String)
temp: 19.8 (String)
uvI: 0.1 (String)
wDir: 0 (String)
wGst: 0.0 (String)
wSpd: 0.0 (String)
以及一个单独的“weather_index”表,其中仅包含写入主表的最新数据的 deviceId 和 tm 属性(有点像原子计数器,但用于定期更新的 unix 时间戳价值)。因此,如果上面的“weather_index”项是最新条目,“weather_index”表中的项将如下所示:
Item{2}
deviceIdString: 540056000a51343334363138 (String) (Primary Partition Key)
tmNumber: 1458754711 (Number)
我目前正在尝试在 Node.js 中编写一个非常基本的 Web 前端(在这个项目之前,我没有使用过,所以我仍在学习)并且不知道如何:
- 执行 DynamoDB getItem,其中包含通过先前的 getItem 检索到的参数。喜欢:
latestTime = getItem(weather_index, deviceId) // 获取最近一次天气观测的时间“tm”并存储在“latestTime”中 // 其中“weather_index”是表名
currentWeather = getItem(deviceId, tm) // 获取指定“tm”值的天气观测值并将其存储在“currentWeather”中 // 其中“tm”是最近观察的 unix 时间戳
然后我希望能够将各个值打印到终端/网页/信鸽/等...(类似于currentWeather.deviceId、currentWeather.tm、currentWeather.batSoC 等...
我有以下代码无法正常工作:
/*
* Module dependencies
*/
var AWS = require('aws-sdk')
// weathermon_dev credentials
AWS.config.update({accessKeyId: 'REDACTED for obvious reasons', secretAccessKey: 'This bit too'});
// Select AWS region
AWS.config.update({region: 'us-east-1'});
var db = new AWS.DynamoDB();
// db.listTables(function(err,data) {
// console.log(data.TableNames);
// });
var time = Date.now() / 1000;
time = Math.round(time);
//console.log("Time: ");
//console.log(time);
time = Math.round(time);
var deviceId = "540056000a51343334363138"
var params = {
Key: {
deviceId: {S: deviceId}
},
TableName: 'weather_index'
};
var timeJson;
db.getItem(params, function(err,data) {
if (err) console.log(err); // an error occurred
else console.log(data); // successful response
var timeJson = JSON.parse(data);
})
// var timeJson = JSON.parse(data);
// var itemTime = timeJson.item;
console.log("timeJSON: " + timeJson);
// console.log("itemTime: " + itemTime);
var params = {
Key: {
deviceId: {S: deviceId},
time: {N: 'tm'}
},
TableName: 'weather'
};
db.getItem(params, function(err, data) {
if (err) console.log(err); // an error occurred
else console.log(data); // successful response
})
任何帮助将不胜感激。
【问题讨论】:
标签: node.js amazon-web-services amazon-dynamodb aws-sdk iot