LowPowerLab Forum

Software support => Pi Gateway => Topic started by: Kylix on October 20, 2016, 06:35:10 PM

Title: How to get latest node metric value from the neDB database? [solution]
Post by: Kylix on October 20, 2016, 06:35:10 PM
How could I get the last values inserted into the DB for a certain node? (some sort of a query).

Thanks
Title: Re: How to get the last record from the neDB database?
Post by: Felix on October 20, 2016, 11:30:29 PM
Last record of what?
Last inserted record?
Is this while the app runs (inside the app)? Or just query the db from the pi console?

For last inserted record you'd just query for your node Id, which gives you the last entry automatically.
Regardless, you can look at the neDB API for query options or see the many examples in gateway.js.
Title: Re: How to get the last record from the neDB database?
Post by: Kylix on October 21, 2016, 03:18:31 AM
Practically, I'd like to create a Telegram Bot that answers to a few commands like:
/temperature .../humidity ....

For doing this, I'd need to query the database for the last temperature value of a node and so on ...

I'll search in the gateway.js file and hope to understand how do you pass the query result into a variable.
By the way, I thought you don't use neDB anymore in the last version of the Gateway.

Title: Re: How to get the last record from the neDB database?
Post by: Felix on October 21, 2016, 09:09:38 AM
neDB is used for storing the latest status and metric values of each node. So simply searching by Id will return the last value received from that node.
The binary files database stores logged historical data for each node-metrics where this feature was turned ON.

So to get the latest values of a node just query the neDB database.
You could create an HTTP endpoint that responds to REST queries, which in turn searches the neDB object like this:

    var searchId = 123;
    var metricKey = "T";
    db.find({ _id : searchId }, function (err, results) {
      if (results.length == 1)
      {
        var dbNode = results[0];
        console.log('Temperature is: ' + dbNode.metrics[metricKey]);
      }
    });


Other available node metric properties:

            dbNode.metrics[metricKey].label (string)
            dbNode.metrics[metricKey].value (decimal)
            dbNode.metrics[metricKey].unit (string, optional)
            dbNode.metrics[metricKey].updated (unix timestamp, secconds)
            dbNode.metrics[metricKey].pin (bool, optional)
            dbNode.metrics[metricKey].graph (bool, optional)



Title: Re: How to get the latest node metric value from the neDB database?
Post by: Kylix on October 22, 2016, 04:21:26 PM
Thanks! I have now the Telegram Bot up and running 8)