]> git.basschouten.com Git - openhab-addons.git/blob
4d086129f6927897788dba97f5551bb9e0064580
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.bridge;
14
15 import java.io.IOException;
16 import java.util.concurrent.ScheduledExecutorService;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jetty.client.HttpClient;
20 import org.openhab.binding.automower.internal.rest.api.automowerconnect.AutomowerConnectApi;
21 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.Mower;
22 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCommand;
23 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCommandAttributes;
24 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCommandRequest;
25 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerListResult;
26 import org.openhab.binding.automower.internal.rest.exceptions.AutomowerCommunicationException;
27 import org.openhab.binding.automower.internal.things.AutomowerCommand;
28 import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
29 import org.openhab.core.auth.client.oauth2.OAuthClientService;
30 import org.openhab.core.auth.client.oauth2.OAuthException;
31 import org.openhab.core.auth.client.oauth2.OAuthResponseException;
32
33 /**
34  * The {@link AutomowerBridge} allows the communication to the various Husqvarna rest apis like the
35  * AutomowerConnectApi or the AuthenticationApi
36  *
37  * @author Markus Pfleger - Initial contribution
38  */
39 @NonNullByDefault
40 public class AutomowerBridge {
41     private final OAuthClientService authService;
42     private final String appKey;
43
44     private final AutomowerConnectApi automowerApi;
45
46     public AutomowerBridge(OAuthClientService authService, String appKey, HttpClient httpClient,
47             ScheduledExecutorService scheduler) {
48         this.authService = authService;
49         this.appKey = appKey;
50
51         this.automowerApi = new AutomowerConnectApi(httpClient);
52     }
53
54     private AccessTokenResponse authenticate() throws AutomowerCommunicationException {
55         try {
56             AccessTokenResponse result = authService.getAccessTokenResponse();
57             if (result == null) {
58                 result = authService.getAccessTokenByClientCredentials(null);
59             }
60             return result;
61         } catch (OAuthException | IOException | OAuthResponseException e) {
62             throw new AutomowerCommunicationException("Unable to authenticate", e);
63         }
64     }
65
66     /**
67      * @return A result containing a list of mowers that are available for the current user
68      * @throws AutomowerCommunicationException In case the query cannot be executed successfully
69      */
70     public MowerListResult getAutomowers() throws AutomowerCommunicationException {
71         return automowerApi.getMowers(appKey, authenticate().getAccessToken());
72     }
73
74     /**
75      * @param id The id of the mower to query
76      * @return A detailed status of the mower with the specified id
77      * @throws AutomowerCommunicationException In case the query cannot be executed successfully
78      */
79     public Mower getAutomowerStatus(String id) throws AutomowerCommunicationException {
80         return automowerApi.getMower(appKey, authenticate().getAccessToken(), id).getData();
81     }
82
83     /**
84      * Sends a command to the automower with the specified id
85      *
86      * @param id The id of the mower
87      * @param command The command that should be sent. Valid values are: "Start", "ResumeSchedule", "Pause", "Park",
88      *            "ParkUntilNextSchedule", "ParkUntilFurtherNotice"
89      * @param commandDuration The duration of the command. This is only evaluated for "Start" and "Park" commands
90      * @throws AutomowerCommunicationException In case the query cannot be executed successfully
91      */
92     public void sendAutomowerCommand(String id, AutomowerCommand command, long commandDuration)
93             throws AutomowerCommunicationException {
94         MowerCommandAttributes attributes = new MowerCommandAttributes();
95         attributes.setDuration(commandDuration);
96
97         MowerCommand mowerCommand = new MowerCommand();
98         mowerCommand.setType(command.getCommand());
99         mowerCommand.setAttributes(attributes);
100
101         MowerCommandRequest request = new MowerCommandRequest();
102         request.setData(mowerCommand);
103         automowerApi.sendCommand(appKey, authenticate().getAccessToken(), id, request);
104     }
105 }