demosteinkjer-api
See https://api.demosteinkjer.no/ for documentation. The API requires a key and a secret to be used.
var api = require('demosteinkjer-api')('key', 'secret');
To get time series data for a given meter, a download url must first be obtained.
api.requestDownloadURL(meterID, options, function(downloadURL) {
console.log(downloadURL);
});
Although you receive a link, it will not be immediately available to use. This can be solved in multiple ways.
If one tries to send a GET request to a resource that is not yet available. A response code of 202 will be returned. Thus you can keep on sending requests until it becomes available (check for status code) or you can download the url ahead of time to be somewhat certain it will be available when you need it.
var intervalId = setInterval(function() {
api.readDownloadURL(downloadURL, function(err, readings) {
if (err) return;
console.log(readings);
clearInterval(intervalId);
});
}, 1000);
Get the latest reading for a meter
To get the latest reading for a meter, a url must not be obtained first:
api.getLatestMeterReading(meterID, function(err, res) {
console.log(res);
});
Minute Readers
Some of the meters have minute precision. Their IDs are available in the object api.minuteReaders
.
Caching
It is wise to cache results from the API, both meter readings and download urls. Redis is a wise choice for such a task. One way to do it is to create a key hash made of the meter id and the options for that reading
function hash() {
return [].slice.apply(arguments).reduce(function(h, e) {
return h + JSON.stringify(e);
}, '');
}
var key hash(meterID, options)
and store it in redis
var client = require('redis').createClient();
client.set(key, data)
where key
is a hash made using any method you'd prefer and data
is the data for that request.
License
MIT