]> git.basschouten.com Git - openhab-addons.git/blob
4e6bad47d53a9fdfaaba1326159dc06e81323045
[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 config Interface settings
51      * @return OpenSprinkler HTTP API class for control of the device.
52      * @throws CommunicationApiException
53      * @throws GeneralApiException
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                     "Problem fetching the firmware version from the OpenSprinkler: " + exp.getMessage());
65         }
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);
75         } else {
76             /* Need to make sure we have an older OpenSprinkler device by checking the first station. */
77             try {
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: "
82                                 + exp.getMessage());
83             }
84             return lowestSupportedApi;
85         }
86     }
87 }