Search in sources :

Example 1 with InfluxDBClient

use of com.influxdb.client.InfluxDBClient in project plc4x by apache.

the class HelloInflux method run.

public void run() {
    InfluxDBClient dbConnection = connectToDb();
    WriteApi writeApi = dbConnection.getWriteApi();
    try {
        PlcConnection plcConnection = connectToPlc();
        final PlcSubscriptionRequest subscriptionRequest = plcConnection.subscriptionRequestBuilder().addChangeOfStateField("query", configuration.getString("plc.query")).build();
        final PlcSubscriptionResponse subscriptionResponse = subscriptionRequest.execute().get(10, TimeUnit.SECONDS);
        subscriptionResponse.getSubscriptionHandle("query").register(plcSubscriptionEvent -> {
            DefaultPlcSubscriptionEvent internalEvent = (DefaultPlcSubscriptionEvent) plcSubscriptionEvent;
            final Point point = Point.measurement(configuration.getString("influx.measurement")).time(plcSubscriptionEvent.getTimestamp().toEpochMilli(), WritePrecision.MS);
            final Map<String, ResponseItem<PlcValue>> values = internalEvent.getValues();
            values.forEach((fieldName, fieldResponsePair) -> {
                final PlcResponseCode responseCode = fieldResponsePair.getCode();
                final PlcValue plcValue = fieldResponsePair.getValue();
                if (responseCode == PlcResponseCode.OK) {
                    PlcStruct structValue = (PlcStruct) plcValue;
                    for (String key : structValue.getKeys()) {
                        PlcValue subValue = structValue.getValue(key);
                        registerFields(point, key, subValue);
                    }
                }
            });
            writeApi.writePoint(configuration.getString("influx.bucket"), configuration.getString("influx.org"), point);
        });
    } catch (PlcException e) {
        logger.error("PLC Error", e);
    } catch (Exception e) {
        logger.error("General Error", e);
    }
}
Also used : InfluxDBClient(com.influxdb.client.InfluxDBClient) PlcSubscriptionRequest(org.apache.plc4x.java.api.messages.PlcSubscriptionRequest) DefaultPlcSubscriptionEvent(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent) Point(com.influxdb.client.write.Point) PlcConnection(org.apache.plc4x.java.api.PlcConnection) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) PlcException(org.apache.plc4x.java.api.exceptions.PlcException) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) PlcException(org.apache.plc4x.java.api.exceptions.PlcException) WriteApi(com.influxdb.client.WriteApi) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem)

Example 2 with InfluxDBClient

use of com.influxdb.client.InfluxDBClient in project openhab-addons by openhab.

the class InfluxDBClientFacadeImpl method checkConnectionStatus.

private boolean checkConnectionStatus() {
    final InfluxDBClient currentClient = client;
    if (currentClient != null) {
        Ready ready = currentClient.ready();
        boolean isUp = ready != null && ready.getStatus() == Ready.StatusEnum.READY;
        if (isUp) {
            logger.debug("database status is OK");
        } else {
            logger.warn("database not ready");
        }
        return isUp;
    } else {
        logger.warn("checkConnection: database is not connected");
        return false;
    }
}
Also used : InfluxDBClient(com.influxdb.client.InfluxDBClient) Ready(com.influxdb.client.domain.Ready)

Example 3 with InfluxDBClient

use of com.influxdb.client.InfluxDBClient in project openhab-addons by openhab.

the class InfluxDBClientFacadeImpl method disconnect.

@Override
public boolean disconnect() {
    final InfluxDBClient currentClient = client;
    if (currentClient != null) {
        currentClient.close();
        client = null;
        queryAPI = null;
        logger.debug("Succesfully disconnected from InfluxDB");
    } else {
        logger.debug("Already disconnected");
    }
    return true;
}
Also used : InfluxDBClient(com.influxdb.client.InfluxDBClient)

Example 4 with InfluxDBClient

use of com.influxdb.client.InfluxDBClient in project openhab-addons by openhab.

the class InfluxDB2RepositoryImpl method checkConnectionStatus.

/**
 * Check if connection is currently ready
 *
 * @return True if its ready, otherwise false
 */
@Override
public boolean checkConnectionStatus() {
    final InfluxDBClient currentClient = client;
    if (currentClient != null) {
        Ready ready = currentClient.ready();
        boolean isUp = ready != null && ready.getStatus() == Ready.StatusEnum.READY;
        if (isUp) {
            logger.debug("database status is OK");
        } else {
            logger.warn("database not ready");
        }
        return isUp;
    } else {
        logger.warn("checkConnection: database is not connected");
        return false;
    }
}
Also used : InfluxDBClient(com.influxdb.client.InfluxDBClient) Ready(com.influxdb.client.domain.Ready)

Example 5 with InfluxDBClient

use of com.influxdb.client.InfluxDBClient in project pulsar by apache.

the class InfluxDBClientBuilderImpl method build.

@Override
public InfluxDBClient build(InfluxDBSinkConfig influxDBSinkConfig) {
    val options = InfluxDBClientOptions.builder().url(influxDBSinkConfig.getInfluxdbUrl()).authenticateToken(influxDBSinkConfig.getToken().toCharArray()).org(influxDBSinkConfig.getOrganization()).bucket(influxDBSinkConfig.getBucket()).logLevel(LogLevel.valueOf(influxDBSinkConfig.getLogLevel().toUpperCase())).build();
    InfluxDBClient influxDBClient = InfluxDBClientFactory.create(options);
    if (!influxDBSinkConfig.isGzipEnable()) {
        return influxDBClient;
    }
    influxDBClient.enableGzip();
    return influxDBClient;
}
Also used : lombok.val(lombok.val) InfluxDBClient(com.influxdb.client.InfluxDBClient)

Aggregations

InfluxDBClient (com.influxdb.client.InfluxDBClient)23 InfluxDBClientOptions (com.influxdb.client.InfluxDBClientOptions)4 QueryApi (com.influxdb.client.QueryApi)3 Ready (com.influxdb.client.domain.Ready)3 IOException (java.io.IOException)3 lombok.val (lombok.val)3 Cancellable (com.influxdb.Cancellable)2 Query (com.influxdb.client.domain.Query)2 Point (com.influxdb.client.write.Point)2 GeneralSecurityException (java.security.GeneralSecurityException)2 InitializationException (org.apache.nifi.reporting.InitializationException)2 MockProcessContext (org.apache.nifi.util.MockProcessContext)2 MockProcessorInitializationContext (org.apache.nifi.util.MockProcessorInitializationContext)2 InfluxDatabaseService_2 (org.influxdata.nifi.services.InfluxDatabaseService_2)2 StandardInfluxDatabaseService_2 (org.influxdata.nifi.services.StandardInfluxDatabaseService_2)2 NonExecutablePhysicalTaskException (cn.edu.tsinghua.iginx.engine.physical.exception.NonExecutablePhysicalTaskException)1 PhysicalException (cn.edu.tsinghua.iginx.engine.physical.exception.PhysicalException)1 PhysicalTaskExecuteFailureException (cn.edu.tsinghua.iginx.engine.physical.exception.PhysicalTaskExecuteFailureException)1 StorageInitializationException (cn.edu.tsinghua.iginx.engine.physical.exception.StorageInitializationException)1 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1