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.opengarage.internal;
15 import java.io.IOException;
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;
24 * Handles performing the actual HTTP requests for communicating with Opengarage units.
26 * @author Paul Smedley - Initial Contribution
29 public class OpenGarageWebTargets {
30 private static final int TIMEOUT_MS = 30000;
32 private String getControllerVariablesUri;
33 private String changeControllerVariablesUri;
34 private final Logger logger = LoggerFactory.getLogger(OpenGarageWebTargets.class);
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;
42 public ControllerVariables getControllerVariables() throws OpenGarageCommunicationException {
43 String response = invoke(getControllerVariablesUri);
44 return ControllerVariables.parse(response);
47 public void setControllerVariables(OpenGarageCommand request) throws OpenGarageCommunicationException {
48 logger.debug("Received request: {}", request);
49 String queryParams = null;
52 queryParams = "&open=1";
55 queryParams = "&close=1";
58 queryParams = "&click=1";
61 if (queryParams != null) {
62 invoke(changeControllerVariablesUri, queryParams);
66 private String invoke(String uri) throws OpenGarageCommunicationException {
67 return invoke(uri, "");
70 private String invoke(String uri, String params) throws OpenGarageCommunicationException {
71 String uriWithParams = uri + params;
72 logger.debug("Calling url: {}", uriWithParams);
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.
85 if (response == null) {
86 throw new OpenGarageCommunicationException(
87 String.format("OpenGaragecontroller returned error while invoking %s", uriWithParams));