]> git.basschouten.com Git - openhab-addons.git/blob
cd4ab8abad6e3d50df8cd831489e13f3d66cd28d
[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 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;
26
27 /**
28  * The {@link OpenSprinklerApiFactory} class is used for creating instances of
29  * the OpenSprinkler API classes to interact with the OpenSprinklers HTTP or
30  * GPIO API's.
31  *
32  * @author Chris Graham - Initial contribution
33  * @author Florian Schmidt - Refactoring
34  */
35 @Component(service = OpenSprinklerApiFactory.class)
36 @NonNullByDefault
37 public class OpenSprinklerApiFactory {
38     private final Logger logger = LoggerFactory.getLogger(this.getClass());
39     private HttpClient httpClient;
40
41     @Activate
42     public OpenSprinklerApiFactory(@Reference HttpClientFactory httpClientFactory) {
43         this.httpClient = httpClientFactory.getCommonHttpClient();
44     }
45
46     /**
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.
49      *
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.
56      * @throws Exception
57      */
58     public OpenSprinklerApi getHttpApi(OpenSprinklerHttpInterfaceConfig config)
59             throws CommunicationApiException, GeneralApiException {
60         int version = -1;
61
62         OpenSprinklerApi lowestSupportedApi = new OpenSprinklerHttpApiV100(this.httpClient, config);
63         try {
64             version = lowestSupportedApi.getFirmwareVersion();
65         } catch (CommunicationApiException exp) {
66             throw new CommunicationApiException(
67                     "Problem fetching the firmware version from the OpenSprinkler: " + exp.getMessage());
68         }
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);
78         } else {
79             /* Need to make sure we have an older OpenSprinkler device by checking the first station. */
80             try {
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: "
85                                 + exp.getMessage());
86             }
87             return lowestSupportedApi;
88         }
89     }
90 }