]> git.basschouten.com Git - openhab-addons.git/blob
962aa7c7d46dc36776cbbcfe6d7499f43b060b4d
[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.opensprinkler.internal.api;
14
15 import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.CMD_PROGRAM_DATA;
16
17 import java.util.ArrayList;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jetty.client.HttpClient;
21 import org.openhab.binding.opensprinkler.internal.OpenSprinklerState.JpResponse;
22 import org.openhab.binding.opensprinkler.internal.api.exception.CommunicationApiException;
23 import org.openhab.binding.opensprinkler.internal.api.exception.GeneralApiException;
24 import org.openhab.binding.opensprinkler.internal.api.exception.UnauthorizedApiException;
25 import org.openhab.binding.opensprinkler.internal.config.OpenSprinklerHttpInterfaceConfig;
26 import org.openhab.core.types.StateOption;
27
28 import com.google.gson.JsonParseException;
29
30 /**
31  * The {@link OpenSprinklerHttpApiV220} class is used for communicating with
32  * the firmware versions 2.2.0 and up.
33  *
34  * @author Matthew Skinner - Initial contribution
35  */
36 @NonNullByDefault
37 public class OpenSprinklerHttpApiV220 extends OpenSprinklerHttpApiV219 {
38
39     OpenSprinklerHttpApiV220(HttpClient httpClient, OpenSprinklerHttpInterfaceConfig config)
40             throws GeneralApiException, CommunicationApiException {
41         super(httpClient, config);
42     }
43
44     @Override
45     public void getProgramData() throws CommunicationApiException, UnauthorizedApiException {
46         String returnContent;
47         try {
48             returnContent = http.sendHttpGet(getBaseUrl() + CMD_PROGRAM_DATA, getRequestRequiredOptions());
49         } catch (CommunicationApiException exp) {
50             throw new CommunicationApiException(
51                     "There was a problem in the HTTP communication with the OpenSprinkler API: " + exp.getMessage());
52         }
53         try {
54             JpResponse resp = gson.fromJson(returnContent, JpResponse.class);
55             if (resp != null && resp.pd.length > 0) {
56                 state.programs = new ArrayList<>();
57                 int counter = 0;
58                 for (Object x : resp.pd) {
59                     String temp = x.toString();
60                     logger.trace("Program Data:{}", temp);
61                     int end = temp.lastIndexOf('[') - 2;
62                     int start = temp.lastIndexOf((','), end - 1) + 2;
63                     if (start > -1 && end > -1) {
64                         temp = temp.substring(start, end);
65                         state.programs.add(new StateOption(Integer.toString(counter++), temp));
66                     }
67                 }
68             }
69         } catch (JsonParseException e) {
70             logger.debug("Following json could not be parsed:{}", returnContent);
71         }
72     }
73
74     @Override
75     public void setPausePrograms(int seconds) throws UnauthorizedApiException, CommunicationApiException {
76         http.sendHttpGet(getBaseUrl() + "pq", getRequestRequiredOptions() + "&dur=" + seconds);
77     }
78 }