]> git.basschouten.com Git - openhab-addons.git/blob
f9c2fb826096b84c7dafefbfb01ad2c70e938de9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.time.Instant;
17 import java.util.concurrent.ScheduledExecutorService;
18
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;
33
34 /**
35  * The {@link AutomowerBridge} allows the communication to the various Husqvarna rest apis like the
36  * AutomowerConnectApi or the AuthenticationApi
37  *
38  * @author Markus Pfleger - Initial contribution
39  */
40 @NonNullByDefault
41 public class AutomowerBridge {
42     private final OAuthClientService authService;
43     private final String appKey;
44
45     private final AutomowerConnectApi automowerApi;
46
47     public AutomowerBridge(OAuthClientService authService, String appKey, HttpClient httpClient,
48             ScheduledExecutorService scheduler) {
49         this.authService = authService;
50         this.appKey = appKey;
51
52         this.automowerApi = new AutomowerConnectApi(httpClient);
53     }
54
55     private AccessTokenResponse authenticate() throws AutomowerCommunicationException {
56         try {
57             AccessTokenResponse result = authService.getAccessTokenResponse();
58             if (result == null || result.isExpired(Instant.now(), 120)) {
59                 result = authService.getAccessTokenByClientCredentials(null);
60             }
61             return result;
62         } catch (OAuthException | IOException | OAuthResponseException e) {
63             throw new AutomowerCommunicationException("Unable to authenticate", e);
64         }
65     }
66
67     /**
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
70      */
71     public MowerListResult getAutomowers() throws AutomowerCommunicationException {
72         return automowerApi.getMowers(appKey, authenticate().getAccessToken());
73     }
74
75     /**
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
79      */
80     public Mower getAutomowerStatus(String id) throws AutomowerCommunicationException {
81         return automowerApi.getMower(appKey, authenticate().getAccessToken(), id).getData();
82     }
83
84     /**
85      * Sends a command to the automower with the specified id
86      *
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
92      */
93     public void sendAutomowerCommand(String id, AutomowerCommand command, long commandDuration)
94             throws AutomowerCommunicationException {
95         MowerCommandAttributes attributes = new MowerCommandAttributes();
96         attributes.setDuration(commandDuration);
97
98         MowerCommand mowerCommand = new MowerCommand();
99         mowerCommand.setType(command.getCommand());
100         mowerCommand.setAttributes(attributes);
101
102         MowerCommandRequest request = new MowerCommandRequest();
103         request.setData(mowerCommand);
104         automowerApi.sendCommand(appKey, authenticate().getAccessToken(), id, request);
105     }
106 }