2 * Copyright (c) 2010-2021 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.handler;
15 import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.DEFAULT_REFRESH_RATE;
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;
31 * @author Florian Schmidt - Refactoring
34 public class OpenSprinklerHttpBridgeHandler extends OpenSprinklerBaseBridgeHandler {
35 private final Logger logger = LoggerFactory.getLogger(OpenSprinklerHttpBridgeHandler.class);
38 private OpenSprinklerHttpInterfaceConfig openSprinklerConfig;
39 private OpenSprinklerApiFactory apiFactory;
41 public OpenSprinklerHttpBridgeHandler(Bridge bridge, OpenSprinklerApiFactory apiFactory) {
43 this.apiFactory = apiFactory;
47 public void initialize() {
48 OpenSprinklerHttpInterfaceConfig openSprinklerConfig = getConfig().as(OpenSprinklerHttpInterfaceConfig.class);
49 this.openSprinklerConfig = openSprinklerConfig;
51 logger.debug("Initializing OpenSprinkler with config (Hostname: {}, Port: {}, Refresh: {}).",
52 openSprinklerConfig.hostname, openSprinklerConfig.port, openSprinklerConfig.refresh);
54 OpenSprinklerApi openSprinklerDevice;
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);
65 logger.debug("Successfully created API connection to the OpenSprinkler device.");
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);
74 if (openSprinklerDevice.isManualModeEnabled()) {
75 updateStatus(ThingStatus.ONLINE);
77 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
78 "Could not initialize the connection to the OpenSprinkler.");
87 protected long getRefreshInterval() {
88 OpenSprinklerHttpInterfaceConfig openSprinklerConfig = this.openSprinklerConfig;
89 if (openSprinklerConfig == null) {
90 return DEFAULT_REFRESH_RATE;
92 return openSprinklerConfig.refresh;