2 * Copyright (c) 2010-2023 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.time.Instant;
17 import java.util.concurrent.ScheduledExecutorService;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jetty.client.HttpClient;
21 import org.openhab.binding.automower.internal.rest.api.automowerconnect.AutomowerConnectApi;
22 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.Mower;
23 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCommand;
24 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCommandAttributes;
25 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCommandRequest;
26 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerListResult;
27 import org.openhab.binding.automower.internal.rest.exceptions.AutomowerCommunicationException;
28 import org.openhab.binding.automower.internal.things.AutomowerCommand;
29 import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
30 import org.openhab.core.auth.client.oauth2.OAuthClientService;
31 import org.openhab.core.auth.client.oauth2.OAuthException;
32 import org.openhab.core.auth.client.oauth2.OAuthResponseException;
35 * The {@link AutomowerBridge} allows the communication to the various Husqvarna rest apis like the
36 * AutomowerConnectApi or the AuthenticationApi
38 * @author Markus Pfleger - Initial contribution
41 public class AutomowerBridge {
42 private final OAuthClientService authService;
43 private final String appKey;
45 private final AutomowerConnectApi automowerApi;
47 public AutomowerBridge(OAuthClientService authService, String appKey, HttpClient httpClient,
48 ScheduledExecutorService scheduler) {
49 this.authService = authService;
52 this.automowerApi = new AutomowerConnectApi(httpClient);
55 private AccessTokenResponse authenticate() throws AutomowerCommunicationException {
57 AccessTokenResponse result = authService.getAccessTokenResponse();
58 if (result == null || result.isExpired(Instant.now(), 120)) {
59 result = authService.getAccessTokenByClientCredentials(null);
62 } catch (OAuthException | IOException | OAuthResponseException e) {
63 throw new AutomowerCommunicationException("Unable to authenticate", e);
68 * @return A result containing a list of mowers that are available for the current user
69 * @throws AutomowerCommunicationException In case the query cannot be executed successfully
71 public MowerListResult getAutomowers() throws AutomowerCommunicationException {
72 return automowerApi.getMowers(appKey, authenticate().getAccessToken());
76 * @param id The id of the mower to query
77 * @return A detailed status of the mower with the specified id
78 * @throws AutomowerCommunicationException In case the query cannot be executed successfully
80 public Mower getAutomowerStatus(String id) throws AutomowerCommunicationException {
81 return automowerApi.getMower(appKey, authenticate().getAccessToken(), id).getData();
85 * Sends a command to the automower with the specified id
87 * @param id The id of the mower
88 * @param command The command that should be sent. Valid values are: "Start", "ResumeSchedule", "Pause", "Park",
89 * "ParkUntilNextSchedule", "ParkUntilFurtherNotice"
90 * @param commandDuration The duration of the command. This is only evaluated for "Start" and "Park" commands
91 * @throws AutomowerCommunicationException In case the query cannot be executed successfully
93 public void sendAutomowerCommand(String id, AutomowerCommand command, long commandDuration)
94 throws AutomowerCommunicationException {
95 MowerCommandAttributes attributes = new MowerCommandAttributes();
96 attributes.setDuration(commandDuration);
98 MowerCommand mowerCommand = new MowerCommand();
99 mowerCommand.setType(command.getCommand());
100 mowerCommand.setAttributes(attributes);
102 MowerCommandRequest request = new MowerCommandRequest();
103 request.setData(mowerCommand);
104 automowerApi.sendCommand(appKey, authenticate().getAccessToken(), id, request);