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

Commit 0e14716

Browse files
committed
Merge pull request #14 from marcuspocus/master
Added getThermostatsData and documentation.
2 parents 8d49273 + 5429658 commit 0e14716

File tree

2 files changed

+159
-48
lines changed

2 files changed

+159
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ build
1919

2020
node_modules
2121

22+
/.idea

netatmo.js

Lines changed: 158 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,27 @@ var client_secret;
1212
var scope;
1313
var access_token;
1414

15-
var netatmo = function(args) {
15+
/**
16+
* @constructor
17+
* @param args
18+
*/
19+
var netatmo = function (args) {
1620
EventEmitter.call(this);
1721
this.authenticate(args);
1822
};
1923

2024
util.inherits(netatmo, EventEmitter);
2125

22-
netatmo.prototype.handleRequestError = function(err,response,body,message,critical) {
26+
/**
27+
* handleRequestError
28+
* @param err
29+
* @param response
30+
* @param body
31+
* @param message
32+
* @param critical
33+
* @returns {Error}
34+
*/
35+
netatmo.prototype.handleRequestError = function (err, response, body, message, critical) {
2336
var errorMessage = "";
2437
if (body) {
2538
errorMessage = JSON.parse(body);
@@ -31,16 +44,21 @@ netatmo.prototype.handleRequestError = function(err,response,body,message,critic
3144
errorMessage = "No response";
3245
}
3346
var error = new Error(message + ": " + errorMessage);
34-
if(critical) {
47+
if (critical) {
3548
this.emit("error", error);
3649
} else {
3750
this.emit("warning", error);
3851
}
3952
return error;
4053
};
4154

42-
// http://dev.netatmo.com/doc/authentication
43-
netatmo.prototype.authenticate = function(args, callback) {
55+
/**
56+
* http://dev.netatmo.com/doc/authentication
57+
* @param args
58+
* @param callback
59+
* @returns {netatmo}
60+
*/
61+
netatmo.prototype.authenticate = function (args, callback) {
4462
if (!args) {
4563
this.emit("error", new Error("Authenticate 'args' not set."));
4664
return this;
@@ -87,9 +105,9 @@ netatmo.prototype.authenticate = function(args, callback) {
87105
url: url,
88106
method: "POST",
89107
form: form,
90-
}, function(err, response, body) {
108+
}, function (err, response, body) {
91109
if (err || response.statusCode != 200) {
92-
return this.handleRequestError(err,response,body,"Authenticate error", true);
110+
return this.handleRequestError(err, response, body, "Authenticate error", true);
93111
}
94112

95113
body = JSON.parse(body);
@@ -112,8 +130,12 @@ netatmo.prototype.authenticate = function(args, callback) {
112130
return this;
113131
};
114132

115-
// http://dev.netatmo.com/doc/authentication
116-
netatmo.prototype.authenticate_refresh = function(refresh_token) {
133+
/**
134+
* http://dev.netatmo.com/doc/authentication
135+
* @param refresh_token
136+
* @returns {netatmo}
137+
*/
138+
netatmo.prototype.authenticate_refresh = function (refresh_token) {
117139

118140
var form = {
119141
grant_type: 'refresh_token',
@@ -128,17 +150,17 @@ netatmo.prototype.authenticate_refresh = function(refresh_token) {
128150
url: url,
129151
method: "POST",
130152
form: form,
131-
}, function(err, response, body) {
153+
}, function (err, response, body) {
132154
if (err || response.statusCode != 200) {
133-
return this.handleRequestError(err,response,body,"Authenticate refresh error");
155+
return this.handleRequestError(err, response, body, "Authenticate refresh error");
134156
}
135157

136158
body = JSON.parse(body);
137159

138160
access_token = body.access_token;
139161

140162
if (body.expires_in) {
141-
setTimeout(this.authenticate_refresh.bind(this), body.expires_in * 1000, body.refresh_token);
163+
setTimeout(this.authenticate_refresh.bind(this), body.expires_in * 1000, body.refresh_token);
142164
}
143165

144166
return this;
@@ -147,11 +169,17 @@ netatmo.prototype.authenticate_refresh = function(refresh_token) {
147169
return this;
148170
};
149171

150-
// https://dev.netatmo.com/doc/methods/getuser
151-
netatmo.prototype.getUser = function(callback) {
172+
173+
/**
174+
* https://dev.netatmo.com/doc/methods/getuser
175+
* @param callback
176+
* @returns {*}
177+
* @deprecated
178+
*/
179+
netatmo.prototype.getUser = function (callback) {
152180
// Wait until authenticated.
153181
if (!access_token) {
154-
return this.on('authenticated', function() {
182+
return this.on('authenticated', function () {
155183
this.getUser(callback);
156184
});
157185
}
@@ -166,9 +194,9 @@ netatmo.prototype.getUser = function(callback) {
166194
url: url,
167195
method: "POST",
168196
form: form,
169-
}, function(err, response, body) {
197+
}, function (err, response, body) {
170198
if (err || response.statusCode != 200) {
171-
return this.handleRequestError(err,response,body,"getUser error");
199+
return this.handleRequestError(err, response, body, "getUser error");
172200
}
173201

174202
body = JSON.parse(body);
@@ -186,11 +214,18 @@ netatmo.prototype.getUser = function(callback) {
186214
return this;
187215
};
188216

189-
// https://dev.netatmo.com/doc/methods/devicelist
190-
netatmo.prototype.getDevicelist = function(options, callback) {
217+
218+
/**
219+
* https://dev.netatmo.com/doc/methods/devicelist
220+
* @param options
221+
* @param callback
222+
* @returns {*}
223+
* @deprecated
224+
*/
225+
netatmo.prototype.getDevicelist = function (options, callback) {
191226
// Wait until authenticated.
192227
if (!access_token) {
193-
return this.on('authenticated', function() {
228+
return this.on('authenticated', function () {
194229
this.getDevicelist(options, callback);
195230
});
196231
}
@@ -214,9 +249,9 @@ netatmo.prototype.getDevicelist = function(options, callback) {
214249
url: url,
215250
method: "POST",
216251
form: form,
217-
}, function(err, response, body) {
252+
}, function (err, response, body) {
218253
if (err || response.statusCode != 200) {
219-
return this.handleRequestError(err,response,body,"getDevicelist error");
254+
return this.handleRequestError(err, response, body, "getDevicelist error");
220255
}
221256

222257
body = JSON.parse(body);
@@ -237,11 +272,16 @@ netatmo.prototype.getDevicelist = function(options, callback) {
237272
return this;
238273
};
239274

240-
// https://dev.netatmo.com/doc/methods/getstationsdata
241-
netatmo.prototype.getStationsData = function(options, callback) {
275+
/**
276+
* https://dev.netatmo.com/doc/methods/getstationsdata
277+
* @param options
278+
* @param callback
279+
* @returns {*}
280+
*/
281+
netatmo.prototype.getStationsData = function (options, callback) {
242282
// Wait until authenticated.
243283
if (!access_token) {
244-
return this.on('authenticated', function() {
284+
return this.on('authenticated', function () {
245285
this.getStationsData(options, callback);
246286
});
247287
}
@@ -265,9 +305,9 @@ netatmo.prototype.getStationsData = function(options, callback) {
265305
url: url,
266306
method: "POST",
267307
form: form,
268-
}, function(err, response, body) {
308+
}, function (err, response, body) {
269309
if (err || response.statusCode != 200) {
270-
return this.handleRequestError(err,response,body,"getStationsDataError error");
310+
return this.handleRequestError(err, response, body, "getStationsDataError error");
271311
}
272312

273313
body = JSON.parse(body);
@@ -287,11 +327,65 @@ netatmo.prototype.getStationsData = function(options, callback) {
287327
return this;
288328
};
289329

290-
// https://dev.netatmo.com/doc/methods/getmeasure
291-
netatmo.prototype.getMeasure = function(options, callback) {
330+
/**
331+
* https://dev.netatmo.com/doc/methods/getthermostatsdata
332+
* @param options
333+
* @param callback
334+
* @returns {*}
335+
*/
336+
netatmo.prototype.getThermostatsData = function (options, callback) {
337+
// Wait until authenticated.
338+
if (!access_token) {
339+
return this.on('authenticated', function () {
340+
this.getThermostatsData(options, callback);
341+
});
342+
}
343+
344+
if (options != null && callback == null) {
345+
callback = options;
346+
options = null;
347+
}
348+
349+
var url = util.format('%s/api/getthermostatsdata?access_token=%s', BASE_URL, access_token);
350+
if (options != null) {
351+
url = util.format(url + '&device_id=%s', options.device_id);
352+
}
353+
354+
request({
355+
url: url,
356+
method: "GET",
357+
}, function (err, response, body) {
358+
if (err || response.statusCode != 200) {
359+
return this.handleRequestError(err, response, body, "getThermostatsDataError error");
360+
}
361+
362+
body = JSON.parse(body);
363+
364+
var devices = body.body.devices;
365+
366+
this.emit('get-thermostatsdata', err, devices);
367+
368+
if (callback) {
369+
return callback(err, devices);
370+
}
371+
372+
return this;
373+
374+
}.bind(this));
375+
376+
return this;
377+
};
378+
379+
/**
380+
* https://dev.netatmo.com/doc/methods/getmeasure
381+
* @param options
382+
* @param callback
383+
* @returns {*}
384+
*/
385+
netatmo.prototype.getMeasure = function (options, callback) {
292386
// Wait until authenticated.
293387
if (!access_token) {
294-
return this.on('authenticated', function() {
388+
return this.on('authenticated', function () {
295389
this.getMeasure(options, callback);
296390
});
297391
}
@@ -377,9 +471,9 @@ netatmo.prototype.getMeasure = function(options, callback) {
377471
url: url,
378472
method: "POST",
379473
form: form,
380-
}, function(err, response, body) {
474+
}, function (err, response, body) {
381475
if (err || response.statusCode != 200) {
382-
var error = this.handleRequestError(err,response,body,"getMeasure error");
476+
var error = this.handleRequestError(err, response, body, "getMeasure error");
383477
if (callback) {
384478
callback(error);
385479
}
@@ -403,11 +497,17 @@ netatmo.prototype.getMeasure = function(options, callback) {
403497
return this;
404498
};
405499

406-
// https://dev.netatmo.com/doc/methods/getthermstate
407-
netatmo.prototype.getThermstate = function(options, callback) {
500+
/**
501+
* https://dev.netatmo.com/doc/methods/getthermstate
502+
* @param options
503+
* @param callback
504+
* @returns {*}
505+
* @deprecated
506+
*/
507+
netatmo.prototype.getThermstate = function (options, callback) {
408508
// Wait until authenticated.
409509
if (!access_token) {
410-
return this.on('authenticated', function() {
510+
return this.on('authenticated', function () {
411511
this.getThermstate(options, callback);
412512
});
413513
}
@@ -439,9 +539,9 @@ netatmo.prototype.getThermstate = function(options, callback) {
439539
url: url,
440540
method: "POST",
441541
form: form,
442-
}, function(err, response, body) {
542+
}, function (err, response, body) {
443543
if (err || response.statusCode != 200) {
444-
return this.handleRequestError(err,response,body,"getThermstate error");
544+
return this.handleRequestError(err, response, body, "getThermstate error");
445545
}
446546

447547
body = JSON.parse(body);
@@ -459,11 +559,16 @@ netatmo.prototype.getThermstate = function(options, callback) {
459559
return this;
460560
};
461561

462-
// https://dev.netatmo.com/doc/methods/syncschedule
463-
netatmo.prototype.setSyncSchedule = function(options, callback) {
562+
/**
563+
* https://dev.netatmo.com/doc/methods/syncschedule
564+
* @param options
565+
* @param callback
566+
* @returns {*}
567+
*/
568+
netatmo.prototype.setSyncSchedule = function (options, callback) {
464569
// Wait until authenticated.
465570
if (!access_token) {
466-
return this.on('authenticated', function() {
571+
return this.on('authenticated', function () {
467572
this.setSyncSchedule(options, callback);
468573
});
469574
}
@@ -507,9 +612,9 @@ netatmo.prototype.setSyncSchedule = function(options, callback) {
507612
url: url,
508613
method: "POST",
509614
form: form,
510-
}, function(err, response, body) {
615+
}, function (err, response, body) {
511616
if (err || response.statusCode != 200) {
512-
return this.handleRequestError(err,response,body,"setSyncSchedule error");
617+
return this.handleRequestError(err, response, body, "setSyncSchedule error");
513618
}
514619

515620
body = JSON.parse(body);
@@ -527,11 +632,16 @@ netatmo.prototype.setSyncSchedule = function(options, callback) {
527632
return this;
528633
};
529634

530-
// https://dev.netatmo.com/doc/methods/setthermpoint
531-
netatmo.prototype.setThermpoint = function(options, callback) {
635+
/**
636+
* https://dev.netatmo.com/doc/methods/setthermpoint
637+
* @param options
638+
* @param callback
639+
* @returns {*}
640+
*/
641+
netatmo.prototype.setThermpoint = function (options, callback) {
532642
// Wait until authenticated.
533643
if (!access_token) {
534-
return this.on('authenticated', function() {
644+
return this.on('authenticated', function () {
535645
this.setThermpoint(options, callback);
536646
});
537647
}
@@ -581,16 +691,16 @@ netatmo.prototype.setThermpoint = function(options, callback) {
581691
url: url,
582692
method: "POST",
583693
form: form,
584-
}, function(err, response, body) {
694+
}, function (err, response, body) {
585695
if (err || response.statusCode != 200) {
586-
return this.handleRequestError(err,response,body,"setThermpoint error");
696+
return this.handleRequestError(err, response, body, "setThermpoint error");
587697
}
588698

589699
body = JSON.parse(body);
590700

591701
console.log(body);
592702

593-
this.emit('get-thermstate', err, body.status);
703+
this.emit('get-thermostatsdata', err, body.status);
594704

595705
if (callback) {
596706
return callback(err, body.status);

0 commit comments

Comments
 (0)