[Minor] added simpler results for REST API

This commit is contained in:
Robert von Burg 2021-05-29 23:00:46 +02:00
parent 974c56f283
commit 3dbf426a09
3 changed files with 42 additions and 17 deletions

View File

@ -70,7 +70,8 @@ public class PlcLogicalDevicesResource {
@GET
@Path("{id}/addresses")
@Produces(MediaType.APPLICATION_JSON)
public Response getAddresses(@Context HttpServletRequest request, @PathParam("id") String id) {
public Response getAddresses(@Context HttpServletRequest request, @PathParam("id") String id,
@QueryParam("simple") boolean simple) {
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
@ -80,7 +81,8 @@ public class PlcLogicalDevicesResource {
tx.assertHasPrivilege(Operation.GET, plcLogicalDevice);
dataJ = tx.getResourcesByRelation(plcLogicalDevice, PARAM_ADDRESSES, true).stream()
.map(e -> e.accept(plcAddressToJson())).collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
.map(e -> e.accept(plcAddressToJson(simple)))
.collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
}
return ResponseUtil.toResponse(DATA, dataJ);
@ -89,7 +91,8 @@ public class PlcLogicalDevicesResource {
@GET
@Path("{id}/notifications")
@Produces(MediaType.APPLICATION_JSON)
public Response getNotifications(@Context HttpServletRequest request, @PathParam("id") String id) {
public Response getNotifications(@Context HttpServletRequest request, @PathParam("id") String id,
@QueryParam("simple") boolean simple) {
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
@ -99,7 +102,8 @@ public class PlcLogicalDevicesResource {
tx.assertHasPrivilege(Operation.GET, plcLogicalDevice);
dataJ = tx.getResourcesByRelation(plcLogicalDevice, PARAM_ADDRESSES, true).stream()
.map(e -> e.accept(plcAddressToJson())).collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
.map(e -> e.accept(plcAddressToJson(simple)))
.collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
}
return ResponseUtil.toResponse(DATA, dataJ);
@ -108,7 +112,8 @@ public class PlcLogicalDevicesResource {
@GET
@Path("{id}/telegrams")
@Produces(MediaType.APPLICATION_JSON)
public Response getTelegrams(@Context HttpServletRequest request, @PathParam("id") String id) {
public Response getTelegrams(@Context HttpServletRequest request, @PathParam("id") String id,
@QueryParam("simple") boolean simple) {
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
@ -118,7 +123,8 @@ public class PlcLogicalDevicesResource {
tx.assertHasPrivilege(Operation.GET, plcLogicalDevice);
dataJ = tx.getResourcesByRelation(plcLogicalDevice, PARAM_TELEGRAMS, true).stream()
.map(e -> e.accept(plcTelegramToJson())).collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
.map(e -> e.accept(plcTelegramToJson(simple)))
.collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
}
return ResponseUtil.toResponse(DATA, dataJ);

View File

@ -1,15 +1,16 @@
package li.strolch.plc.rest;
import static li.strolch.plc.model.PlcConstants.*;
import static java.util.Comparator.comparing;
import static li.strolch.model.StrolchModelConstants.BAG_PARAMETERS;
import static li.strolch.plc.model.PlcConstants.*;
import li.strolch.plc.model.PlcAddress;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import li.strolch.model.Tags;
import li.strolch.model.json.StrolchRootElementToJsonVisitor;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.visitor.ResourceVisitor;
import li.strolch.plc.model.PlcAddress;
public class PlcModelVisitor {
@ -18,7 +19,7 @@ public class PlcModelVisitor {
}
public static StrolchRootElementToJsonVisitor toJsonFlat() {
return toJson().flat();
return toJson().flat().withoutVersion();
}
public static StrolchRootElementToJsonVisitor plcConnectionToJson() {
@ -44,21 +45,40 @@ public class PlcModelVisitor {
return toJsonFlat();
}
public static StrolchRootElementToJsonVisitor plcAddressToJson() {
public static ResourceVisitor<JsonObject> plcAddressToJson(boolean simple) {
if (simple)
return telegram -> {
JsonObject telegramJ = new JsonObject();
telegramJ.addProperty(PARAM_ADDRESS, telegram.getString(PARAM_ADDRESS));
telegramJ.addProperty(PARAM_RESOURCE, telegram.getString(PARAM_RESOURCE));
telegramJ.addProperty(PARAM_VALUE_TYPE, telegram.getParameter(PARAM_VALUE).getValueType().getType());
telegramJ.addProperty(PARAM_ACTION, telegram.getString(PARAM_ACTION));
telegramJ.addProperty(PARAM_VALUE, telegram.getParameter(PARAM_VALUE, true).getValueAsString());
return telegramJ;
};
return toJsonFlat().resourceHook((address, addressJ) -> {
addressJ.addProperty(PARAM_VALUE_TYPE, address.getParameter(PARAM_VALUE).getValueType().getType());
});
addressJ.addProperty(PARAM_VALUE_TYPE, address.getParameter(PARAM_VALUE, true).getValueType().getType());
}).asResourceVisitor();
}
public static StrolchRootElementToJsonVisitor plcTelegramToJson() {
public static ResourceVisitor<JsonObject> plcTelegramToJson(boolean simple) {
if (simple)
return telegram -> {
JsonObject telegramJ = new JsonObject();
telegramJ.addProperty(PARAM_RESOURCE, telegram.getString(PARAM_RESOURCE));
telegramJ.addProperty(PARAM_ACTION, telegram.getString(PARAM_ACTION));
telegramJ.addProperty(PARAM_VALUE, telegram.getParameter(PARAM_VALUE, true).getValueAsString());
return telegramJ;
};
return toJsonFlat().resourceHook((address, addressJ) -> {
addressJ.addProperty(PARAM_VALUE_TYPE, address.getParameter(PARAM_VALUE).getValueType().getType());
});
}).asResourceVisitor();
}
public static JsonObject plcAddressToJson(PlcAddress plcAddress) {
JsonObject addressJ = new JsonObject();
addressJ.addProperty(PARAM_ADDRESS, plcAddress.address);
addressJ.addProperty(PARAM_RESOURCE, plcAddress.resource);
addressJ.addProperty(PARAM_ACTION, plcAddress.action);
@ -67,7 +87,6 @@ public class PlcModelVisitor {
addressJ.add(Tags.Json.VALUE, plcAddress.valueType.valueToJson(plcAddress.defaultValue));
addressJ.addProperty(Tags.Json.TYPE, plcAddress.valueType.name());
addressJ.addProperty(PARAM_VALUE_TYPE, plcAddress.valueType.name());
return addressJ;
}
}

View File

@ -25,7 +25,7 @@ public class PlcWebSocketObserverHandler extends WebSocketObserverHandler {
@Override
protected JsonObject toJson(StrolchRootElement e) {
if (e.isResource() && e.getType().equals(TYPE_PLC_ADDRESS))
return e.accept(plcAddressToJson());
return e.accept(plcAddressToJson(false));
return super.toJson(e);
}
}