2 * Copyright (c) 2010-2020 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.opensprinkler.internal.api;
15 import static org.openhab.binding.opensprinkler.internal.api.OpenSprinklerApiConstants.*;
17 import java.math.BigDecimal;
19 import org.eclipse.jetty.client.HttpClient;
20 import org.openhab.binding.opensprinkler.internal.api.exception.CommunicationApiException;
21 import org.openhab.binding.opensprinkler.internal.api.exception.DataFormatErrorApiException;
22 import org.openhab.binding.opensprinkler.internal.api.exception.DataMissingApiException;
23 import org.openhab.binding.opensprinkler.internal.api.exception.GeneralApiException;
24 import org.openhab.binding.opensprinkler.internal.api.exception.MismatchApiException;
25 import org.openhab.binding.opensprinkler.internal.api.exception.NotPermittedApiException;
26 import org.openhab.binding.opensprinkler.internal.api.exception.OutOfRangeApiException;
27 import org.openhab.binding.opensprinkler.internal.api.exception.PageNotFoundApiException;
28 import org.openhab.binding.opensprinkler.internal.api.exception.UnauthorizedApiException;
29 import org.openhab.binding.opensprinkler.internal.api.exception.UnknownApiException;
30 import org.openhab.binding.opensprinkler.internal.config.OpenSprinklerHttpInterfaceConfig;
31 import org.openhab.binding.opensprinkler.internal.util.Parse;
34 * The {@link OpenSprinklerHttpApiV210} class is used for communicating with
35 * the OpenSprinkler API for firmware versions 2.1.0, 2.1.1 and 2.1.1
37 * @author Chris Graham - Initial contribution
38 * @author Florian Schmidt - Refactor class visibility
40 class OpenSprinklerHttpApiV210 extends OpenSprinklerHttpApiV100 {
42 * Constructor for the OpenSprinkler API class to create a connection to the OpenSprinkler
43 * device for control and obtaining status info.
45 * @param hostname Hostname or IP address as a String of the OpenSprinkler device.
46 * @param port The port number the OpenSprinkler API is listening on.
47 * @param password Admin password for the OpenSprinkler device.
48 * @param basicUsername only needed if basic auth is required
49 * @param basicPassword only needed if basic auth is required
52 OpenSprinklerHttpApiV210(final HttpClient httpClient, final OpenSprinklerHttpInterfaceConfig config)
53 throws GeneralApiException {
54 super(httpClient, config);
58 public boolean isStationOpen(int station) throws GeneralApiException, CommunicationApiException {
60 int stationStatus = -1;
62 if (station < 0 || station >= numberOfStations) {
63 throw new GeneralApiException("This OpenSprinkler device only has " + this.numberOfStations
64 + " but station " + station + " was requested for a status update.");
67 returnContent = http.sendHttpGet(getBaseUrl() + CMD_STATION_INFO, getRequestRequiredOptions());
68 } catch (CommunicationApiException exp) {
69 throw new CommunicationApiException(
70 "There was a problem in the HTTP communication with the OpenSprinkler API: " + exp.getMessage());
74 stationStatus = Parse.jsonIntAtArrayIndex(returnContent, JSON_OPTION_STATION, station);
75 } catch (Exception exp) {
76 throw new GeneralApiException("There was a problem parsing the station status for station " + station
77 + ". Got the error: " + exp.getMessage());
80 if (stationStatus == 1) {
88 public void openStation(int station, BigDecimal duration) throws CommunicationApiException, GeneralApiException {
91 if (station < 0 || station >= numberOfStations) {
92 throw new GeneralApiException("This OpenSprinkler device only has " + this.numberOfStations
93 + " but station " + station + " was requested to be opened.");
97 returnContent = http.sendHttpGet(getBaseUrl() + CMD_STATION_CONTROL, getRequestRequiredOptions() + "&"
98 + CMD_STATION + station + "&" + CMD_STATION_ENABLE + "&t=" + duration);
99 } catch (CommunicationApiException exp) {
100 throw new CommunicationApiException(
101 "There was a problem in the HTTP communication with the OpenSprinkler API: " + exp.getMessage());
104 resultParser(returnContent);
108 public void closeStation(int station) throws CommunicationApiException, GeneralApiException {
109 String returnContent;
111 if (station < 0 || station > numberOfStations) {
112 throw new GeneralApiException("This OpenSprinkler device only has " + this.numberOfStations
113 + " but station " + station + " was requested to be closed.");
117 returnContent = http.sendHttpGet(getBaseUrl() + CMD_STATION_CONTROL,
118 getRequestRequiredOptions() + "&" + CMD_STATION + station + "&" + CMD_STATION_DISABLE);
119 } catch (Exception exp) {
120 throw new CommunicationApiException(
121 "There was a problem in the HTTP communication with the OpenSprinkler API: " + exp.getMessage());
124 resultParser(returnContent);
133 public void enterManualMode() throws CommunicationApiException {
134 this.firmwareVersion = getFirmwareVersion();
135 this.numberOfStations = getNumberOfStations();
137 isInManualMode = true;
141 public void leaveManualMode() {
142 isInManualMode = false;
146 * Creates a custom exception based on a result code from the OpenSprinkler device. This is a
147 * formatted response from the API as {"result: : ##}.
149 * @param returnContent String value of the return content from the OpenSprinkler device when
150 * an action result is returned from the API.
151 * @throws Exception Returns a custom exception based on the result key.
153 protected void resultParser(String returnContent) throws GeneralApiException {
157 returnCode = Parse.jsonInt(returnContent, JSON_OPTION_RESULT);
158 } catch (Exception exp) {
162 switch (returnCode) {
164 throw new UnknownApiException(
165 "The OpenSprinkler API returnd an result that was not parseable: " + returnContent);
169 throw new UnauthorizedApiException("The OpenSprinkler API returned Unauthorized response code.");
171 throw new MismatchApiException("The OpenSprinkler API returned Mismatch response code.");
173 throw new DataMissingApiException("The OpenSprinkler API returned Data Missing response code.");
175 throw new OutOfRangeApiException("The OpenSprinkler API returned Out of Range response code.");
177 throw new DataFormatErrorApiException(
178 "The OpenSprinkler API returned Data Format Error response code.");
180 throw new PageNotFoundApiException("The OpenSprinkler API returned Page Not Found response code.");
182 throw new NotPermittedApiException("The OpenSprinkler API returned Not Permitted response code.");
184 throw new UnknownApiException("Unknown response code from OpenSprinkler API: " + returnCode);