Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
Added getPublicData API call (#38)
Browse files Browse the repository at this point in the history
Adding getPublicData capabilities and tests. Thanks @osos.
  • Loading branch information
osos authored and karbassi committed Mar 16, 2018
1 parent 31d0038 commit 0f76815
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Added getPublicData API call

## [2.1.0] / 2017-02-18
### Added
Expand Down
89 changes: 89 additions & 0 deletions netatmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -915,4 +915,93 @@ netatmo.prototype.getHealthyHomeCoachData = function (options, callback) {
return this;
};

/**
* https://dev.netatmo.com/resources/technical/reference/weatherapi/getpublicdata
* @param options
* @param callback
* @returns {*}
*/
netatmo.prototype.getPublicData = function (options, callback) {
// Wait until authenticated.
if (!access_token) {
return this.on('authenticated', function () {
this.getPublicData(options, callback);
});
}

if (!options) {
this.emit("error", new Error("getPublicData 'options' not set."));
return this;
}

if (!options.lat_ne) {
this.emit("error", new Error("getPublicData 'lat_ne' not set."));
return this;
}

if (!options.lon_ne) {
this.emit("error", new Error("getPublicData 'lon_ne' not set."));
return this;
}

if (!options.lat_sw) {
this.emit("error", new Error("getPublicData 'lat_sw' not set."));
return this;
}

if (!options.lon_sw) {
this.emit("error", new Error("getPublicData 'lat_sw' not set."));
return this;
}

if (util.isArray(options.required_data)) {
options.required_data = options.required_data.join(',');
}

// Remove any spaces from the type list if there is any.
options.required_data = options.required_data.replace(/\s/g, '').toLowerCase();


var url = util.format('%s/api/getpublicdata', BASE_URL);

var form = {
access_token: access_token,
lat_ne: options.lat_ne,
lon_ne: options.lon_ne,
lat_sw: options.lat_sw,
lon_sw: options.lon_sw,
required_data: options.required_data,
filter: options.filter,
};

request({
url: url,
method: "POST",
form: form,
}, function (err, response, body) {
if (err || response.statusCode != 200) {
var error = this.handleRequestError(err, response, body, "getPublicData error");
if (callback) {
callback(error);
}
return;
}

body = JSON.parse(body);

var measure = body.body;

this.emit('get-publicdata', err, measure);

if (callback) {
return callback(err, measure);
}

return this;

}.bind(this));

return this;
};

module.exports = netatmo;
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ var handleEvents = function(err, data) {
console.log(data.events_list);
};

var getPublicData = function(err, data) {
console.log(data);
};


// Event Listeners
api.on('get-stationsdata', getStationsData);
Expand All @@ -132,6 +136,7 @@ api.on('get-homedata', getHomeData);
api.on('get-nextevents', handleEvents);
api.on('get-lasteventof', handleEvents);
api.on('get-eventsuntil', handleEvents);
api.on('get-publicdata', getPublicData);

// Get Stations Data
// See docs: https://dev.netatmo.com/doc/methods/getstationsdata
Expand Down Expand Up @@ -256,3 +261,15 @@ var options = {

api.getHealthyHomeCoachData(options);

// Get Public Data
// See docs: https://dev.netatmo.com/resources/technical/reference/weatherapi/getpublicdata
var options = {
lat_ne: '',
lon_ne: '',
lat_sw: '',
lon_sw: '',
required_data: '',
filter: false,
};

api.getPublicData(options);

0 comments on commit 0f76815

Please sign in to comment.