forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetatmoBridgeHandler.java
371 lines (323 loc) · 14.9 KB
/
NetatmoBridgeHandler.java
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.netatmo.internal.handler;
import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusDetail;
import org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler;
import org.eclipse.smarthome.core.types.Command;
import org.openhab.binding.netatmo.internal.config.NetatmoBridgeConfiguration;
import org.openhab.binding.netatmo.internal.webhook.NAWebhookCameraEvent;
import org.openhab.binding.netatmo.internal.webhook.NAWebhookCameraEventPerson;
import org.openhab.binding.netatmo.internal.webhook.WelcomeWebHookServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.squareup.okhttp.OkHttpClient;
import io.swagger.client.ApiClient;
import io.swagger.client.CollectionFormats.CSVParams;
import io.swagger.client.api.HealthyhomecoachApi;
import io.swagger.client.api.PartnerApi;
import io.swagger.client.api.StationApi;
import io.swagger.client.api.ThermostatApi;
import io.swagger.client.api.WelcomeApi;
import io.swagger.client.auth.OAuth;
import io.swagger.client.auth.OAuthFlow;
import io.swagger.client.model.NAHealthyHomeCoachDataBody;
import io.swagger.client.model.NAMeasureBodyElem;
import io.swagger.client.model.NAStationDataBody;
import io.swagger.client.model.NAThermostatDataBody;
import io.swagger.client.model.NAWelcomeHomeData;
import retrofit.RestAdapter.LogLevel;
import retrofit.RetrofitError;
import retrofit.RetrofitError.Kind;
/**
* {@link NetatmoBridgeHandler} is the handler for a Netatmo API and connects it
* to the framework. The devices and modules uses the
* {@link NetatmoBridgeHandler} to request informations about their status
*
* @author Gaël L'hopital - Initial contribution OH2 version
* @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules
*
*/
public class NetatmoBridgeHandler extends BaseBridgeHandler {
private Logger logger = LoggerFactory.getLogger(NetatmoBridgeHandler.class);
public NetatmoBridgeConfiguration configuration;
private ScheduledFuture<?> refreshJob;
private APIMap apiMap;
private WelcomeWebHookServlet webHookServlet;
private List<NetatmoDataListener> dataListeners = new CopyOnWriteArrayList<>();
@NonNullByDefault
private class APIMap extends HashMap<Class<?>, Object> {
private static final long serialVersionUID = -2024031764691952343L;
private ApiClient apiClient;
public APIMap(ApiClient apiClient) {
super();
this.apiClient = apiClient;
}
public Object get(Class<?> apiClass) {
if (!super.containsKey(apiClass)) {
Object api = apiClient.createService(apiClass);
super.put(apiClass, api);
}
return super.get(apiClass);
}
}
public NetatmoBridgeHandler(@NonNull Bridge bridge, WelcomeWebHookServlet webHookServlet) {
super(bridge);
this.webHookServlet = webHookServlet;
}
@Override
public void initialize() {
logger.debug("Initializing Netatmo API bridge handler.");
configuration = getConfigAs(NetatmoBridgeConfiguration.class);
scheduleTokenInitAndRefresh();
}
private void connectionSucceed() {
updateStatus(ThingStatus.ONLINE);
String webHookURI = getWebHookURI();
if (webHookURI != null) {
webHookServlet.activate(this);
logger.debug("Setting up Netatmo Welcome WebHook");
getWelcomeApi().addwebhook(webHookURI, WEBHOOK_APP);
}
}
private void scheduleTokenInitAndRefresh() {
refreshJob = scheduler.scheduleWithFixedDelay(() -> {
logger.debug("Initializing API Connection and scheduling token refresh every {}s",
configuration.reconnectInterval);
try {
initializeApiClient();
// I use a connection to Netatmo API using PartnerAPI to ensure that API is reachable
getPartnerApi().partnerdevices();
connectionSucceed();
} catch (RetrofitError e) {
if (e.getKind() == Kind.NETWORK) {
logger.warn("Network error while connecting to Netatmo API, will retry in {} s",
configuration.reconnectInterval);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Netatmo Access Failed, will retry in " + configuration.reconnectInterval + " seconds.");
} else {
switch (e.getResponse().getStatus()) {
case 404: // If no partner station has been associated - likely to happen - we'll have this
// error
// but it means connection to API is OK
connectionSucceed();
break;
case 403: // Forbidden Access maybe too many requests ? Let's wait next cycle
logger.warn("Error 403 while connecting to Netatmo API, will retry in {} s",
configuration.reconnectInterval);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Netatmo Access Forbidden, will retry in " + configuration.reconnectInterval
+ " seconds.");
break;
default:
if (logger.isDebugEnabled()) {
// we also attach the stack trace
logger.error("Unable to connect Netatmo API : {}", e.getMessage(), e);
} else {
logger.error("Unable to connect Netatmo API : {}", e.getMessage());
}
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Unable to connect Netatmo API : " + e.getLocalizedMessage());
return;
}
}
}
// We'll do this every x seconds to guaranty token refresh
}, 2, configuration.reconnectInterval, TimeUnit.SECONDS);
}
private void initializeApiClient() throws RetrofitError {
ApiClient apiClient = new ApiClient();
OAuth auth = new OAuth(new OkHttpClient(),
OAuthClientRequest.tokenLocation("https://api.netatmo.net/oauth2/token"));
auth.setFlow(OAuthFlow.password);
auth.setAuthenticationRequestBuilder(OAuthClientRequest.authorizationLocation(""));
apiClient.getApiAuthorizations().put("password_oauth", auth);
apiClient.getTokenEndPoint().setClientId(configuration.clientId).setClientSecret(configuration.clientSecret)
.setUsername(configuration.username).setPassword(configuration.password).setScope(getApiScope());
apiClient.configureFromOkclient(new OkHttpClient());
apiClient.getAdapterBuilder().setLogLevel(logger.isDebugEnabled() ? LogLevel.FULL : LogLevel.NONE);
apiMap = new APIMap(apiClient);
}
private String getApiScope() {
List<String> scopes = new ArrayList<>();
if (configuration.readStation) {
scopes.add("read_station");
}
if (configuration.readThermostat) {
scopes.add("read_thermostat");
scopes.add("write_thermostat");
}
if (configuration.readHealthyHomeCoach) {
scopes.add("read_homecoach");
}
if (configuration.readWelcome) {
scopes.add("read_camera");
scopes.add("access_camera");
scopes.add("write_camera");
}
return String.join(" ", scopes);
}
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
logger.debug("Netatmo Bridge is read-only and does not handle commands");
}
public PartnerApi getPartnerApi() {
return (PartnerApi) apiMap.get(PartnerApi.class);
}
private StationApi getStationApi() {
return (StationApi) apiMap.get(StationApi.class);
}
private HealthyhomecoachApi getHomeCoachApi() {
return (HealthyhomecoachApi) apiMap.get(HealthyhomecoachApi.class);
}
public ThermostatApi getThermostatApi() {
return (ThermostatApi) apiMap.get(ThermostatApi.class);
}
public WelcomeApi getWelcomeApi() {
return (WelcomeApi) apiMap.get(WelcomeApi.class);
}
@Override
public void dispose() {
logger.debug("Running dispose()");
if (getWebHookURI() != null) {
logger.debug("Releasing Netatmo Welcome WebHook");
webHookServlet.deactivate();
getWelcomeApi().dropwebhook(WEBHOOK_APP);
}
if (refreshJob != null && !refreshJob.isCancelled()) {
refreshJob.cancel(true);
refreshJob = null;
}
}
public NAStationDataBody getStationsDataBody(String equipmentId) {
NAStationDataBody data = getStationApi().getstationsdata(equipmentId).getBody();
updateStatus(ThingStatus.ONLINE);
return data;
}
public List<Float> getStationMeasureResponses(String equipmentId, String moduleId, String scale,
List<@NonNull String> types) {
List<NAMeasureBodyElem> data = getStationApi()
.getmeasure(equipmentId, scale, new CSVParams(types), moduleId, null, "last", 1, true, false).getBody();
updateStatus(ThingStatus.ONLINE);
NAMeasureBodyElem element = data.get(0);
return element != null ? element.getValue().get(0) : Collections.emptyList();
}
public NAHealthyHomeCoachDataBody getHomecoachDataBody(String equipmentId) {
NAHealthyHomeCoachDataBody data = getHomeCoachApi().gethomecoachsdata(equipmentId).getBody();
updateStatus(ThingStatus.ONLINE);
return data;
}
public NAThermostatDataBody getThermostatsDataBody(String equipmentId) {
NAThermostatDataBody data = getThermostatApi().getthermostatsdata(equipmentId).getBody();
updateStatus(ThingStatus.ONLINE);
return data;
}
public NAWelcomeHomeData getWelcomeDataBody(String homeId) {
NAWelcomeHomeData data = getWelcomeApi().gethomedata(homeId, null).getBody();
updateStatus(ThingStatus.ONLINE);
return data;
}
/**
* Returns the Url of the picture
*
* @return Url of the picture or UnDefType.UNDEF
*/
public String getPictureUrl(String id, String key) {
StringBuilder ret = new StringBuilder();
if (id != null && key != null) {
ret.append(WELCOME_PICTURE_URL).append("?").append(WELCOME_PICTURE_IMAGEID).append("=").append(id)
.append("&").append(WELCOME_PICTURE_KEY).append("=").append(key);
}
return ret.toString();
}
public Optional<AbstractNetatmoThingHandler> findNAThing(String searchedId) {
List<Thing> things = getThing().getThings();
Stream<AbstractNetatmoThingHandler> naHandlers = things.stream().map(Thing::getHandler)
.filter(AbstractNetatmoThingHandler.class::isInstance).map(AbstractNetatmoThingHandler.class::cast)
.filter(handler -> handler.matchesId(searchedId));
return naHandlers.findAny();
}
public void webHookEvent(NAWebhookCameraEvent event) {
// This currently the only known event type but I suspect usage can grow in the future...
if (event.getAppType() == NAWebhookCameraEvent.AppTypeEnum.CAMERA) {
Set<AbstractNetatmoThingHandler> modules = new HashSet<>();
if (WELCOME_EVENTS.contains(event.getEventType()) || PRESENCE_EVENTS.contains(event.getEventType())) {
String cameraId = event.getCameraId();
if (cameraId != null) {
Optional<AbstractNetatmoThingHandler> camera = findNAThing(cameraId);
camera.ifPresent(modules::add);
}
}
if (HOME_EVENTS.contains(event.getEventType())) {
String homeId = event.getHomeId();
if (homeId != null) {
Optional<AbstractNetatmoThingHandler> home = findNAThing(homeId);
home.ifPresent(modules::add);
}
}
if (PERSON_EVENTS.contains(event.getEventType())) {
List<NAWebhookCameraEventPerson> persons = event.getPersons();
persons.forEach(person -> {
String personId = person.getId();
if (personId != null) {
Optional<AbstractNetatmoThingHandler> personHandler = findNAThing(personId);
personHandler.ifPresent(modules::add);
}
});
}
modules.forEach(module -> {
Channel channel = module.getThing().getChannel(CHANNEL_WELCOME_HOME_EVENT);
if (channel != null) {
triggerChannel(channel.getUID(), event.getEventType().toString());
}
});
}
}
private String getWebHookURI() {
String webHookURI = null;
if (configuration.webHookUrl != null && configuration.readWelcome && webHookServlet != null) {
webHookURI = configuration.webHookUrl + webHookServlet.getPath();
}
return webHookURI;
}
public boolean registerDataListener(@NonNull NetatmoDataListener dataListener) {
return dataListeners.add(dataListener);
}
public boolean unregisterDataListener(@NonNull NetatmoDataListener dataListener) {
return dataListeners.remove(dataListener);
}
public void checkForNewThings(Object data) {
for (NetatmoDataListener dataListener : dataListeners) {
dataListener.onDataRefreshed(data);
}
}
}