forked from planetk/homebridge-netatmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (68 loc) · 2.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict';
var homebridge;
var NetatmoWeatherStationAccessory, NetatmoThermostatAccessory;
var async = require('async');
// TODO: user info auswerten (metrisch /imperial ...)
module.exports = function (pHomebridge) {
homebridge = pHomebridge;
homebridge.registerPlatform("homebridge-netatmo", "netatmo", NetatmoPlatform);
};
var netatmo = require("netatmo");
var inherits = require('util').inherits;
class NetatmoPlatform {
constructor(log, config) {
this.log = log;
this.config = config || {};
this.foundAccessories = [];
// If this log message is not seen, most likely the config.js is not found.
this.log.debug('Creating NetatmoPlatform');
if (config.mockapi) {
this.log.warn('CAUTION! USING FAKE NETATMO API: ' + config.mockapi);
this.api = require("./lib/netatmo-api-mock")(config.mockapi);
} else {
this.api = new netatmo(config.auth);
}
this.api.on("error", function (error) {
this.log.error('ERROR - Netatmo: ' + error);
}.bind(this));
this.api.on("warning", function (error) {
this.log.warn('WARN - Netatmo: ' + error);
}.bind(this));
}
accessories(callback) {
this.log.debug("Loading accessories");
var calls = this.loadDevices();
async.parallel(calls, function(err, result) {
if (err) {
this.log("Error: " + err);
} else {
for (var i = 0; i < result.length; i++) {
for (var j = 0; j < result[i].length; j++) {
this.foundAccessories.push(result[i][j]);
}
}
}
callback(this.foundAccessories);
}.bind(this));
}
loadDevices() {
var deviceTypes = this.config.deviceTypes || [ "weatherstation", "thermostat", "camera" ];
var calls = [];
deviceTypes.forEach(function(deviceType) {
try {
calls.push(function(callback) {
var DeviceType = require('./device/' + deviceType + '-device.js')(homebridge);
var devType = new DeviceType(this.log, this.api, this.config);
devType.buildAccessoriesForDevices(function(err, deviceAccessories) {
callback(err, deviceAccessories);
});
}.bind(this));
} catch (err) {
this.log("Could not process device " + deviceType);
this.log(err);
this.log(err.stack);
}
}.bind(this));
return calls;
}
}