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.opensprinkler.internal.api;
15 import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.CMD_PROGRAM_DATA;
17 import java.util.ArrayList;
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;
28 import com.google.gson.JsonParseException;
31 * The {@link OpenSprinklerHttpApiV220} class is used for communicating with
32 * the firmware versions 2.2.0 and up.
34 * @author Matthew Skinner - Initial contribution
37 public class OpenSprinklerHttpApiV220 extends OpenSprinklerHttpApiV219 {
39 OpenSprinklerHttpApiV220(HttpClient httpClient, OpenSprinklerHttpInterfaceConfig config)
40 throws GeneralApiException, CommunicationApiException {
41 super(httpClient, config);
45 public void getProgramData() throws CommunicationApiException, UnauthorizedApiException {
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());
54 JpResponse resp = gson.fromJson(returnContent, JpResponse.class);
55 if (resp != null && resp.pd.length > 0) {
56 state.programs = new ArrayList<>();
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));
69 } catch (JsonParseException e) {
70 logger.debug("Following json could not be parsed:{}", returnContent);
75 public void setPausePrograms(int seconds) throws UnauthorizedApiException, CommunicationApiException {
76 http.sendHttpGet(getBaseUrl() + "pq", getRequestRequiredOptions() + "&dur=" + seconds);