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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jetty.client.HttpClient;
17 import org.openhab.binding.opensprinkler.internal.api.exception.CommunicationApiException;
18 import org.openhab.binding.opensprinkler.internal.api.exception.GeneralApiException;
19 import org.openhab.binding.opensprinkler.internal.config.OpenSprinklerHttpInterfaceConfig;
20 import org.openhab.core.io.net.http.HttpClientFactory;
21 import org.osgi.service.component.annotations.Activate;
22 import org.osgi.service.component.annotations.Component;
23 import org.osgi.service.component.annotations.Reference;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * The {@link OpenSprinklerApiFactory} class is used for creating instances of
29 * the OpenSprinkler API classes to interact with the OpenSprinklers HTTP or
32 * @author Chris Graham - Initial contribution
33 * @author Florian Schmidt - Refactoring
35 @Component(service = OpenSprinklerApiFactory.class)
37 public class OpenSprinklerApiFactory {
38 private final Logger logger = LoggerFactory.getLogger(this.getClass());
39 private HttpClient httpClient;
42 public OpenSprinklerApiFactory(@Reference HttpClientFactory httpClientFactory) {
43 this.httpClient = httpClientFactory.getCommonHttpClient();
47 * Factory method used to determine what version of the API is in use at the
48 * OpenSprinkler API and return the proper class for control of the device.
50 * @param config Interface settings
51 * @return OpenSprinkler HTTP API class for control of the device.
52 * @throws CommunicationApiException
53 * @throws GeneralApiException
55 public OpenSprinklerApi getHttpApi(OpenSprinklerHttpInterfaceConfig config)
56 throws CommunicationApiException, GeneralApiException {
59 OpenSprinklerApi lowestSupportedApi = new OpenSprinklerHttpApiV100(this.httpClient, config);
61 version = lowestSupportedApi.getFirmwareVersion();
62 } catch (CommunicationApiException exp) {
63 throw new CommunicationApiException(
64 "Problem fetching the firmware version from the OpenSprinkler: " + exp.getMessage());
66 logger.debug("Firmware was reported as {}", version);
67 if (version >= 210 && version < 213) {
68 return new OpenSprinklerHttpApiV210(this.httpClient, config);
69 } else if (version >= 213 && version < 217) {
70 return new OpenSprinklerHttpApiV213(this.httpClient, config);
71 } else if (version >= 217 && version < 219) {
72 return new OpenSprinklerHttpApiV217(this.httpClient, config);
73 } else if (version >= 219) {
74 return new OpenSprinklerHttpApiV219(this.httpClient, config);
76 /* Need to make sure we have an older OpenSprinkler device by checking the first station. */
78 lowestSupportedApi.isStationOpen(0);
79 } catch (GeneralApiException | CommunicationApiException exp) {
80 throw new CommunicationApiException(
81 "There was a problem in the HTTP communication with the OpenSprinkler API: "
84 return lowestSupportedApi;