forked from PArns/ioBroker.netatmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
163 lines (122 loc) · 4.6 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* jshint -W097 */// jshint strict:false
/*jslint node: true */
"use strict";
var utils = require(__dirname + '/lib/utils');
var adapter = utils.Adapter('netatmo');
var netatmo = require('./netatmoLib');
var api = null;
var NetatmoCoach = require("./netatmoCoach");
var coach = null;
var NetatmoStation = require("./netatmoStation");
var station = null;
var NetatmoWelcome = require("./netatmoWelcome");
var welcome = null;
var _deviceUpdateTimer;
var _welcomeUpdateTimer;
String.prototype.replaceAll = function (search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
adapter.on('message', function (obj) {
adapter.log.info(JSON.stringify(obj));
if (obj.command === "send") {
obj.command = obj.message;
obj.message = null;
}
if (obj) {
switch (obj.command) {
case 'setAway':
if (welcome) {
welcome.setAway(obj.message);
}
if (obj.callback)
adapter.sendTo(obj.from, obj.command, {}, obj.callback);
break;
default:
adapter.log.warn("Unknown command: " + obj.command);
break;
}
}
return true;
});
adapter.on('unload', function (callback) {
try {
if (welcome)
welcome.finalize();
adapter.log.info('cleaned everything up...');
callback();
} catch (e) {
callback();
}
});
adapter.on('ready', function () {
if (adapter.config.username && adapter.config.password) {
var scope = "";
var id = "574ddd152baa3cf9598b46cd";
var secret = "6e3UcBKp005k9N0tpwp69fGYECqOpuhtEE9sWJW";
// Backward compatibility begin ...
// --------------------------------------------------------
// If nothing is set, activate at least the Weatherstation
if (!(adapter.config.netatmoCoach || adapter.config.netatmoWeather || adapter.config.netatmoWelcome)) {
adapter.log.info("No product was choosen, using Weatherstation as default!");
adapter.config.netatmoWeather = true;
}
if (!adapter.config.check_interval)
adapter.config.check_interval = 5;
if (!adapter.config.cleanup_interval)
adapter.config.cleanup_interval = 60;
if (!adapter.config.unknown_person_time)
adapter.config.unknown_person_time = 24;
if (!adapter.config.location_elevation)
adapter.config.location_elevation = 0;
if (adapter.config.netatmoWeather) {
scope += " read_station";
}
if (adapter.config.netatmoCoach) {
scope += " read_homecoach";
}
// --------------------------------------------------------
// Backward compatibility end ...
if (adapter.config.netatmoWelcome) {
scope += " read_camera read_presence";
if (adapter.config.id && adapter.config.secret) {
id = adapter.config.id;
secret = adapter.config.secret;
scope += " access_camera access_presence write_camera"
}
}
scope = scope.trim();
var auth = {
"client_id": id,
"client_secret": secret,
"scope": scope,
"username": adapter.config.username,
"password": adapter.config.password
};
api = new netatmo(auth);
api.setAdapter(adapter);
if (adapter.config.netatmoCoach) {
coach = new NetatmoCoach(api, adapter);
coach.requestUpdateCoachStation();
_deviceUpdateTimer = setInterval(function () {
coach.requestUpdateCoachStation();
}, adapter.config.check_interval * 60 * 1000);
}
if (adapter.config.netatmoWeather) {
station = new NetatmoStation(api, adapter);
station.requestUpdateWeatherStation();
_deviceUpdateTimer = setInterval(function () {
station.requestUpdateWeatherStation();
}, adapter.config.check_interval * 60 * 1000);
}
if (adapter.config.netatmoWelcome) {
welcome = new NetatmoWelcome(api, adapter);
welcome.init();
welcome.requestUpdateIndoorCamera();
_welcomeUpdateTimer = setInterval(function () {
welcome.requestUpdateIndoorCamera();
}, adapter.config.check_interval * 2 * 60 * 1000);
}
} else
adapter.log.error("Please add username, password and choose at least one product within the adapter settings!");
});