mirror of
https://github.com/jlengrand/cellar.git
synced 2026-03-10 08:01:19 +00:00
Uses CDI for DataDrivers too
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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(){
|
||||
|
||||
@@ -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()); };
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user