]> git.basschouten.com Git - openhab-addons.git/blob
d24c6cdba1d26365322fc506abef8b9da4a9bb6d
[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.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     private final String userName;
44     private final String password;
45
46     private final AutomowerConnectApi automowerApi;
47
48     public AutomowerBridge(OAuthClientService authService, String appKey, String userName, String password,
49             HttpClient httpClient, ScheduledExecutorService scheduler) {
50         this.authService = authService;
51         this.appKey = appKey;
52         this.userName = userName;
53         this.password = password;
54
55         this.automowerApi = new AutomowerConnectApi(httpClient);
56     }
57
58     private AccessTokenResponse authenticate() throws AutomowerCommunicationException {
59         try {
60             AccessTokenResponse result = authService.getAccessTokenResponse();
61             if (result == null) {
62                 result = authService.getAccessTokenByResourceOwnerPasswordCredentials(userName, password, null);
63             }
64             return result;
65         } catch (OAuthException | IOException | OAuthResponseException e) {
66             throw new AutomowerCommunicationException("Unable to authenticate", e);
67         }
68     }
69
70     /**
71      * @return A result containing a list of mowers that are available for the current user
72      * @throws AutomowerCommunicationException In case the query cannot be executed successfully
73      */
74     public MowerListResult getAutomowers() throws AutomowerCommunicationException {
75         return automowerApi.getMowers(appKey, authenticate().getAccessToken());
76     }
77
78     /**
79      * @param id The id of the mower to query
80      * @return A detailed status of the mower with the specified id
81      * @throws AutomowerCommunicationException In case the query cannot be executed successfully
82      */
83     public Mower getAutomowerStatus(String id) throws AutomowerCommunicationException {
84         return automowerApi.getMower(appKey, authenticate().getAccessToken(), id).getData();
85     }
86
87     /**
88      * Sends a command to the automower with the specified id
89      *
90      * @param id The id of the mower
91      * @param command The command that should be sent. Valid values are: "Start", "ResumeSchedule", "Pause", "Park",
92      *            "ParkUntilNextSchedule", "ParkUntilFurtherNotice"
93      * @param commandDuration The duration of the command. This is only evaluated for "Start" and "Park" commands
94      * @throws AutomowerCommunicationException In case the query cannot be executed successfully
95      */
96     public void sendAutomowerCommand(String id, AutomowerCommand command, long commandDuration)
97             throws AutomowerCommunicationException {
98         MowerCommandAttributes attributes = new MowerCommandAttributes();
99         attributes.setDuration(commandDuration);
100
101         MowerCommand mowerCommand = new MowerCommand();
102         mowerCommand.setType(command.getCommand());
103         mowerCommand.setAttributes(attributes);
104
105         MowerCommandRequest request = new MowerCommandRequest();
106         request.setData(mowerCommand);
107         automowerApi.sendCommand(appKey, authenticate().getAccessToken(), id, request);
108     }
109 }