aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2017-05-23 21:41:37 +1200
committerGitHub <noreply@github.com>2017-05-23 21:41:37 +1200
commit2858f2b852c39c9517a4b353d2ed4bff6bccc627 (patch)
treeb2a6ee52ff86a727448efd8e29440980bbc200d6
parent23d502088fc76990687d4363099a7b92d1cf7d21 (diff)
parent51e77091952a1279bfbfa1fdec194f04308136ac (diff)
downloadtraccar-server-2858f2b852c39c9517a4b353d2ed4bff6bccc627.tar.gz
traccar-server-2858f2b852c39c9517a4b353d2ed4bff6bccc627.tar.bz2
traccar-server-2858f2b852c39c9517a4b353d2ed4bff6bccc627.zip
Merge pull request #3183 from Abyss777/test_computed_attribute
Implement testing API for computedAttribute
-rw-r--r--src/org/traccar/api/resource/AttributeResource.java39
1 files changed, 36 insertions, 3 deletions
diff --git a/src/org/traccar/api/resource/AttributeResource.java b/src/org/traccar/api/resource/AttributeResource.java
index 7751a9360..4d326779b 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,15 +83,46 @@ public class AttributeResource extends BaseResource {
return attributesManager.getAttributes(result);
}
- @POST
- public Response add(Attribute entity) throws SQLException {
- Context.getPermissionsManager().checkReadonly(getUserId());
+
+ 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 post(@QueryParam("deviceId") long deviceId, Attribute entity) throws SQLException {
+ Context.getPermissionsManager().checkReadonly(getUserId());
+ if (deviceId != 0) {
+ Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
+ return test(deviceId, entity);
+ } else {
+ return add(entity);
+ }
+ }
+
@Path("{id}")
@PUT
public Response update(Attribute entity) throws SQLException {