]> git.basschouten.com Git - openhab-addons.git/blob
feb9c195cf6b9432c690b457c59daec4b450c565
[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     private static final int TIMEOUT_MS = 30000;
31
32     private String getControllerVariablesUri;
33     private String changeControllerVariablesUri;
34     private final Logger logger = LoggerFactory.getLogger(OpenGarageWebTargets.class);
35
36     public OpenGarageWebTargets(String ipAddress, long port, String password) {
37         String baseUri = "http://" + ipAddress + ":" + port + "/";
38         getControllerVariablesUri = baseUri + "jc";
39         changeControllerVariablesUri = baseUri + "cc?dkey=" + password;
40     }
41
42     public ControllerVariables getControllerVariables() throws OpenGarageCommunicationException {
43         String response = invoke(getControllerVariablesUri);
44         return ControllerVariables.parse(response);
45     }
46
47     public void setControllerVariables(OpenGarageCommand request) throws OpenGarageCommunicationException {
48         logger.debug("Received request: {}", request);
49         String queryParams = null;
50         switch (request) {
51             case OPEN:
52                 queryParams = "&open=1";
53                 break;
54             case CLOSE:
55                 queryParams = "&close=1";
56                 break;
57             case CLICK:
58                 queryParams = "&click=1";
59                 break;
60         }
61         if (queryParams != null) {
62             invoke(changeControllerVariablesUri, queryParams);
63         }
64     }
65
66     private String invoke(String uri) throws OpenGarageCommunicationException {
67         return invoke(uri, "");
68     }
69
70     private String invoke(String uri, String params) throws OpenGarageCommunicationException {
71         String uriWithParams = uri + params;
72         logger.debug("Calling url: {}", uriWithParams);
73         String response;
74         synchronized (this) {
75             try {
76                 response = HttpUtil.executeUrl("GET", uriWithParams, TIMEOUT_MS);
77             } catch (IOException ex) {
78                 logger.debug("{}", ex.getLocalizedMessage(), ex);
79                 // Response will also be set to null if parsing in executeUrl fails so we use null here to make the
80                 // error check below consistent.
81                 response = null;
82             }
83         }
84
85         if (response == null) {
86             throw new OpenGarageCommunicationException(
87                     String.format("OpenGaragecontroller returned error while invoking %s", uriWithParams));
88         }
89         return response;
90     }
91 }