From 41fe4ca770875842f4d17531506c4bc74dc90501 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 10 Jun 2016 16:02:06 +0500 Subject: Geofences --- src/org/traccar/BasePipelineFactory.java | 10 ++ src/org/traccar/Context.java | 10 ++ .../api/resource/DeviceGeofenceResource.java | 61 +++++++ .../api/resource/DevicePermissionResource.java | 2 + src/org/traccar/api/resource/DeviceResource.java | 2 + .../api/resource/GeofencePermissionResource.java | 56 ++++++ src/org/traccar/api/resource/GeofenceResource.java | 85 +++++++++ .../api/resource/GroupGeofenceResource.java | 56 ++++++ .../api/resource/GroupPermissionResource.java | 2 + src/org/traccar/api/resource/GroupResource.java | 2 + src/org/traccar/api/resource/UserResource.java | 3 + src/org/traccar/database/ConnectionManager.java | 3 +- src/org/traccar/database/DataManager.java | 92 ++++++++++ src/org/traccar/database/GeofenceManager.java | 191 +++++++++++++++++++++ src/org/traccar/database/PermissionsManager.java | 7 + src/org/traccar/events/GeofenceEventHandler.java | 74 ++++++++ src/org/traccar/events/MotionEventHandler.java | 3 + src/org/traccar/geofence/GeofenceCircle.java | 82 +++++++++ src/org/traccar/geofence/GeofenceGeometry.java | 13 ++ src/org/traccar/geofence/GeofencePolygon.java | 160 +++++++++++++++++ src/org/traccar/model/Device.java | 9 + src/org/traccar/model/Event.java | 20 +-- src/org/traccar/model/Extensible.java | 56 ++++++ src/org/traccar/model/Geofence.java | 67 ++++++++ src/org/traccar/model/GeofencePermission.java | 25 +++ src/org/traccar/model/GroupGeofence.java | 25 +++ src/org/traccar/model/Message.java | 43 +---- src/org/traccar/model/Position.java | 10 -- src/org/traccar/model/UserDeviceGeofence.java | 35 ++++ src/org/traccar/web/WebServer.java | 7 +- 30 files changed, 1147 insertions(+), 64 deletions(-) create mode 100644 src/org/traccar/api/resource/DeviceGeofenceResource.java create mode 100644 src/org/traccar/api/resource/GeofencePermissionResource.java create mode 100644 src/org/traccar/api/resource/GeofenceResource.java create mode 100644 src/org/traccar/api/resource/GroupGeofenceResource.java create mode 100644 src/org/traccar/database/GeofenceManager.java create mode 100644 src/org/traccar/events/GeofenceEventHandler.java create mode 100644 src/org/traccar/geofence/GeofenceCircle.java create mode 100644 src/org/traccar/geofence/GeofenceGeometry.java create mode 100644 src/org/traccar/geofence/GeofencePolygon.java create mode 100644 src/org/traccar/model/Extensible.java create mode 100644 src/org/traccar/model/Geofence.java create mode 100644 src/org/traccar/model/GeofencePermission.java create mode 100644 src/org/traccar/model/GroupGeofence.java create mode 100644 src/org/traccar/model/UserDeviceGeofence.java (limited to 'src/org/traccar') diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 634c6d6a4..b61d95171 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -30,6 +30,7 @@ import org.jboss.netty.channel.SimpleChannelHandler; import org.jboss.netty.handler.logging.LoggingHandler; import org.jboss.netty.handler.timeout.IdleStateHandler; import org.traccar.events.CommandResultEventHandler; +import org.traccar.events.GeofenceEventHandler; import org.traccar.events.MotionEventHandler; import org.traccar.events.OverspeedEventHandler; import org.traccar.helper.Log; @@ -50,6 +51,7 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { private CommandResultEventHandler commandResultEventHandler; private OverspeedEventHandler overspeedEventHandler; private MotionEventHandler motionEventHandler; + private GeofenceEventHandler geofenceEventHandler; private static final class OpenChannelHandler extends SimpleChannelHandler { @@ -140,6 +142,10 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { motionEventHandler = new MotionEventHandler(); } + if (Context.getConfig().getBoolean("event.geofenceHandler")) { + geofenceEventHandler = new GeofenceEventHandler(); + } + } protected abstract void addSpecificHandlers(ChannelPipeline pipeline); @@ -197,6 +203,10 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { pipeline.addLast("MotionEventHandler", motionEventHandler); } + if (geofenceEventHandler != null) { + pipeline.addLast("GeofenceEventHandler", geofenceEventHandler); + } + pipeline.addLast("mainHandler", new MainEventHandler()); return pipeline; } diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 1aba24b8c..b78d11e7b 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -20,6 +20,7 @@ import org.traccar.database.ConnectionManager; import org.traccar.database.DataManager; import org.traccar.database.IdentityManager; import org.traccar.database.PermissionsManager; +import org.traccar.database.GeofenceManager; import org.traccar.geocode.BingMapsReverseGeocoder; import org.traccar.geocode.FactualReverseGeocoder; import org.traccar.geocode.GisgraphyReverseGeocoder; @@ -99,6 +100,12 @@ public final class Context { return serverManager; } + private static GeofenceManager geofenceManager; + + public static GeofenceManager getGeofenceManager() { + return geofenceManager; + } + private static final AsyncHttpClient ASYNC_HTTP_CLIENT = new AsyncHttpClient(); public static AsyncHttpClient getAsyncHttpClient() { @@ -177,9 +184,12 @@ public final class Context { permissionsManager = new PermissionsManager(dataManager); + geofenceManager = new GeofenceManager(dataManager); + connectionManager = new ConnectionManager(dataManager); serverManager = new ServerManager(); + } public static void init(IdentityManager testIdentityManager) { diff --git a/src/org/traccar/api/resource/DeviceGeofenceResource.java b/src/org/traccar/api/resource/DeviceGeofenceResource.java new file mode 100644 index 000000000..dc7014d79 --- /dev/null +++ b/src/org/traccar/api/resource/DeviceGeofenceResource.java @@ -0,0 +1,61 @@ +/* + * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.api.resource; + +import org.traccar.Context; +import org.traccar.api.BaseResource; +import org.traccar.model.UserDeviceGeofence; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import java.sql.SQLException; + +@Path("devices/geofences") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class DeviceGeofenceResource extends BaseResource { + + @POST + public Response add(UserDeviceGeofence entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); + Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); + Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); + Context.getDataManager().linkUserDeviceGeofence(entity.getUserId(), + entity.getDeviceId(), entity.getGeofenceId()); + Context.getGeofenceManager().refresh(); + return Response.ok(entity).build(); + } + + @DELETE + public Response remove(UserDeviceGeofence entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); + Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); + Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); + Context.getDataManager().unlinkUserDeviceGeofence(entity.getUserId(), entity.getDeviceId(), + entity.getGeofenceId()); + Context.getGeofenceManager().refresh(); + return Response.noContent().build(); + } + +} diff --git a/src/org/traccar/api/resource/DevicePermissionResource.java b/src/org/traccar/api/resource/DevicePermissionResource.java index f77375cba..2ef436f11 100644 --- a/src/org/traccar/api/resource/DevicePermissionResource.java +++ b/src/org/traccar/api/resource/DevicePermissionResource.java @@ -38,6 +38,7 @@ public class DevicePermissionResource extends BaseResource { Context.getPermissionsManager().checkAdmin(getUserId()); Context.getDataManager().linkDevice(entity.getUserId(), entity.getDeviceId()); Context.getPermissionsManager().refresh(); + Context.getGeofenceManager().refresh(); return Response.ok(entity).build(); } @@ -46,6 +47,7 @@ public class DevicePermissionResource extends BaseResource { Context.getPermissionsManager().checkAdmin(getUserId()); Context.getDataManager().unlinkDevice(entity.getUserId(), entity.getDeviceId()); Context.getPermissionsManager().refresh(); + Context.getGeofenceManager().refresh(); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index 6c0ef32ca..26880c1f8 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -60,6 +60,7 @@ public class DeviceResource extends BaseResource { Context.getDataManager().addDevice(entity); Context.getDataManager().linkDevice(getUserId(), entity.getId()); Context.getPermissionsManager().refresh(); + Context.getGeofenceManager().refresh(); return Response.ok(entity).build(); } @@ -79,6 +80,7 @@ public class DeviceResource extends BaseResource { Context.getPermissionsManager().checkDevice(getUserId(), id); Context.getDataManager().removeDevice(id); Context.getPermissionsManager().refresh(); + Context.getGeofenceManager().refresh(); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/GeofencePermissionResource.java b/src/org/traccar/api/resource/GeofencePermissionResource.java new file mode 100644 index 000000000..329c72b07 --- /dev/null +++ b/src/org/traccar/api/resource/GeofencePermissionResource.java @@ -0,0 +1,56 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.api.resource; + +import org.traccar.Context; +import org.traccar.api.BaseResource; +import org.traccar.model.GeofencePermission; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.sql.SQLException; + +@Path("permissions/geofences") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class GeofencePermissionResource extends BaseResource { + + @POST + public Response add(GeofencePermission entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); + Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); + Context.getDataManager().linkGeofence(entity.getUserId(), entity.getGeofenceId()); + Context.getGeofenceManager().refresh(); + return Response.ok(entity).build(); + } + + @DELETE + public Response remove(GeofencePermission entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); + Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); + Context.getDataManager().unlinkGeofence(entity.getUserId(), entity.getGeofenceId()); + Context.getGeofenceManager().refresh(); + return Response.noContent().build(); + } + +} diff --git a/src/org/traccar/api/resource/GeofenceResource.java b/src/org/traccar/api/resource/GeofenceResource.java new file mode 100644 index 000000000..b56e20e08 --- /dev/null +++ b/src/org/traccar/api/resource/GeofenceResource.java @@ -0,0 +1,85 @@ +/* + * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.api.resource; + +import org.traccar.Context; +import org.traccar.api.BaseResource; +import org.traccar.model.Geofence; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import java.sql.SQLException; +import java.util.Collection; + +@Path("geofences") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class GeofenceResource extends BaseResource { + + @GET + public Collection get( + @QueryParam("all") boolean all, @QueryParam("userId") long userId) throws SQLException { + if (all) { + Context.getPermissionsManager().checkAdmin(getUserId()); + return Context.getGeofenceManager().getAllGeofences(); + } else { + if (userId == 0) { + userId = getUserId(); + } + Context.getPermissionsManager().checkUser(getUserId(), userId); + return Context.getGeofenceManager().getUserGeofences(userId); + } + } + + @POST + public Response add(Geofence entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + Context.getDataManager().addGeofence(entity); + Context.getDataManager().linkGeofence(getUserId(), entity.getId()); + Context.getGeofenceManager().refresh(); + return Response.ok(entity).build(); + } + + @Path("{id}") + @PUT + public Response update(@PathParam("id") long id, Geofence entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + Context.getPermissionsManager().checkGeofence(getUserId(), id); + Context.getGeofenceManager().updateGeofence(entity); + return Response.ok(entity).build(); + } + + @Path("{id}") + @DELETE + public Response remove(@PathParam("id") long id) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + Context.getPermissionsManager().checkGeofence(getUserId(), id); + Context.getDataManager().removeGeofence(id); + Context.getGeofenceManager().refresh(); + return Response.noContent().build(); + } + +} diff --git a/src/org/traccar/api/resource/GroupGeofenceResource.java b/src/org/traccar/api/resource/GroupGeofenceResource.java new file mode 100644 index 000000000..1ef495a86 --- /dev/null +++ b/src/org/traccar/api/resource/GroupGeofenceResource.java @@ -0,0 +1,56 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.api.resource; + +import org.traccar.Context; +import org.traccar.api.BaseResource; +import org.traccar.model.GroupGeofence; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.sql.SQLException; + +@Path("groups/geofences") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class GroupGeofenceResource extends BaseResource { + + @POST + public Response add(GroupGeofence entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); + Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); + Context.getDataManager().linkGroupGeofence(entity.getGroupId(), entity.getGeofenceId()); + Context.getGeofenceManager().refresh(); + return Response.ok(entity).build(); + } + + @DELETE + public Response remove(GroupGeofence entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); + Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); + Context.getDataManager().unlinkGroupGeofence(entity.getGroupId(), entity.getGeofenceId()); + Context.getGeofenceManager().refresh(); + return Response.noContent().build(); + } + +} diff --git a/src/org/traccar/api/resource/GroupPermissionResource.java b/src/org/traccar/api/resource/GroupPermissionResource.java index b8ec4ae3c..564a379d2 100644 --- a/src/org/traccar/api/resource/GroupPermissionResource.java +++ b/src/org/traccar/api/resource/GroupPermissionResource.java @@ -38,6 +38,7 @@ public class GroupPermissionResource extends BaseResource { Context.getPermissionsManager().checkAdmin(getUserId()); Context.getDataManager().linkGroup(entity.getUserId(), entity.getGroupId()); Context.getPermissionsManager().refresh(); + Context.getGeofenceManager().refresh(); return Response.ok(entity).build(); } @@ -46,6 +47,7 @@ public class GroupPermissionResource extends BaseResource { Context.getPermissionsManager().checkAdmin(getUserId()); Context.getDataManager().unlinkGroup(entity.getUserId(), entity.getGroupId()); Context.getPermissionsManager().refresh(); + Context.getGeofenceManager().refresh(); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/GroupResource.java b/src/org/traccar/api/resource/GroupResource.java index e22796645..dda9ab03b 100644 --- a/src/org/traccar/api/resource/GroupResource.java +++ b/src/org/traccar/api/resource/GroupResource.java @@ -59,6 +59,7 @@ public class GroupResource extends BaseResource { Context.getDataManager().addGroup(entity); Context.getDataManager().linkGroup(getUserId(), entity.getId()); Context.getPermissionsManager().refresh(); + Context.getGeofenceManager().refresh(); return Response.ok(entity).build(); } @@ -78,6 +79,7 @@ public class GroupResource extends BaseResource { Context.getPermissionsManager().checkGroup(getUserId(), id); Context.getDataManager().removeGroup(id); Context.getPermissionsManager().refresh(); + Context.getGeofenceManager().refresh(); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index 0b307ab88..7e503dcfb 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -52,6 +52,7 @@ public class UserResource extends BaseResource { } Context.getDataManager().addUser(entity); Context.getPermissionsManager().refresh(); + Context.getGeofenceManager().refresh(); return Response.ok(entity).build(); } @@ -65,6 +66,7 @@ public class UserResource extends BaseResource { } Context.getDataManager().updateUser(entity); Context.getPermissionsManager().refresh(); + Context.getGeofenceManager().refresh(); return Response.ok(entity).build(); } @@ -74,6 +76,7 @@ public class UserResource extends BaseResource { Context.getPermissionsManager().checkUser(getUserId(), id); Context.getDataManager().removeUser(id); Context.getPermissionsManager().refresh(); + Context.getGeofenceManager().refresh(); return Response.noContent().build(); } diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index ec5903548..6e47dfad3 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -155,7 +155,8 @@ public class ConnectionManager { Log.warning(error); } for (long userId : Context.getPermissionsManager().getDeviceUsers(deviceId)) { - if (listeners.containsKey(userId)) { + if (listeners.containsKey(userId) && (event.getGeofenceId() == 0 + || Context.getGeofenceManager().checkGeofence(userId, event.getGeofenceId()))) { for (UpdateListener listener : listeners.get(userId)) { listener.onUpdateEvent(event, position); } diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 67b7d1e55..86c930c0b 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -48,11 +48,15 @@ import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.DevicePermission; import org.traccar.model.Event; +import org.traccar.model.Geofence; import org.traccar.model.Group; +import org.traccar.model.GroupGeofence; import org.traccar.model.GroupPermission; import org.traccar.model.Position; import org.traccar.model.Server; import org.traccar.model.User; +import org.traccar.model.UserDeviceGeofence; +import org.traccar.model.GeofencePermission; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; @@ -368,6 +372,7 @@ public class DataManager implements IdentityManager { Device cachedDevice = getDeviceById(device.getId()); cachedDevice.setStatus(device.getStatus()); cachedDevice.setMotion(device.getMotion()); + cachedDevice.setGeofenceId(device.getGeofenceId()); } public void removeDevice(long deviceId) throws SQLException { @@ -524,4 +529,91 @@ public class DataManager implements IdentityManager { return getEvents(deviceId, type, new Date(), to); } + public Collection getGeofences() throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectGeofencesAll")) + .executeQuery(Geofence.class); + } + + public Geofence getGeofence(long geofenceId) throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectGeofences")) + .setLong("id", geofenceId) + .executeQuerySingle(Geofence.class); + } + + public void addGeofence(Geofence geofence) throws SQLException { + geofence.setId(QueryBuilder.create(dataSource, getQuery("database.insertGeofence"), true) + .setObject(geofence) + .executeUpdate()); + } + + public void updateGeofence(Geofence geofence) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.updateGeofence")) + .setObject(geofence) + .executeUpdate(); + } + + public void removeGeofence(long geofenceId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.deleteGeofence")) + .setLong("id", geofenceId) + .executeUpdate(); + } + + public Collection getGeofencePermissions() throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectGeofencePermissions")) + .executeQuery(GeofencePermission.class); + } + + public void linkGeofence(long userId, long geofenceId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.linkGeofence")) + .setLong("userId", userId) + .setLong("geofenceId", geofenceId) + .executeUpdate(); + } + + public void unlinkGeofence(long userId, long geofenceId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.unlinkGeofence")) + .setLong("userId", userId) + .setLong("geofenceId", geofenceId) + .executeUpdate(); + } + + public Collection getGroupGeofences() throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectGroupGeofences")) + .executeQuery(GroupGeofence.class); + } + + public void linkGroupGeofence(long groupId, long geofenceId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.linkGroupGeofence")) + .setLong("groupId", groupId) + .setLong("geofenceId", geofenceId) + .executeUpdate(); + } + + public void unlinkGroupGeofence(long groupId, long geofenceId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.unlinkGroupGeofence")) + .setLong("groupId", groupId) + .setLong("geofenceId", geofenceId) + .executeUpdate(); + } + + public Collection getUserDeviceGeofences() throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectUserDeviceGeofences")) + .executeQuery(UserDeviceGeofence.class); + } + + public void linkUserDeviceGeofence(long userId, long deviceId, long geofenceId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.linkUserDeviceGeofence")) + .setLong("userId", userId) + .setLong("deviceId", deviceId) + .setLong("geofenceId", geofenceId) + .executeUpdate(); + } + + public void unlinkUserDeviceGeofence(long userId, long deviceId, long geofenceId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.unlinkUserDeviceGeofence")) + .setLong("userId", userId) + .setLong("deviceId", deviceId) + .setLong("geofenceId", geofenceId) + .executeUpdate(); + } } diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java new file mode 100644 index 000000000..64afc876c --- /dev/null +++ b/src/org/traccar/database/GeofenceManager.java @@ -0,0 +1,191 @@ +package org.traccar.database; + +import java.sql.SQLException; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import org.traccar.helper.Log; +import org.traccar.model.Device; +import org.traccar.model.Geofence; +import org.traccar.model.GroupGeofence; +import org.traccar.model.UserDeviceGeofence; +import org.traccar.model.GeofencePermission; + +public class GeofenceManager { + + private final DataManager dataManager; + + private final Map geofences = new HashMap<>(); + private final Map> userGeofences = new HashMap<>(); + private final Map> groupGeofences = new HashMap<>(); + + private final Map> deviceGeofences = new HashMap<>(); + private final Map>> userDeviceGeofences = new HashMap<>(); + + private final ReadWriteLock deviceGeofencesLock = new ReentrantReadWriteLock(); + private final ReadWriteLock geofencesLock = new ReentrantReadWriteLock(); + private final ReadWriteLock groupGeofencesLock = new ReentrantReadWriteLock(); + + public GeofenceManager(DataManager dataManager) { + this.dataManager = dataManager; + refresh(); + } + + public Set getUserGeofencesIds(long userId) { + if (!userGeofences.containsKey(userId)) { + userGeofences.put(userId, new HashSet()); + } + return userGeofences.get(userId); + } + + private Set getGroupGeofences(long groupId) { + if (!groupGeofences.containsKey(groupId)) { + groupGeofences.put(groupId, new HashSet()); + } + return groupGeofences.get(groupId); + } + + public Set getAllDeviceGeofences(long deviceId) { + deviceGeofencesLock.readLock().lock(); + try { + return getDeviceGeofences(deviceGeofences, deviceId); + } finally { + deviceGeofencesLock.readLock().unlock(); + } + + } + + public Set getUserDeviceGeofences(long userId, long deviceId) { + deviceGeofencesLock.readLock().lock(); + try { + return getUserDeviceGeofencesUnlocked(userId, deviceId); + } finally { + deviceGeofencesLock.readLock().unlock(); + } + } + + private Set getDeviceGeofences(Map> deviceGeofences, long deviceId) { + if (!deviceGeofences.containsKey(deviceId)) { + deviceGeofences.put(deviceId, new HashSet()); + } + return deviceGeofences.get(deviceId); + } + + private Set getUserDeviceGeofencesUnlocked(long userId, long deviceId) { + if (!userDeviceGeofences.containsKey(userId)) { + userDeviceGeofences.put(userId, new HashMap>()); + } + return getDeviceGeofences(userDeviceGeofences.get(userId), deviceId); + } + + public final void refresh() { + if (dataManager != null) { + try { + geofencesLock.writeLock().lock(); + groupGeofencesLock.writeLock().lock(); + deviceGeofencesLock.writeLock().lock(); + try { + geofences.clear(); + for (Geofence geofence : dataManager.getGeofences()) { + geofences.put(geofence.getId(), geofence); + } + + userGeofences.clear(); + for (GeofencePermission geofencePermission : dataManager.getGeofencePermissions()) { + getUserGeofencesIds(geofencePermission.getUserId()).add(geofencePermission.getGeofenceId()); + } + + groupGeofences.clear(); + for (GroupGeofence groupGeofence : dataManager.getGroupGeofences()) { + getGroupGeofences(groupGeofence.getGroupId()).add(groupGeofence.getGeofenceId()); + } + + deviceGeofences.clear(); + + for (Map.Entry>> deviceGeofence : userDeviceGeofences.entrySet()) { + deviceGeofence.getValue().clear(); + } + userDeviceGeofences.clear(); + + for (UserDeviceGeofence userDeviceGeofence : dataManager.getUserDeviceGeofences()) { + getDeviceGeofences(deviceGeofences, userDeviceGeofence.getDeviceId()) + .add(userDeviceGeofence.getGeofenceId()); + getUserDeviceGeofencesUnlocked(userDeviceGeofence.getUserId(), userDeviceGeofence.getDeviceId()) + .add(userDeviceGeofence.getGeofenceId()); + } + for (Device device : dataManager.getAllDevices()) { + long groupId = device.getGroupId(); + while (groupId != 0) { + getDeviceGeofences(deviceGeofences, device.getId()).addAll(getGroupGeofences(groupId)); + groupId = dataManager.getGroupById(groupId).getGroupId(); + } + } + + } finally { + geofencesLock.writeLock().unlock(); + groupGeofencesLock.writeLock().unlock(); + deviceGeofencesLock.writeLock().unlock(); + } + + } catch (SQLException error) { + Log.warning(error); + } + } + } + + public final Collection getAllGeofences() { + geofencesLock.readLock().lock(); + try { + return geofences.values(); + } finally { + geofencesLock.readLock().unlock(); + } + } + + public final Collection getUserGeofences(long userId) { + geofencesLock.readLock().lock(); + try { + Collection result = new LinkedList<>(); + for (Long geofenceId : getUserGeofencesIds(userId)) { + result.add(getGeofence(geofenceId)); + } + return result; + } finally { + geofencesLock.readLock().unlock(); + } + } + + public final Geofence getGeofence(Long geofenceId) { + geofencesLock.readLock().lock(); + try { + return geofences.get(geofenceId); + } finally { + geofencesLock.readLock().unlock(); + } + } + + public final void updateGeofence(Geofence geofence) { + geofencesLock.writeLock().lock(); + try { + geofences.put(geofence.getId(), geofence); + } finally { + geofencesLock.writeLock().unlock(); + } + try { + dataManager.updateGeofence(geofence); + } catch (SQLException error) { + Log.warning(error); + } + } + + public boolean checkGeofence(long userId, long geofenceId) { + return getUserGeofencesIds(userId).contains(geofenceId); + } + +} diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 08d44b382..96a6488ef 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -15,6 +15,7 @@ */ package org.traccar.database; +import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.DevicePermission; @@ -145,4 +146,10 @@ public class PermissionsManager { } } + public void checkGeofence(long userId, long geofenceId) throws SecurityException { + if (!Context.getGeofenceManager().checkGeofence(userId, geofenceId) && !isAdmin(userId)) { + throw new SecurityException("Geofence access denied"); + } + } + } diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java new file mode 100644 index 000000000..bf9060ca1 --- /dev/null +++ b/src/org/traccar/events/GeofenceEventHandler.java @@ -0,0 +1,74 @@ +package org.traccar.events; + +import java.sql.SQLException; +import java.util.Set; + +import org.traccar.BaseEventHandler; +import org.traccar.Context; +import org.traccar.database.DataManager; +import org.traccar.database.GeofenceManager; +import org.traccar.helper.Log; +import org.traccar.model.Device; +import org.traccar.model.Event; +import org.traccar.model.Position; + +public class GeofenceEventHandler extends BaseEventHandler { + + private int suppressRepeated; + private GeofenceManager geofenceManager; + private DataManager dataManager; + + public GeofenceEventHandler() { + suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60); + geofenceManager = Context.getGeofenceManager(); + dataManager = Context.getDataManager(); + } + + @Override + protected Event analizePosition(Position position) { + Event event = null; + if (!isLastPosition() || !position.getValid()) { + return event; + } + + Device device = dataManager.getDeviceById(position.getDeviceId()); + if (device == null) { + return event; + } + + Set geofences = geofenceManager.getAllDeviceGeofences(position.getDeviceId()); + if (geofences == null) { + return event; + } + long geofenceId = 0; + for (Long geofence : geofences) { + if (geofenceManager.getGeofence(geofence).getGeometry() + .containsPoint(position.getLatitude(), position.getLongitude())) { + geofenceId = geofence; + break; + } + } + + if (device.getGeofenceId() != geofenceId) { + try { + if (geofenceId == 0) { + event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); + event.setGeofenceId(device.getGeofenceId()); + } else { + event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); + event.setGeofenceId(geofenceId); + } + if (event != null && !dataManager.getLastEvents( + position.getDeviceId(), event.getType(), suppressRepeated).isEmpty()) { + event = null; + } + device.setGeofenceId(geofenceId); + dataManager.updateDeviceStatus(device); + } catch (SQLException error) { + Log.warning(error); + } + + } + return event; + } +} diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index a3b81ddc4..306fa8a4e 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -33,6 +33,9 @@ public class MotionEventHandler extends BaseEventHandler { return event; } String motion = device.getMotion(); + if (motion == null) { + motion = Device.STATUS_STOPPED; + } if (valid && speed > SPEED_THRESHOLD && !motion.equals(Device.STATUS_MOVING)) { Context.getConnectionManager().updateDevice(position.getDeviceId(), Device.STATUS_MOVING, null); event = new Event(Event.TYPE_DEVICE_MOVING, position.getDeviceId(), position.getId()); diff --git a/src/org/traccar/geofence/GeofenceCircle.java b/src/org/traccar/geofence/GeofenceCircle.java new file mode 100644 index 000000000..76d5a2816 --- /dev/null +++ b/src/org/traccar/geofence/GeofenceCircle.java @@ -0,0 +1,82 @@ +package org.traccar.geofence; + +import java.text.DecimalFormat; +import java.text.ParseException; + +import org.traccar.helper.DistanceCalculator; + +public class GeofenceCircle extends GeofenceGeometry { + + private double centerLatitude; + private double centerLongitude; + private double radius; + + public GeofenceCircle() { + super(); + } + + public GeofenceCircle(String wkt) throws ParseException { + super(); + fromWKT(wkt); + } + + public GeofenceCircle(double latitude, double longitude, double radius) { + super(); + this.centerLatitude = latitude; + this.centerLongitude = longitude; + this.radius = radius; + } + + @Override + public boolean containsPoint(double latitude, double longitude) { + return DistanceCalculator.distance(centerLatitude, centerLongitude, latitude, longitude) <= radius; + } + + @Override + public String toWKT() { + String wkt = ""; + wkt = "CIRCLE ("; + wkt += String.valueOf(centerLatitude); + wkt += " "; + wkt += String.valueOf(centerLongitude); + wkt += ", "; + DecimalFormat format = new DecimalFormat("0.#"); + wkt += format.format(radius); + wkt += ")"; + return wkt; + } + + @Override + public void fromWKT(String wkt) throws ParseException { + if (!wkt.startsWith("CIRCLE")) { + throw new ParseException("Mismatch geometry type", 0); + } + String content = wkt.substring(wkt.indexOf("(") + 1, wkt.indexOf(")")); + if (content == null || content.equals("")) { + throw new ParseException("No content", 0); + } + String[] commatokens = content.split(","); + if (commatokens.length != 2) { + throw new ParseException("Not valid content", 0); + } + String[] tokens = commatokens[0].split("\\s"); + if (tokens.length != 2) { + throw new ParseException("Too much or less coordinates", 0); + } + try { + centerLatitude = Double.parseDouble(tokens[0]); + } catch (NumberFormatException e) { + throw new ParseException(tokens[0] + " is not a double", 0); + } + try { + centerLongitude = Double.parseDouble(tokens[1]); + } catch (NumberFormatException e) { + throw new ParseException(tokens[1] + " is not a double", 0); + } + try { + radius = Double.parseDouble(commatokens[1]); + } catch (NumberFormatException e) { + throw new ParseException(commatokens[1] + " is not a double", 0); + } + } +} diff --git a/src/org/traccar/geofence/GeofenceGeometry.java b/src/org/traccar/geofence/GeofenceGeometry.java new file mode 100644 index 000000000..c8f042413 --- /dev/null +++ b/src/org/traccar/geofence/GeofenceGeometry.java @@ -0,0 +1,13 @@ +package org.traccar.geofence; + +import java.text.ParseException; + +public abstract class GeofenceGeometry { + + public abstract boolean containsPoint(double latitude, double longitude); + + public abstract String toWKT(); + + public abstract void fromWKT(String wkt) throws ParseException; + +} diff --git a/src/org/traccar/geofence/GeofencePolygon.java b/src/org/traccar/geofence/GeofencePolygon.java new file mode 100644 index 000000000..08178375a --- /dev/null +++ b/src/org/traccar/geofence/GeofencePolygon.java @@ -0,0 +1,160 @@ +package org.traccar.geofence; + +import java.text.ParseException; +import java.util.ArrayList; + +public class GeofencePolygon extends GeofenceGeometry { + + public GeofencePolygon() { + super(); + } + + public GeofencePolygon(String wkt) throws ParseException { + super(); + fromWKT(wkt); + } + + private static class Coordinate { + + public static final double DEGREE360 = 360; + + private double lat; + private double lon; + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } + + public double getLon() { + return lon; + } + + // Need not to confuse algorithm by the abrupt reset of longitude + public double getLon360() { + return lon + DEGREE360; + } + + public void setLon(double lon) { + this.lon = lon; + } + } + + private ArrayList coordinates; + + private double[] constant; + private double[] multiple; + + private void precalc() { + if (coordinates == null) { + return; + } + int polyCorners = coordinates.size(); + int i; + int j = polyCorners - 1; + + if (constant != null) { + constant = null; + } + if (multiple != null) { + multiple = null; + } + + constant = new double[polyCorners]; + multiple = new double[polyCorners]; + + + for (i = 0; i < polyCorners; j = i++) { + if (coordinates.get(j).getLon360() == coordinates.get(i).getLon360()) { + constant[i] = coordinates.get(i).getLat(); + multiple[i] = 0; + } else { + constant[i] = coordinates.get(i).getLat() + - (coordinates.get(i).getLon360() * coordinates.get(j).getLat()) + / (coordinates.get(j).getLon360() - coordinates.get(i).getLon360()) + + (coordinates.get(i).getLon360() * coordinates.get(i).getLat()) + / (coordinates.get(j).getLon360() - coordinates.get(i).getLon360()); + multiple[i] = (coordinates.get(j).getLat() - coordinates.get(i).getLat()) + / (coordinates.get(j).getLon360() - coordinates.get(i).getLon360()); + } + } + } + + @Override + public boolean containsPoint(double latitude, double longitude) { + + int polyCorners = coordinates.size(); + int i; + int j = polyCorners - 1; + double longitude360 = longitude + Coordinate.DEGREE360; + boolean oddNodes = false; + + for (i = 0; i < polyCorners; j = i++) { + if (coordinates.get(i).getLon360() < longitude360 + && coordinates.get(j).getLon360() >= longitude360 + || coordinates.get(j).getLon360() < longitude360 + && coordinates.get(i).getLon360() >= longitude360) { + oddNodes ^= longitude360 * multiple[i] + constant[i] < latitude; + } + } + return oddNodes; + } + + @Override + public String toWKT() { + StringBuffer buf = new StringBuffer(); + buf.append("POLYGON ("); + for (Coordinate coordinate : coordinates) { + buf.append(String.valueOf(coordinate.getLat())); + buf.append(" "); + buf.append(String.valueOf(coordinate.getLon())); + buf.append(", "); + } + return buf.substring(0, buf.length() - 2) + ")"; + } + + @Override + public void fromWKT(String wkt) throws ParseException { + if (coordinates == null) { + coordinates = new ArrayList(); + } else { + coordinates.clear(); + } + + if (!wkt.startsWith("POLYGON")) { + throw new ParseException("Mismatch geometry type", 0); + } + String content = wkt.substring(wkt.indexOf("(") + 1, wkt.indexOf(")")); + if (content == null || content.equals("")) { + throw new ParseException("No content", 0); + } + String[] commatokens = content.split(","); + if (commatokens.length < 3) { + throw new ParseException("Not valid content", 0); + } + + for (String commatoken : commatokens) { + String[] tokens = commatoken.trim().split("\\s"); + if (tokens.length != 2) { + throw new ParseException("Here must be two coordinates: " + commatoken, 0); + } + Coordinate coordinate = new Coordinate(); + try { + coordinate.setLat(Double.parseDouble(tokens[0])); + } catch (NumberFormatException e) { + throw new ParseException(tokens[0] + " is not a double", 0); + } + try { + coordinate.setLon(Double.parseDouble(tokens[1])); + } catch (NumberFormatException e) { + throw new ParseException(tokens[1] + " is not a double", 0); + } + coordinates.add(coordinate); + } + precalc(); + } + +} diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java index d32f9f851..45c3d46dc 100644 --- a/src/org/traccar/model/Device.java +++ b/src/org/traccar/model/Device.java @@ -114,4 +114,13 @@ public class Device { this.motion = motion; } + private long geofenceId; + + public long getGeofenceId() { + return geofenceId; + } + + public void setGeofenceId(long geofenceId) { + this.geofenceId = geofenceId; + } } diff --git a/src/org/traccar/model/Event.java b/src/org/traccar/model/Event.java index 6de885c70..863acd621 100644 --- a/src/org/traccar/model/Event.java +++ b/src/org/traccar/model/Event.java @@ -20,16 +20,6 @@ public class Event extends Message { public Event() { } - private long id; - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } - public static final String TYPE_COMMAND_RESULT = "commandResult"; public static final String TYPE_DEVICE_ONLINE = "deviceOnline"; @@ -71,4 +61,14 @@ public class Event extends Message { this.positionId = positionId; } + private long geofenceId = 0; + + public long getGeofenceId() { + return geofenceId; + } + + public void setGeofenceId(long geofenceId) { + this.geofenceId = geofenceId; + } + } diff --git a/src/org/traccar/model/Extensible.java b/src/org/traccar/model/Extensible.java new file mode 100644 index 000000000..b4052dbda --- /dev/null +++ b/src/org/traccar/model/Extensible.java @@ -0,0 +1,56 @@ +package org.traccar.model; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class Extensible { + + private long id; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + private Map attributes = new LinkedHashMap<>(); + + public Map getAttributes() { + return attributes; + } + + public void setAttributes(Map attributes) { + this.attributes = attributes; + } + + public void set(String key, boolean value) { + attributes.put(key, value); + } + + public void set(String key, int value) { + attributes.put(key, value); + } + + public void set(String key, long value) { + attributes.put(key, value); + } + + public void set(String key, double value) { + attributes.put(key, value); + } + + public void set(String key, String value) { + if (value != null && !value.isEmpty()) { + attributes.put(key, value); + } + } + + public void add(Map.Entry entry) { + if (entry != null && entry.getValue() != null) { + attributes.put(entry.getKey(), entry.getValue()); + } + } + +} diff --git a/src/org/traccar/model/Geofence.java b/src/org/traccar/model/Geofence.java new file mode 100644 index 000000000..0723c21e0 --- /dev/null +++ b/src/org/traccar/model/Geofence.java @@ -0,0 +1,67 @@ +package org.traccar.model; + +import java.text.ParseException; + +import org.traccar.geofence.GeofenceCircle; +import org.traccar.geofence.GeofenceGeometry; +import org.traccar.geofence.GeofencePolygon; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +public class Geofence extends Extensible { + + public static final String TYPE_GEOFENCE_CILCLE = "geofenceCircle"; + public static final String TYPE_GEOFENCE_POLYGON = "geofencePolygon"; + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + private String description; + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + private String area; + + public String getArea() { + return area; + } + + public void setArea(String area) throws ParseException { + + if (area.startsWith("CIRCLE")) { + geometry = new GeofenceCircle(area); + } else if (area.startsWith("POLYGON")) { + geometry = new GeofencePolygon(area); + } else { + throw new ParseException("Unknown geometry type", 0); + } + + this.area = area; + } + + private GeofenceGeometry geometry; + + @JsonIgnore + public GeofenceGeometry getGeometry() { + return geometry; + } + + public void setGeometry(GeofenceGeometry geometry) { + area = geometry.toWKT(); + this.geometry = geometry; + } + +} diff --git a/src/org/traccar/model/GeofencePermission.java b/src/org/traccar/model/GeofencePermission.java new file mode 100644 index 000000000..38fe7b6c1 --- /dev/null +++ b/src/org/traccar/model/GeofencePermission.java @@ -0,0 +1,25 @@ +package org.traccar.model; + +public class GeofencePermission { + + private long userId; + + public long getUserId() { + return userId; + } + + public void setUserId(long userId) { + this.userId = userId; + } + + private long geofenceId; + + public long getGeofenceId() { + return geofenceId; + } + + public void setGeofenceId(long geofenceId) { + this.geofenceId = geofenceId; + } + +} diff --git a/src/org/traccar/model/GroupGeofence.java b/src/org/traccar/model/GroupGeofence.java new file mode 100644 index 000000000..a8f6bd475 --- /dev/null +++ b/src/org/traccar/model/GroupGeofence.java @@ -0,0 +1,25 @@ +package org.traccar.model; + +public class GroupGeofence { + + private long groupId; + + public long getGroupId() { + return groupId; + } + + public void setGroupId(long groupId) { + this.groupId = groupId; + } + + private long geofenceId; + + public long getGeofenceId() { + return geofenceId; + } + + public void setGeofenceId(long geofenceId) { + this.geofenceId = geofenceId; + } + +} diff --git a/src/org/traccar/model/Message.java b/src/org/traccar/model/Message.java index 8722acc16..5015b9339 100644 --- a/src/org/traccar/model/Message.java +++ b/src/org/traccar/model/Message.java @@ -15,10 +15,7 @@ */ package org.traccar.model; -import java.util.LinkedHashMap; -import java.util.Map; - -public class Message { +public class Message extends Extensible { private long deviceId; @@ -40,42 +37,4 @@ public class Message { this.type = type; } - private Map attributes = new LinkedHashMap<>(); - - public Map getAttributes() { - return attributes; - } - - public void setAttributes(Map attributes) { - this.attributes = attributes; - } - - public void set(String key, boolean value) { - attributes.put(key, value); - } - - public void set(String key, int value) { - attributes.put(key, value); - } - - public void set(String key, long value) { - attributes.put(key, value); - } - - public void set(String key, double value) { - attributes.put(key, value); - } - - public void set(String key, String value) { - if (value != null && !value.isEmpty()) { - attributes.put(key, value); - } - } - - public void add(Map.Entry entry) { - if (entry != null && entry.getValue() != null) { - attributes.put(entry.getKey(), entry.getValue()); - } - } - } diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index 22d1be846..b4079dae6 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -66,16 +66,6 @@ public class Position extends Message { public static final String PREFIX_IO = "io"; public static final String PREFIX_COUNT = "count"; - private long id; - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } - private String protocol; public String getProtocol() { diff --git a/src/org/traccar/model/UserDeviceGeofence.java b/src/org/traccar/model/UserDeviceGeofence.java new file mode 100644 index 000000000..c84aa46b8 --- /dev/null +++ b/src/org/traccar/model/UserDeviceGeofence.java @@ -0,0 +1,35 @@ +package org.traccar.model; + +public class UserDeviceGeofence { + + private long userId; + + public long getUserId() { + return userId; + } + + public void setUserId(long userId) { + this.userId = userId; + } + + private long deviceId; + + public long getDeviceId() { + return deviceId; + } + + public void setDeviceId(long deviceId) { + this.deviceId = deviceId; + } + + private long geofenceId; + + public long getGeofenceId() { + return geofenceId; + } + + public void setGeofenceId(long geofenceId) { + this.geofenceId = geofenceId; + } + +} diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index 751db7a33..c06ee5d35 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -44,7 +44,11 @@ import org.traccar.api.resource.GroupResource; import org.traccar.api.resource.DeviceResource; import org.traccar.api.resource.PositionResource; import org.traccar.api.resource.CommandTypeResource; +import org.traccar.api.resource.DeviceGeofenceResource; import org.traccar.api.resource.EventResource; +import org.traccar.api.resource.GeofencePermissionResource; +import org.traccar.api.resource.GeofenceResource; +import org.traccar.api.resource.GroupGeofenceResource; import org.traccar.helper.Log; import javax.naming.InitialContext; @@ -150,7 +154,8 @@ public class WebServer { resourceConfig.registerClasses(ServerResource.class, SessionResource.class, CommandResource.class, GroupPermissionResource.class, DevicePermissionResource.class, UserResource.class, GroupResource.class, DeviceResource.class, PositionResource.class, - CommandTypeResource.class, EventResource.class); + CommandTypeResource.class, EventResource.class, GeofenceResource.class, + DeviceGeofenceResource.class, GeofencePermissionResource.class, GroupGeofenceResource.class); servletHandler.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*"); handlers.addHandler(servletHandler); -- cgit v1.2.3 From a5cdddd4a304f75645a2b89b7e834e5750466c5b Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 10 Jun 2016 20:08:44 +0500 Subject: Fixed broken repeated event suppression --- src/org/traccar/database/DataManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 86c930c0b..30334b78b 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -525,8 +525,8 @@ public class DataManager implements IdentityManager { public Collection getLastEvents(long deviceId, String type, int interval) throws SQLException { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.SECOND, -interval); - Date to = calendar.getTime(); - return getEvents(deviceId, type, new Date(), to); + Date from = calendar.getTime(); + return getEvents(deviceId, type, from, new Date()); } public Collection getGeofences() throws SQLException { -- cgit v1.2.3 From 3b5b60dace5ad75fe92bee5adef0c7e4c61b7757 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 13 Jun 2016 16:55:01 +0500 Subject: Style fixes --- src/org/traccar/geofence/GeofenceCircle.java | 6 +++--- src/org/traccar/geofence/GeofenceGeometry.java | 4 ++-- src/org/traccar/geofence/GeofencePolygon.java | 22 +++++++++++----------- 3 files changed, 16 insertions(+), 16 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/geofence/GeofenceCircle.java b/src/org/traccar/geofence/GeofenceCircle.java index 76d5a2816..e1c8f8a02 100644 --- a/src/org/traccar/geofence/GeofenceCircle.java +++ b/src/org/traccar/geofence/GeofenceCircle.java @@ -17,7 +17,7 @@ public class GeofenceCircle extends GeofenceGeometry { public GeofenceCircle(String wkt) throws ParseException { super(); - fromWKT(wkt); + fromWkt(wkt); } public GeofenceCircle(double latitude, double longitude, double radius) { @@ -33,7 +33,7 @@ public class GeofenceCircle extends GeofenceGeometry { } @Override - public String toWKT() { + public String toWkt() { String wkt = ""; wkt = "CIRCLE ("; wkt += String.valueOf(centerLatitude); @@ -47,7 +47,7 @@ public class GeofenceCircle extends GeofenceGeometry { } @Override - public void fromWKT(String wkt) throws ParseException { + public void fromWkt(String wkt) throws ParseException { if (!wkt.startsWith("CIRCLE")) { throw new ParseException("Mismatch geometry type", 0); } diff --git a/src/org/traccar/geofence/GeofenceGeometry.java b/src/org/traccar/geofence/GeofenceGeometry.java index c8f042413..83656a029 100644 --- a/src/org/traccar/geofence/GeofenceGeometry.java +++ b/src/org/traccar/geofence/GeofenceGeometry.java @@ -6,8 +6,8 @@ public abstract class GeofenceGeometry { public abstract boolean containsPoint(double latitude, double longitude); - public abstract String toWKT(); + public abstract String toWkt(); - public abstract void fromWKT(String wkt) throws ParseException; + public abstract void fromWkt(String wkt) throws ParseException; } diff --git a/src/org/traccar/geofence/GeofencePolygon.java b/src/org/traccar/geofence/GeofencePolygon.java index 08178375a..52920b7b1 100644 --- a/src/org/traccar/geofence/GeofencePolygon.java +++ b/src/org/traccar/geofence/GeofencePolygon.java @@ -11,7 +11,7 @@ public class GeofencePolygon extends GeofenceGeometry { public GeofencePolygon(String wkt) throws ParseException { super(); - fromWKT(wkt); + fromWkt(wkt); } private static class Coordinate { @@ -94,9 +94,9 @@ public class GeofencePolygon extends GeofenceGeometry { for (i = 0; i < polyCorners; j = i++) { if (coordinates.get(i).getLon360() < longitude360 - && coordinates.get(j).getLon360() >= longitude360 - || coordinates.get(j).getLon360() < longitude360 - && coordinates.get(i).getLon360() >= longitude360) { + && coordinates.get(j).getLon360() >= longitude360 + || coordinates.get(j).getLon360() < longitude360 + && coordinates.get(i).getLon360() >= longitude360) { oddNodes ^= longitude360 * multiple[i] + constant[i] < latitude; } } @@ -104,7 +104,7 @@ public class GeofencePolygon extends GeofenceGeometry { } @Override - public String toWKT() { + public String toWkt() { StringBuffer buf = new StringBuffer(); buf.append("POLYGON ("); for (Coordinate coordinate : coordinates) { @@ -117,7 +117,7 @@ public class GeofencePolygon extends GeofenceGeometry { } @Override - public void fromWKT(String wkt) throws ParseException { + public void fromWkt(String wkt) throws ParseException { if (coordinates == null) { coordinates = new ArrayList(); } else { @@ -131,15 +131,15 @@ public class GeofencePolygon extends GeofenceGeometry { if (content == null || content.equals("")) { throw new ParseException("No content", 0); } - String[] commatokens = content.split(","); - if (commatokens.length < 3) { + String[] commaTokens = content.split(","); + if (commaTokens.length < 3) { throw new ParseException("Not valid content", 0); } - for (String commatoken : commatokens) { - String[] tokens = commatoken.trim().split("\\s"); + for (String commaToken : commaTokens) { + String[] tokens = commaToken.trim().split("\\s"); if (tokens.length != 2) { - throw new ParseException("Here must be two coordinates: " + commatoken, 0); + throw new ParseException("Here must be two coordinates: " + commaToken, 0); } Coordinate coordinate = new Coordinate(); try { -- cgit v1.2.3 From b588b3c723cad4629dcecbce8983933f7ff2a255 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 14 Jun 2016 18:05:05 +0500 Subject: - Overlapping geofences - Simplified user-device link --- debug.xml | 14 ++--- setup/unix/traccar.xml | 14 ++--- setup/windows/traccar.xml | 14 ++--- src/org/traccar/BaseEventHandler.java | 12 ++-- src/org/traccar/Context.java | 11 +++- .../api/resource/DeviceGeofenceResource.java | 14 ++--- src/org/traccar/database/ConnectionManager.java | 19 ++---- src/org/traccar/database/DataManager.java | 19 +++--- src/org/traccar/database/GeofenceManager.java | 65 +++++++++++++------- src/org/traccar/database/NotificationManager.java | 42 +++++++++++++ .../traccar/events/CommandResultEventHandler.java | 9 ++- src/org/traccar/events/GeofenceEventHandler.java | 70 ++++++++++++---------- src/org/traccar/events/MotionEventHandler.java | 30 ++++++---- src/org/traccar/events/OverspeedEventHandler.java | 12 ++-- src/org/traccar/model/Device.java | 11 ++-- src/org/traccar/model/DeviceGeofence.java | 25 ++++++++ src/org/traccar/model/Geofence.java | 2 +- src/org/traccar/model/UserDeviceGeofence.java | 35 ----------- test/org/traccar/geofence/GeofenceCircleTest.java | 6 +- test/org/traccar/geofence/GeofencePolygonTest.java | 6 +- 20 files changed, 249 insertions(+), 181 deletions(-) create mode 100644 src/org/traccar/database/NotificationManager.java create mode 100644 src/org/traccar/model/DeviceGeofence.java delete mode 100644 src/org/traccar/model/UserDeviceGeofence.java (limited to 'src/org/traccar') diff --git a/debug.xml b/debug.xml index bc09b9f48..f04ceb271 100644 --- a/debug.xml +++ b/debug.xml @@ -147,7 +147,7 @@ - UPDATE devices SET status = :status, lastUpdate = :lastUpdate, motion = :motion, geofenceId = :geofenceId WHERE id = :id; + UPDATE devices SET status = :status, lastUpdate = :lastUpdate, motion = :motion WHERE id = :id; @@ -267,16 +267,16 @@ DELETE FROM group_geofence WHERE groupId = :groupId AND geofenceId = :geofenceId; - - SELECT userId, deviceId, geofenceId FROM user_device_geofence; + + SELECT deviceId, geofenceId FROM device_geofence; - - INSERT INTO user_device_geofence (userId, deviceId, geofenceId) VALUES (:userId, :deviceId, :geofenceId); + + INSERT INTO device_geofence (deviceId, geofenceId) VALUES (:deviceId, :geofenceId); - - DELETE FROM user_device_geofence WHERE userId = :userId AND deviceId = :deviceId AND geofenceId = :geofenceId; + + DELETE FROM device_geofence WHERE deviceId = :deviceId AND geofenceId = :geofenceId; diff --git a/setup/unix/traccar.xml b/setup/unix/traccar.xml index 37ff31bbe..ab739e4d6 100644 --- a/setup/unix/traccar.xml +++ b/setup/unix/traccar.xml @@ -118,7 +118,7 @@ - UPDATE devices SET status = :status, lastUpdate = :lastUpdate, motion = :motion, geofenceId = :geofenceId WHERE id = :id; + UPDATE devices SET status = :status, lastUpdate = :lastUpdate, motion = :motion WHERE id = :id; @@ -238,16 +238,16 @@ DELETE FROM group_geofence WHERE groupId = :groupId AND geofenceId = :geofenceId; - - SELECT userId, deviceId, geofenceId FROM user_device_geofence; + + SELECT deviceId, geofenceId FROM device_geofence; - - INSERT INTO user_device_geofence (userId, deviceId, geofenceId) VALUES (:userId, :deviceId, :geofenceId); + + INSERT INTO device_geofence (deviceId, geofenceId) VALUES (:deviceId, :geofenceId); - - DELETE FROM user_device_geofence WHERE userId = :userId AND deviceId = :deviceId AND geofenceId = :geofenceId; + + DELETE FROM device_geofence WHERE deviceId = :deviceId AND geofenceId = :geofenceId; diff --git a/setup/windows/traccar.xml b/setup/windows/traccar.xml index 5f86e53b0..17f2ab4f3 100644 --- a/setup/windows/traccar.xml +++ b/setup/windows/traccar.xml @@ -118,7 +118,7 @@ - UPDATE devices SET status = :status, lastUpdate = :lastUpdate, motion = :motion, geofenceId = :geofenceId WHERE id = :id; + UPDATE devices SET status = :status, lastUpdate = :lastUpdate, motion = :motion WHERE id = :id; @@ -238,16 +238,16 @@ DELETE FROM group_geofence WHERE groupId = :groupId AND geofenceId = :geofenceId; - - SELECT userId, deviceId, geofenceId FROM user_device_geofence; + + SELECT deviceId, geofenceId FROM device_geofence; - - INSERT INTO user_device_geofence (userId, deviceId, geofenceId) VALUES (:userId, :deviceId, :geofenceId); + + INSERT INTO device_geofence (deviceId, geofenceId) VALUES (:deviceId, :geofenceId); - - DELETE FROM user_device_geofence WHERE userId = :userId AND deviceId = :deviceId AND geofenceId = :geofenceId; + + DELETE FROM device_geofence WHERE deviceId = :deviceId AND geofenceId = :geofenceId; diff --git a/src/org/traccar/BaseEventHandler.java b/src/org/traccar/BaseEventHandler.java index 3e3317f0a..16d911dac 100644 --- a/src/org/traccar/BaseEventHandler.java +++ b/src/org/traccar/BaseEventHandler.java @@ -1,5 +1,7 @@ package org.traccar; +import java.util.Collection; + import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; @@ -23,13 +25,15 @@ public abstract class BaseEventHandler extends BaseDataHandler { } } - Event event = analizePosition(position); - if (event != null) { - Context.getConnectionManager().updateEvent(event, position); + Collection events = analizePosition(position); + if (events != null) { + for (Event event : events) { + Context.getNotificationManager().updateEvent(event, position); + } } return position; } - protected abstract Event analizePosition(Position position); + protected abstract Collection analizePosition(Position position); } diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index b78d11e7b..6f99ff97d 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -19,6 +19,7 @@ import com.ning.http.client.AsyncHttpClient; import org.traccar.database.ConnectionManager; import org.traccar.database.DataManager; import org.traccar.database.IdentityManager; +import org.traccar.database.NotificationManager; import org.traccar.database.PermissionsManager; import org.traccar.database.GeofenceManager; import org.traccar.geocode.BingMapsReverseGeocoder; @@ -106,6 +107,12 @@ public final class Context { return geofenceManager; } + private static NotificationManager notificationManager; + + public static NotificationManager getNotificationManager() { + return notificationManager; + } + private static final AsyncHttpClient ASYNC_HTTP_CLIENT = new AsyncHttpClient(); public static AsyncHttpClient getAsyncHttpClient() { @@ -184,9 +191,11 @@ public final class Context { permissionsManager = new PermissionsManager(dataManager); + connectionManager = new ConnectionManager(dataManager); + geofenceManager = new GeofenceManager(dataManager); - connectionManager = new ConnectionManager(dataManager); + notificationManager = new NotificationManager(dataManager); serverManager = new ServerManager(); diff --git a/src/org/traccar/api/resource/DeviceGeofenceResource.java b/src/org/traccar/api/resource/DeviceGeofenceResource.java index dc7014d79..99d64292b 100644 --- a/src/org/traccar/api/resource/DeviceGeofenceResource.java +++ b/src/org/traccar/api/resource/DeviceGeofenceResource.java @@ -17,7 +17,7 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; -import org.traccar.model.UserDeviceGeofence; +import org.traccar.model.DeviceGeofence; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -35,25 +35,21 @@ import java.sql.SQLException; public class DeviceGeofenceResource extends BaseResource { @POST - public Response add(UserDeviceGeofence entity) throws SQLException { + public Response add(DeviceGeofence entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); - Context.getDataManager().linkUserDeviceGeofence(entity.getUserId(), - entity.getDeviceId(), entity.getGeofenceId()); + Context.getDataManager().linkDeviceGeofence(entity.getDeviceId(), entity.getGeofenceId()); Context.getGeofenceManager().refresh(); return Response.ok(entity).build(); } @DELETE - public Response remove(UserDeviceGeofence entity) throws SQLException { + public Response remove(DeviceGeofence entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); - Context.getDataManager().unlinkUserDeviceGeofence(entity.getUserId(), entity.getDeviceId(), - entity.getGeofenceId()); + Context.getDataManager().unlinkDeviceGeofence(entity.getDeviceId(), entity.getGeofenceId()); Context.getGeofenceManager().refresh(); return Response.noContent().build(); } diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index 6e47dfad3..4f12c44d4 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -94,7 +94,7 @@ public class ConnectionManager { if (status.equals(Device.STATUS_ONLINE)) { event.setType(Event.TYPE_DEVICE_ONLINE); } - updateEvent(event, null); + Context.getNotificationManager().updateEvent(event, null); } device.setStatus(status); @@ -147,19 +147,10 @@ public class ConnectionManager { } } - public synchronized void updateEvent(Event event, Position position) { - long deviceId = event.getDeviceId(); - try { - Context.getDataManager().addEvent(event); - } catch (SQLException error) { - Log.warning(error); - } - for (long userId : Context.getPermissionsManager().getDeviceUsers(deviceId)) { - if (listeners.containsKey(userId) && (event.getGeofenceId() == 0 - || Context.getGeofenceManager().checkGeofence(userId, event.getGeofenceId()))) { - for (UpdateListener listener : listeners.get(userId)) { - listener.onUpdateEvent(event, position); - } + public synchronized void updateEvent(long userId, Event event, Position position) { + if (listeners.containsKey(userId)) { + for (UpdateListener listener : listeners.get(userId)) { + listener.onUpdateEvent(event, position); } } } diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 30334b78b..2b09e2fb0 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -55,7 +55,7 @@ import org.traccar.model.GroupPermission; import org.traccar.model.Position; import org.traccar.model.Server; import org.traccar.model.User; -import org.traccar.model.UserDeviceGeofence; +import org.traccar.model.DeviceGeofence; import org.traccar.model.GeofencePermission; import com.zaxxer.hikari.HikariConfig; @@ -372,7 +372,6 @@ public class DataManager implements IdentityManager { Device cachedDevice = getDeviceById(device.getId()); cachedDevice.setStatus(device.getStatus()); cachedDevice.setMotion(device.getMotion()); - cachedDevice.setGeofenceId(device.getGeofenceId()); } public void removeDevice(long deviceId) throws SQLException { @@ -596,22 +595,20 @@ public class DataManager implements IdentityManager { .executeUpdate(); } - public Collection getUserDeviceGeofences() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectUserDeviceGeofences")) - .executeQuery(UserDeviceGeofence.class); + public Collection getDeviceGeofences() throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectDeviceGeofences")) + .executeQuery(DeviceGeofence.class); } - public void linkUserDeviceGeofence(long userId, long deviceId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkUserDeviceGeofence")) - .setLong("userId", userId) + public void linkDeviceGeofence(long deviceId, long geofenceId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.linkDeviceGeofence")) .setLong("deviceId", deviceId) .setLong("geofenceId", geofenceId) .executeUpdate(); } - public void unlinkUserDeviceGeofence(long userId, long deviceId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkUserDeviceGeofence")) - .setLong("userId", userId) + public void unlinkDeviceGeofence(long deviceId, long geofenceId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.unlinkDeviceGeofence")) .setLong("deviceId", deviceId) .setLong("geofenceId", geofenceId) .executeUpdate(); diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java index 64afc876c..b551bc467 100644 --- a/src/org/traccar/database/GeofenceManager.java +++ b/src/org/traccar/database/GeofenceManager.java @@ -1,20 +1,24 @@ package org.traccar.database; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Geofence; import org.traccar.model.GroupGeofence; -import org.traccar.model.UserDeviceGeofence; +import org.traccar.model.Position; +import org.traccar.model.DeviceGeofence; import org.traccar.model.GeofencePermission; public class GeofenceManager { @@ -25,8 +29,8 @@ public class GeofenceManager { private final Map> userGeofences = new HashMap<>(); private final Map> groupGeofences = new HashMap<>(); + private final Map> deviceGeofencesWithGroups = new HashMap<>(); private final Map> deviceGeofences = new HashMap<>(); - private final Map>> userDeviceGeofences = new HashMap<>(); private final ReadWriteLock deviceGeofencesLock = new ReentrantReadWriteLock(); private final ReadWriteLock geofencesLock = new ReentrantReadWriteLock(); @@ -54,17 +58,17 @@ public class GeofenceManager { public Set getAllDeviceGeofences(long deviceId) { deviceGeofencesLock.readLock().lock(); try { - return getDeviceGeofences(deviceGeofences, deviceId); + return getDeviceGeofences(deviceGeofencesWithGroups, deviceId); } finally { deviceGeofencesLock.readLock().unlock(); } } - public Set getUserDeviceGeofences(long userId, long deviceId) { + public Set getDeviceGeofences(long deviceId) { deviceGeofencesLock.readLock().lock(); try { - return getUserDeviceGeofencesUnlocked(userId, deviceId); + return getDeviceGeofences(deviceGeofences, deviceId); } finally { deviceGeofencesLock.readLock().unlock(); } @@ -77,13 +81,6 @@ public class GeofenceManager { return deviceGeofences.get(deviceId); } - private Set getUserDeviceGeofencesUnlocked(long userId, long deviceId) { - if (!userDeviceGeofences.containsKey(userId)) { - userDeviceGeofences.put(userId, new HashMap>()); - } - return getDeviceGeofences(userDeviceGeofences.get(userId), deviceId); - } - public final void refresh() { if (dataManager != null) { try { @@ -107,24 +104,38 @@ public class GeofenceManager { } deviceGeofences.clear(); + deviceGeofencesWithGroups.clear(); - for (Map.Entry>> deviceGeofence : userDeviceGeofences.entrySet()) { - deviceGeofence.getValue().clear(); + for (DeviceGeofence deviceGeofence : dataManager.getDeviceGeofences()) { + getDeviceGeofences(deviceGeofences, deviceGeofence.getDeviceId()) + .add(deviceGeofence.getGeofenceId()); + getDeviceGeofences(deviceGeofencesWithGroups, deviceGeofence.getDeviceId()) + .add(deviceGeofence.getGeofenceId()); } - userDeviceGeofences.clear(); - for (UserDeviceGeofence userDeviceGeofence : dataManager.getUserDeviceGeofences()) { - getDeviceGeofences(deviceGeofences, userDeviceGeofence.getDeviceId()) - .add(userDeviceGeofence.getGeofenceId()); - getUserDeviceGeofencesUnlocked(userDeviceGeofence.getUserId(), userDeviceGeofence.getDeviceId()) - .add(userDeviceGeofence.getGeofenceId()); - } for (Device device : dataManager.getAllDevices()) { long groupId = device.getGroupId(); while (groupId != 0) { - getDeviceGeofences(deviceGeofences, device.getId()).addAll(getGroupGeofences(groupId)); + getDeviceGeofences(deviceGeofencesWithGroups, + device.getId()).addAll(getGroupGeofences(groupId)); groupId = dataManager.getGroupById(groupId).getGroupId(); } + List deviceGeofenceIds = device.getGeofenceIds(); + if (deviceGeofenceIds == null) { + deviceGeofenceIds = new ArrayList(); + } else { + deviceGeofenceIds.clear(); + } + Position lastPosition = Context.getConnectionManager().getLastPosition(device.getId()); + if (lastPosition != null) { + for (Long geofenceId : deviceGeofencesWithGroups.get(device.getId())) { + if (getGeofence(geofenceId).getGeometry() + .containsPoint(lastPosition.getLatitude(), lastPosition.getLongitude())) { + deviceGeofenceIds.add(geofenceId); + } + } + } + device.setGeofenceIds(deviceGeofenceIds); } } finally { @@ -188,4 +199,14 @@ public class GeofenceManager { return getUserGeofencesIds(userId).contains(geofenceId); } + public List getCurrentDeviceGeofences(Position position) { + List result = new ArrayList(); + for (Long geofenceId : getAllDeviceGeofences(position.getDeviceId())) { + if (getGeofence(geofenceId).getGeometry().containsPoint(position.getLatitude(), position.getLongitude())) { + result.add(geofenceId); + } + } + return result; + } + } diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java new file mode 100644 index 000000000..8c8e958c8 --- /dev/null +++ b/src/org/traccar/database/NotificationManager.java @@ -0,0 +1,42 @@ +package org.traccar.database; + +import java.sql.SQLException; +import java.util.Collection; +import java.util.Set; + +import org.traccar.Context; +import org.traccar.helper.Log; +import org.traccar.model.Event; +import org.traccar.model.Position; + +public class NotificationManager { + + private final DataManager dataManager; + + public NotificationManager(DataManager dataManager) { + this.dataManager = dataManager; + } + + public void updateEvent(Event event, Position position) { + try { + dataManager.addEvent(event); + } catch (SQLException error) { + Log.warning(error); + } + + Set users = Context.getPermissionsManager().getDeviceUsers(event.getDeviceId()); + for (Long userId : users) { + if (event.getGeofenceId() == 0 + || Context.getGeofenceManager().checkGeofence(userId, event.getGeofenceId())) { + Context.getConnectionManager().updateEvent(userId, event, position); + } + } + } + + public void updateEvents(Collection events, Position position) { + + for (Event event : events) { + updateEvent(event, position); + } + } +} diff --git a/src/org/traccar/events/CommandResultEventHandler.java b/src/org/traccar/events/CommandResultEventHandler.java index 23c62566a..9dbdb4b4c 100644 --- a/src/org/traccar/events/CommandResultEventHandler.java +++ b/src/org/traccar/events/CommandResultEventHandler.java @@ -1,5 +1,8 @@ package org.traccar.events; +import java.util.ArrayList; +import java.util.Collection; + import org.traccar.BaseEventHandler; import org.traccar.model.Event; import org.traccar.model.Position; @@ -7,10 +10,12 @@ import org.traccar.model.Position; public class CommandResultEventHandler extends BaseEventHandler { @Override - protected Event analizePosition(Position position) { + protected Collection analizePosition(Position position) { Object commandResult = position.getAttributes().get(Position.KEY_RESULT); if (commandResult != null) { - return new Event(Event.TYPE_COMMAND_RESULT, position.getDeviceId(), position.getId()); + Collection events = new ArrayList<>(); + events.add(new Event(Event.TYPE_COMMAND_RESULT, position.getDeviceId(), position.getId())); + return events; } return null; } diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java index bf9060ca1..ed63b6f7d 100644 --- a/src/org/traccar/events/GeofenceEventHandler.java +++ b/src/org/traccar/events/GeofenceEventHandler.java @@ -1,7 +1,9 @@ package org.traccar.events; import java.sql.SQLException; -import java.util.Set; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; import org.traccar.BaseEventHandler; import org.traccar.Context; @@ -25,50 +27,52 @@ public class GeofenceEventHandler extends BaseEventHandler { } @Override - protected Event analizePosition(Position position) { - Event event = null; - if (!isLastPosition() || !position.getValid()) { - return event; + protected Collection analizePosition(Position position) { + if (!isLastPosition() || !position.getValid()) { + return null; } Device device = dataManager.getDeviceById(position.getDeviceId()); if (device == null) { - return event; + return null; } - Set geofences = geofenceManager.getAllDeviceGeofences(position.getDeviceId()); - if (geofences == null) { - return event; - } - long geofenceId = 0; - for (Long geofence : geofences) { - if (geofenceManager.getGeofence(geofence).getGeometry() - .containsPoint(position.getLatitude(), position.getLongitude())) { - geofenceId = geofence; - break; - } + List currentGeofences = geofenceManager.getCurrentDeviceGeofences(position); + List oldGeofences = new ArrayList(); + if (device.getGeofenceIds() != null) { + oldGeofences.addAll(device.getGeofenceIds()); } + List newGeofences = new ArrayList(currentGeofences); + newGeofences.removeAll(oldGeofences); + oldGeofences.removeAll(currentGeofences); - if (device.getGeofenceId() != geofenceId) { - try { - if (geofenceId == 0) { - event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); - event.setGeofenceId(device.getGeofenceId()); - } else { - event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); + device.setGeofenceIds(currentGeofences); + + Collection events = new ArrayList<>(); + try { + if (dataManager.getLastEvents(position.getDeviceId(), + Event.TYPE_GEOFENCE_ENTER, suppressRepeated).isEmpty()) { + for (Long geofenceId : newGeofences) { + Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); + events.add(event); } - if (event != null && !dataManager.getLastEvents( - position.getDeviceId(), event.getType(), suppressRepeated).isEmpty()) { - event = null; + } + } catch (SQLException error) { + Log.warning(error); + } + try { + if (dataManager.getLastEvents(position.getDeviceId(), + Event.TYPE_GEOFENCE_EXIT, suppressRepeated).isEmpty()) { + for (Long geofenceId : oldGeofences) { + Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); + event.setGeofenceId(geofenceId); + events.add(event); } - device.setGeofenceId(geofenceId); - dataManager.updateDeviceStatus(device); - } catch (SQLException error) { - Log.warning(error); } - + } catch (SQLException error) { + Log.warning(error); } - return event; + return events; } } diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index 306fa8a4e..54b6b922d 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -1,6 +1,8 @@ package org.traccar.events; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; import org.traccar.BaseEventHandler; import org.traccar.Context; @@ -19,18 +21,17 @@ public class MotionEventHandler extends BaseEventHandler { } @Override - protected Event analizePosition(Position position) { - Event event = null; - + protected Collection analizePosition(Position position) { + Collection result = null; if (!isLastPosition()) { - return event; + return null; } double speed = position.getSpeed(); boolean valid = position.getValid(); Device device = Context.getIdentityManager().getDeviceById(position.getDeviceId()); if (device == null) { - return event; + return null; } String motion = device.getMotion(); if (motion == null) { @@ -38,21 +39,26 @@ public class MotionEventHandler extends BaseEventHandler { } if (valid && speed > SPEED_THRESHOLD && !motion.equals(Device.STATUS_MOVING)) { Context.getConnectionManager().updateDevice(position.getDeviceId(), Device.STATUS_MOVING, null); - event = new Event(Event.TYPE_DEVICE_MOVING, position.getDeviceId(), position.getId()); + result = new ArrayList<>(); + result.add(new Event(Event.TYPE_DEVICE_MOVING, position.getDeviceId(), position.getId())); } else if (valid && speed < SPEED_THRESHOLD && motion.equals(Device.STATUS_MOVING)) { Context.getConnectionManager().updateDevice(position.getDeviceId(), Device.STATUS_STOPPED, null); - event = new Event(Event.TYPE_DEVICE_STOPPED, position.getDeviceId(), position.getId()); + result = new ArrayList<>(); + result.add(new Event(Event.TYPE_DEVICE_STOPPED, position.getDeviceId(), position.getId())); } try { - if (event != null && !Context.getDataManager().getLastEvents( - position.getDeviceId(), event.getType(), suppressRepeated).isEmpty()) { - event = null; + if (result != null && !result.isEmpty()) { + for (Event event : result) { + if (!Context.getDataManager().getLastEvents(position.getDeviceId(), + event.getType(), suppressRepeated).isEmpty()) { + event = null; + } + } } - } catch (SQLException error) { Log.warning(error); } - return event; + return result; } } diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index 30410ff32..152fe6f22 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -1,6 +1,8 @@ package org.traccar.events; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; import org.traccar.BaseEventHandler; import org.traccar.Context; @@ -20,10 +22,10 @@ public class OverspeedEventHandler extends BaseEventHandler { } @Override - protected Event analizePosition(Position position) { - Event event = null; + protected Collection analizePosition(Position position) { + Collection events = new ArrayList<>(); if (!isLastPosition()) { - return event; + return null; } double speed = position.getSpeed(); boolean valid = position.getValid(); @@ -32,14 +34,14 @@ public class OverspeedEventHandler extends BaseEventHandler { try { if (Context.getDataManager().getLastEvents( position.getDeviceId(), Event.TYPE_DEVICE_OVERSPEED, suppressRepeated).isEmpty()) { - event = new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId()); + events.add(new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId())); } } catch (SQLException error) { Log.warning(error); } } - return event; + return events; } } diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java index 45c3d46dc..c42eb3718 100644 --- a/src/org/traccar/model/Device.java +++ b/src/org/traccar/model/Device.java @@ -16,6 +16,7 @@ package org.traccar.model; import java.util.Date; +import java.util.List; public class Device { @@ -114,13 +115,13 @@ public class Device { this.motion = motion; } - private long geofenceId; + private List geofenceIds; - public long getGeofenceId() { - return geofenceId; + public List getGeofenceIds() { + return geofenceIds; } - public void setGeofenceId(long geofenceId) { - this.geofenceId = geofenceId; + public void setGeofenceIds(List geofenceIds) { + this.geofenceIds = geofenceIds; } } diff --git a/src/org/traccar/model/DeviceGeofence.java b/src/org/traccar/model/DeviceGeofence.java new file mode 100644 index 000000000..f55c8ca69 --- /dev/null +++ b/src/org/traccar/model/DeviceGeofence.java @@ -0,0 +1,25 @@ +package org.traccar.model; + +public class DeviceGeofence { + + private long deviceId; + + public long getDeviceId() { + return deviceId; + } + + public void setDeviceId(long deviceId) { + this.deviceId = deviceId; + } + + private long geofenceId; + + public long getGeofenceId() { + return geofenceId; + } + + public void setGeofenceId(long geofenceId) { + this.geofenceId = geofenceId; + } + +} diff --git a/src/org/traccar/model/Geofence.java b/src/org/traccar/model/Geofence.java index 0723c21e0..300d8fb74 100644 --- a/src/org/traccar/model/Geofence.java +++ b/src/org/traccar/model/Geofence.java @@ -60,7 +60,7 @@ public class Geofence extends Extensible { } public void setGeometry(GeofenceGeometry geometry) { - area = geometry.toWKT(); + area = geometry.toWkt(); this.geometry = geometry; } diff --git a/src/org/traccar/model/UserDeviceGeofence.java b/src/org/traccar/model/UserDeviceGeofence.java deleted file mode 100644 index c84aa46b8..000000000 --- a/src/org/traccar/model/UserDeviceGeofence.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.traccar.model; - -public class UserDeviceGeofence { - - private long userId; - - public long getUserId() { - return userId; - } - - public void setUserId(long userId) { - this.userId = userId; - } - - private long deviceId; - - public long getDeviceId() { - return deviceId; - } - - public void setDeviceId(long deviceId) { - this.deviceId = deviceId; - } - - private long geofenceId; - - public long getGeofenceId() { - return geofenceId; - } - - public void setGeofenceId(long geofenceId) { - this.geofenceId = geofenceId; - } - -} diff --git a/test/org/traccar/geofence/GeofenceCircleTest.java b/test/org/traccar/geofence/GeofenceCircleTest.java index 820f34e34..52c214b53 100644 --- a/test/org/traccar/geofence/GeofenceCircleTest.java +++ b/test/org/traccar/geofence/GeofenceCircleTest.java @@ -12,11 +12,11 @@ public class GeofenceCircleTest { String test = "CIRCLE (55.75414 37.6204, 100)"; GeofenceGeometry geofenceGeometry = new GeofenceCircle(); try { - geofenceGeometry.fromWKT(test); + geofenceGeometry.fromWkt(test); } catch (ParseException e){ Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); } - Assert.assertEquals(geofenceGeometry.toWKT(), test); + Assert.assertEquals(geofenceGeometry.toWkt(), test); } @Test @@ -24,7 +24,7 @@ public class GeofenceCircleTest { String test = "CIRCLE (55.75414 37.6204, 100)"; GeofenceGeometry geofenceGeometry = new GeofenceCircle(); try { - geofenceGeometry.fromWKT(test); + geofenceGeometry.fromWkt(test); } catch (ParseException e){ Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); } diff --git a/test/org/traccar/geofence/GeofencePolygonTest.java b/test/org/traccar/geofence/GeofencePolygonTest.java index cc0ed77ad..8711a3696 100644 --- a/test/org/traccar/geofence/GeofencePolygonTest.java +++ b/test/org/traccar/geofence/GeofencePolygonTest.java @@ -12,11 +12,11 @@ public class GeofencePolygonTest { String test = "POLYGON (55.75474 37.61823, 55.75513 37.61888, 55.7535 37.6222, 55.75315 37.62165)"; GeofenceGeometry geofenceGeometry = new GeofencePolygon(); try { - geofenceGeometry.fromWKT(test); + geofenceGeometry.fromWkt(test); } catch (ParseException e){ Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); } - Assert.assertEquals(geofenceGeometry.toWKT(), test); + Assert.assertEquals(geofenceGeometry.toWkt(), test); } @Test @@ -24,7 +24,7 @@ public class GeofencePolygonTest { String test = "POLYGON (55.75474 37.61823, 55.75513 37.61888, 55.7535 37.6222, 55.75315 37.62165)"; GeofenceGeometry geofenceGeometry = new GeofencePolygon(); try { - geofenceGeometry.fromWKT(test); + geofenceGeometry.fromWkt(test); } catch (ParseException e){ Assert.assertTrue("ParseExceprion: " + e.getMessage(), true); } -- cgit v1.2.3 From 6da14aded9775469a044d36e9a8ce5d8483f9003 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 14 Jun 2016 22:34:29 +0500 Subject: Current device geofences should survive cache update --- src/org/traccar/database/DataManager.java | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/org/traccar') diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 2b09e2fb0..a32d31d68 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -154,6 +154,10 @@ public class DataManager implements IdentityManager { devicesById.put(device.getId(), device); devicesByUniqueId.put(device.getUniqueId(), device); } + GeofenceManager geofenceManager = Context.getGeofenceManager(); + if (geofenceManager != null) { + geofenceManager.refresh(); + } devicesLastUpdate = System.currentTimeMillis(); } } finally { -- cgit v1.2.3 From f08bb5adf425269a00e760c669bdeeadc8c7e112 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 15 Jun 2016 14:22:23 +0500 Subject: - Work only with cached devices from everywhere - Some optimization --- src/org/traccar/BaseEventHandler.java | 8 +++++++- src/org/traccar/api/resource/DeviceResource.java | 2 +- src/org/traccar/database/DataManager.java | 25 +++++++++++++++++++++++- src/org/traccar/database/GeofenceManager.java | 2 +- src/org/traccar/database/PermissionsManager.java | 2 +- src/org/traccar/events/GeofenceEventHandler.java | 10 ++++------ src/org/traccar/events/MotionEventHandler.java | 5 ++--- 7 files changed, 40 insertions(+), 14 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/BaseEventHandler.java b/src/org/traccar/BaseEventHandler.java index 16d911dac..78542b33a 100644 --- a/src/org/traccar/BaseEventHandler.java +++ b/src/org/traccar/BaseEventHandler.java @@ -14,10 +14,16 @@ public abstract class BaseEventHandler extends BaseDataHandler { return isLastPosition; } + private Device device; + + public Device getDevice() { + return device; + } + @Override protected Position handlePosition(Position position) { - Device device = Context.getDataManager().getDeviceById(position.getDeviceId()); + device = Context.getDataManager().getDeviceById(position.getDeviceId()); if (device != null) { long lastPositionId = device.getPositionId(); if (position.getId() == lastPositionId) { diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index 26880c1f8..fcbeed97b 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -44,7 +44,7 @@ public class DeviceResource extends BaseResource { @QueryParam("all") boolean all, @QueryParam("userId") long userId) throws SQLException { if (all) { Context.getPermissionsManager().checkAdmin(getUserId()); - return Context.getDataManager().getAllDevices(); + return Context.getDataManager().getAllDevicesCached(); } else { if (userId == 0) { userId = getUserId(); diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index a32d31d68..f5df1b84b 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -342,11 +342,34 @@ public class DataManager implements IdentityManager { .executeQuery(GroupPermission.class); } - public Collection getAllDevices() throws SQLException { + private Collection getAllDevices() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectDevicesAll")) .executeQuery(Device.class); } + public Collection getAllDevicesCached() { + boolean forceUpdate; + devicesLock.readLock().lock(); + try { + forceUpdate = devicesById.values().isEmpty(); + } finally { + devicesLock.readLock().unlock(); + } + + try { + updateDeviceCache(forceUpdate); + } catch (SQLException e) { + Log.warning(e); + } + + devicesLock.readLock().lock(); + try { + return devicesById.values(); + } finally { + devicesLock.readLock().unlock(); + } + } + public Collection getDevices(long userId) throws SQLException { Collection devices = new ArrayList<>(); for (long id : Context.getPermissionsManager().getDevicePermissions(userId)) { diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java index b551bc467..0119a62ca 100644 --- a/src/org/traccar/database/GeofenceManager.java +++ b/src/org/traccar/database/GeofenceManager.java @@ -113,7 +113,7 @@ public class GeofenceManager { .add(deviceGeofence.getGeofenceId()); } - for (Device device : dataManager.getAllDevices()) { + for (Device device : dataManager.getAllDevicesCached()) { long groupId = device.getGroupId(); while (groupId != 0) { getDeviceGeofences(deviceGeofencesWithGroups, diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 96a6488ef..b6dd2e2a9 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -78,7 +78,7 @@ public class PermissionsManager { users.put(user.getId(), user); } - GroupTree groupTree = new GroupTree(dataManager.getAllGroups(), dataManager.getAllDevices()); + GroupTree groupTree = new GroupTree(dataManager.getAllGroups(), dataManager.getAllDevicesCached()); for (GroupPermission permission : dataManager.getGroupPermissions()) { Set userGroupPermissions = getGroupPermissions(permission.getUserId()); Set userDevicePermissions = getDevicePermissions(permission.getUserId()); diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java index ed63b6f7d..56029fced 100644 --- a/src/org/traccar/events/GeofenceEventHandler.java +++ b/src/org/traccar/events/GeofenceEventHandler.java @@ -10,7 +10,6 @@ import org.traccar.Context; import org.traccar.database.DataManager; import org.traccar.database.GeofenceManager; import org.traccar.helper.Log; -import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; @@ -32,21 +31,20 @@ public class GeofenceEventHandler extends BaseEventHandler { return null; } - Device device = dataManager.getDeviceById(position.getDeviceId()); - if (device == null) { + if (getDevice() == null) { return null; } List currentGeofences = geofenceManager.getCurrentDeviceGeofences(position); List oldGeofences = new ArrayList(); - if (device.getGeofenceIds() != null) { - oldGeofences.addAll(device.getGeofenceIds()); + if (getDevice().getGeofenceIds() != null) { + oldGeofences.addAll(getDevice().getGeofenceIds()); } List newGeofences = new ArrayList(currentGeofences); newGeofences.removeAll(oldGeofences); oldGeofences.removeAll(currentGeofences); - device.setGeofenceIds(currentGeofences); + getDevice().setGeofenceIds(currentGeofences); Collection events = new ArrayList<>(); try { diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index 54b6b922d..2ba5979e3 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -29,11 +29,10 @@ public class MotionEventHandler extends BaseEventHandler { double speed = position.getSpeed(); boolean valid = position.getValid(); - Device device = Context.getIdentityManager().getDeviceById(position.getDeviceId()); - if (device == null) { + if (getDevice() == null) { return null; } - String motion = device.getMotion(); + String motion = getDevice().getMotion(); if (motion == null) { motion = Device.STATUS_STOPPED; } -- cgit v1.2.3 From 7933e79e708792569b7dade228f8642392978141 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 15 Jun 2016 15:49:53 +0500 Subject: Fix rare Stack Overflow while server idle --- src/org/traccar/database/DataManager.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index f5df1b84b..a530399c7 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -150,13 +150,18 @@ public class DataManager implements IdentityManager { if (force || System.currentTimeMillis() - devicesLastUpdate > dataRefreshDelay) { devicesById.clear(); devicesByUniqueId.clear(); + ConnectionManager connectionManager = Context.getConnectionManager(); + GeofenceManager geofenceManager = Context.getGeofenceManager(); for (Device device : getAllDevices()) { devicesById.put(device.getId(), device); devicesByUniqueId.put(device.getUniqueId(), device); - } - GeofenceManager geofenceManager = Context.getGeofenceManager(); - if (geofenceManager != null) { - geofenceManager.refresh(); + if (connectionManager != null && geofenceManager != null) { + Position lastPosition = Context.getConnectionManager().getLastPosition(device.getId()); + if (lastPosition != null) { + device.setGeofenceIds(Context.getGeofenceManager() + .getCurrentDeviceGeofences(lastPosition)); + } + } } devicesLastUpdate = System.currentTimeMillis(); } -- cgit v1.2.3 From 906276ac3f1e5b76abbec3632357f9c8c7163b21 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 15 Jun 2016 16:27:49 +0500 Subject: Use variables --- src/org/traccar/database/DataManager.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index a530399c7..860bf1b84 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -156,10 +156,9 @@ public class DataManager implements IdentityManager { devicesById.put(device.getId(), device); devicesByUniqueId.put(device.getUniqueId(), device); if (connectionManager != null && geofenceManager != null) { - Position lastPosition = Context.getConnectionManager().getLastPosition(device.getId()); + Position lastPosition = connectionManager.getLastPosition(device.getId()); if (lastPosition != null) { - device.setGeofenceIds(Context.getGeofenceManager() - .getCurrentDeviceGeofences(lastPosition)); + device.setGeofenceIds(geofenceManager.getCurrentDeviceGeofences(lastPosition)); } } } -- cgit v1.2.3 From 5d56e9e452c8bebe93047497eb55f527fcc40ffc Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 16 Jun 2016 10:05:38 +0500 Subject: Fix typo and remove unnecessary super() --- src/org/traccar/BaseEventHandler.java | 4 ++-- src/org/traccar/events/CommandResultEventHandler.java | 2 +- src/org/traccar/events/GeofenceEventHandler.java | 2 +- src/org/traccar/events/MotionEventHandler.java | 2 +- src/org/traccar/events/OverspeedEventHandler.java | 2 +- src/org/traccar/geofence/GeofenceCircle.java | 2 -- src/org/traccar/geofence/GeofencePolygon.java | 2 -- 7 files changed, 6 insertions(+), 10 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/BaseEventHandler.java b/src/org/traccar/BaseEventHandler.java index 78542b33a..d5755be89 100644 --- a/src/org/traccar/BaseEventHandler.java +++ b/src/org/traccar/BaseEventHandler.java @@ -31,7 +31,7 @@ public abstract class BaseEventHandler extends BaseDataHandler { } } - Collection events = analizePosition(position); + Collection events = analyzePosition(position); if (events != null) { for (Event event : events) { Context.getNotificationManager().updateEvent(event, position); @@ -40,6 +40,6 @@ public abstract class BaseEventHandler extends BaseDataHandler { return position; } - protected abstract Collection analizePosition(Position position); + protected abstract Collection analyzePosition(Position position); } diff --git a/src/org/traccar/events/CommandResultEventHandler.java b/src/org/traccar/events/CommandResultEventHandler.java index 9dbdb4b4c..8e19e9523 100644 --- a/src/org/traccar/events/CommandResultEventHandler.java +++ b/src/org/traccar/events/CommandResultEventHandler.java @@ -10,7 +10,7 @@ import org.traccar.model.Position; public class CommandResultEventHandler extends BaseEventHandler { @Override - protected Collection analizePosition(Position position) { + protected Collection analyzePosition(Position position) { Object commandResult = position.getAttributes().get(Position.KEY_RESULT); if (commandResult != null) { Collection events = new ArrayList<>(); diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java index 56029fced..e3598bd16 100644 --- a/src/org/traccar/events/GeofenceEventHandler.java +++ b/src/org/traccar/events/GeofenceEventHandler.java @@ -26,7 +26,7 @@ public class GeofenceEventHandler extends BaseEventHandler { } @Override - protected Collection analizePosition(Position position) { + protected Collection analyzePosition(Position position) { if (!isLastPosition() || !position.getValid()) { return null; } diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index 2ba5979e3..5b1020aec 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -21,7 +21,7 @@ public class MotionEventHandler extends BaseEventHandler { } @Override - protected Collection analizePosition(Position position) { + protected Collection analyzePosition(Position position) { Collection result = null; if (!isLastPosition()) { return null; diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index 152fe6f22..61089274d 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -22,7 +22,7 @@ public class OverspeedEventHandler extends BaseEventHandler { } @Override - protected Collection analizePosition(Position position) { + protected Collection analyzePosition(Position position) { Collection events = new ArrayList<>(); if (!isLastPosition()) { return null; diff --git a/src/org/traccar/geofence/GeofenceCircle.java b/src/org/traccar/geofence/GeofenceCircle.java index e1c8f8a02..f71497475 100644 --- a/src/org/traccar/geofence/GeofenceCircle.java +++ b/src/org/traccar/geofence/GeofenceCircle.java @@ -12,11 +12,9 @@ public class GeofenceCircle extends GeofenceGeometry { private double radius; public GeofenceCircle() { - super(); } public GeofenceCircle(String wkt) throws ParseException { - super(); fromWkt(wkt); } diff --git a/src/org/traccar/geofence/GeofencePolygon.java b/src/org/traccar/geofence/GeofencePolygon.java index 52920b7b1..970734214 100644 --- a/src/org/traccar/geofence/GeofencePolygon.java +++ b/src/org/traccar/geofence/GeofencePolygon.java @@ -6,11 +6,9 @@ import java.util.ArrayList; public class GeofencePolygon extends GeofenceGeometry { public GeofencePolygon() { - super(); } public GeofencePolygon(String wkt) throws ParseException { - super(); fromWkt(wkt); } -- cgit v1.2.3 From 3f0f6c1f6ec4665783ec62a65f8f30a33f1d2152 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 16 Jun 2016 10:20:44 +0500 Subject: Added/updated license headers --- src/org/traccar/BaseEventHandler.java | 15 +++++++++++++++ src/org/traccar/Context.java | 2 +- src/org/traccar/api/resource/DeviceGeofenceResource.java | 2 +- src/org/traccar/api/resource/DeviceResource.java | 2 +- src/org/traccar/api/resource/GeofenceResource.java | 2 +- src/org/traccar/api/resource/UserResource.java | 2 +- src/org/traccar/database/ConnectionManager.java | 2 +- src/org/traccar/database/GeofenceManager.java | 15 +++++++++++++++ src/org/traccar/database/NotificationManager.java | 15 +++++++++++++++ src/org/traccar/events/CommandResultEventHandler.java | 15 +++++++++++++++ src/org/traccar/events/GeofenceEventHandler.java | 15 +++++++++++++++ src/org/traccar/events/MotionEventHandler.java | 15 +++++++++++++++ src/org/traccar/events/OverspeedEventHandler.java | 15 +++++++++++++++ src/org/traccar/geofence/GeofenceCircle.java | 15 +++++++++++++++ src/org/traccar/geofence/GeofenceGeometry.java | 15 +++++++++++++++ src/org/traccar/geofence/GeofencePolygon.java | 15 +++++++++++++++ src/org/traccar/model/DeviceGeofence.java | 15 +++++++++++++++ src/org/traccar/model/Event.java | 15 +++++++++++++++ src/org/traccar/model/Extensible.java | 15 +++++++++++++++ src/org/traccar/model/Geofence.java | 15 +++++++++++++++ src/org/traccar/model/GeofencePermission.java | 15 +++++++++++++++ src/org/traccar/model/GroupGeofence.java | 15 +++++++++++++++ src/org/traccar/model/Message.java | 2 +- src/org/traccar/model/Position.java | 2 +- 24 files changed, 248 insertions(+), 8 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/BaseEventHandler.java b/src/org/traccar/BaseEventHandler.java index d5755be89..143c2dc2b 100644 --- a/src/org/traccar/BaseEventHandler.java +++ b/src/org/traccar/BaseEventHandler.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar; import java.util.Collection; diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 6f99ff97d..f40db72e5 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2015 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/org/traccar/api/resource/DeviceGeofenceResource.java b/src/org/traccar/api/resource/DeviceGeofenceResource.java index 99d64292b..27535617d 100644 --- a/src/org/traccar/api/resource/DeviceGeofenceResource.java +++ b/src/org/traccar/api/resource/DeviceGeofenceResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index fcbeed97b..d6032bb1e 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2015 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/org/traccar/api/resource/GeofenceResource.java b/src/org/traccar/api/resource/GeofenceResource.java index b56e20e08..9c80e61a1 100644 --- a/src/org/traccar/api/resource/GeofenceResource.java +++ b/src/org/traccar/api/resource/GeofenceResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index 7e503dcfb..7cc4ed09a 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2015 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index 4f12c44d4..8a1debdfa 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2015 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java index 0119a62ca..0a07e2cf3 100644 --- a/src/org/traccar/database/GeofenceManager.java +++ b/src/org/traccar/database/GeofenceManager.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.database; import java.sql.SQLException; diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 8c8e958c8..7593367a3 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.database; import java.sql.SQLException; diff --git a/src/org/traccar/events/CommandResultEventHandler.java b/src/org/traccar/events/CommandResultEventHandler.java index 8e19e9523..3f4ff521b 100644 --- a/src/org/traccar/events/CommandResultEventHandler.java +++ b/src/org/traccar/events/CommandResultEventHandler.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.events; import java.util.ArrayList; diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java index e3598bd16..7d24c4efe 100644 --- a/src/org/traccar/events/GeofenceEventHandler.java +++ b/src/org/traccar/events/GeofenceEventHandler.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.events; import java.sql.SQLException; diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index 5b1020aec..c05bd4843 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.events; import java.sql.SQLException; diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index 61089274d..f4676b995 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.events; import java.sql.SQLException; diff --git a/src/org/traccar/geofence/GeofenceCircle.java b/src/org/traccar/geofence/GeofenceCircle.java index f71497475..56230240b 100644 --- a/src/org/traccar/geofence/GeofenceCircle.java +++ b/src/org/traccar/geofence/GeofenceCircle.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.geofence; import java.text.DecimalFormat; diff --git a/src/org/traccar/geofence/GeofenceGeometry.java b/src/org/traccar/geofence/GeofenceGeometry.java index 83656a029..e717ede0b 100644 --- a/src/org/traccar/geofence/GeofenceGeometry.java +++ b/src/org/traccar/geofence/GeofenceGeometry.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.geofence; import java.text.ParseException; diff --git a/src/org/traccar/geofence/GeofencePolygon.java b/src/org/traccar/geofence/GeofencePolygon.java index 970734214..be5f971a5 100644 --- a/src/org/traccar/geofence/GeofencePolygon.java +++ b/src/org/traccar/geofence/GeofencePolygon.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.geofence; import java.text.ParseException; diff --git a/src/org/traccar/model/DeviceGeofence.java b/src/org/traccar/model/DeviceGeofence.java index f55c8ca69..05f06bb3d 100644 --- a/src/org/traccar/model/DeviceGeofence.java +++ b/src/org/traccar/model/DeviceGeofence.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.model; public class DeviceGeofence { diff --git a/src/org/traccar/model/Event.java b/src/org/traccar/model/Event.java index 863acd621..235a39f9a 100644 --- a/src/org/traccar/model/Event.java +++ b/src/org/traccar/model/Event.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.model; import java.util.Date; diff --git a/src/org/traccar/model/Extensible.java b/src/org/traccar/model/Extensible.java index b4052dbda..eceeccadf 100644 --- a/src/org/traccar/model/Extensible.java +++ b/src/org/traccar/model/Extensible.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.model; import java.util.LinkedHashMap; diff --git a/src/org/traccar/model/Geofence.java b/src/org/traccar/model/Geofence.java index 300d8fb74..9a60f784f 100644 --- a/src/org/traccar/model/Geofence.java +++ b/src/org/traccar/model/Geofence.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.model; import java.text.ParseException; diff --git a/src/org/traccar/model/GeofencePermission.java b/src/org/traccar/model/GeofencePermission.java index 38fe7b6c1..269918d66 100644 --- a/src/org/traccar/model/GeofencePermission.java +++ b/src/org/traccar/model/GeofencePermission.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.model; public class GeofencePermission { diff --git a/src/org/traccar/model/GroupGeofence.java b/src/org/traccar/model/GroupGeofence.java index a8f6bd475..0e261fd54 100644 --- a/src/org/traccar/model/GroupGeofence.java +++ b/src/org/traccar/model/GroupGeofence.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.model; public class GroupGeofence { diff --git a/src/org/traccar/model/Message.java b/src/org/traccar/model/Message.java index 5015b9339..55d9fd0c7 100644 --- a/src/org/traccar/model/Message.java +++ b/src/org/traccar/model/Message.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2013 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index b4079dae6..4e03b2097 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. -- cgit v1.2.3 From 1b8e05ce34ff7c576c29e383601f74cb8c01d728 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 16 Jun 2016 11:23:41 +0500 Subject: Moved variables from parent to inheritor --- src/org/traccar/BaseEventHandler.java | 21 --------------------- src/org/traccar/events/GeofenceEventHandler.java | 13 +++++++------ src/org/traccar/events/MotionEventHandler.java | 14 ++++++++------ src/org/traccar/events/OverspeedEventHandler.java | 11 +++++++++-- 4 files changed, 24 insertions(+), 35 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/BaseEventHandler.java b/src/org/traccar/BaseEventHandler.java index 143c2dc2b..1ae9d2c6d 100644 --- a/src/org/traccar/BaseEventHandler.java +++ b/src/org/traccar/BaseEventHandler.java @@ -17,35 +17,14 @@ package org.traccar; import java.util.Collection; -import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; public abstract class BaseEventHandler extends BaseDataHandler { - private boolean isLastPosition = false; - - public boolean isLastPosition() { - return isLastPosition; - } - - private Device device; - - public Device getDevice() { - return device; - } - @Override protected Position handlePosition(Position position) { - device = Context.getDataManager().getDeviceById(position.getDeviceId()); - if (device != null) { - long lastPositionId = device.getPositionId(); - if (position.getId() == lastPositionId) { - isLastPosition = true; - } - } - Collection events = analyzePosition(position); if (events != null) { for (Event event : events) { diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java index 7d24c4efe..e9a4a640f 100644 --- a/src/org/traccar/events/GeofenceEventHandler.java +++ b/src/org/traccar/events/GeofenceEventHandler.java @@ -25,6 +25,7 @@ import org.traccar.Context; import org.traccar.database.DataManager; import org.traccar.database.GeofenceManager; import org.traccar.helper.Log; +import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; @@ -42,24 +43,24 @@ public class GeofenceEventHandler extends BaseEventHandler { @Override protected Collection analyzePosition(Position position) { - if (!isLastPosition() || !position.getValid()) { + Device device = dataManager.getDeviceById(position.getDeviceId()); + if (device == null) { return null; } - - if (getDevice() == null) { + if (position.getId() != device.getPositionId() || !position.getValid()) { return null; } List currentGeofences = geofenceManager.getCurrentDeviceGeofences(position); List oldGeofences = new ArrayList(); - if (getDevice().getGeofenceIds() != null) { - oldGeofences.addAll(getDevice().getGeofenceIds()); + if (device.getGeofenceIds() != null) { + oldGeofences.addAll(device.getGeofenceIds()); } List newGeofences = new ArrayList(currentGeofences); newGeofences.removeAll(oldGeofences); oldGeofences.removeAll(currentGeofences); - getDevice().setGeofenceIds(currentGeofences); + device.setGeofenceIds(currentGeofences); Collection events = new ArrayList<>(); try { diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index c05bd4843..d10513d26 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -37,17 +37,19 @@ public class MotionEventHandler extends BaseEventHandler { @Override protected Collection analyzePosition(Position position) { - Collection result = null; - if (!isLastPosition()) { + + Device device = Context.getDataManager().getDeviceById(position.getDeviceId()); + if (device == null) { + return null; + } + if (position.getId() != device.getPositionId() || !position.getValid()) { return null; } + Collection result = null; double speed = position.getSpeed(); boolean valid = position.getValid(); - if (getDevice() == null) { - return null; - } - String motion = getDevice().getMotion(); + String motion = device.getMotion(); if (motion == null) { motion = Device.STATUS_STOPPED; } diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index f4676b995..e14d4bcea 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -21,6 +21,7 @@ import java.util.Collection; import org.traccar.BaseEventHandler; import org.traccar.Context; +import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.helper.Log; @@ -38,10 +39,16 @@ public class OverspeedEventHandler extends BaseEventHandler { @Override protected Collection analyzePosition(Position position) { - Collection events = new ArrayList<>(); - if (!isLastPosition()) { + + Device device = Context.getDataManager().getDeviceById(position.getDeviceId()); + if (device == null) { + return null; + } + if (position.getId() != device.getPositionId() || !position.getValid()) { return null; } + + Collection events = new ArrayList<>(); double speed = position.getSpeed(); boolean valid = position.getValid(); -- cgit v1.2.3 From 950979e7dc955aace97c496bba9f374c5756c9b1 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 16 Jun 2016 11:51:32 +0500 Subject: Missed super() --- src/org/traccar/geofence/GeofenceCircle.java | 1 - 1 file changed, 1 deletion(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/geofence/GeofenceCircle.java b/src/org/traccar/geofence/GeofenceCircle.java index 56230240b..28b55f59e 100644 --- a/src/org/traccar/geofence/GeofenceCircle.java +++ b/src/org/traccar/geofence/GeofenceCircle.java @@ -34,7 +34,6 @@ public class GeofenceCircle extends GeofenceGeometry { } public GeofenceCircle(double latitude, double longitude, double radius) { - super(); this.centerLatitude = latitude; this.centerLongitude = longitude; this.radius = radius; -- cgit v1.2.3 From 82a78ff77a076231a8429f0dd84678d61c31d44a Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 16 Jun 2016 11:56:49 +0500 Subject: Variable naming fix --- src/org/traccar/geofence/GeofenceCircle.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/geofence/GeofenceCircle.java b/src/org/traccar/geofence/GeofenceCircle.java index 28b55f59e..a36620aec 100644 --- a/src/org/traccar/geofence/GeofenceCircle.java +++ b/src/org/traccar/geofence/GeofenceCircle.java @@ -67,11 +67,11 @@ public class GeofenceCircle extends GeofenceGeometry { if (content == null || content.equals("")) { throw new ParseException("No content", 0); } - String[] commatokens = content.split(","); - if (commatokens.length != 2) { + String[] commaTokens = content.split(","); + if (commaTokens.length != 2) { throw new ParseException("Not valid content", 0); } - String[] tokens = commatokens[0].split("\\s"); + String[] tokens = commaTokens[0].split("\\s"); if (tokens.length != 2) { throw new ParseException("Too much or less coordinates", 0); } @@ -86,9 +86,9 @@ public class GeofenceCircle extends GeofenceGeometry { throw new ParseException(tokens[1] + " is not a double", 0); } try { - radius = Double.parseDouble(commatokens[1]); + radius = Double.parseDouble(commaTokens[1]); } catch (NumberFormatException e) { - throw new ParseException(commatokens[1] + " is not a double", 0); + throw new ParseException(commaTokens[1] + " is not a double", 0); } } } -- cgit v1.2.3