2 * Copyright (c) 2010-2020 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.NonNull;
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;
26 * The {@link OpenSprinklerApiFactory} class is used for creating instances of
27 * the OpenSprinkler API classes to interact with the OpenSprinklers HTTP or
30 * @author Chris Graham - Initial contribution
31 * @author Florian Schmidt - Refactoring
33 @Component(service = OpenSprinklerApiFactory.class)
34 public class OpenSprinklerApiFactory {
36 private @NonNull HttpClient httpClient;
39 public OpenSprinklerApiFactory(@Reference HttpClientFactory httpClientFactory) {
40 this.httpClient = httpClientFactory.getCommonHttpClient();
44 * Factory method used to determine what version of the API is in use at the
45 * OpenSprinkler API and return the proper class for control of the device.
47 * @param hostname Hostname or IP address as a String of the OpenSprinkler device.
48 * @param port The port number the OpenSprinkler API is listening on.
49 * @param password Admin password for the OpenSprinkler device.
50 * @param basicUsername Used when basic auth is required
51 * @param basicPassword Used when basic auth is required
52 * @return OpenSprinkler HTTP API class for control of the device.
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 "There was a problem in the HTTP communication with the OpenSprinkler API: " + exp.getMessage());
67 if (version >= 210 && version < 213) {
68 return new OpenSprinklerHttpApiV210(this.httpClient, config);
69 } else if (version >= 213) {
70 return new OpenSprinklerHttpApiV213(this.httpClient, config);
72 /* Need to make sure we have an older OpenSprinkler device by checking the first station. */
74 lowestSupportedApi.isStationOpen(0);
75 } catch (GeneralApiException | CommunicationApiException exp) {
76 throw new CommunicationApiException(
77 "There was a problem in the HTTP communication with the OpenSprinkler API: "
81 return lowestSupportedApi;