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 public static int DEFAULT_TIMEOUT_MS = 30000;
32 private String getControllerVariablesUri;
33 private String changeControllerVariablesUri;
34 private final Logger logger = LoggerFactory.getLogger(OpenGarageWebTargets.class);
35 private int timeoutMs;
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;
44 public ControllerVariables getControllerVariables() throws OpenGarageCommunicationException {
45 String response = invoke(getControllerVariablesUri);
46 return ControllerVariables.parse(response);
49 public void setControllerVariables(OpenGarageCommand request) throws OpenGarageCommunicationException {
50 logger.debug("Received request: {}", request);
51 String queryParams = null;
54 queryParams = "&open=1";
57 queryParams = "&close=1";
60 queryParams = "&click=1";
63 if (queryParams != null) {
64 invoke(changeControllerVariablesUri, queryParams);
68 private String invoke(String uri) throws OpenGarageCommunicationException {
69 return invoke(uri, "");
72 private String invoke(String uri, String params) throws OpenGarageCommunicationException {
73 String uriWithParams = uri + params;
74 logger.debug("Calling url: {}", uriWithParams);
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.
87 if (response == null) {
88 throw new OpenGarageCommunicationException(
89 String.format("OpenGaragecontroller returned error while invoking %s", uriWithParams));