]> git.basschouten.com Git - openhab-addons.git/blob
ea313e7717cf16737d91a96bd11623977a431cfa
[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.opengarage.internal;
14
15 import java.io.IOException;
16
17 import org.openhab.binding.opengarage.internal.api.ControllerVariables;
18 import org.openhab.binding.opengarage.internal.api.Enums.OpenGarageCommand;
19 import org.openhab.core.io.net.http.HttpUtil;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Handles performing the actual HTTP requests for communicating with Opengarage units.
25  *
26  * @author Paul Smedley - Initial Contribution
27  *
28  */
29 public class OpenGarageWebTargets {
30     public static int DEFAULT_TIMEOUT_MS = 30000;
31
32     private String getControllerVariablesUri;
33     private String changeControllerVariablesUri;
34     private final Logger logger = LoggerFactory.getLogger(OpenGarageWebTargets.class);
35     private int timeoutMs;
36
37     public OpenGarageWebTargets(String ipAddress, long port, String password, int timeoutMs) {
38         String baseUri = "http://" + ipAddress + ":" + port + "/";
39         this.timeoutMs = timeoutMs;
40         this.getControllerVariablesUri = baseUri + "jc";
41         this.changeControllerVariablesUri = baseUri + "cc?dkey=" + password;
42     }
43
44     public ControllerVariables getControllerVariables() throws OpenGarageCommunicationException {
45         String response = invoke(getControllerVariablesUri);
46         return ControllerVariables.parse(response);
47     }
48
49     public void setControllerVariables(OpenGarageCommand request) throws OpenGarageCommunicationException {
50         logger.debug("Received request: {}", request);
51         String queryParams = null;
52         switch (request) {
53             case OPEN:
54                 queryParams = "&open=1";
55                 break;
56             case CLOSE:
57                 queryParams = "&close=1";
58                 break;
59             case CLICK:
60                 queryParams = "&click=1";
61                 break;
62         }
63         if (queryParams != null) {
64             invoke(changeControllerVariablesUri, queryParams);
65         }
66     }
67
68     private String invoke(String uri) throws OpenGarageCommunicationException {
69         return invoke(uri, "");
70     }
71
72     private String invoke(String uri, String params) throws OpenGarageCommunicationException {
73         String uriWithParams = uri + params;
74         logger.debug("Calling url: {}", uriWithParams);
75         String response;
76         synchronized (this) {
77             try {
78                 response = HttpUtil.executeUrl("GET", uriWithParams, this.timeoutMs);
79             } catch (IOException ex) {
80                 logger.debug("{}", ex.getLocalizedMessage(), ex);
81                 // Response will also be set to null if parsing in executeUrl fails so we use null here to make the
82                 // error check below consistent.
83                 response = null;
84             }
85         }
86
87         if (response == null) {
88             throw new OpenGarageCommunicationException(
89                     String.format("OpenGaragecontroller returned error while invoking %s", uriWithParams));
90         }
91         return response;
92     }
93 }