2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.automower.internal.bridge;
15 import java.io.IOException;
16 import java.util.concurrent.ScheduledExecutorService;
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;
34 * The {@link AutomowerBridge} allows the communication to the various Husqvarna rest apis like the
35 * AutomowerConnectApi or the AuthenticationApi
37 * @author Markus Pfleger - Initial contribution
40 public class AutomowerBridge {
42 private final OAuthClientService authService;
43 private final String appKey;
44 private final String userName;
45 private final String password;
47 private final AutomowerConnectApi automowerApi;
49 public AutomowerBridge(OAuthClientService authService, String appKey, String userName, String password,
50 HttpClient httpClient, ScheduledExecutorService scheduler) {
51 this.authService = authService;
53 this.userName = userName;
54 this.password = password;
56 this.automowerApi = new AutomowerConnectApi(httpClient);
59 private AccessTokenResponse authenticate() throws AutomowerCommunicationException {
61 AccessTokenResponse result = authService.getAccessTokenResponse();
63 result = authService.getAccessTokenByResourceOwnerPasswordCredentials(userName, password, null);
66 } catch (OAuthException | IOException | OAuthResponseException e) {
67 throw new AutomowerCommunicationException("Unable to authenticate", e);
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
75 public MowerListResult getAutomowers() throws AutomowerCommunicationException {
76 return automowerApi.getMowers(appKey, authenticate().getAccessToken());
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
84 public Mower getAutomowerStatus(String id) throws AutomowerCommunicationException {
85 return automowerApi.getMower(appKey, authenticate().getAccessToken(), id).getData();
89 * Sends a command to the automower with the specified id
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
97 public void sendAutomowerCommand(String id, AutomowerCommand command, long commandDuration)
98 throws AutomowerCommunicationException {
100 MowerCommandAttributes attributes = new MowerCommandAttributes();
101 attributes.setDuration(commandDuration);
103 MowerCommand mowerCommand = new MowerCommand();
104 mowerCommand.setType(command.getCommand());
105 mowerCommand.setAttributes(attributes);
107 MowerCommandRequest request = new MowerCommandRequest();
108 request.setData(mowerCommand);
109 automowerApi.sendCommand(appKey, authenticate().getAccessToken(), id, request);