Add: get groupId by deviceId for dynamic groupId config on startup
This commit is contained in:
@@ -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 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 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 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 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 = RAW_API_PREFIX + "/groups/:groupId/devices/:deviceId/sensors/:sensorId"; // GET, PUT
|
||||||
|
|||||||
@@ -116,6 +116,7 @@ public class DataLayerAPIVerticle extends AbstractVerticle {
|
|||||||
router.route(HttpMethod.GET, Constants.DEVICE).handler(this::getDeviceById);
|
router.route(HttpMethod.GET, Constants.DEVICE).handler(this::getDeviceById);
|
||||||
router.route(HttpMethod.POST, Constants.DEVICES).handler(this::addDevice);
|
router.route(HttpMethod.POST, Constants.DEVICES).handler(this::addDevice);
|
||||||
router.route(HttpMethod.PUT, Constants.DEVICE).handler(this::updateDevice);
|
router.route(HttpMethod.PUT, Constants.DEVICE).handler(this::updateDevice);
|
||||||
|
router.route(HttpMethod.GET, Constants.DEVICE_GROUP_ID).handler(this::getDeviceGroupId);
|
||||||
|
|
||||||
// Sensor Routes
|
// Sensor Routes
|
||||||
router.route(HttpMethod.GET, Constants.SENSORS).handler(this::getAllSensors);
|
router.route(HttpMethod.GET, Constants.SENSORS).handler(this::getAllSensors);
|
||||||
@@ -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) {
|
private void addDevice(RoutingContext context) {
|
||||||
JsonObject body = context.body().asJsonObject();
|
JsonObject body = context.body().asJsonObject();
|
||||||
Device device = gson.fromJson(body.toString(), Device.class);
|
Device device = gson.fromJson(body.toString(), Device.class);
|
||||||
|
|||||||
Reference in New Issue
Block a user