mirror of
https://github.com/jlengrand/cellar.git
synced 2026-03-10 08:01:19 +00:00
Merge pull request #14 from jlengrand/feat/influx-db
Add support for InfluxDb
This commit is contained in:
@@ -51,6 +51,11 @@
|
||||
<artifactId>faunadb-java</artifactId>
|
||||
<version>2.9.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.influxdb</groupId>
|
||||
<artifactId>influxdb-client-java</artifactId>
|
||||
<version>1.8.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package nl.lengrand.cellar;
|
||||
|
||||
import nl.lengrand.cellar.store.SensorValue;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
|
||||
@ApplicationScoped
|
||||
@@ -20,6 +22,7 @@ public class CellarProvider {
|
||||
while (noSensorData(values) && counter < N_TRIES);
|
||||
|
||||
return new SensorValue(values);
|
||||
|
||||
}
|
||||
|
||||
private boolean noSensorData(float[] values) {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package nl.lengrand.cellar;
|
||||
|
||||
|
||||
import nl.lengrand.cellar.store.SensorValue;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.json.Json;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package nl.lengrand.cellar;
|
||||
|
||||
import io.helidon.microprofile.server.Server;
|
||||
import nl.lengrand.cellar.store.CellarMonitoring;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package nl.lengrand.cellar;
|
||||
package nl.lengrand.cellar.store;
|
||||
|
||||
import nl.lengrand.cellar.faunadb.SensorApi;
|
||||
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;
|
||||
@@ -14,14 +16,14 @@ 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;
|
||||
|
||||
private CellarProvider provider = new CellarProvider();
|
||||
private SensorApi faunaApi = new SensorApi();
|
||||
private SensorApi sensorApi = new FaunaSensorApi();
|
||||
|
||||
final Runnable monitoring = () -> { faunaApi.add(provider.getSensorValues()); };
|
||||
final Runnable monitoring = () -> { sensorApi.add(provider.getSensorValues()); };
|
||||
|
||||
public void startMonitoring(){
|
||||
monitorHandle = scheduler.scheduleAtFixedRate(monitoring, START, SPAN, UNIT);
|
||||
@@ -1,4 +1,4 @@
|
||||
package nl.lengrand.cellar;
|
||||
package nl.lengrand.cellar.store;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package nl.lengrand.cellar.store;
|
||||
|
||||
public interface SensorApi {
|
||||
void add(SensorValue value);
|
||||
}
|
||||
@@ -1,16 +1,31 @@
|
||||
package nl.lengrand.cellar;
|
||||
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){
|
||||
this.timestamp = Instant.now();
|
||||
if(values[0] == 0 && values[1] == 0){
|
||||
this.reading = READING.ERROR;
|
||||
this.temperature = 0;
|
||||
@@ -34,4 +49,16 @@ public class SensorValue {
|
||||
public float getHumidity() {
|
||||
return humidity;
|
||||
}
|
||||
|
||||
public Instant getTimestamp() { return timestamp; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SensorValue{" +
|
||||
"reading=" + reading +
|
||||
", temperature=" + temperature +
|
||||
", humidity=" + humidity +
|
||||
", timestamp=" + timestamp +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package nl.lengrand.cellar.faunadb;
|
||||
package nl.lengrand.cellar.store.faunadb;
|
||||
|
||||
import com.faunadb.client.FaunaClient;
|
||||
import com.faunadb.client.query.Language;
|
||||
@@ -19,13 +19,6 @@ public class Connection {
|
||||
|
||||
private FaunaClient client;
|
||||
|
||||
public Connection(){
|
||||
}
|
||||
|
||||
private boolean isInit(){
|
||||
return client != null;
|
||||
}
|
||||
|
||||
public void init(){
|
||||
try {
|
||||
this.client = createConnection();
|
||||
@@ -1,4 +1,4 @@
|
||||
package nl.lengrand.cellar.faunadb;
|
||||
package nl.lengrand.cellar.store.faunadb;
|
||||
|
||||
public class ConnectionException extends Exception {
|
||||
public ConnectionException(String errorMessage) {
|
||||
@@ -1,36 +1,36 @@
|
||||
package nl.lengrand.cellar.faunadb;
|
||||
package nl.lengrand.cellar.store.faunadb;
|
||||
|
||||
import com.faunadb.client.query.Language;
|
||||
import com.faunadb.client.types.Value;
|
||||
import nl.lengrand.cellar.SensorValue;
|
||||
import nl.lengrand.cellar.store.SensorValue;
|
||||
import nl.lengrand.cellar.store.SensorApi;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import static com.faunadb.client.query.Language.*;
|
||||
import static com.faunadb.client.query.Language.Obj;
|
||||
import static nl.lengrand.cellar.faunadb.Connection.*;
|
||||
import static nl.lengrand.cellar.store.faunadb.Connection.*;
|
||||
|
||||
public class SensorApi {
|
||||
public class FaunaSensorApi implements SensorApi {
|
||||
|
||||
private Connection connection;
|
||||
|
||||
public SensorApi(){
|
||||
public FaunaSensorApi(){
|
||||
this.connection = new Connection();
|
||||
connection.init();
|
||||
}
|
||||
|
||||
public void add(SensorValue values) {
|
||||
Value addDataResult = null;
|
||||
@Override
|
||||
public void add(SensorValue value) {
|
||||
try {
|
||||
addDataResult = connection.getClient().query(
|
||||
Value addDataResult = connection.getClient().query(
|
||||
Create(
|
||||
Collection(Language.Value(COLLECTION_NAME)),
|
||||
Obj("data",
|
||||
Obj( "temperature", Language.Value(values.getTemperature()),
|
||||
"humidity", Language.Value(values.getHumidity()) ,
|
||||
"reading", Language.Value(values.getReading()),
|
||||
"timestamp", Language.Value(Instant.now())
|
||||
Obj( "temperature", Language.Value(value.getTemperature()),
|
||||
"humidity", Language.Value(value.getHumidity()) ,
|
||||
"reading", Language.Value(value.getReading()),
|
||||
"timestamp", Language.Value(value.getTimestamp())
|
||||
)
|
||||
)
|
||||
)
|
||||
@@ -1,11 +1,13 @@
|
||||
package nl.lengrand.cellar.faunadb;
|
||||
package nl.lengrand.cellar.store.faunadb.setup;
|
||||
import com.faunadb.client.FaunaClient;
|
||||
import com.faunadb.client.types.Value;
|
||||
import nl.lengrand.cellar.store.faunadb.Connection;
|
||||
import nl.lengrand.cellar.store.faunadb.ConnectionException;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import static com.faunadb.client.query.Language.*;
|
||||
import static nl.lengrand.cellar.faunadb.Connection.*;
|
||||
import static nl.lengrand.cellar.store.faunadb.Connection.*;
|
||||
|
||||
|
||||
/*
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
1
cellar-influxdb/Dockerfile
Normal file
1
cellar-influxdb/Dockerfile
Normal file
@@ -0,0 +1 @@
|
||||
FROM quay.io/influxdb/influxdb:2.0.0-rc
|
||||
15
cellar-influxdb/README.md
Normal file
15
cellar-influxdb/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
Run influxdb locally
|
||||
|
||||
```bash
|
||||
$ docker run --name influxdb -p 8086:8086 quay.io/influxdb/influxdb:2.0.0-rc
|
||||
or
|
||||
$ docker start influxdb
|
||||
```
|
||||
|
||||
cellar / robottle
|
||||
|
||||
for the CLI
|
||||
```bash
|
||||
$ docker exec -it influxdb /bin/bash
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user