]> git.basschouten.com Git - openhab-addons.git/blob
7bca814129efc37949462605ffbddff8a0459f70
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.automower.internal.rest.api.automowerconnect;
14
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jetty.client.HttpClient;
21 import org.eclipse.jetty.client.api.ContentResponse;
22 import org.eclipse.jetty.client.api.Request;
23 import org.eclipse.jetty.client.util.StringContentProvider;
24 import org.eclipse.jetty.http.HttpMethod;
25 import org.eclipse.jetty.http.HttpStatus;
26 import org.openhab.binding.automower.internal.rest.api.HusqvarnaApi;
27 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCommandRequest;
28 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerListResult;
29 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerResult;
30 import org.openhab.binding.automower.internal.rest.exceptions.AutomowerCommunicationException;
31 import org.openhab.binding.automower.internal.rest.exceptions.UnauthorizedException;
32
33 import com.google.gson.JsonSyntaxException;
34
35 /**
36  * Allows access to the AutomowerConnectApi
37  *
38  * @author Markus Pfleger - Initial contribution
39  */
40 @NonNullByDefault
41 public class AutomowerConnectApi extends HusqvarnaApi {
42
43     public AutomowerConnectApi(HttpClient httpClient) {
44         super(httpClient);
45     }
46
47     @Override
48     public String getBaseUrl() {
49         return "https://api.amc.husqvarna.dev/v1";
50     }
51
52     public MowerListResult getMowers(String appKey, String token) throws AutomowerCommunicationException {
53         final Request request = getHttpClient().newRequest(getBaseUrl() + "/mowers");
54         request.method(HttpMethod.GET);
55
56         ContentResponse response = executeRequest(appKey, token, request);
57
58         return parseResponse(response, MowerListResult.class);
59     }
60
61     public MowerResult getMower(String appKey, String token, String mowerId) throws AutomowerCommunicationException {
62         final Request request = getHttpClient().newRequest(getBaseUrl() + "/mowers/" + mowerId);
63         request.method(HttpMethod.GET);
64
65         ContentResponse response = executeRequest(appKey, token, request);
66
67         return parseResponse(response, MowerResult.class);
68     }
69
70     public void sendCommand(String appKey, String token, String id, MowerCommandRequest command)
71             throws AutomowerCommunicationException {
72         final Request request = getHttpClient().newRequest(getBaseUrl() + "/mowers/" + id + "/actions");
73         request.method(HttpMethod.POST);
74
75         request.content(new StringContentProvider(gson.toJson(command)));
76
77         ContentResponse response = executeRequest(appKey, token, request);
78
79         checkForError(response, response.getStatus());
80     }
81
82     private ContentResponse executeRequest(String appKey, String token, final Request request)
83             throws AutomowerCommunicationException {
84
85         request.timeout(10, TimeUnit.SECONDS);
86
87         request.header("Authorization-Provider", "husqvarna");
88         request.header("Authorization", "Bearer " + token);
89         request.header("X-Api-Key", appKey);
90         request.header("Content-Type", "application/vnd.api+json");
91
92         ContentResponse response;
93         try {
94             response = request.send();
95         } catch (InterruptedException | TimeoutException | ExecutionException e) {
96             throw new AutomowerCommunicationException(e);
97         }
98         return response;
99     }
100
101     private <T> T parseResponse(ContentResponse response, Class<T> type) throws AutomowerCommunicationException {
102         int statusCode = response.getStatus();
103
104         checkForError(response, statusCode);
105
106         try {
107             return gson.fromJson(response.getContentAsString(), type);
108         } catch (JsonSyntaxException e) {
109             throw new AutomowerCommunicationException(e);
110         }
111     }
112
113     private void checkForError(ContentResponse response, int statusCode) throws AutomowerCommunicationException {
114         if (statusCode >= 200 && statusCode < 300) {
115             return;
116         }
117
118         switch (statusCode) {
119             case HttpStatus.NOT_FOUND_404:
120                 throw new AutomowerCommunicationException(statusCode, "Target '" + response.getRequest().getURI()
121                         + "' seems to be not available: " + response.getContentAsString());
122
123             case HttpStatus.FORBIDDEN_403:
124             case HttpStatus.UNAUTHORIZED_401:
125                 throw new UnauthorizedException(statusCode, response.getContentAsString());
126
127             default:
128                 throw new AutomowerCommunicationException(statusCode, response.getContentAsString());
129         }
130     }
131 }