From 864bc98ab078d5bc13249553c9f7865195083354 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 23 May 2017 13:33:22 +0500 Subject: Implement testing API for computedAttribute --- .../traccar/api/resource/AttributeResource.java | 35 ++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/api/resource/AttributeResource.java b/src/org/traccar/api/resource/AttributeResource.java index 7751a9360..a1840cf51 100644 --- a/src/org/traccar/api/resource/AttributeResource.java +++ b/src/org/traccar/api/resource/AttributeResource.java @@ -37,6 +37,8 @@ import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.database.AttributesManager; import org.traccar.model.Attribute; +import org.traccar.model.Position; +import org.traccar.processing.ComputedAttributesHandler; @Path("attributes/computed") @Produces(MediaType.APPLICATION_JSON) @@ -81,13 +83,36 @@ public class AttributeResource extends BaseResource { return attributesManager.getAttributes(result); } + @POST - public Response add(Attribute entity) throws SQLException { + public Response add(@QueryParam("deviceId") long deviceId, Attribute entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getAttributesManager().addAttribute(entity); - Context.getDataManager().linkAttribute(getUserId(), entity.getId()); - Context.getAttributesManager().refreshUserAttributes(); - return Response.ok(entity).build(); + if (deviceId != 0) { + Context.getPermissionsManager().checkDevice(getUserId(), deviceId); + Position last = Context.getIdentityManager().getLastPosition(deviceId); + if (last != null) { + Object result = new ComputedAttributesHandler().computeAttribute(entity, last); + if (result != null) { + switch (entity.getType()) { + case "number": + return Response.ok((Number) result).build(); + case "boolean": + return Response.ok((Boolean) result).build(); + default: + return Response.ok(result.toString()).build(); + } + } else { + return Response.noContent().build(); + } + } else { + throw new IllegalArgumentException("Device has no last position"); + } + } else { + Context.getAttributesManager().addAttribute(entity); + Context.getDataManager().linkAttribute(getUserId(), entity.getId()); + Context.getAttributesManager().refreshUserAttributes(); + return Response.ok(entity).build(); + } } @Path("{id}") -- cgit v1.2.3 From 51e77091952a1279bfbfa1fdec194f04308136ac Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 23 May 2017 14:37:33 +0500 Subject: Split POST on two functions --- .../traccar/api/resource/AttributeResource.java | 54 +++++++++++++--------- 1 file changed, 31 insertions(+), 23 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/api/resource/AttributeResource.java b/src/org/traccar/api/resource/AttributeResource.java index a1840cf51..4d326779b 100644 --- a/src/org/traccar/api/resource/AttributeResource.java +++ b/src/org/traccar/api/resource/AttributeResource.java @@ -84,34 +84,42 @@ public class AttributeResource extends BaseResource { } + private Response add(Attribute entity) throws SQLException { + Context.getAttributesManager().addAttribute(entity); + Context.getDataManager().linkAttribute(getUserId(), entity.getId()); + Context.getAttributesManager().refreshUserAttributes(); + return Response.ok(entity).build(); + } + + private Response test(long deviceId, Attribute entity) { + Position last = Context.getIdentityManager().getLastPosition(deviceId); + if (last != null) { + Object result = new ComputedAttributesHandler().computeAttribute(entity, last); + if (result != null) { + switch (entity.getType()) { + case "number": + return Response.ok((Number) result).build(); + case "boolean": + return Response.ok((Boolean) result).build(); + default: + return Response.ok(result.toString()).build(); + } + } else { + return Response.noContent().build(); + } + } else { + throw new IllegalArgumentException("Device has no last position"); + } + } + @POST - public Response add(@QueryParam("deviceId") long deviceId, Attribute entity) throws SQLException { + public Response post(@QueryParam("deviceId") long deviceId, Attribute entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); if (deviceId != 0) { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - Position last = Context.getIdentityManager().getLastPosition(deviceId); - if (last != null) { - Object result = new ComputedAttributesHandler().computeAttribute(entity, last); - if (result != null) { - switch (entity.getType()) { - case "number": - return Response.ok((Number) result).build(); - case "boolean": - return Response.ok((Boolean) result).build(); - default: - return Response.ok(result.toString()).build(); - } - } else { - return Response.noContent().build(); - } - } else { - throw new IllegalArgumentException("Device has no last position"); - } + return test(deviceId, entity); } else { - Context.getAttributesManager().addAttribute(entity); - Context.getDataManager().linkAttribute(getUserId(), entity.getId()); - Context.getAttributesManager().refreshUserAttributes(); - return Response.ok(entity).build(); + return add(entity); } } -- cgit v1.2.3