]> git.basschouten.com Git - openhab-addons.git/blob
98522a5d7665582ebbc2d9d2692e219e23db5137
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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     public AutomowerConnectApi(HttpClient httpClient) {
43         super(httpClient);
44     }
45
46     @Override
47     public String getBaseUrl() {
48         return "https://api.amc.husqvarna.dev/v1";
49     }
50
51     public MowerListResult getMowers(String appKey, String token) throws AutomowerCommunicationException {
52         final Request request = getHttpClient().newRequest(getBaseUrl() + "/mowers");
53         request.method(HttpMethod.GET);
54
55         ContentResponse response = executeRequest(appKey, token, request);
56
57         return parseResponse(response, MowerListResult.class);
58     }
59
60     public MowerResult getMower(String appKey, String token, String mowerId) throws AutomowerCommunicationException {
61         final Request request = getHttpClient().newRequest(getBaseUrl() + "/mowers/" + mowerId);
62         request.method(HttpMethod.GET);
63
64         ContentResponse response = executeRequest(appKey, token, request);
65
66         return parseResponse(response, MowerResult.class);
67     }
68
69     public void sendCommand(String appKey, String token, String id, MowerCommandRequest command)
70             throws AutomowerCommunicationException {
71         final Request request = getHttpClient().newRequest(getBaseUrl() + "/mowers/" + id + "/actions");
72         request.method(HttpMethod.POST);
73
74         request.content(new StringContentProvider(gson.toJson(command)));
75
76         ContentResponse response = executeRequest(appKey, token, request);
77
78         checkForError(response, response.getStatus());
79     }
80
81     private ContentResponse executeRequest(String appKey, String token, final Request request)
82             throws AutomowerCommunicationException {
83         request.timeout(10, TimeUnit.SECONDS);
84
85         request.header("Authorization-Provider", "husqvarna");
86         request.header("Authorization", "Bearer " + token);
87         request.header("X-Api-Key", appKey);
88         request.header("Content-Type", "application/vnd.api+json");
89
90         ContentResponse response;
91         try {
92             response = request.send();
93         } catch (InterruptedException | TimeoutException | ExecutionException e) {
94             throw new AutomowerCommunicationException(e);
95         }
96         return response;
97     }
98
99     private <T> T parseResponse(ContentResponse response, Class<T> type) throws AutomowerCommunicationException {
100         int statusCode = response.getStatus();
101
102         checkForError(response, statusCode);
103
104         try {
105             return gson.fromJson(response.getContentAsString(), type);
106         } catch (JsonSyntaxException e) {
107             throw new AutomowerCommunicationException(e);
108         }
109     }
110
111     private void checkForError(ContentResponse response, int statusCode) throws AutomowerCommunicationException {
112         if (statusCode >= 200 && statusCode < 300) {
113             return;
114         }
115
116         switch (statusCode) {
117             case HttpStatus.NOT_FOUND_404:
118                 throw new AutomowerCommunicationException(statusCode, "Target '" + response.getRequest().getURI()
119                         + "' seems to be not available: " + response.getContentAsString());
120
121             case HttpStatus.FORBIDDEN_403:
122             case HttpStatus.UNAUTHORIZED_401:
123                 throw new UnauthorizedException(statusCode, response.getContentAsString());
124
125             default:
126                 throw new AutomowerCommunicationException(statusCode, response.getContentAsString());
127         }
128     }
129 }