mirror of
https://github.com/jlengrand/cellar.git
synced 2026-03-10 08:01:19 +00:00
First influxdb writes
This commit is contained in:
@@ -22,6 +22,7 @@ public class CellarProvider {
|
||||
while (noSensorData(values) && counter < N_TRIES);
|
||||
|
||||
return new SensorValue(values);
|
||||
|
||||
}
|
||||
|
||||
private boolean noSensorData(float[] values) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package nl.lengrand.cellar.store;
|
||||
|
||||
import nl.lengrand.cellar.CellarProvider;
|
||||
import nl.lengrand.cellar.store.faunadb.FaunaSensorApi;
|
||||
import nl.lengrand.cellar.store.influxdb.InfluxDbSensorApi;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
@@ -15,7 +16,7 @@ public class CellarMonitor {
|
||||
|
||||
private static final long START = 0;
|
||||
private static final long SPAN = 30;
|
||||
private static final TimeUnit UNIT = TimeUnit.MINUTES;
|
||||
private static final TimeUnit UNIT = TimeUnit.SECONDS;
|
||||
|
||||
private ScheduledFuture monitorHandle;
|
||||
|
||||
|
||||
@@ -1,16 +1,27 @@
|
||||
package nl.lengrand.cellar.store;
|
||||
|
||||
import com.influxdb.annotations.Column;
|
||||
import com.influxdb.annotations.Measurement;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@Measurement(name="cellar-point")
|
||||
public class SensorValue {
|
||||
|
||||
public enum READING{
|
||||
OK, ERROR
|
||||
}
|
||||
|
||||
@Column(tag = true)
|
||||
private READING reading;
|
||||
|
||||
@Column(name = "temperature")
|
||||
private float temperature;
|
||||
|
||||
@Column(name = "humidity")
|
||||
private float humidity;
|
||||
|
||||
@Column(timestamp = true)
|
||||
private Instant timestamp;
|
||||
|
||||
public SensorValue(float[] values){
|
||||
@@ -40,4 +51,14 @@ public class SensorValue {
|
||||
}
|
||||
|
||||
public Instant getTimestamp() { return timestamp; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SensorValue{" +
|
||||
"reading=" + reading +
|
||||
", temperature=" + temperature +
|
||||
", humidity=" + humidity +
|
||||
", timestamp=" + timestamp +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package nl.lengrand.cellar.store.influxdb;
|
||||
|
||||
import com.influxdb.client.InfluxDBClient;
|
||||
import com.influxdb.client.InfluxDBClientFactory;
|
||||
import com.influxdb.client.domain.WritePrecision;
|
||||
|
||||
public class Connection {
|
||||
|
||||
private static final String API_LOCATION = "http://localhost:8086";
|
||||
private static final String API_KEY_NAME = "INFLUX_CELLAR_KEY";
|
||||
private static final String ORG_NAME = "cellar";
|
||||
private static final String BUCKET_NAME = "cellar-data";
|
||||
protected static final WritePrecision PRECISION = WritePrecision.S;
|
||||
|
||||
private InfluxDBClient client;
|
||||
|
||||
public Connection(){
|
||||
client = InfluxDBClientFactory.create(API_LOCATION, getKey().toCharArray(), ORG_NAME, BUCKET_NAME);
|
||||
}
|
||||
|
||||
private String getKey() {
|
||||
return System.getenv(API_KEY_NAME);
|
||||
}
|
||||
|
||||
public InfluxDBClient getClient() {
|
||||
return client;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package nl.lengrand.cellar.store.influxdb;
|
||||
|
||||
import com.influxdb.client.WriteApi;
|
||||
import nl.lengrand.cellar.store.SensorApi;
|
||||
import nl.lengrand.cellar.store.SensorValue;
|
||||
|
||||
|
||||
public class InfluxDbSensorApi implements SensorApi {
|
||||
|
||||
private Connection connection;
|
||||
|
||||
public InfluxDbSensorApi(){
|
||||
this.connection = new Connection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(SensorValue value) {
|
||||
try (WriteApi writeApi = connection.getClient().getWriteApi()) {
|
||||
writeApi.writeMeasurement(connection.PRECISION, value);
|
||||
System.out.println("Added sensor data to bucket :\n " + value + "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user