diff --git a/backend/.gitignore b/backend/.gitignore index 89540d8..93faf13 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1 +1,2 @@ /dependency-reduced-pom.xml +/target/ diff --git a/backend/src/main/java/net/miarma/contaminus/common/Constants.java b/backend/src/main/java/net/miarma/contaminus/common/Constants.java index 02eca4e..3f26a39 100644 --- a/backend/src/main/java/net/miarma/contaminus/common/Constants.java +++ b/backend/src/main/java/net/miarma/contaminus/common/Constants.java @@ -27,6 +27,7 @@ public class Constants { public static final String PUT_ACTUATOR_BY_ID = API_PREFIX + "/actuators/:actuatorId"; public static final String GET_CO_BY_DEVICE_VIEW = API_PREFIX + "/v_co_by_device"; + public static final String GET_GPS_BY_DEVICE_VIEW = API_PREFIX + "/v_gps_by_device"; public static final String GET_LATEST_VALUES_VIEW = API_PREFIX + "/v_latest_values"; public static final String GET_POLLUTION_MAP_VIEW = API_PREFIX + "/v_pollution_map"; diff --git a/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceCO.java b/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceCO.java new file mode 100644 index 0000000..2b86eea --- /dev/null +++ b/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceCO.java @@ -0,0 +1,64 @@ +package net.miarma.contaminus.database.entities; + +import java.util.Objects; + +import io.vertx.sqlclient.Row; +import net.miarma.contaminus.common.Table; + +@Table("v_co_by_device") +public class DeviceCO { + int deviceId; + float carbonMonoxide; + long timestamp; + + public DeviceCO() {} + + public DeviceCO(Row row) { + this.deviceId = row.getInteger("deviceId"); + this.carbonMonoxide = row.getFloat("carbonMonoxide"); + this.timestamp = row.getLong("timestamp"); + } + + public DeviceCO(int deviceId, float carbonMonoxide, long timestamp) { + super(); + this.deviceId = deviceId; + this.carbonMonoxide = carbonMonoxide; + this.timestamp = timestamp; + } + + public int getDeviceId() { + return deviceId; + } + + public float getCarbonMonoxide() { + return carbonMonoxide; + } + + public long getTimestamp() { + return timestamp; + } + + @Override + public int hashCode() { + return Objects.hash(carbonMonoxide, deviceId, timestamp); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DeviceCO other = (DeviceCO) obj; + return Float.floatToIntBits(carbonMonoxide) == Float.floatToIntBits(other.carbonMonoxide) + && deviceId == other.deviceId && timestamp == other.timestamp; + } + + @Override + public String toString() { + return "DeviceCO [deviceId=" + deviceId + ", carbonMonoxide=" + carbonMonoxide + ", timestamp=" + timestamp + + "]"; + } +} \ No newline at end of file diff --git a/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceGPS.java b/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceGPS.java new file mode 100644 index 0000000..4d81157 --- /dev/null +++ b/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceGPS.java @@ -0,0 +1,69 @@ +package net.miarma.contaminus.database.entities; + +import java.util.Objects; + +import io.vertx.sqlclient.Row; +import net.miarma.contaminus.common.Table; + +@Table("v_gps_by_device") +public class DeviceGPS { + int deviceId; + float lat; + float lon; + long timestamp; + + public DeviceGPS() {} + + public DeviceGPS(Row row) { + this.deviceId = row.getInteger("deviceId"); + this.lat = row.getFloat("lat"); + this.lon = row.getFloat("lon"); + this.timestamp = row.getLong("timestamp"); + } + + public DeviceGPS(int deviceId, long lat, long lon) { + super(); + this.deviceId = deviceId; + this.lat = lat; + this.lon = lon; + } + + public int getDeviceId() { + return deviceId; + } + + public float getLat() { + return lat; + } + + public float getLon() { + return lon; + } + + public long getTimestamp() { + return timestamp; + } + + @Override + public int hashCode() { + return Objects.hash(deviceId, lat, lon, timestamp); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DeviceGPS other = (DeviceGPS) obj; + return deviceId == other.deviceId && Float.floatToIntBits(lat) == Float.floatToIntBits(other.lat) + && Float.floatToIntBits(lon) == Float.floatToIntBits(other.lon) && timestamp == other.timestamp; + } + + @Override + public String toString() { + return "DeviceGPS [deviceId=" + deviceId + ", lat=" + lat + ", lon=" + lon + ", timestamp=" + timestamp + "]"; + } +} diff --git a/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceLatestValuesView.java b/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceLatestValuesView.java new file mode 100644 index 0000000..b7cb453 --- /dev/null +++ b/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceLatestValuesView.java @@ -0,0 +1,140 @@ +package net.miarma.contaminus.database.entities; +import java.util.Objects; + +import io.vertx.sqlclient.Row; +import net.miarma.contaminus.common.Table; + +@Table("v_latest_values") +public class DeviceLatestValuesView { + int deviceId; + int sensorId; + String sensorType; + String unit; + int sensorStatus; + long sensorTimestamp; + float temperature; + float humidity; + float carbonMonoxide; + float lat; + float lon; + long airValuesTimestamp; + + public DeviceLatestValuesView() {} + + public DeviceLatestValuesView(Row row) { + this.deviceId = row.getInteger("deviceId"); + this.sensorId = row.getInteger("sensorId"); + this.sensorType = row.getString("sensorType"); + this.unit = row.getString("unit"); + this.sensorStatus = row.getInteger("sensorStatus"); + this.sensorTimestamp = row.getLong("sensorTimestamp"); + this.temperature = row.getFloat("temperature"); + this.humidity = row.getFloat("humidity"); + this.carbonMonoxide = row.getFloat("carbonMonoxide"); + this.lat = row.getFloat("lat"); + this.lon = row.getFloat("lon"); + this.airValuesTimestamp = row.getLong("airValuesTimestamp"); + } + + public DeviceLatestValuesView(int deviceId, int sensorId, String sensorType, String unit, int sensorStatus, + long sensorTimestamp, float temperature, float humidity, float carbonMonoxide, float lat, float lon, + long airValuesTimestamp) { + super(); + this.deviceId = deviceId; + this.sensorId = sensorId; + this.sensorType = sensorType; + this.unit = unit; + this.sensorStatus = sensorStatus; + this.sensorTimestamp = sensorTimestamp; + this.temperature = temperature; + this.humidity = humidity; + this.carbonMonoxide = carbonMonoxide; + this.lat = lat; + this.lon = lon; + this.airValuesTimestamp = airValuesTimestamp; + } + + public int getDeviceId() { + return deviceId; + } + + public int getSensorId() { + return sensorId; + } + + public String getSensorType() { + return sensorType; + } + + public String getUnit() { + return unit; + } + + public int getSensorStatus() { + return sensorStatus; + } + + public long getSensorTimestamp() { + return sensorTimestamp; + } + + public float getTemperature() { + return temperature; + } + + public float getHumidity() { + return humidity; + } + + public float getCarbonMonoxide() { + return carbonMonoxide; + } + + public float getLat() { + return lat; + } + + public float getLon() { + return lon; + } + + public long getAirValuesTimestamp() { + return airValuesTimestamp; + } + + @Override + public int hashCode() { + return Objects.hash(airValuesTimestamp, carbonMonoxide, deviceId, humidity, lat, lon, sensorId, sensorStatus, + sensorTimestamp, sensorType, temperature, unit); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DeviceLatestValuesView other = (DeviceLatestValuesView) obj; + return airValuesTimestamp == other.airValuesTimestamp + && Float.floatToIntBits(carbonMonoxide) == Float.floatToIntBits(other.carbonMonoxide) + && deviceId == other.deviceId && Float.floatToIntBits(humidity) == Float.floatToIntBits(other.humidity) + && Float.floatToIntBits(lat) == Float.floatToIntBits(other.lat) + && Float.floatToIntBits(lon) == Float.floatToIntBits(other.lon) && sensorId == other.sensorId + && sensorStatus == other.sensorStatus && sensorTimestamp == other.sensorTimestamp + && Objects.equals(sensorType, other.sensorType) + && Float.floatToIntBits(temperature) == Float.floatToIntBits(other.temperature) + && Objects.equals(unit, other.unit); + } + + @Override + public String toString() { + return "DeviceLatestValuesView [deviceId=" + deviceId + ", sensorId=" + sensorId + ", sensorType=" + sensorType + + ", unit=" + unit + ", sensorStatus=" + sensorStatus + ", sensorTimestamp=" + sensorTimestamp + + ", temperature=" + temperature + ", humidity=" + humidity + ", carbonMonoxide=" + carbonMonoxide + + ", lat=" + lat + ", lon=" + lon + ", airValuesTimestamp=" + airValuesTimestamp + "]"; + } + + +} diff --git a/backend/src/main/java/net/miarma/contaminus/database/entities/DevicePollutionMap.java b/backend/src/main/java/net/miarma/contaminus/database/entities/DevicePollutionMap.java new file mode 100644 index 0000000..b5aff20 --- /dev/null +++ b/backend/src/main/java/net/miarma/contaminus/database/entities/DevicePollutionMap.java @@ -0,0 +1,88 @@ +package net.miarma.contaminus.database.entities; + +import java.util.Objects; + +import io.vertx.sqlclient.Row; +import net.miarma.contaminus.common.Table; + +@Table("v_pollution_map") +public class DevicePollutionMap { + int deviceId; + String deviceName; + float lat; + float lon; + float carbonMonoxide; + long timestamp; + + public DevicePollutionMap() {} + + public DevicePollutionMap(Row row) { + this.deviceId = row.getInteger("deviceId"); + this.deviceName = row.getString("deviceName"); + this.lat = row.getFloat("lat"); + this.lon = row.getFloat("lon"); + this.carbonMonoxide = row.getFloat("carbonMonoxide"); + this.timestamp = row.getLong("timestamp"); + } + + public DevicePollutionMap(int deviceId, String deviceName, float lat, float lon, float carbonMonoxide, + long timestamp) { + super(); + this.deviceId = deviceId; + this.deviceName = deviceName; + this.lat = lat; + this.lon = lon; + this.carbonMonoxide = carbonMonoxide; + this.timestamp = timestamp; + } + + public int getDeviceId() { + return deviceId; + } + + public String getDeviceName() { + return deviceName; + } + + public float getLat() { + return lat; + } + + public float getLon() { + return lon; + } + + public float getCarbonMonoxide() { + return carbonMonoxide; + } + + public long getTimestamp() { + return timestamp; + } + + @Override + public int hashCode() { + return Objects.hash(carbonMonoxide, deviceId, deviceName, lat, lon, timestamp); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DevicePollutionMap other = (DevicePollutionMap) obj; + return Float.floatToIntBits(carbonMonoxide) == Float.floatToIntBits(other.carbonMonoxide) + && deviceId == other.deviceId && Objects.equals(deviceName, other.deviceName) + && Float.floatToIntBits(lat) == Float.floatToIntBits(other.lat) + && Float.floatToIntBits(lon) == Float.floatToIntBits(other.lon) && timestamp == other.timestamp; + } + + @Override + public String toString() { + return "DevicePollutionMap [deviceId=" + deviceId + ", deviceName=" + deviceName + ", lat=" + lat + ", lon=" + + lon + ", carbonMonoxide=" + carbonMonoxide + ", timestamp=" + timestamp + "]"; + } +} \ No newline at end of file diff --git a/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceSensorHistory.java b/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceSensorHistory.java new file mode 100644 index 0000000..34f1d1e --- /dev/null +++ b/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceSensorHistory.java @@ -0,0 +1,79 @@ +package net.miarma.contaminus.database.entities; + +import java.util.Objects; + +import io.vertx.sqlclient.Row; +import net.miarma.contaminus.common.Table; + +@Table("v_sensor_history_by_device") +public class DeviceSensorHistory { + int deviceId; + String deviceName; + float value; + String valueType; + long timestamp; + + public DeviceSensorHistory() {} + + public DeviceSensorHistory(Row row) { + this.deviceId = row.getInteger("deviceId"); + this.deviceName = row.getString("deviceName"); + this.value = row.getFloat("value"); + this.valueType = row.getString("valueType"); + this.timestamp = row.getLong("timestamp"); + } + + public DeviceSensorHistory(int deviceId, String deviceName, float value, String valueType, long timestamp) { + super(); + this.deviceId = deviceId; + this.deviceName = deviceName; + this.value = value; + this.valueType = valueType; + this.timestamp = timestamp; + } + + public int getDeviceId() { + return deviceId; + } + + public String getDeviceName() { + return deviceName; + } + + public float getValue() { + return value; + } + + public String getValueType() { + return valueType; + } + + public long getTimestamp() { + return timestamp; + } + + @Override + public int hashCode() { + return Objects.hash(deviceId, deviceName, timestamp, value, valueType); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DeviceSensorHistory other = (DeviceSensorHistory) obj; + return deviceId == other.deviceId && Objects.equals(deviceName, other.deviceName) + && timestamp == other.timestamp && Float.floatToIntBits(value) == Float.floatToIntBits(other.value) + && Objects.equals(valueType, other.valueType); + } + + @Override + public String toString() { + return "DeviceSensorHistory [deviceId=" + deviceId + ", deviceName=" + deviceName + ", value=" + value + + ", valueType=" + valueType + ", timestamp=" + timestamp + "]"; + } +} diff --git a/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceSensorValue.java b/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceSensorValue.java new file mode 100644 index 0000000..39e2676 --- /dev/null +++ b/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceSensorValue.java @@ -0,0 +1,129 @@ +package net.miarma.contaminus.database.entities; + +import java.util.Objects; + +import io.vertx.sqlclient.Row; +import net.miarma.contaminus.common.Table; + +@Table("v_sensor_values") +public class DeviceSensorValue { + int sensorId; + int deviceId; + String sensorType; + String unit; + int sensorStatus; + float temperature; + float humidity; + float carbonMonoxide; + float lat; + float lon; + long timestamp; + + public DeviceSensorValue() {} + + public DeviceSensorValue(Row row) { + this.sensorId = row.getInteger("sensorId"); + this.deviceId = row.getInteger("deviceId"); + this.sensorType = row.getString("sensorType"); + this.unit = row.getString("unit"); + this.sensorStatus = row.getInteger("sensorStatus"); + this.temperature = row.getFloat("temperature"); + this.humidity = row.getFloat("humidty"); + this.carbonMonoxide = row.getFloat("carbonMonoxide"); + this.lat = row.getFloat("lat"); + this.lon = row.getFloat("lon"); + this.timestamp = row.getLong("timestamp"); + } + + public DeviceSensorValue(int sensorId, int deviceId, String sensorType, String unit, int sensorStatus, + float temperature, float humidity, float carbonMonoxide, float lat, float lon, long timestamp) { + super(); + this.sensorId = sensorId; + this.deviceId = deviceId; + this.sensorType = sensorType; + this.unit = unit; + this.sensorStatus = sensorStatus; + this.temperature = temperature; + this.humidity = humidity; + this.carbonMonoxide = carbonMonoxide; + this.lat = lat; + this.lon = lon; + this.timestamp = timestamp; + } + + public int getSensorId() { + return sensorId; + } + + public int getDeviceId() { + return deviceId; + } + + public String getSensorType() { + return sensorType; + } + + public String getUnit() { + return unit; + } + + public int getSensorStatus() { + return sensorStatus; + } + + public float getTemperature() { + return temperature; + } + + public float getHumidity() { + return humidity; + } + + public float getCarbonMonoxide() { + return carbonMonoxide; + } + + public float getLat() { + return lat; + } + + public float getLon() { + return lon; + } + + public long getTimestamp() { + return timestamp; + } + + @Override + public int hashCode() { + return Objects.hash(carbonMonoxide, deviceId, humidity, lat, lon, sensorId, sensorStatus, sensorType, + temperature, timestamp, unit); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DeviceSensorValue other = (DeviceSensorValue) obj; + return Float.floatToIntBits(carbonMonoxide) == Float.floatToIntBits(other.carbonMonoxide) + && deviceId == other.deviceId && Float.floatToIntBits(humidity) == Float.floatToIntBits(other.humidity) + && Float.floatToIntBits(lat) == Float.floatToIntBits(other.lat) + && Float.floatToIntBits(lon) == Float.floatToIntBits(other.lon) && sensorId == other.sensorId + && sensorStatus == other.sensorStatus && Objects.equals(sensorType, other.sensorType) + && Float.floatToIntBits(temperature) == Float.floatToIntBits(other.temperature) + && timestamp == other.timestamp && Objects.equals(unit, other.unit); + } + + @Override + public String toString() { + return "DeviceSensorValue [sensorId=" + sensorId + ", deviceId=" + deviceId + ", sensorType=" + sensorType + + ", unit=" + unit + ", sensorStatus=" + sensorStatus + ", temperature=" + temperature + ", humidity=" + + humidity + ", carbonMonoxide=" + carbonMonoxide + ", lat=" + lat + ", lon=" + lon + ", timestamp=" + + timestamp + "]"; + } +} diff --git a/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceWeather.java b/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceWeather.java new file mode 100644 index 0000000..540ad8a --- /dev/null +++ b/backend/src/main/java/net/miarma/contaminus/database/entities/DeviceWeather.java @@ -0,0 +1,72 @@ +package net.miarma.contaminus.database.entities; + +import java.util.Objects; + +import io.vertx.sqlclient.Row; +import net.miarma.contaminus.common.Table; + +@Table("v_weather_by_device") +public class DeviceWeather { + int deviceId; + float temperature; + float humidity; + long timestamp; + + public DeviceWeather() {} + + public DeviceWeather(Row row) { + this.deviceId = row.getInteger("deviceId"); + this.temperature = row.getFloat("temperature"); + this.humidity = row.getFloat("humidity"); + this.timestamp = row.getLong("timestamp"); + } + + public DeviceWeather(int deviceId, float temperature, float humidity, long timestamp) { + super(); + this.deviceId = deviceId; + this.temperature = temperature; + this.humidity = humidity; + this.timestamp = timestamp; + } + + public int getDeviceId() { + return deviceId; + } + + public float getTemperature() { + return temperature; + } + + public float getHumidity() { + return humidity; + } + + public long getTimestamp() { + return timestamp; + } + + @Override + public int hashCode() { + return Objects.hash(deviceId, humidity, temperature, timestamp); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DeviceWeather other = (DeviceWeather) obj; + return deviceId == other.deviceId && Float.floatToIntBits(humidity) == Float.floatToIntBits(other.humidity) + && Float.floatToIntBits(temperature) == Float.floatToIntBits(other.temperature) + && timestamp == other.timestamp; + } + + @Override + public String toString() { + return "DeviceWeather [deviceId=" + deviceId + ", temperature=" + temperature + ", humidity=" + humidity + + ", timestamp=" + timestamp + "]"; + } +} \ No newline at end of file diff --git a/backend/src/main/java/net/miarma/contaminus/server/LogicLayerAPIVerticle.java b/backend/src/main/java/net/miarma/contaminus/server/LogicLayerAPIVerticle.java index 63d7e47..f86236a 100644 --- a/backend/src/main/java/net/miarma/contaminus/server/LogicLayerAPIVerticle.java +++ b/backend/src/main/java/net/miarma/contaminus/server/LogicLayerAPIVerticle.java @@ -19,6 +19,11 @@ import io.vertx.ext.web.handler.BodyHandler; import io.vertx.ext.web.handler.CorsHandler; import net.miarma.contaminus.common.ConfigManager; import net.miarma.contaminus.common.Constants; +import net.miarma.contaminus.database.entities.Device; +import net.miarma.contaminus.database.entities.DeviceLatestValuesView; +import net.miarma.contaminus.database.entities.DevicePollutionMap; +import net.miarma.contaminus.database.entities.DeviceSensorHistory; +import net.miarma.contaminus.database.entities.DeviceSensorValue; import net.miarma.contaminus.database.entities.Actuator; import net.miarma.contaminus.database.entities.Device; import net.miarma.contaminus.database.entities.Sensor; @@ -26,15 +31,14 @@ import net.miarma.contaminus.util.RestClientUtil; public class LogicLayerAPIVerticle extends AbstractVerticle { private ConfigManager configManager; - private RestClientUtil restClient; private final Gson gson = new Gson(); - + private RestClientUtil restClient; + public LogicLayerAPIVerticle() { this.configManager = ConfigManager.getInstance(); WebClientOptions options = new WebClientOptions() - .setUserAgent("ContaminUS") - .setKeepAlive(false); - this.restClient = new RestClientUtil(WebClient.create(Vertx.vertx(), options)); + .setUserAgent("ContaminUS"); + this.restClient = new RestClientUtil(WebClient.create(vertx, options)); } @Override @@ -72,25 +76,26 @@ public class LogicLayerAPIVerticle extends AbstractVerticle { startPromise.complete(); } - private void getGroupDevices(RoutingContext context) { - Integer groupId = Integer.parseInt(context.request().getParam("groupId")); - Promise resultList = Promise.promise(); - resultList.future().onComplete(result -> { - if (result.succeeded()) { - Device[] devices = result.result(); - List aux = Arrays.stream(devices) - .filter(d -> d.getGroupId() == groupId) - .toList(); - context.response().putHeader("Content-Type", "application/json").end(gson.toJson(aux)); - } else { - context.response().setStatusCode(500).end(result.cause().getMessage()); - } - }); - - restClient.getRequest(configManager.getDataApiPort(), "http://" + configManager.getHost(), - Constants.GET_DEVICES, Device[].class, resultList); - } + Integer groupId = Integer.parseInt(context.request().getParam("groupId")); + + Promise resultList = Promise.promise(); + resultList.future().onComplete(complete -> { + if(complete.succeeded()) { + List aux = Arrays.asList(complete.result()).stream() + .filter(d -> d.getGroupId() == groupId) + .toList(); + context.response() + .putHeader("content-type", "application/json; charset=utf-8") + .end(gson.toJson(aux)); + } else { + context.fail(500, complete.cause()); + } + }); + + this.restClient.getRequest(configManager.getDataApiPort(), configManager.getHost(), + Constants.GET_GROUP_DEVICES, Device[].class, resultList); + } private void getDeviceSensors(RoutingContext context) { Integer deviceId = Integer.parseInt(context.request().getParam("deviceId")); @@ -131,19 +136,94 @@ public class LogicLayerAPIVerticle extends AbstractVerticle { } private void getDeviceLatestValues(RoutingContext context) { - context.response().end("TODO"); + Integer deviceId = Integer.parseInt(context.request().getParam("deviceId")); + + Promise resultList = Promise.promise(); + + resultList.future().onComplete(complete -> { + if (complete.succeeded()) { + List aux = Arrays.asList(complete.result()).stream() + .filter(d -> d.getDeviceId() == deviceId) + .toList(); + + context.response() + .putHeader("content-type", "application/json; charset=utf-8") + .end(gson.toJson(aux)); + } else { + context.fail(500, complete.cause()); + } + }); + + this.restClient.getRequest(configManager.getDataApiPort(), configManager.getHost(), + Constants.GET_DEVICE_LATEST_VALUES, DeviceLatestValuesView[].class, resultList); } private void getDevicePollutionMap(RoutingContext context) { - context.response().end("TODO"); + Integer deviceId = Integer.parseInt(context.request().getParam("deviceId")); + + Promise resultList = Promise.promise(); + + resultList.future().onComplete(complete -> { + if (complete.succeeded()) { + List aux = Arrays.asList(complete.result()).stream() + .filter(d -> d.getDeviceId() == deviceId) + .toList(); + + context.response() + .putHeader("content-type", "application/json; charset=utf-8") + .end(gson.toJson(aux)); + } else { + context.fail(500, complete.cause()); + } + }); + + this.restClient.getRequest(configManager.getDataApiPort(), configManager.getHost(), + Constants.GET_DEVICE_POLLUTION_MAP, DevicePollutionMap[].class, resultList); } private void getDeviceHistory(RoutingContext context) { - context.response().end("TODO"); + Integer deviceId = Integer.parseInt(context.request().getParam("deviceId")); + + Promise resultList = Promise.promise(); + + resultList.future().onComplete(complete -> { + if (complete.succeeded()) { + List aux = Arrays.asList(complete.result()).stream() + .filter(d -> d.getDeviceId() == deviceId) + .toList(); + + context.response() + .putHeader("content-type", "application/json; charset=utf-8") + .end(gson.toJson(aux)); + } else { + context.fail(500, complete.cause()); + } + }); + + this.restClient.getRequest(configManager.getDataApiPort(), configManager.getHost(), + Constants.GET_DEVICE_HISTORY, DeviceSensorHistory[].class, resultList); } private void getSensorValues(RoutingContext context) { - context.response().end("TODO"); + Integer deviceId = Integer.parseInt(context.request().getParam("deviceId")); + + Promise resultList = Promise.promise(); + + resultList.future().onComplete(complete -> { + if (complete.succeeded()) { + List aux = Arrays.asList(complete.result()).stream() + .filter(d -> d.getDeviceId() == deviceId) + .toList(); + + context.response() + .putHeader("content-type", "application/json; charset=utf-8") + .end(gson.toJson(aux)); + } else { + context.fail(500, complete.cause()); + } + }); + + this.restClient.getRequest(configManager.getDataApiPort(), configManager.getHost(), + Constants.GET_SENSOR_VALUES, DeviceSensorValue[].class, resultList); } - } \ No newline at end of file