From 66dbefa18290d2f31d545fd1f511207cdcd7c3a2 Mon Sep 17 00:00:00 2001 From: Demian Date: Mon, 29 Jun 2015 11:23:08 -0300 Subject: Added service to retrieve the positions for a group of devices from a particular user --- src/org/traccar/model/MiscFormatter.java | 6 ++++++ src/org/traccar/web/PositionServlet.java | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) (limited to 'src/org') diff --git a/src/org/traccar/model/MiscFormatter.java b/src/org/traccar/model/MiscFormatter.java index d1e1ae959..7a1e69082 100644 --- a/src/org/traccar/model/MiscFormatter.java +++ b/src/org/traccar/model/MiscFormatter.java @@ -15,6 +15,8 @@ */ package org.traccar.model; +import org.traccar.web.JsonConverter; + import java.text.DecimalFormat; import java.util.LinkedHashMap; import java.util.Map; @@ -73,6 +75,10 @@ public class MiscFormatter { json.add(entry.getKey(), (Double) entry.getValue()); } else if (entry.getValue() instanceof Boolean) { json.add(entry.getKey(), (Boolean) entry.getValue()); + } else if (entry.getValue() == null) { + json.add(entry.getKey(), JsonValue.NULL); + } else { + json.add(entry.getKey(), JsonConverter.objectToJson(entry.getValue())); } } diff --git a/src/org/traccar/web/PositionServlet.java b/src/org/traccar/web/PositionServlet.java index a29b5b274..78bcb22e7 100644 --- a/src/org/traccar/web/PositionServlet.java +++ b/src/org/traccar/web/PositionServlet.java @@ -18,6 +18,11 @@ package org.traccar.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.traccar.Context; +import org.traccar.model.MiscFormatter; +import org.traccar.model.Position; + +import java.util.HashMap; +import java.util.Map; public class PositionServlet extends BaseServlet { @@ -28,6 +33,9 @@ public class PositionServlet extends BaseServlet { case "/get": get(req, resp); break; + case "/devices": + devices(req, resp); + break; default: return false; } @@ -44,4 +52,19 @@ public class PositionServlet extends BaseServlet { JsonConverter.parseDate(req.getParameter("to"))))); } + private void devices(HttpServletRequest req, HttpServletResponse resp) throws Exception { + long userId = getUserId(req); + Map positions = new HashMap<>(); + + for(String deviceIdString : req.getParameterValues("devicesId")) { + Long deviceId = Long.parseLong(deviceIdString); + + Context.getPermissionsManager().checkDevice(userId, deviceId); + + Position position = Context.getConnectionManager().getLastPosition(deviceId); + positions.put(deviceId.toString(), position); + } + + sendResponse(resp.getWriter(), MiscFormatter.toJson(positions)); + } } -- cgit v1.2.3