]> git.basschouten.com Git - openhab-addons.git/blob
9204d956f9004b633a90542bcf1c2979b638946e
[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.handler;
14
15 import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.DEFAULT_REFRESH_RATE;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.opensprinkler.internal.api.OpenSprinklerApi;
20 import org.openhab.binding.opensprinkler.internal.api.OpenSprinklerApiFactory;
21 import org.openhab.binding.opensprinkler.internal.api.exception.CommunicationApiException;
22 import org.openhab.binding.opensprinkler.internal.api.exception.GeneralApiException;
23 import org.openhab.binding.opensprinkler.internal.config.OpenSprinklerHttpInterfaceConfig;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.ThingStatusDetail;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * @author Florian Schmidt - Refactoring
32  */
33 @NonNullByDefault
34 public class OpenSprinklerHttpBridgeHandler extends OpenSprinklerBaseBridgeHandler {
35     private final Logger logger = LoggerFactory.getLogger(OpenSprinklerHttpBridgeHandler.class);
36
37     @Nullable
38     private OpenSprinklerHttpInterfaceConfig openSprinklerConfig;
39     private OpenSprinklerApiFactory apiFactory;
40
41     public OpenSprinklerHttpBridgeHandler(Bridge bridge, OpenSprinklerApiFactory apiFactory) {
42         super(bridge);
43         this.apiFactory = apiFactory;
44     }
45
46     @Override
47     public void initialize() {
48         OpenSprinklerHttpInterfaceConfig openSprinklerConfig = getConfig().as(OpenSprinklerHttpInterfaceConfig.class);
49         this.openSprinklerConfig = openSprinklerConfig;
50
51         logger.debug("Initializing OpenSprinkler with config (Hostname: {}, Port: {}, Refresh: {}).",
52                 openSprinklerConfig.hostname, openSprinklerConfig.port, openSprinklerConfig.refresh);
53
54         OpenSprinklerApi openSprinklerDevice;
55         try {
56             openSprinklerDevice = apiFactory.getHttpApi(openSprinklerConfig);
57             this.openSprinklerDevice = openSprinklerDevice;
58         } catch (CommunicationApiException | GeneralApiException exp) {
59             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
60                     "Could not create API connection to the OpenSprinkler device. Error received: " + exp);
61
62             return;
63         }
64
65         logger.debug("Successfully created API connection to the OpenSprinkler device.");
66
67         try {
68             openSprinklerDevice.enterManualMode();
69         } catch (CommunicationApiException exp) {
70             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
71                     "Could not open API connection to the OpenSprinkler device. Error received: " + exp);
72         }
73
74         if (openSprinklerDevice.isManualModeEnabled()) {
75             updateStatus(ThingStatus.ONLINE);
76         } else {
77             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
78                     "Could not initialize the connection to the OpenSprinkler.");
79
80             return;
81         }
82
83         super.initialize();
84     }
85
86     @Override
87     protected long getRefreshInterval() {
88         OpenSprinklerHttpInterfaceConfig openSprinklerConfig = this.openSprinklerConfig;
89         if (openSprinklerConfig == null) {
90             return DEFAULT_REFRESH_RATE;
91         }
92         return openSprinklerConfig.refresh;
93     }
94 }