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 hostname Hostname or IP address as a String of the OpenSprinkler device.
51 * @param port The port number the OpenSprinkler API is listening on.
52 * @param password Admin password for the OpenSprinkler device.
53 * @param basicUsername Used when basic auth is required
54 * @param basicPassword Used when basic auth is required
55 * @return OpenSprinkler HTTP API class for control of the device.
58 public OpenSprinklerApi getHttpApi(OpenSprinklerHttpInterfaceConfig config)
59 throws CommunicationApiException, GeneralApiException {
62 OpenSprinklerApi lowestSupportedApi = new OpenSprinklerHttpApiV100(this.httpClient, config);
64 version = lowestSupportedApi.getFirmwareVersion();
65 } catch (CommunicationApiException exp) {
66 throw new CommunicationApiException(
67 "Problem fetching the firmware version from the OpenSprinkler: " + exp.getMessage());
69 logger.debug("Firmware was reported as {}", version);
70 if (version >= 210 && version < 213) {
71 return new OpenSprinklerHttpApiV210(this.httpClient, config);
72 } else if (version >= 213 && version < 217) {
73 return new OpenSprinklerHttpApiV213(this.httpClient, config);
74 } else if (version >= 217 && version < 219) {
75 return new OpenSprinklerHttpApiV217(this.httpClient, config);
76 } else if (version >= 219) {
77 return new OpenSprinklerHttpApiV219(this.httpClient, config);
79 /* Need to make sure we have an older OpenSprinkler device by checking the first station. */
81 lowestSupportedApi.isStationOpen(0);
82 } catch (GeneralApiException | CommunicationApiException exp) {
83 throw new CommunicationApiException(
84 "There was a problem in the HTTP communication with the OpenSprinkler API: "
87 return lowestSupportedApi;