aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/api
diff options
context:
space:
mode:
authorAbyss777 <abyss@fox5.ru>2017-05-23 13:33:22 +0500
committerAbyss777 <abyss@fox5.ru>2017-05-23 13:33:22 +0500
commit864bc98ab078d5bc13249553c9f7865195083354 (patch)
tree22e8fbb35423f22e1ec8edcb1f89f38090785f6f /src/org/traccar/api
parent284f2835b4ab9a1a921eb431c2884eeed897dea8 (diff)
downloadtraccar-server-864bc98ab078d5bc13249553c9f7865195083354.tar.gz
traccar-server-864bc98ab078d5bc13249553c9f7865195083354.tar.bz2
traccar-server-864bc98ab078d5bc13249553c9f7865195083354.zip
Implement testing API for computedAttribute
Diffstat (limited to 'src/org/traccar/api')
-rw-r--r--src/org/traccar/api/resource/AttributeResource.java35
1 files changed, 30 insertions, 5 deletions
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}")