Search in sources :

Example 1 with WriteApi

use of com.influxdb.client.WriteApi 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 WriteApi

use of com.influxdb.client.WriteApi in project MCAntiMalware by OpticFusion1.

the class MetricsManager method start.

public void start() {
    client = InfluxDBClientFactory.create(url, token, org, bucket);
    WriteApi api = client.getWriteApi();
    EXECUTOR.scheduleAtFixedRate(() -> {
        METRICS.forEach(metric -> {
            api.writeMeasurement(WritePrecision.MS, metric.getMeasurement(antiMalware));
        });
    }, 0, interval, TimeUnit.SECONDS);
}
Also used : WriteApi(com.influxdb.client.WriteApi)

Aggregations

WriteApi (com.influxdb.client.WriteApi)2 InfluxDBClient (com.influxdb.client.InfluxDBClient)1 Point (com.influxdb.client.write.Point)1 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)1 PlcConnection (org.apache.plc4x.java.api.PlcConnection)1 PlcException (org.apache.plc4x.java.api.exceptions.PlcException)1 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)1 PlcSubscriptionRequest (org.apache.plc4x.java.api.messages.PlcSubscriptionRequest)1 PlcSubscriptionResponse (org.apache.plc4x.java.api.messages.PlcSubscriptionResponse)1 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)1 DefaultPlcSubscriptionEvent (org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent)1 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)1