1

I am creating a weather station using the Particle Electron and AWS. I have managed to get the returned data sent to a DynamoDB table "weather" which contains all of the weather data with the following schema (with included sample values):

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)

as well as a separate "weather_index" table which contains only the deviceId and tm attributes for the most recent data that was written to the main table (kind of like an atomic counter but for a periodically updated unix timestamp value). So if the "weather_index" item above was the most recent entry, the item in the "weather_index" table would look like this:

Item{2}
deviceIdString: 540056000a51343334363138 (String) (Primary Partition Key)
tmNumber: 1458754711 (Number)

I am currently trying to write a very basic web frontend in Node.js (which, prior to this project, I have had no experience with, so I am still learning) and can't figure out how to:

  1. Perform a DynamoDB getItem which contains a parameter retrieved via a previous getItem. Like:

latestTime = getItem(weather_index, deviceId) // Gets the time "tm" of the most recent weather observation and stores it in "latestTime" // Where "weather_index" is the table name

currentWeather = getItem(deviceId, tm) // Gets the weather observation for the specified "tm" value and stores it in "currentWeather" // Where "tm" is the unix time-stamp of the most recent observation

I then want to be able to print the individual values to the terminal/webpage/carrier pigeon/etc... (Something along the lines of currentWeather.deviceId, currentWeather.tm, currentWeather.batSoC, etc...

I have the following code that I can't really make work properly:

/*
 * 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
})

Any help would be greatly appreciated.

smd75jr
  • 127
  • 1
  • 1
  • 6

1 Answers1

0

You need to look into how NodeJS asynchronous calls work. You would need to wait until the callback from the first getItem() is called before you perform the second getItem().

I've rewritten the relevant part of your code here, to show you what I'm talking about, but I recommend you try to understand why the code needs to be written in this way instead of just copy/pasting it.

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);
    console.log("timeJSON: " + timeJson);

    // Inside this callback we have the weather_index tm value, 
    // so query the weather table here.
    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
        // TODO: Use the database response data here
      }
    });
  }
});
Mark B
  • 183,023
  • 24
  • 297
  • 295