Simple retry mechanism, and start script

This commit is contained in:
Julien Lengrand-Lambert
2019-12-20 11:20:16 +01:00
parent 873d588f12
commit 3dc8965a53
4 changed files with 22 additions and 34 deletions

View File

@@ -32,4 +32,6 @@ ping raspberrypi.local
http://hirt.se/blog/?p=1116
https://github.com/fauna/faunadb-jvm/blob/master/docs/java.md
https://github.com/fauna/faunadb-jvm/blob/master/docs/java.md
Stack Overflow Question : https://iot.stackexchange.com/questions/4662/in-what-form-should-i-store-data-in-the-cloud-for-a-single-device

View File

@@ -7,10 +7,24 @@ import javax.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class CellarProvider {
private static final int N_TRIES = 5;
private Dht11Driver driver = new Dht11Driver();
public SensorValue getSensorValues(){
float[] values = driver.getTemperatureAndHumidity();
int counter = 0;
float[] values;
do {
values = driver.getTemperatureAndHumidity();
counter++;
}
while (noSensorData(values) && counter < N_TRIES);
return new SensorValue(values);
}
private boolean noSensorData(float[] values) {
return values[0] == 0 && values[1] == 0;
}
}

View File

@@ -27,6 +27,7 @@ public class SensorApi {
Obj("data",
Obj( "temperature", Language.Value(values.getTemperature()),
"humidity", Language.Value(values.getHumidity()) ,
"reading", Language.Value(values.getReading()),
"timestamp", Language.Value(Instant.now())
)
)
@@ -34,36 +35,4 @@ public class SensorApi {
).get();
System.out.println("Added sensor data to collection " + COLLECTION_NAME + ":\n " + addDataResult + "\n");
}
public void add(float[] res) throws ExecutionException, InterruptedException {
Value addDataResult = connection.getClient().query(
Create(
Collection(Language.Value(COLLECTION_NAME)),
Obj("data",
Obj( "temperature", Language.Value(res[0]),
"humidity", Language.Value(res[1]),
"timestamp", Language.Value(Instant.now())
)
)
)
).get();
System.out.println("Added sensor data to collection " + COLLECTION_NAME + ":\n " + addDataResult + "\n");
}
public void update(float[] res) throws ExecutionException, InterruptedException {
Value updateDataResult =
connection.getClient().query(
Update(
Select(Language.Value("ref"), Get(Match(Index(INDEX_NAME), Language.Value(UPDATE_DEVICE_NAME)))),
Obj("data",
Obj(
"temperature", Language.Value(res[0]),
"humidity", Language.Value(res[1]),
"timestamp", Language.Value(Instant.now())
)
)
)
).get();
System.out.println("Updated sensor data from collection " + COLLECTION_NAME + ":\n " + updateDataResult + "\n");
}
}

3
startCellar.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/bash
nohup java -jar cellar-app/target/cellar-app.jar &