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);
}
}
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;
}
}
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;
}
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;
}
}
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;
}
Aggregations