Merge pull request #6 from jlengrand/restart-connection

Adding some dumb resiliency
This commit is contained in:
julien Lengrand-Lambert
2020-07-20 22:53:21 +02:00
committed by GitHub
2 changed files with 22 additions and 15 deletions

View File

@@ -43,7 +43,7 @@ public final class Main {
Server server = startServer();
startMonitoring();
System.out.println("http://localhost:" + server.port() + "/greet");
System.out.println("http://localhost:" + server.port());
}
static void startMonitoring(){

View File

@@ -20,19 +20,26 @@ public class SensorApi {
connection.init();
}
public void add(SensorValue values) throws ExecutionException, InterruptedException {
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())
)
)
)
).get();
System.out.println("Added sensor data to collection " + COLLECTION_NAME + ":\n " + addDataResult + "\n");
public void add(SensorValue values) {
Value addDataResult = null;
try {
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())
)
)
)
).get();
System.out.println("Added sensor data to collection " + COLLECTION_NAME + ":\n " + addDataResult + "\n");
} catch (InterruptedException | ExecutionException e) {
System.out.println("Failing to upload data! Restarting Connection");
e.printStackTrace();
connection.init();
}
}
}