2 * Copyright (c) 2010-2022 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 {
41 private final OAuthClientService authService;
42 private final String appKey;
43 private final String userName;
44 private final String password;
46 private final AutomowerConnectApi automowerApi;
48 public AutomowerBridge(OAuthClientService authService, String appKey, String userName, String password,
49 HttpClient httpClient, ScheduledExecutorService scheduler) {
50 this.authService = authService;
52 this.userName = userName;
53 this.password = password;
55 this.automowerApi = new AutomowerConnectApi(httpClient);
58 private AccessTokenResponse authenticate() throws AutomowerCommunicationException {
60 AccessTokenResponse result = authService.getAccessTokenResponse();
62 result = authService.getAccessTokenByResourceOwnerPasswordCredentials(userName, password, null);
65 } catch (OAuthException | IOException | OAuthResponseException e) {
66 throw new AutomowerCommunicationException("Unable to authenticate", e);
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
74 public MowerListResult getAutomowers() throws AutomowerCommunicationException {
75 return automowerApi.getMowers(appKey, authenticate().getAccessToken());
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
83 public Mower getAutomowerStatus(String id) throws AutomowerCommunicationException {
84 return automowerApi.getMower(appKey, authenticate().getAccessToken(), id).getData();
88 * Sends a command to the automower with the specified id
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
96 public void sendAutomowerCommand(String id, AutomowerCommand command, long commandDuration)
97 throws AutomowerCommunicationException {
98 MowerCommandAttributes attributes = new MowerCommandAttributes();
99 attributes.setDuration(commandDuration);
101 MowerCommand mowerCommand = new MowerCommand();
102 mowerCommand.setType(command.getCommand());
103 mowerCommand.setAttributes(attributes);
105 MowerCommandRequest request = new MowerCommandRequest();
106 request.setData(mowerCommand);
107 automowerApi.sendCommand(appKey, authenticate().getAccessToken(), id, request);