1
0

Cambios backend/frontend

This commit is contained in:
Jose
2025-05-26 22:30:04 +02:00
parent 870933f389
commit 7b13affb3c
18 changed files with 3752 additions and 250 deletions

View File

@@ -94,6 +94,12 @@
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-api-contract</artifactId>
<version>4.5.1</version>
</dependency>
</dependencies>
<build>

View File

@@ -10,33 +10,36 @@ public class Constants {
public static final String CONTAMINUS_EB = "contaminus.eventbus";
public static Logger LOGGER = LoggerFactory.getLogger(Constants.APP_NAME);
public static final int SENSOR_ROLE = 0;
public static final int ACTUATOR_ROLE = 1;
/* API Endpoints */
public static final String GROUPS = RAW_API_PREFIX + "/groups";
public static final String GROUP = RAW_API_PREFIX + "/groups/:groupId";
public static final String GROUPS = RAW_API_PREFIX + "/groups"; // GET, POST
public static final String GROUP = RAW_API_PREFIX + "/groups/:groupId"; // GET, PUT
public static final String DEVICES = RAW_API_PREFIX + "/groups/:groupId/devices";
public static final String DEVICE = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId";
public static final String LATEST_VALUES = API_PREFIX + "/groups/:groupId/devices/:deviceId/latest-values";
public static final String POLLUTION_MAP = API_PREFIX + "/groups/:groupId/devices/:deviceId/pollution-map";
public static final String HISTORY = API_PREFIX + "/groups/:groupId/devices/:deviceId/history";
public static final String DEVICES = RAW_API_PREFIX + "/groups/:groupId/devices"; // GET, POST
public static final String DEVICE = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId"; // GET, PUT
public static final String LATEST_VALUES = API_PREFIX + "/groups/:groupId/devices/:deviceId/latest-values"; // GET
public static final String POLLUTION_MAP = API_PREFIX + "/groups/:groupId/devices/:deviceId/pollution-map"; // GET
public static final String HISTORY = API_PREFIX + "/groups/:groupId/devices/:deviceId/history"; // GET
public static final String SENSORS = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/sensors";
public static final String SENSOR = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/sensors/:sensorId";
public static final String SENSOR_VALUES = API_PREFIX + "/groups/:groupId/devices/:deviceId/sensors/:sensorId/values";
public static final String SENSORS = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/sensors"; // GET, POST
public static final String SENSOR = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/sensors/:sensorId"; // GET, PUT
public static final String SENSOR_VALUES = API_PREFIX + "/groups/:groupId/devices/:deviceId/sensors/:sensorId/values"; // GET
public static final String BATCH = API_PREFIX + "/batch";
public static final String ADD_GPS_VALUE = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/sensors/:sensorId/gps_values";
public static final String ADD_WEATHER_VALUE = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/sensors/:sensorId/weather_values";
public static final String ADD_CO_VALUE = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/sensors/:sensorId/co_values";
public static final String BATCH = API_PREFIX + "/batch"; // POST
public static final String ADD_GPS_VALUE = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/sensors/:sensorId/gps_values"; // POST
public static final String ADD_WEATHER_VALUE = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/sensors/:sensorId/weather_values"; // POST
public static final String ADD_CO_VALUE = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/sensors/:sensorId/co_values"; // POST
public static final String ACTUATORS = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/actuators";
public static final String ACTUATOR = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/actuators/:actuator_id";
public static final String ACTUATOR_STATUS = API_PREFIX + "/groups/:groupId/devices/:deviceId/actuators/:actuator_id/status";
public static final String ACTUATORS = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/actuators"; // GET, POST
public static final String ACTUATOR = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/actuators/:actuator_id"; // GET, PUT
public static final String ACTUATOR_STATUS = API_PREFIX + "/groups/:groupId/devices/:deviceId/actuators/:actuator_id/status"; // GET
public static final String VIEW_LATEST_VALUES = RAW_API_PREFIX + "/v_latest_values";
public static final String VIEW_POLLUTION_MAP = RAW_API_PREFIX + "/v_pollution_map";
public static final String VIEW_SENSOR_HISTORY = RAW_API_PREFIX + "/v_sensor_history_by_device";
public static final String VIEW_SENSOR_VALUES = RAW_API_PREFIX + "/v_sensor_values";
public static final String VIEW_LATEST_VALUES = RAW_API_PREFIX + "/v_latest_values"; // GET
public static final String VIEW_POLLUTION_MAP = RAW_API_PREFIX + "/v_pollution_map"; // GET
public static final String VIEW_SENSOR_HISTORY = RAW_API_PREFIX + "/v_sensor_history_by_device"; // GET
public static final String VIEW_SENSOR_VALUES = RAW_API_PREFIX + "/v_sensor_values"; // GET
private Constants() {
throw new AssertionError("Utility class cannot be instantiated.");

View File

@@ -24,16 +24,16 @@ public class VoronoiZoneDetector {
private static class Zone {
Polygon polygon;
String actuatorId;
Integer groupId;
public Zone(Polygon polygon, String actuatorId) {
public Zone(Polygon polygon, Integer groupId) {
this.polygon = polygon;
this.actuatorId = actuatorId;
this.groupId = groupId;
}
}
private final List<Zone> zones = new ArrayList<>();
private final GeometryFactory geometryFactory = new GeometryFactory();
private static final List<Zone> zones = new ArrayList<>();
private static final GeometryFactory geometryFactory = new GeometryFactory();
private final Gson gson = new Gson();
public VoronoiZoneDetector(String geojsonUrl, boolean isUrl) throws Exception {
@@ -54,10 +54,10 @@ public class VoronoiZoneDetector {
for (int i = 0; i < features.size(); i++) {
JsonObject feature = features.get(i).getAsJsonObject();
String actuatorId = feature
Integer groupId = feature
.getAsJsonObject("properties")
.get("actuatorId")
.getAsString();
.get("groupId")
.getAsInt();
JsonObject geometryJson = feature.getAsJsonObject("geometry");
String geometryStr = gson.toJson(geometryJson);
@@ -65,36 +65,22 @@ public class VoronoiZoneDetector {
Geometry geometry = reader.read(geometryStr);
if (geometry instanceof Polygon polygon) {
zones.add(new Zone(polygon, actuatorId));
zones.add(new Zone(polygon, groupId));
} else {
Constants.LOGGER.error("⚠️ Geometría ignorada: no es un polígono");
}
}
}
public String getZoneForPoint(double lon, double lat) {
public static Integer getZoneForPoint(double lon, double lat) {
Point p = geometryFactory.createPoint(new Coordinate(lon, lat));
for (Zone z : zones) {
if (z.polygon.covers(p)) {
return z.actuatorId;
return z.groupId;
}
}
return null; // no está dentro de ninguna zona
}
public static void main(String[] args) throws Exception {
VoronoiZoneDetector detector = new VoronoiZoneDetector("https://miarma.net/files/voronoi_sevilla_geovoronoi.geojson", true);
double lon = -5.9752;
double lat = 37.3887;
String actuatorId = detector.getZoneForPoint(lon, lat);
if (actuatorId != null) {
System.out.println("📍 El punto pertenece al actuator: " + actuatorId);
} else {
System.out.println("🚫 El punto no pertenece a ninguna zona");
}
}
}

View File

@@ -9,6 +9,7 @@ public class Device {
private String deviceId;
private Integer groupId;
private String deviceName;
private Integer deviceRole;
public Device() {}
@@ -16,13 +17,16 @@ public class Device {
this.deviceId = row.getString("deviceId");
this.groupId = row.getInteger("groupId");
this.deviceName = row.getString("deviceName");
this.deviceRole = row.getInteger("deviceRole");
}
public Device(String deviceId, Integer groupId, String deviceName) {
public Device(String deviceId, Integer groupId, String deviceName, Integer deviceRole) {
super();
this.deviceId = deviceId;
this.groupId = groupId;
this.deviceName = deviceName;
this.deviceRole = deviceRole;
}
public String getDeviceId() {
@@ -49,9 +53,13 @@ public class Device {
this.deviceName = deviceName;
}
public Integer getDeviceRole() {
return deviceRole;
}
public void setDevice(Integer deviceRole) {
this.deviceRole = deviceRole;
}
}

View File

@@ -474,6 +474,7 @@ public class DataLayerAPIVerticle extends AbstractVerticle {
dbManager.execute(query, ViewLatestValues.class,
onSuccess -> {
Constants.LOGGER.info(gson.toJson(onSuccess));
context.response()
.putHeader("content-type", "application/json; charset=utf-8")
.end(gson.toJson(onSuccess));

View File

@@ -25,7 +25,9 @@ import io.vertx.mqtt.MqttClient;
import io.vertx.mqtt.MqttClientOptions;
import net.miarma.contaminus.common.ConfigManager;
import net.miarma.contaminus.common.Constants;
import net.miarma.contaminus.common.VoronoiZoneDetector;
import net.miarma.contaminus.entities.COValue;
import net.miarma.contaminus.entities.Device;
import net.miarma.contaminus.entities.GpsValue;
import net.miarma.contaminus.entities.ViewLatestValues;
import net.miarma.contaminus.entities.ViewPollutionMap;
@@ -155,7 +157,7 @@ public class LogicLayerAPIVerticle extends AbstractVerticle {
return;
}
String groupId = body.getString("groupId");
String groupId = body.getString("groupId");
String deviceId = body.getString("deviceId");
JsonObject gps = body.getJsonObject("gps");
@@ -171,30 +173,45 @@ public class LogicLayerAPIVerticle extends AbstractVerticle {
WeatherValue weatherValue = gson.fromJson(weather.toString(), WeatherValue.class);
COValue coValue = gson.fromJson(co.toString(), COValue.class);
// MQTT publish =============================
float coAmount = coValue.getValue();
Constants.LOGGER.info("CO amount received: " + coAmount);
String topic = buildTopic(Integer.parseInt(groupId), deviceId, "matrix");
Constants.LOGGER.info("Topic: " + topic);
if (mqttClient.isConnected()) {
Constants.LOGGER.info("🟢 Publishing to MQTT topic: " + topic + " with value: " + coAmount);
mqttClient.publish(topic, Buffer.buffer(coAmount >= 80.0f ? "ECO" : "GAS"),
MqttQoS.AT_LEAST_ONCE, false, false);
Constants.LOGGER.info("🟢 Message published to MQTT topic: " + topic);
if(!VoronoiZoneDetector.getZoneForPoint(gpsValue.getLat(), gpsValue.getLon())
.equals(Integer.valueOf(groupId))) {
Constants.LOGGER.info("El dispositivo no ha medido en su zona");
return;
}
// ============================================
String host = "http://" + configManager.getHost();
int port = configManager.getDataApiPort();
String gpsPath = Constants.ADD_GPS_VALUE.replace(":groupId", groupId).replace(":deviceId", deviceId);
String weatherPath = Constants.ADD_WEATHER_VALUE.replace(":groupId", groupId).replace(":deviceId", deviceId);
String coPath = Constants.ADD_CO_VALUE.replace(":groupId", groupId).replace(":deviceId", deviceId);
String devicesPath = Constants.DEVICES.replace(":groupId", groupId);
restClient.getRequest(port, host, devicesPath, Device[].class)
.onSuccess(ar -> {
Arrays.stream(ar)
.filter(d -> d.getDeviceRole().equals(Constants.ACTUATOR_ROLE))
.forEach(d -> {
float coAmount = coValue.getValue();
Constants.LOGGER.info("CO amount received: " + coAmount);
String topic = buildTopic(Integer.parseInt(groupId), d.getDeviceId(), "matrix");
Constants.LOGGER.info("Topic: " + topic);
if (mqttClient.isConnected()) {
Constants.LOGGER.info("🟢 Publishing to MQTT topic: " + topic + " with value: " + coAmount);
mqttClient.publish(topic, Buffer.buffer(coAmount >= 80.0f ? "ECO" : "GAS"),
MqttQoS.AT_LEAST_ONCE, false, false);
Constants.LOGGER.info("🟢 Message published to MQTT topic: " + topic);
}
});
})
.onFailure(err -> {
context.fail(500, err);
});
gpsValue.setDeviceId(deviceId);
weatherValue.setDeviceId(deviceId);
coValue.setDeviceId(deviceId);
String host = "http://" + configManager.getHost();
int port = configManager.getDataApiPort();
String gpsPath = Constants.ADD_GPS_VALUE.replace(":groupId", groupId).replace(":deviceId", deviceId);
String weatherPath = Constants.ADD_WEATHER_VALUE.replace(":groupId", groupId).replace(":deviceId", deviceId);
String coPath = Constants.ADD_CO_VALUE.replace(":groupId", groupId).replace(":deviceId", deviceId);
restClient.postRequest(port, host, gpsPath, gpsValue, GpsValue.class)
.compose(_ -> restClient.postRequest(port, host, weatherPath, weatherValue, WeatherValue.class))
.compose(_ -> restClient.postRequest(port, host, coPath, coValue, COValue.class))

View File

@@ -31,7 +31,7 @@ public class MainVerticle extends AbstractVerticle {
File baseDir = new File(this.configManager.getBaseDir());
if (!baseDir.exists()) {
baseDir.mkdirs();
}
}
}
private void copyDefaultConfig() {

View File

@@ -1,18 +0,0 @@
package net.miarma.contaminus.verticles;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
public class MqttVerticle extends AbstractVerticle {
@Override
public void start(Promise<Void> startPromise) {
}
@Override
public void stop(Promise<Void> startPromise) {
}
}

View File

@@ -0,0 +1,325 @@
openapi: 3.0.0
info:
title: ContaminUS API
version: 1.0.0
description: Documentación de la API del proyecto ContaminUS
servers:
- url: http://localhost:8888
description: Servidor local de desarrollo
paths:
/api/v1/groups/{groupId}/devices/{deviceId}/latest-values:
get:
summary: Últimos valores de los sensores del dispositivo
parameters:
- name: groupId
in: path
required: true
schema:
type: string
- name: deviceId
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa
/api/v1/groups/{groupId}/devices/{deviceId}/pollution-map:
get:
summary: Mapa de contaminación del dispositivo
parameters:
- name: groupId
in: path
required: true
schema:
type: string
- name: deviceId
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa
/api/v1/groups/{groupId}/devices/{deviceId}/history:
get:
summary: Historial de valores del dispositivo
parameters:
- name: groupId
in: path
required: true
schema:
type: string
- name: deviceId
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa
/api/v1/groups/{groupId}/devices/{deviceId}/sensors/{sensorId}/values:
get:
summary: Valores del sensor
parameters:
- name: groupId
in: path
required: true
schema:
type: string
- name: deviceId
in: path
required: true
schema:
type: string
- name: sensorId
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa
/api/raw/v1/groups:
get:
summary: Lista de grupos
responses:
"200":
description: Operación exitosa
/api/raw/v1/groups/{groupId}:
get:
summary: Información de un grupo
parameters:
- name: groupId
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa
/api/raw/v1/groups/{groupId}/devices:
get:
summary: Lista de dispositivos de un grupo
parameters:
- name: groupId
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa
/api/raw/v1/groups/{groupId}/devices/{deviceId}:
get:
summary: Información de un dispositivo
parameters:
- name: groupId
in: path
required: true
schema:
type: string
- name: deviceId
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa
/api/raw/v1/groups/{groupId}/devices/{deviceId}/sensors:
get:
summary: Lista de sensores
parameters:
- name: groupId
in: path
required: true
schema:
type: string
- name: deviceId
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa
/api/raw/v1/groups/{groupId}/devices/{deviceId}/sensors/{sensorId}:
get:
summary: Información de un sensor
parameters:
- name: groupId
in: path
required: true
schema:
type: string
- name: deviceId
in: path
required: true
schema:
type: string
- name: sensorId
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa
/api/raw/v1/groups/{groupId}/devices/{deviceId}/actuators:
get:
summary: Lista de actuadores
parameters:
- name: groupId
in: path
required: true
schema:
type: string
- name: deviceId
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa
/api/raw/v1/groups/{groupId}/devices/{deviceId}/actuators/{actuator_id}:
get:
summary: Información de un actuador
parameters:
- name: groupId
in: path
required: true
schema:
type: string
- name: deviceId
in: path
required: true
schema:
type: string
- name: actuator_id
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa
/api/v1/groups/{groupId}/devices/{deviceId}/actuators/{actuator_id}/status:
get:
summary: Estado de un actuador
parameters:
- name: groupId
in: path
required: true
schema:
type: string
- name: deviceId
in: path
required: true
schema:
type: string
- name: actuator_id
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa
/api/raw/v1/v_latest_values:
get:
summary: Vista de últimos valores
responses:
"200":
description: Operación exitosa
/api/raw/v1/v_pollution_map:
get:
summary: Vista de mapa de contaminación
responses:
"200":
description: Operación exitosa
/api/raw/v1/v_sensor_history_by_device:
get:
summary: Vista de historial de sensores
responses:
"200":
description: Operación exitosa
/api/raw/v1/v_sensor_values:
get:
summary: Vista de valores de sensores
responses:
"200":
description: Operación exitosa
/api/v1/batch:
post:
summary: Insertar batch de datos
responses:
"200":
description: Operación exitosa
/api/raw/v1/groups/{groupId}/devices/{deviceId}/sensors/{sensorId}/gps_values:
post:
summary: Insertar valor GPS
parameters:
- name: groupId
in: path
required: true
schema:
type: string
- name: deviceId
in: path
required: true
schema:
type: string
- name: sensorId
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa
/api/raw/v1/groups/{groupId}/devices/{deviceId}/sensors/{sensorId}/weather_values:
post:
summary: Insertar valor climático
parameters:
- name: groupId
in: path
required: true
schema:
type: string
- name: deviceId
in: path
required: true
schema:
type: string
- name: sensorId
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa
/api/raw/v1/groups/{groupId}/devices/{deviceId}/sensors/{sensorId}/co_values:
post:
summary: Insertar valor de CO
parameters:
- name: groupId
in: path
required: true
schema:
type: string
- name: deviceId
in: path
required: true
schema:
type: string
- name: sensorId
in: path
required: true
schema:
type: string
responses:
"200":
description: Operación exitosa