diff options
author | Rafael Guterres <guterresrafael@gmail.com> | 2015-11-21 02:13:24 -0200 |
---|---|---|
committer | Rafael Guterres <guterresrafael@gmail.com> | 2015-11-21 02:13:24 -0200 |
commit | 77cd23da84ebebcf99a97a8aef88aa9a4884ca40 (patch) | |
tree | f95fa042b4755e925be8d0c8416fcb648ea23aa3 /src/org/traccar/web | |
parent | f5a266953e53a7f1785bcb584759582621ec9de3 (diff) | |
parent | 7623704e12a3e60420c86add5cfdc18b14061752 (diff) | |
download | trackermap-server-77cd23da84ebebcf99a97a8aef88aa9a4884ca40.tar.gz trackermap-server-77cd23da84ebebcf99a97a8aef88aa9a4884ca40.tar.bz2 trackermap-server-77cd23da84ebebcf99a97a8aef88aa9a4884ca40.zip |
Merge tananaev/master
Diffstat (limited to 'src/org/traccar/web')
-rw-r--r-- | src/org/traccar/web/AsyncServlet.java | 47 | ||||
-rw-r--r-- | src/org/traccar/web/JsonConverter.java | 2 |
2 files changed, 39 insertions, 10 deletions
diff --git a/src/org/traccar/web/AsyncServlet.java b/src/org/traccar/web/AsyncServlet.java index 63b08ff3e..c55fd6e75 100644 --- a/src/org/traccar/web/AsyncServlet.java +++ b/src/org/traccar/web/AsyncServlet.java @@ -36,6 +36,7 @@ import org.traccar.Context; import org.traccar.GlobalTimer; import org.traccar.database.ConnectionManager; import org.traccar.helper.Log; +import org.traccar.model.Device; import org.traccar.model.Position; public class AsyncServlet extends BaseServlet { @@ -50,7 +51,7 @@ public class AsyncServlet extends BaseServlet { public static class AsyncSession { - private static final boolean DEBUG_ASYNC = true; + public static final boolean DEBUG_ASYNC = false; private static final long SESSION_TIMEOUT = 30; private static final long REQUEST_TIMEOUT = 20; @@ -60,7 +61,8 @@ public class AsyncServlet extends BaseServlet { private final Set<Long> devices = new HashSet<>(); private Timeout sessionTimeout; private Timeout requestTimeout; - private final Map<Long, Position> positions = new HashMap<>(); + private final Set<Device> deviceUpdates = new HashSet<>(); + private final Set<Position> positionUpdates = new HashSet<>(); private AsyncContext activeContext; private void logEvent(String message) { @@ -76,7 +78,7 @@ public class AsyncServlet extends BaseServlet { Collection<Position> initialPositions = Context.getConnectionManager().getInitialState(devices); for (Position position : initialPositions) { - positions.put(position.getDeviceId(), position); + positionUpdates.add(position); } Context.getConnectionManager().addListener(devices, dataListener); @@ -86,17 +88,34 @@ public class AsyncServlet extends BaseServlet { return devices.contains(deviceId); } - private final ConnectionManager.DataCacheListener dataListener = new ConnectionManager.DataCacheListener() { + private final ConnectionManager.UpdateListener dataListener = new ConnectionManager.UpdateListener() { + @Override + public void onUpdateDevice(Device device) { + synchronized (AsyncSession.this) { + logEvent("onUpdateDevice deviceId: " + device.getId()); + if (!destroyed) { + if (requestTimeout != null) { + requestTimeout.cancel(); + requestTimeout = null; + } + deviceUpdates.add(device); + if (activeContext != null) { + response(); + } + } + } + } + @Override public void onUpdatePosition(Position position) { synchronized (AsyncSession.this) { - logEvent("onUpdate deviceId: " + position.getDeviceId()); + logEvent("onUpdatePosition deviceId: " + position.getDeviceId()); if (!destroyed) { if (requestTimeout != null) { requestTimeout.cancel(); requestTimeout = null; } - positions.put(position.getDeviceId(), position); + positionUpdates.add(position); if (activeContext != null) { response(); } @@ -142,7 +161,7 @@ public class AsyncServlet extends BaseServlet { sessionTimeout = null; } - if (!positions.isEmpty()) { + if (!deviceUpdates.isEmpty() || !positionUpdates.isEmpty()) { response(); } else { requestTimeout = GlobalTimer.getTimer().newTimeout( @@ -158,8 +177,18 @@ public class AsyncServlet extends BaseServlet { JsonObjectBuilder result = Json.createObjectBuilder(); result.add("success", true); - result.add("data", JsonConverter.arrayToJson(positions.values())); - positions.clear(); + + if (Context.getConfig().getBoolean("web.oldAsyncFormat")) { + result.add("data", JsonConverter.arrayToJson(positionUpdates)); + } else { + JsonObjectBuilder data = Json.createObjectBuilder(); + data.add("devices", JsonConverter.arrayToJson(deviceUpdates)); + data.add("positions", JsonConverter.arrayToJson(positionUpdates)); + result.add("data", data.build()); + } + + deviceUpdates.clear(); + positionUpdates.clear(); try { response.getWriter().println(result.build().toString()); diff --git a/src/org/traccar/web/JsonConverter.java b/src/org/traccar/web/JsonConverter.java index d39e6b488..a8b68613b 100644 --- a/src/org/traccar/web/JsonConverter.java +++ b/src/org/traccar/web/JsonConverter.java @@ -64,7 +64,7 @@ public final class JsonConverter { final String name = Introspector.decapitalize(method.getName().substring(3)); Class<?> parameterType = method.getParameterTypes()[0]; - if (json.containsKey(name)) { + if (json.containsKey(name) && !json.isNull(name)) { try { if (parameterType.equals(boolean.class)) { method.invoke(object, json.getBoolean(name)); |