]> git.basschouten.com Git - openhab-addons.git/blob
3ad855767c4c0c04f1e78be10eab4d42c79e175f
[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.dbquery.internal.dbimpl.influx2;
14
15 import java.util.function.BiConsumer;
16 import java.util.function.Consumer;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.dbquery.internal.config.InfluxDB2BridgeConfiguration;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.influxdb.Cancellable;
25 import com.influxdb.client.InfluxDBClient;
26 import com.influxdb.client.InfluxDBClientFactory;
27 import com.influxdb.client.InfluxDBClientOptions;
28 import com.influxdb.client.QueryApi;
29 import com.influxdb.client.domain.Ready;
30 import com.influxdb.query.FluxRecord;
31
32 /**
33  * Real implementation of {@link InfluxDBClientFacade}
34  *
35  * @author Joan Pujol - Initial contribution
36  */
37 @NonNullByDefault
38 public class InfluxDBClientFacadeImpl implements InfluxDBClientFacade {
39     private final Logger logger = LoggerFactory.getLogger(InfluxDBClientFacadeImpl.class);
40
41     private final InfluxDB2BridgeConfiguration config;
42
43     private @Nullable InfluxDBClient client;
44     private @Nullable QueryApi queryAPI;
45
46     public InfluxDBClientFacadeImpl(InfluxDB2BridgeConfiguration config) {
47         this.config = config;
48     }
49
50     @Override
51     public boolean connect() {
52         var clientOptions = InfluxDBClientOptions.builder().url(config.getUrl()).org(config.getOrganization())
53                 .bucket(config.getBucket()).authenticateToken(config.getToken().toCharArray()).build();
54
55         final InfluxDBClient createdClient = InfluxDBClientFactory.create(clientOptions);
56         this.client = createdClient;
57         var currentQueryAPI = createdClient.getQueryApi();
58         this.queryAPI = currentQueryAPI;
59
60         boolean connected = checkConnectionStatus();
61         if (connected) {
62             logger.debug("Successfully connected to InfluxDB. Instance ready={}", createdClient.ready());
63         } else {
64             logger.warn("Not able to connect to InfluxDB with config {}", config);
65         }
66
67         return connected;
68     }
69
70     private boolean checkConnectionStatus() {
71         final InfluxDBClient currentClient = client;
72         if (currentClient != null) {
73             Ready ready = currentClient.ready();
74             boolean isUp = ready != null && ready.getStatus() == Ready.StatusEnum.READY;
75             if (isUp) {
76                 logger.debug("database status is OK");
77             } else {
78                 logger.warn("database not ready");
79             }
80             return isUp;
81         } else {
82             logger.warn("checkConnection: database is not connected");
83             return false;
84         }
85     }
86
87     @Override
88     public boolean isConnected() {
89         return checkConnectionStatus();
90     }
91
92     @Override
93     public boolean disconnect() {
94         final InfluxDBClient currentClient = client;
95         if (currentClient != null) {
96             currentClient.close();
97             client = null;
98             queryAPI = null;
99             logger.debug("Succesfully disconnected from InfluxDB");
100         } else {
101             logger.debug("Already disconnected");
102         }
103         return true;
104     }
105
106     @Override
107     public void query(String query, BiConsumer<Cancellable, FluxRecord> onNext, Consumer<? super Throwable> onError,
108             Runnable onComplete) {
109         var currentQueryAPI = queryAPI;
110         if (currentQueryAPI != null) {
111             currentQueryAPI.query(query, onNext, onError, onComplete);
112         } else {
113             logger.warn("Query ignored as current queryAPI is null");
114         }
115     }
116 }