diff --git a/.gitignore b/.gitignore index c778fea..43c0913 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,7 @@ local.properties /cellar-app/target/ /cellar-driver/target/ /.idea/ +/cellar-influxdb/gradle/wrapper/gradle-wrapper.jar +/cellar-influxdb/gradle/wrapper/gradle-wrapper.properties +/cellar-influxdb/gradlew +/cellar-influxdb/gradlew.bat diff --git a/cellar-app/src/main/java/nl/lengrand/cellar/CellarResource.java b/cellar-app/src/main/java/nl/lengrand/cellar/CellarResource.java index 91b4de5..38a3d1d 100644 --- a/cellar-app/src/main/java/nl/lengrand/cellar/CellarResource.java +++ b/cellar-app/src/main/java/nl/lengrand/cellar/CellarResource.java @@ -2,6 +2,7 @@ package nl.lengrand.cellar; import nl.lengrand.cellar.driver.DataDriver; +import nl.lengrand.cellar.driver.DataDriverProvider; import nl.lengrand.cellar.driver.Dht11DataDriver; import nl.lengrand.cellar.store.SensorValue; @@ -22,7 +23,7 @@ public class CellarResource { private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap()); - @Inject + @Inject @DataDriverProvider.SpecificDataDriver private DataDriver dataDriver; @GET diff --git a/cellar-app/src/main/java/nl/lengrand/cellar/driver/DataDriverProvider.java b/cellar-app/src/main/java/nl/lengrand/cellar/driver/DataDriverProvider.java index 73277f3..e9475a1 100644 --- a/cellar-app/src/main/java/nl/lengrand/cellar/driver/DataDriverProvider.java +++ b/cellar-app/src/main/java/nl/lengrand/cellar/driver/DataDriverProvider.java @@ -5,23 +5,39 @@ import org.eclipse.microprofile.config.inject.ConfigProperty; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Produces; import javax.inject.Inject; +import javax.inject.Qualifier; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @ApplicationScoped public class DataDriverProvider { + @Qualifier + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.LOCAL_VARIABLE, ElementType.FIELD, ElementType.METHOD}) + public @interface SpecificDataDriver{} + @Inject @ConfigProperty(name = "driver.type", defaultValue = "dht11") private String driverType; - @Produces + @Inject + private Dht11DataDriver dht11DataDriver; + + @Inject + private DummyDataDriver dummyDataDriver; + + @Produces @SpecificDataDriver public DataDriver getDataProvider() { System.out.println("THE DRIVER TYPE " + driverType); if (driverType.equals("dht11")) { System.out.println("dht11 data driver requested"); - return new Dht11DataDriver(); + return dht11DataDriver; } else { System.out.println("dummy driver requested"); - return new DummyDataDriver(); + return dummyDataDriver; } } diff --git a/cellar-app/src/main/java/nl/lengrand/cellar/driver/Dht11DataDriver.java b/cellar-app/src/main/java/nl/lengrand/cellar/driver/Dht11DataDriver.java index b141d9d..ffe7725 100644 --- a/cellar-app/src/main/java/nl/lengrand/cellar/driver/Dht11DataDriver.java +++ b/cellar-app/src/main/java/nl/lengrand/cellar/driver/Dht11DataDriver.java @@ -3,6 +3,9 @@ package nl.lengrand.cellar.driver; import nl.lengrand.cellar.Dht11Driver; import nl.lengrand.cellar.store.SensorValue; +import javax.enterprise.context.ApplicationScoped; + +@ApplicationScoped public class Dht11DataDriver implements DataDriver { private static final int N_TRIES = 5; diff --git a/cellar-app/src/main/java/nl/lengrand/cellar/driver/DummyDataDriver.java b/cellar-app/src/main/java/nl/lengrand/cellar/driver/DummyDataDriver.java index a959aa1..342d95a 100644 --- a/cellar-app/src/main/java/nl/lengrand/cellar/driver/DummyDataDriver.java +++ b/cellar-app/src/main/java/nl/lengrand/cellar/driver/DummyDataDriver.java @@ -2,8 +2,10 @@ package nl.lengrand.cellar.driver; import nl.lengrand.cellar.store.SensorValue; +import javax.enterprise.context.ApplicationScoped; import java.util.Random; +@ApplicationScoped public class DummyDataDriver implements DataDriver { public SensorValue getSensorValues(){ diff --git a/cellar-app/src/main/java/nl/lengrand/cellar/store/CellarMonitor.java b/cellar-app/src/main/java/nl/lengrand/cellar/store/CellarMonitor.java index 7bad032..6cc5494 100644 --- a/cellar-app/src/main/java/nl/lengrand/cellar/store/CellarMonitor.java +++ b/cellar-app/src/main/java/nl/lengrand/cellar/store/CellarMonitor.java @@ -1,8 +1,7 @@ package nl.lengrand.cellar.store; import nl.lengrand.cellar.driver.DataDriver; -import nl.lengrand.cellar.driver.Dht11DataDriver; - +import nl.lengrand.cellar.driver.DataDriverProvider; import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; import java.util.concurrent.Executors; @@ -22,10 +21,10 @@ public class CellarMonitor { private ScheduledFuture monitorHandle; - @Inject + @Inject @DataDriverProvider.SpecificDataDriver private DataDriver dataDriver; - @Inject + @Inject @SensorApiProvider.SpecificSensorApi private SensorApi sensorApi; final Runnable monitoring = () -> { sensorApi.add(dataDriver.getSensorValues()); }; diff --git a/cellar-app/src/main/java/nl/lengrand/cellar/store/SensorApiProvider.java b/cellar-app/src/main/java/nl/lengrand/cellar/store/SensorApiProvider.java index 3587b7e..1caf6ea 100644 --- a/cellar-app/src/main/java/nl/lengrand/cellar/store/SensorApiProvider.java +++ b/cellar-app/src/main/java/nl/lengrand/cellar/store/SensorApiProvider.java @@ -7,23 +7,39 @@ import org.eclipse.microprofile.config.inject.ConfigProperty; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Produces; import javax.inject.Inject; +import javax.inject.Qualifier; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @ApplicationScoped public class SensorApiProvider { + @Qualifier + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.LOCAL_VARIABLE, ElementType.FIELD, ElementType.METHOD}) + public @interface SpecificSensorApi{} + + @Inject + private InfluxDbSensorApi influxDbSensorApi; + + @Inject + private FaunaSensorApi faunaSensorApi; + @Inject @ConfigProperty(name = "sensor.api.type", defaultValue = "influx") private String sensorApiType; - @Produces + @Produces @SpecificSensorApi public SensorApi getSensorApi() { System.out.println("THE TYPE " + sensorApiType); if (sensorApiType.equals("influx")) { System.out.println("influx db api requested"); - return new InfluxDbSensorApi(); + return influxDbSensorApi; } else { System.out.println("fauna db api requested"); - return new FaunaSensorApi(); + return faunaSensorApi; } } diff --git a/cellar-app/src/main/java/nl/lengrand/cellar/store/faunadb/FaunaSensorApi.java b/cellar-app/src/main/java/nl/lengrand/cellar/store/faunadb/FaunaSensorApi.java index b1e06b7..6500caf 100644 --- a/cellar-app/src/main/java/nl/lengrand/cellar/store/faunadb/FaunaSensorApi.java +++ b/cellar-app/src/main/java/nl/lengrand/cellar/store/faunadb/FaunaSensorApi.java @@ -7,13 +7,13 @@ import nl.lengrand.cellar.store.SensorValue; import nl.lengrand.cellar.store.SensorApi; import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.Typed; 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.store.faunadb.Connection.*; +@ApplicationScoped public class FaunaSensorApi implements SensorApi { private Connection connection; diff --git a/cellar-app/src/main/java/nl/lengrand/cellar/store/influxdb/InfluxDbSensorApi.java b/cellar-app/src/main/java/nl/lengrand/cellar/store/influxdb/InfluxDbSensorApi.java index 6a1000a..293bb09 100644 --- a/cellar-app/src/main/java/nl/lengrand/cellar/store/influxdb/InfluxDbSensorApi.java +++ b/cellar-app/src/main/java/nl/lengrand/cellar/store/influxdb/InfluxDbSensorApi.java @@ -3,6 +3,10 @@ package nl.lengrand.cellar.store.influxdb; import com.influxdb.client.WriteApi; import nl.lengrand.cellar.store.SensorApi; import nl.lengrand.cellar.store.SensorValue; + +import javax.enterprise.context.ApplicationScoped; + +@ApplicationScoped public class InfluxDbSensorApi implements SensorApi { private Connection connection;