2 * Copyright (c) 2010-2023 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.dbquery.internal;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.concurrent.TimeUnit;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.dbquery.action.DBQueryActions;
23 import org.openhab.binding.dbquery.internal.domain.Database;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
28 import org.openhab.core.thing.binding.BaseBridgeHandler;
29 import org.openhab.core.thing.binding.ThingHandlerService;
30 import org.openhab.core.types.Command;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Base implementation common to all implementation of database bridge
37 * @author Joan Pujol - Initial contribution
40 public abstract class DatabaseBridgeHandler extends BaseBridgeHandler {
41 private static final long RETRY_CONNECTION_ATTEMPT_TIME_SECONDS = 60;
42 private final Logger logger = LoggerFactory.getLogger(DatabaseBridgeHandler.class);
43 private Database database = Database.EMPTY;
44 private @Nullable ScheduledFuture<?> retryConnectionAttemptFuture;
46 public DatabaseBridgeHandler(Bridge bridge) {
51 public void initialize() {
54 database = createDatabase();
59 private void connectDatabase() {
60 logger.debug("connectDatabase {}", database);
61 var completable = database.connect();
62 updateStatus(ThingStatus.UNKNOWN);
63 completable.thenAccept(result -> {
65 logger.trace("Succesfully connected to database {}", getThing().getUID());
66 updateStatus(ThingStatus.ONLINE);
68 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Connect to database failed");
69 if (retryConnectionAttemptFuture == null) {
70 scheduleRetryConnectionAttempt();
76 protected void scheduleRetryConnectionAttempt() {
77 logger.trace("Scheduled retry connection attempt every {}", RETRY_CONNECTION_ATTEMPT_TIME_SECONDS);
78 retryConnectionAttemptFuture = scheduler.scheduleWithFixedDelay(this::connectDatabase,
79 RETRY_CONNECTION_ATTEMPT_TIME_SECONDS, RETRY_CONNECTION_ATTEMPT_TIME_SECONDS, TimeUnit.SECONDS);
82 protected abstract void initConfig();
85 public void dispose() {
86 cancelRetryConnectionAttemptIfPresent();
90 protected void cancelRetryConnectionAttemptIfPresent() {
91 ScheduledFuture<?> currentFuture = retryConnectionAttemptFuture;
92 if (currentFuture != null) {
93 currentFuture.cancel(true);
97 private void disconnectDatabase() {
98 var completable = database.disconnect();
99 updateStatus(ThingStatus.UNKNOWN);
100 completable.thenAccept(result -> {
102 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE, "Successfully disconnected to database");
104 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.COMMUNICATION_ERROR,
105 "Disconnect to database failed");
111 public void handleCommand(ChannelUID channelUID, Command command) {
112 // No commands supported
115 abstract Database createDatabase();
117 public Database getDatabase() {
122 public Collection<Class<? extends ThingHandlerService>> getServices() {
123 return List.of(DBQueryActions.class);