166 lines
6.0 KiB
Java
166 lines
6.0 KiB
Java
package com.example.starter;
|
|
|
|
import io.vertx.sqlclient.Row;
|
|
import io.vertx.sqlclient.RowSet;
|
|
import io.vertx.sqlclient.Tuple;
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
import io.vertx.core.json.JsonArray;
|
|
import io.vertx.core.json.JsonObject;
|
|
|
|
import io.vertx.ext.web.RoutingContext;
|
|
|
|
public class QueryWeatherData {
|
|
private DatabaseService databaseService;
|
|
|
|
public QueryWeatherData(DatabaseService dtbS) {
|
|
this.databaseService = dtbS;
|
|
}
|
|
|
|
public void getWindInfos(RoutingContext context) {
|
|
String id = context.request().getParam("id");
|
|
if (id == null) {
|
|
context.response()
|
|
.setStatusCode(400)
|
|
.end(new JsonObject().put("error", "Paramètre 'id' manquant").encode());
|
|
return;
|
|
}
|
|
|
|
databaseService.pool
|
|
.preparedQuery("SELECT wind_speed,wind_direction,timestamp FROM weather_data WHERE station_id=?")
|
|
.execute(Tuple.of(Integer.parseInt(id)))
|
|
.onFailure(e -> {
|
|
System.err.println("Erreur de récupération de la BDD :" + e.getMessage());
|
|
context.response()
|
|
.setStatusCode(500)
|
|
.end(new JsonObject().put("Erreur", "Erreur de récupération de la BDD").encode());
|
|
})
|
|
.onSuccess(rows -> {
|
|
if (rows.size() == 0) {
|
|
context.response()
|
|
.setStatusCode(404)
|
|
.end(new JsonObject().put("error", "Objet non trouvé").encode());
|
|
return;
|
|
}
|
|
context.response()
|
|
.putHeader("content-type", "application/json: charset=UTF-8")
|
|
.end((convertRowsToJson(rows)).encode());
|
|
});
|
|
}
|
|
|
|
public void getMeteoInfosHomePage(RoutingContext context) {
|
|
JsonObject body = context.body().asJsonObject();
|
|
String location = body.getString("location");
|
|
if (location == null) {
|
|
context.response()
|
|
.setStatusCode(400)
|
|
.end(new JsonObject().put("error", "Paramètre 'location' manquant").encode());
|
|
return;
|
|
}
|
|
databaseService.pool
|
|
.query("SELECT wd.wind_speed, wd.temperature, wd.humidity, wd.pressure FROM weather_data wd " +
|
|
"JOIN weather_objects s ON wd.station_id = s.id WHERE s.location = '" + location +
|
|
"' ORDER BY wd.timestamp DESC LIMIT 1;")
|
|
.execute()
|
|
.onFailure(e -> {
|
|
System.err.println("Erreur de récupération de la BDD : " + e.getMessage());
|
|
context.response()
|
|
.setStatusCode(500)
|
|
.end(new JsonObject().put("error", "Erreur de récupération de la BDD").encode());
|
|
})
|
|
.onSuccess(rows -> {
|
|
if (rows.size() == 0) {
|
|
context.response()
|
|
.setStatusCode(404)
|
|
.end(new JsonObject().put("error", "Aucune donnée trouvée pour cette location").encode());
|
|
return;
|
|
}
|
|
|
|
context.response()
|
|
.putHeader("content-type", "application/json; charset=UTF-8")
|
|
.end(convertRowsToJson(rows).encode());
|
|
});
|
|
}
|
|
|
|
public void getMeteoInfos(RoutingContext context) {
|
|
String id = context.request().getParam("id");
|
|
if (id == null) {
|
|
context.response()
|
|
.setStatusCode(400)
|
|
.end(new JsonObject().put("error", "Paramètre 'id' manquant").encode());
|
|
return;
|
|
}
|
|
|
|
databaseService.pool
|
|
.preparedQuery("SELECT temperature,humidity,pressure,timestamp FROM weather_data WHERE station_id=?")
|
|
.execute(Tuple.of(Integer.parseInt(id)))
|
|
.onFailure(e -> {
|
|
System.err.println("Erreur de récupération de la BDD :" + e.getMessage());
|
|
context.response()
|
|
.setStatusCode(500)
|
|
.end(new JsonObject().put("Erreur", "Erreur de récupération de la BDD").encode());
|
|
})
|
|
.onSuccess(rows -> {
|
|
if (rows.size() == 0) {
|
|
context.response()
|
|
.setStatusCode(404)
|
|
.end(new JsonObject().put("error", "Objet non trouvé").encode());
|
|
return;
|
|
}
|
|
context.response()
|
|
.putHeader("content-type", "application/json: charset=UTF-8")
|
|
.end((convertRowsToJson(rows)).encode());
|
|
});
|
|
}
|
|
|
|
public void getRangeData(RoutingContext context) {
|
|
String id = context.request().getParam("id");
|
|
if (id == null) {
|
|
context.response()
|
|
.setStatusCode(400)
|
|
.end(new JsonObject().put("error", "Paramètre 'id' manquant").encode());
|
|
return;
|
|
}
|
|
databaseService.pool
|
|
.preparedQuery(
|
|
"SELECT temperature_min,temperature_max,pressure_min,pressure_max,humidity_min,humidity_max FROM range_data WHERE station_id=?")
|
|
.execute(Tuple.of(Integer.parseInt(id)))
|
|
.onFailure(e -> {
|
|
System.err.println("Erreur de récupération de la BDD: " + e.getMessage());
|
|
context.response()
|
|
.setStatusCode(500)
|
|
.end(new JsonObject().put("error", "Erreur de récupération de la BDD").encode());
|
|
return;
|
|
})
|
|
.onSuccess(rows -> {
|
|
if (rows.size() == 0) {
|
|
context.response()
|
|
.setStatusCode(404)
|
|
.end(new JsonObject().put("error", "Objet non trouvé").encode());
|
|
return;
|
|
}
|
|
context.response()
|
|
.putHeader("content-type", "application/json:charset=UTF-8")
|
|
.end((convertRowsToJson(rows)).encode());
|
|
});
|
|
}
|
|
|
|
private JsonArray convertRowsToJson(RowSet<Row> rows) {
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
|
|
JsonArray jsonArray = new JsonArray();
|
|
for (Row row : rows) {
|
|
JsonObject jsonObject = new JsonObject();
|
|
for (int i = 0; i < row.size(); i++) {
|
|
String column = row.getColumnName(i);
|
|
if (column.compareTo("timestamp") == 0) {
|
|
jsonObject.put("timestamp", row.getLocalDateTime("timestamp").format(formatter));
|
|
} else {
|
|
jsonObject.put(column, row.getValue(column));
|
|
}
|
|
}
|
|
jsonArray.add(jsonObject);
|
|
}
|
|
return jsonArray;
|
|
}
|
|
} |