]> git.basschouten.com Git - openhab-addons.git/blob
78c21ba983509f628a4a96e61106ab8068d63947
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.opensprinkler.internal.api;
14
15 import static org.openhab.binding.opensprinkler.internal.api.OpenSprinklerApiConstants.*;
16
17 import java.math.BigDecimal;
18
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;
32
33 /**
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
36  *
37  * @author Chris Graham - Initial contribution
38  * @author Florian Schmidt - Refactor class visibility
39  */
40 class OpenSprinklerHttpApiV210 extends OpenSprinklerHttpApiV100 {
41     /**
42      * Constructor for the OpenSprinkler API class to create a connection to the OpenSprinkler
43      * device for control and obtaining status info.
44      *
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
50      * @throws Exception
51      */
52     OpenSprinklerHttpApiV210(final HttpClient httpClient, final OpenSprinklerHttpInterfaceConfig config)
53             throws GeneralApiException {
54         super(httpClient, config);
55     }
56
57     @Override
58     public boolean isStationOpen(int station) throws GeneralApiException, CommunicationApiException {
59         String returnContent;
60         int stationStatus = -1;
61
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.");
65         }
66         try {
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());
71         }
72
73         try {
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());
78         }
79
80         if (stationStatus == 1) {
81             return true;
82         } else {
83             return false;
84         }
85     }
86
87     @Override
88     public void openStation(int station, BigDecimal duration) throws CommunicationApiException, GeneralApiException {
89         String returnContent;
90
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.");
94         }
95
96         try {
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());
102         }
103
104         resultParser(returnContent);
105     }
106
107     @Override
108     public void closeStation(int station) throws CommunicationApiException, GeneralApiException {
109         String returnContent;
110
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.");
114         }
115
116         try {
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());
122         }
123
124         resultParser(returnContent);
125     }
126
127     /**
128      * {@inheritDoc}
129      *
130      * @throws Exception
131      */
132     @Override
133     public void enterManualMode() throws CommunicationApiException {
134         this.firmwareVersion = getFirmwareVersion();
135         this.numberOfStations = getNumberOfStations();
136
137         isInManualMode = true;
138     }
139
140     @Override
141     public void leaveManualMode() {
142         isInManualMode = false;
143     }
144
145     /**
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: : ##}.
148      *
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.
152      */
153     protected void resultParser(String returnContent) throws GeneralApiException {
154         int returnCode;
155
156         try {
157             returnCode = Parse.jsonInt(returnContent, JSON_OPTION_RESULT);
158         } catch (Exception exp) {
159             returnCode = -1;
160         }
161
162         switch (returnCode) {
163             case -1:
164                 throw new UnknownApiException(
165                         "The OpenSprinkler API returnd an result that was not parseable: " + returnContent);
166             case 1:
167                 return;
168             case 2:
169                 throw new UnauthorizedApiException("The OpenSprinkler API returned Unauthorized response code.");
170             case 3:
171                 throw new MismatchApiException("The OpenSprinkler API returned Mismatch response code.");
172             case 16:
173                 throw new DataMissingApiException("The OpenSprinkler API returned Data Missing response code.");
174             case 17:
175                 throw new OutOfRangeApiException("The OpenSprinkler API returned Out of Range response code.");
176             case 18:
177                 throw new DataFormatErrorApiException(
178                         "The OpenSprinkler API returned Data Format Error response code.");
179             case 32:
180                 throw new PageNotFoundApiException("The OpenSprinkler API returned Page Not Found response code.");
181             case 48:
182                 throw new NotPermittedApiException("The OpenSprinkler API returned Not Permitted response code.");
183             default:
184                 throw new UnknownApiException("Unknown response code from OpenSprinkler API: " + returnCode);
185         }
186     }
187 }