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