]> git.basschouten.com Git - openhab-addons.git/blob
377bbb2f14e7ff05f90296536575ba1c563cc0a4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.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;
24
25 /**
26  * The {@link OpenSprinklerApiFactory} class is used for creating instances of
27  * the OpenSprinkler API classes to interact with the OpenSprinklers HTTP or
28  * GPIO API's.
29  *
30  * @author Chris Graham - Initial contribution
31  * @author Florian Schmidt - Refactoring
32  */
33 @Component(service = OpenSprinklerApiFactory.class)
34 public class OpenSprinklerApiFactory {
35
36     private @NonNull HttpClient httpClient;
37
38     @Activate
39     public OpenSprinklerApiFactory(@Reference HttpClientFactory httpClientFactory) {
40         this.httpClient = httpClientFactory.getCommonHttpClient();
41     }
42
43     /**
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.
46      *
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.
53      * @throws Exception
54      */
55     public OpenSprinklerApi getHttpApi(OpenSprinklerHttpInterfaceConfig config)
56             throws CommunicationApiException, GeneralApiException {
57         int version = -1;
58
59         OpenSprinklerApi lowestSupportedApi = new OpenSprinklerHttpApiV100(this.httpClient, config);
60         try {
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());
65         }
66
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);
71         } else {
72             /* Need to make sure we have an older OpenSprinkler device by checking the first station. */
73             try {
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: "
78                                 + exp.getMessage());
79             }
80
81             return lowestSupportedApi;
82         }
83     }
84 }