1
0

Add: get groupId by deviceId for dynamic groupId config on startup

This commit is contained in:
Jose
2025-06-03 22:43:16 +02:00
parent 24802271bb
commit fc9a622252
2 changed files with 23 additions and 1 deletions

View File

@@ -22,6 +22,7 @@ public class Constants {
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 DEVICE_GROUP_ID = RAW_API_PREFIX + "/devices/:deviceId/my-group"; // GET
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

View File

@@ -116,7 +116,8 @@ public class DataLayerAPIVerticle extends AbstractVerticle {
router.route(HttpMethod.GET, Constants.DEVICE).handler(this::getDeviceById);
router.route(HttpMethod.POST, Constants.DEVICES).handler(this::addDevice);
router.route(HttpMethod.PUT, Constants.DEVICE).handler(this::updateDevice);
router.route(HttpMethod.GET, Constants.DEVICE_GROUP_ID).handler(this::getDeviceGroupId);
// Sensor Routes
router.route(HttpMethod.GET, Constants.SENSORS).handler(this::getAllSensors);
router.route(HttpMethod.GET, Constants.SENSOR).handler(this::getSensorById);
@@ -247,6 +248,26 @@ public class DataLayerAPIVerticle extends AbstractVerticle {
});
}
private void getDeviceGroupId(RoutingContext context) {
String deviceId = context.request().getParam("deviceId");
deviceDAO.getById(deviceId)
.onSuccess(device -> {
if (device != null) {
Integer groupId = device.getGroupId();
SingleJsonResponse<Integer> response = SingleJsonResponse.of(groupId);
context.response()
.putHeader("content-type", "application/json; charset=utf-8")
.end(gson.toJson(response));
} else {
context.response().setStatusCode(404).end("Device not found");
}
})
.onFailure(err -> {
context.fail(500, err);
});
}
private void addDevice(RoutingContext context) {
JsonObject body = context.body().asJsonObject();
Device device = gson.fromJson(body.toString(), Device.class);