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

Add getRoomMeasure method #69

Merged
merged 1 commit into from
Dec 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions netatmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,129 @@ netatmo.prototype.getMeasure = function (options, callback) {
return this;
};

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

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

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

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

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

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

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

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


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

var form = {
access_token: access_token,
home_id: options.home_id,
room_id: options.room_id,
scale: options.scale,
type: options.type,
};

if (options) {

if (options.date_begin) {
if (options.date_begin <= 1E10) {
options.date_begin *= 1E3;
}

form.date_begin = moment(options.date_begin).utc().unix();
}

if (options.date_end === 'last') {
form.date_end = 'last';
} else if (options.date_end) {
if (options.date_end <= 1E10) {
options.date_end *= 1E3;
}
form.date_end = moment(options.date_end).utc().unix();
}

if (options.limit) {
form.limit = parseInt(options.limit, 10);

if (form.limit > 1024) {
form.limit = 1024;
}
}

if (options.optimize !== undefined) {
form.optimize = !!options.optimize;
}

if (options.real_time !== undefined) {
form.real_time = !!options.real_time;
}
}

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

body = JSON.parse(body);

var measure = body.body;

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

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

return this;

}.bind(this));

return this;
};

/**
* https://dev.netatmo.com/dev/resources/technical/reference/thermostat/syncschedule
* @param options
Expand Down