From c6cc8a87eb593dbf765814d1aaa09d5d231fa7fe Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 26 May 2016 16:14:52 +0500 Subject: Events subsystem --- src/org/traccar/BaseEventHandler.java | 31 +++++++++ src/org/traccar/BasePipelineFactory.java | 33 ++++++++- src/org/traccar/api/AsyncSocket.java | 7 ++ src/org/traccar/api/resource/EventResource.java | 39 +++++++++++ src/org/traccar/database/ConnectionManager.java | 42 ++++++++++-- src/org/traccar/database/DataManager.java | 31 +++++++++ .../traccar/events/CommandResultEventHandler.java | 18 +++++ src/org/traccar/events/MotionEventHandler.java | 55 +++++++++++++++ src/org/traccar/events/OverspeedEventHandler.java | 79 ++++++++++++++++++++++ src/org/traccar/model/Device.java | 13 ++++ src/org/traccar/model/Event.java | 74 ++++++++++++++++++++ src/org/traccar/web/WebServer.java | 4 +- 12 files changed, 418 insertions(+), 8 deletions(-) create mode 100644 src/org/traccar/BaseEventHandler.java create mode 100644 src/org/traccar/api/resource/EventResource.java create mode 100644 src/org/traccar/events/CommandResultEventHandler.java create mode 100644 src/org/traccar/events/MotionEventHandler.java create mode 100644 src/org/traccar/events/OverspeedEventHandler.java create mode 100644 src/org/traccar/model/Event.java (limited to 'src/org/traccar') diff --git a/src/org/traccar/BaseEventHandler.java b/src/org/traccar/BaseEventHandler.java new file mode 100644 index 000000000..3c2016af0 --- /dev/null +++ b/src/org/traccar/BaseEventHandler.java @@ -0,0 +1,31 @@ +package org.traccar; + +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; + } + + @Override + protected Position handlePosition(Position position) { + + Position lastPosition = Context.getConnectionManager().getLastPosition(position.getDeviceId()); + if (lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) >= 0) { + isLastPosition = true; + } + + Event event = analizePosition(position); + if (event != null) { + Context.getConnectionManager().updateEvent(event); + } + return position; + } + + protected abstract Event analizePosition(Position position); + +} diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 6e350f61d..55820deb7 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -29,6 +29,9 @@ import org.jboss.netty.channel.MessageEvent; 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.MotionEventHandler; +import org.traccar.events.OverspeedEventHandler; import org.traccar.helper.Log; import java.net.InetSocketAddress; @@ -44,6 +47,10 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { private LocationProviderHandler locationProviderHandler; private HemisphereHandler hemisphereHandler; + private CommandResultEventHandler commandResultEventHandler; + private OverspeedEventHandler overspeedEventHandler; + private MotionEventHandler motionEventHandler; + private static final class OpenChannelHandler extends SimpleChannelHandler { private final TrackerServer server; @@ -122,6 +129,17 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { || Context.getConfig().hasKey("location.longitudeHemisphere")) { hemisphereHandler = new HemisphereHandler(); } + + commandResultEventHandler = new CommandResultEventHandler(); + + if (Context.getConfig().getBoolean("event.overspeedhandler")) { + overspeedEventHandler = new OverspeedEventHandler(); + } + + if (Context.getConfig().getBoolean("event.motionhandler")) { + motionEventHandler = new MotionEventHandler(); + } + } protected abstract void addSpecificHandlers(ChannelPipeline pipeline); @@ -162,9 +180,23 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { if (Context.getDataManager() != null) { pipeline.addLast("dataHandler", new DefaultDataHandler()); } + if (Context.getConfig().getBoolean("forward.enable")) { pipeline.addLast("webHandler", new WebDataHandler(Context.getConfig().getString("forward.url"))); } + + if (commandResultEventHandler != null) { + pipeline.addLast("CommandResultEventHandler", commandResultEventHandler); + } + + if (overspeedEventHandler != null) { + pipeline.addLast("OverspeedEventHandler", overspeedEventHandler); + } + + if (motionEventHandler != null) { + pipeline.addLast("MotionEventHandler", motionEventHandler); + } + pipeline.addLast("mainHandler", new MainEventHandler()); return pipeline; } @@ -181,5 +213,4 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { } } } - } diff --git a/src/org/traccar/api/AsyncSocket.java b/src/org/traccar/api/AsyncSocket.java index 35e760613..2259f840f 100644 --- a/src/org/traccar/api/AsyncSocket.java +++ b/src/org/traccar/api/AsyncSocket.java @@ -20,6 +20,7 @@ import org.eclipse.jetty.websocket.api.WebSocketAdapter; import org.traccar.Context; import org.traccar.database.ConnectionManager; import org.traccar.model.Device; +import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.web.JsonConverter; @@ -32,6 +33,7 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U private static final String KEY_DEVICES = "devices"; private static final String KEY_POSITIONS = "positions"; + private static final String KEY_EVENTS = "events"; private long userId; @@ -65,6 +67,11 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U sendData(KEY_POSITIONS, Collections.singletonList(position)); } + @Override + public void onUpdateEvent(Event event) { + sendData(KEY_EVENTS, Collections.singletonList(event)); + } + private void sendData(String key, Collection data) { if (!data.isEmpty() && isConnected()) { JsonObjectBuilder json = Json.createObjectBuilder(); diff --git a/src/org/traccar/api/resource/EventResource.java b/src/org/traccar/api/resource/EventResource.java new file mode 100644 index 000000000..e0ad34728 --- /dev/null +++ b/src/org/traccar/api/resource/EventResource.java @@ -0,0 +1,39 @@ +package org.traccar.api.resource; + +import java.sql.SQLException; +import java.util.Collection; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +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 org.traccar.Context; +import org.traccar.api.BaseResource; +import org.traccar.model.Event; + +@Path("events") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) + +public class EventResource extends BaseResource { + + @Path("{id}") + @GET + public Event get(@PathParam("id") long id) throws SQLException { + Event event = Context.getDataManager().getEvent(id); + Context.getPermissionsManager().checkDevice(getUserId(), event.getDeviceId()); + return event; + } + + @GET + public Collection get( + @QueryParam("deviceId") long deviceId, @QueryParam("type") String type, + @QueryParam("interval") long interval) throws SQLException { + Context.getPermissionsManager().checkDevice(getUserId(), deviceId); + return Context.getDataManager().getLastEvents(deviceId, type, interval); + } +} diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index bb9958232..d6728357c 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -23,6 +23,7 @@ import org.traccar.GlobalTimer; import org.traccar.Protocol; import org.traccar.helper.Log; import org.traccar.model.Device; +import org.traccar.model.Event; import org.traccar.model.Position; import java.net.SocketAddress; @@ -85,14 +86,26 @@ public class ConnectionManager { return; } - device.setStatus(status); - if (time != null) { - device.setLastUpdate(time); + if (status.equals(Device.STATUS_MOVING) || status.equals(Device.STATUS_STOPPED)) { + device.setMotion(status); + } else { + if (!status.equals(device.getStatus())) { + Event event = new Event(Event.DEVICE_OFFLINE, deviceId); + if (status.equals(Device.STATUS_ONLINE)) { + event.setType(Event.DEVICE_ONLINE); + } + updateEvent(event); + } + device.setStatus(status); + + Timeout timeout = timeouts.remove(deviceId); + if (timeout != null) { + timeout.cancel(); + } } - Timeout timeout = timeouts.remove(deviceId); - if (timeout != null) { - timeout.cancel(); + if (time != null) { + device.setLastUpdate(time); } if (status.equals(Device.STATUS_ONLINE)) { @@ -134,6 +147,22 @@ public class ConnectionManager { } } + public synchronized void updateEvent(Event event) { + 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)) { + for (UpdateListener listener : listeners.get(userId)) { + listener.onUpdateEvent(event); + } + } + } + } + public Position getLastPosition(long deviceId) { return positions.get(deviceId); } @@ -154,6 +183,7 @@ public class ConnectionManager { public interface UpdateListener { void onUpdateDevice(Device device); void onUpdatePosition(Position position); + void onUpdateEvent(Event event); } public synchronized void addListener(long userId, UpdateListener listener) { diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index ac3be45ac..4e14f6c97 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -27,6 +27,7 @@ import org.traccar.Config; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; +import org.traccar.model.Event; import org.traccar.model.DevicePermission; import org.traccar.model.Group; import org.traccar.model.GroupPermission; @@ -482,4 +483,34 @@ public class DataManager implements IdentityManager { .setObject(server) .executeUpdate(); } + + public Event getEvent(long eventId) throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectEvent")) + .setLong("id", eventId) + .executeQuerySingle(Event.class); + } + + public void addEvent(Event event) throws SQLException { + event.setId(QueryBuilder.create(dataSource, getQuery("database.insertEvent"), true) + .setObject(event) + .executeUpdate()); + } + + public Collection getEvents(long deviceId, String type, Date from, Date to) throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectEvents")) + .setLong("deviceId", deviceId) + .setString("type", type) + .setDate("from", from) + .setDate("to", to) + .executeQuery(Event.class); + } + + public Collection getLastEvents(long deviceId, String type, long interval) throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectLastEvents")) + .setLong("deviceId", deviceId) + .setString("type", type) + .setLong("interval", interval) + .executeQuery(Event.class); + } + } diff --git a/src/org/traccar/events/CommandResultEventHandler.java b/src/org/traccar/events/CommandResultEventHandler.java new file mode 100644 index 000000000..8ef015841 --- /dev/null +++ b/src/org/traccar/events/CommandResultEventHandler.java @@ -0,0 +1,18 @@ +package org.traccar.events; + +import org.traccar.BaseEventHandler; +import org.traccar.model.Event; +import org.traccar.model.Position; + +public class CommandResultEventHandler extends BaseEventHandler { + + @Override + protected Event analizePosition(Position position) { + Object cmdResult = position.getAttributes().get(Position.KEY_RESULT); + if (cmdResult != null) { + return new Event(Event.COMMAND_RESULT, position.getDeviceId(), position.getId()); + } + return null; + } + +} diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java new file mode 100644 index 000000000..d4dd12a39 --- /dev/null +++ b/src/org/traccar/events/MotionEventHandler.java @@ -0,0 +1,55 @@ +package org.traccar.events; + +import java.sql.SQLException; + +import org.traccar.BaseEventHandler; +import org.traccar.Context; +import org.traccar.helper.Log; +import org.traccar.model.Device; +import org.traccar.model.Event; +import org.traccar.model.Position; + +public class MotionEventHandler extends BaseEventHandler { + + private static final double SPEED_THRESHOLD = 0.01; + private long suppressRepeated; + + public MotionEventHandler() { + suppressRepeated = Context.getConfig().getLong("event.suppressrepeated", 60); + } + + @Override + protected Event analizePosition(Position position) { + Event event = null; + + if (!isLastPosition()) { + return event; + } + + double speed = position.getSpeed(); + boolean valid = position.getValid(); + Device device = Context.getIdentityManager().getDeviceById(position.getDeviceId()); + if (device == null) { + return event; + } + String motion = device.getMotion(); + if (valid && speed > SPEED_THRESHOLD && !motion.equals(Device.STATUS_MOVING)) { + Context.getConnectionManager().updateDevice(position.getDeviceId(), Device.STATUS_MOVING, null); + event = new Event(Event.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.DEVICE_STOPPED, position.getDeviceId(), position.getId()); + } + try { + if (event != null && !Context.getDataManager().getLastEvents( + position.getDeviceId(), event.getType(), suppressRepeated).isEmpty()) { + event = null; + } + + } catch (SQLException error) { + Log.warning(error); + } + return event; + } + +} diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java new file mode 100644 index 000000000..706e9b905 --- /dev/null +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -0,0 +1,79 @@ +package org.traccar.events; + +import java.sql.SQLException; + +import org.traccar.BaseEventHandler; +import org.traccar.Context; +import org.traccar.model.Event; +import org.traccar.model.Position; +import org.traccar.model.Server; +import org.traccar.helper.Log; +//import org.traccar.model.Device; +//import org.traccar.model.Group; +import org.traccar.helper.UnitsConverter; + +public class OverspeedEventHandler extends BaseEventHandler { + + private double globalSpeedLimit; + private long suppressRepeated; + + public OverspeedEventHandler() { + globalSpeedLimit = Context.getConfig().getInteger("event.globalspeedlimit", 0); + suppressRepeated = Context.getConfig().getLong("event.suppressrepeated", 60); + try { + Server server = Context.getDataManager().getServer(); + String speedUnit = server.getSpeedUnit(); + if (speedUnit != null) { + switch (speedUnit) { + case "kmh" : globalSpeedLimit = UnitsConverter.knotsFromKph(globalSpeedLimit); + break; + case "mph" : globalSpeedLimit = UnitsConverter.knotsFromMph(globalSpeedLimit); + default : break; + } + } + } catch (SQLException error) { + Log.warning(error); + } + + } + + @Override + protected Event analizePosition(Position position) { + Event event = null; + if (!isLastPosition()) { + return event; + } + double speed = position.getSpeed(); + boolean valid = position.getValid(); + double deviceSpeedLimit = 0; + double groupSpeedLimit = 0; +// Device device = Context.getIdentityManager().getDeviceById(position.getDeviceId()); +// if (device != null) { +// deviceSpeedLimit = device.getSpeedLimit(); +// try { +// Group group = Context.getDataManager().getGroupById(device.getGroupId()); +// if (group != null) { +// groupSpeedLimit = group.getSpeedLimit(); +// } +// } catch (SQLException error) { +// Log.warning(error); +// } +// } + + if (valid && globalSpeedLimit != 0 && speed > globalSpeedLimit + || valid && groupSpeedLimit != 0 && speed > groupSpeedLimit + || valid && deviceSpeedLimit != 0 && speed > deviceSpeedLimit) { + try { + if (Context.getDataManager().getLastEvents( + position.getDeviceId(), Event.DEVICE_OVERSPEED, suppressRepeated).isEmpty()) { + event = new Event(Event.DEVICE_OVERSPEED, position.getDeviceId(), position.getId()); + } + } catch (SQLException error) { + Log.warning(error); + } + + } + return event; + } + +} diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java index 934e753b1..d32f9f851 100644 --- a/src/org/traccar/model/Device.java +++ b/src/org/traccar/model/Device.java @@ -101,4 +101,17 @@ public class Device { this.groupId = groupId; } + public static final String STATUS_MOVING = "moving"; + public static final String STATUS_STOPPED = "stopped"; + + private String motion; + + public String getMotion() { + return motion; + } + + public void setMotion(String motion) { + this.motion = motion; + } + } diff --git a/src/org/traccar/model/Event.java b/src/org/traccar/model/Event.java new file mode 100644 index 000000000..27c750c49 --- /dev/null +++ b/src/org/traccar/model/Event.java @@ -0,0 +1,74 @@ +package org.traccar.model; + +import java.util.Date; + +public class Event extends Message { + + public Event(String type, long deviceId, long positionId) { + this.setType(type); + this.setDeviceId(deviceId); + this.setPositionId(positionId); + this.eventTime = new Date(); + } + + public Event(String type, long deviceId) { + this.setType(type); + this.setDeviceId(deviceId); + this.eventTime = new Date(); + } + + public Event() { + } + + private long id; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public static final String COMMAND_RESULT = "command-result"; + + public static final String DEVICE_ONLINE = "device-online"; + public static final String DEVICE_OFFLINE = "device-offline"; + + public static final String DEVICE_MOVING = "device-moving"; + public static final String DEVICE_STOPPED = "device-stopped"; + + public static final String DEVICE_OVERSPEED = "device-overspeed"; + + public static final String GEOFENCE_ENTER = "geofence-enter"; + public static final String GEOFENCE_EXIT = "geofence-exit"; + + private Date eventTime; + + public Date getEventTime() { + if (eventTime != null) { + return new Date(eventTime.getTime()); + } else { + return null; + } + } + + public void setEventTime(Date eventTime) { + if (eventTime != null) { + this.eventTime = new Date(eventTime.getTime()); + } else { + this.eventTime = null; + } + } + + private long positionId; + + public long getPositionId() { + return positionId; + } + + public void setPositionId(long positionId) { + this.positionId = positionId; + } + +} diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index 8144af0b6..751db7a33 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -44,6 +44,7 @@ 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.EventResource; import org.traccar.helper.Log; import javax.naming.InitialContext; @@ -148,7 +149,8 @@ public class WebServer { resourceConfig.register(CorsResponseFilter.class); resourceConfig.registerClasses(ServerResource.class, SessionResource.class, CommandResource.class, GroupPermissionResource.class, DevicePermissionResource.class, UserResource.class, - GroupResource.class, DeviceResource.class, PositionResource.class, CommandTypeResource.class); + GroupResource.class, DeviceResource.class, PositionResource.class, + CommandTypeResource.class, EventResource.class); servletHandler.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*"); handlers.addHandler(servletHandler); -- cgit v1.2.3 From 6e00239038002aa383cce39be7f53b85f3d03a94 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 27 May 2016 17:14:47 +0500 Subject: - Synchronization device cache on updateLatestPosition and updateDeviceStatus - Better latest position identification in BaseEventHandler --- src/org/traccar/BaseEventHandler.java | 10 +++++++--- src/org/traccar/database/DataManager.java | 5 +++++ 2 files changed, 12 insertions(+), 3 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/BaseEventHandler.java b/src/org/traccar/BaseEventHandler.java index 3c2016af0..fde57748c 100644 --- a/src/org/traccar/BaseEventHandler.java +++ b/src/org/traccar/BaseEventHandler.java @@ -1,5 +1,6 @@ package org.traccar; +import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; @@ -14,9 +15,12 @@ public abstract class BaseEventHandler extends BaseDataHandler { @Override protected Position handlePosition(Position position) { - Position lastPosition = Context.getConnectionManager().getLastPosition(position.getDeviceId()); - if (lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) >= 0) { - isLastPosition = true; + Device device = Context.getDataManager().getDeviceById(position.getDeviceId()); + if (device != null) { + long lastPositionId = device.getPositionId(); + if (position.getId() == lastPositionId) { + isLastPosition = true; + } } Event event = analizePosition(position); diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 4e14f6c97..fcb8899bc 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -358,6 +358,9 @@ public class DataManager implements IdentityManager { QueryBuilder.create(dataSource, getQuery("database.updateDeviceStatus")) .setObject(device) .executeUpdate(); + Device cachedDevice = getDeviceById(device.getId()); + cachedDevice.setStatus(device.getStatus()); + cachedDevice.setMotion(device.getMotion()); } public void removeDevice(long deviceId) throws SQLException { @@ -466,6 +469,8 @@ public class DataManager implements IdentityManager { .setDate("now", new Date()) .setObject(position) .executeUpdate(); + Device device = getDeviceById(position.getDeviceId()); + device.setPositionId(position.getId()); } public Collection getLatestPositions() throws SQLException { -- cgit v1.2.3 From 80119f59a0d4b89396f5d7d57ba2d80257a8da79 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 30 May 2016 12:07:43 +0500 Subject: - Style fixes (use lower camel case in all cases) - Added TYPE_prefix for event.type strings - Removed commented code - Removed "database.selectLastEvents" because it is not cross-database. Do the same via code. --- setup/unix/traccar.xml | 12 ++---- setup/windows/traccar.xml | 12 ++---- src/org/traccar/BasePipelineFactory.java | 4 +- src/org/traccar/api/resource/EventResource.java | 2 +- src/org/traccar/database/ConnectionManager.java | 4 +- src/org/traccar/database/DataManager.java | 12 +++--- .../traccar/events/CommandResultEventHandler.java | 6 +-- src/org/traccar/events/MotionEventHandler.java | 8 ++-- src/org/traccar/events/OverspeedEventHandler.java | 46 +++------------------- src/org/traccar/model/Event.java | 16 ++++---- 10 files changed, 40 insertions(+), 82 deletions(-) (limited to 'src/org/traccar') diff --git a/setup/unix/traccar.xml b/setup/unix/traccar.xml index a27c9790b..22b71e204 100644 --- a/setup/unix/traccar.xml +++ b/setup/unix/traccar.xml @@ -17,12 +17,12 @@ all /opt/traccar/logs/tracker-server.log - 60 + 60 - true - 90 + true + 90 - true + true @@ -185,10 +185,6 @@ SELECT * FROM events WHERE deviceId = :deviceId AND type LIKE :type AND eventTime BETWEEN :from AND :to ORDER BY eventTime DESC; - - SELECT * FROM events WHERE deviceId = :deviceId AND type LIKE :type AND eventTime >= NOW() - INTERVAL :interval SECOND ORDER BY eventTime DESC; - - 5001 diff --git a/setup/windows/traccar.xml b/setup/windows/traccar.xml index d60640375..649c3bb15 100644 --- a/setup/windows/traccar.xml +++ b/setup/windows/traccar.xml @@ -17,12 +17,12 @@ all [LOG] - 60 + 60 - true - 90 + true + 90 - true + true @@ -185,10 +185,6 @@ SELECT * FROM events WHERE deviceId = :deviceId AND type LIKE :type AND eventTime BETWEEN :from AND :to ORDER BY eventTime DESC; - - SELECT * FROM events WHERE deviceId = :deviceId AND type LIKE :type AND eventTime >= NOW() - INTERVAL :interval SECOND ORDER BY eventTime DESC; - - 5001 diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 55820deb7..634c6d6a4 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -132,11 +132,11 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { commandResultEventHandler = new CommandResultEventHandler(); - if (Context.getConfig().getBoolean("event.overspeedhandler")) { + if (Context.getConfig().getBoolean("event.overspeedHandler")) { overspeedEventHandler = new OverspeedEventHandler(); } - if (Context.getConfig().getBoolean("event.motionhandler")) { + if (Context.getConfig().getBoolean("event.motionHandler")) { motionEventHandler = new MotionEventHandler(); } diff --git a/src/org/traccar/api/resource/EventResource.java b/src/org/traccar/api/resource/EventResource.java index e0ad34728..4ea3e3b49 100644 --- a/src/org/traccar/api/resource/EventResource.java +++ b/src/org/traccar/api/resource/EventResource.java @@ -32,7 +32,7 @@ public class EventResource extends BaseResource { @GET public Collection get( @QueryParam("deviceId") long deviceId, @QueryParam("type") String type, - @QueryParam("interval") long interval) throws SQLException { + @QueryParam("interval") int interval) throws SQLException { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); return Context.getDataManager().getLastEvents(deviceId, type, interval); } diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index d6728357c..9e0da8485 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -90,9 +90,9 @@ public class ConnectionManager { device.setMotion(status); } else { if (!status.equals(device.getStatus())) { - Event event = new Event(Event.DEVICE_OFFLINE, deviceId); + Event event = new Event(Event.TYPE_DEVICE_OFFLINE, deviceId); if (status.equals(Device.STATUS_ONLINE)) { - event.setType(Event.DEVICE_ONLINE); + event.setType(Event.TYPE_DEVICE_ONLINE); } updateEvent(event); } diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index fcb8899bc..dffb8937c 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -43,6 +43,7 @@ import java.net.URL; import java.net.URLClassLoader; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Calendar; import java.util.Collection; import java.util.Date; import java.util.HashMap; @@ -510,12 +511,11 @@ public class DataManager implements IdentityManager { .executeQuery(Event.class); } - public Collection getLastEvents(long deviceId, String type, long interval) throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectLastEvents")) - .setLong("deviceId", deviceId) - .setString("type", type) - .setLong("interval", interval) - .executeQuery(Event.class); + 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); } } diff --git a/src/org/traccar/events/CommandResultEventHandler.java b/src/org/traccar/events/CommandResultEventHandler.java index 8ef015841..23c62566a 100644 --- a/src/org/traccar/events/CommandResultEventHandler.java +++ b/src/org/traccar/events/CommandResultEventHandler.java @@ -8,9 +8,9 @@ public class CommandResultEventHandler extends BaseEventHandler { @Override protected Event analizePosition(Position position) { - Object cmdResult = position.getAttributes().get(Position.KEY_RESULT); - if (cmdResult != null) { - return new Event(Event.COMMAND_RESULT, position.getDeviceId(), position.getId()); + Object commandResult = position.getAttributes().get(Position.KEY_RESULT); + if (commandResult != null) { + return new Event(Event.TYPE_COMMAND_RESULT, position.getDeviceId(), position.getId()); } return null; } diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index d4dd12a39..a3b81ddc4 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -12,10 +12,10 @@ import org.traccar.model.Position; public class MotionEventHandler extends BaseEventHandler { private static final double SPEED_THRESHOLD = 0.01; - private long suppressRepeated; + private int suppressRepeated; public MotionEventHandler() { - suppressRepeated = Context.getConfig().getLong("event.suppressrepeated", 60); + suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60); } @Override @@ -35,10 +35,10 @@ public class MotionEventHandler extends BaseEventHandler { String motion = device.getMotion(); if (valid && speed > SPEED_THRESHOLD && !motion.equals(Device.STATUS_MOVING)) { Context.getConnectionManager().updateDevice(position.getDeviceId(), Device.STATUS_MOVING, null); - event = new Event(Event.DEVICE_MOVING, position.getDeviceId(), position.getId()); + event = 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.DEVICE_STOPPED, position.getDeviceId(), position.getId()); + event = new Event(Event.TYPE_DEVICE_STOPPED, position.getDeviceId(), position.getId()); } try { if (event != null && !Context.getDataManager().getLastEvents( diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index 706e9b905..30410ff32 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -6,35 +6,17 @@ import org.traccar.BaseEventHandler; import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; -import org.traccar.model.Server; import org.traccar.helper.Log; -//import org.traccar.model.Device; -//import org.traccar.model.Group; import org.traccar.helper.UnitsConverter; public class OverspeedEventHandler extends BaseEventHandler { private double globalSpeedLimit; - private long suppressRepeated; + private int suppressRepeated; public OverspeedEventHandler() { - globalSpeedLimit = Context.getConfig().getInteger("event.globalspeedlimit", 0); - suppressRepeated = Context.getConfig().getLong("event.suppressrepeated", 60); - try { - Server server = Context.getDataManager().getServer(); - String speedUnit = server.getSpeedUnit(); - if (speedUnit != null) { - switch (speedUnit) { - case "kmh" : globalSpeedLimit = UnitsConverter.knotsFromKph(globalSpeedLimit); - break; - case "mph" : globalSpeedLimit = UnitsConverter.knotsFromMph(globalSpeedLimit); - default : break; - } - } - } catch (SQLException error) { - Log.warning(error); - } - + globalSpeedLimit = UnitsConverter.knotsFromKph(Context.getConfig().getInteger("event.globalSpeedLimit", 0)); + suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60); } @Override @@ -45,28 +27,12 @@ public class OverspeedEventHandler extends BaseEventHandler { } double speed = position.getSpeed(); boolean valid = position.getValid(); - double deviceSpeedLimit = 0; - double groupSpeedLimit = 0; -// Device device = Context.getIdentityManager().getDeviceById(position.getDeviceId()); -// if (device != null) { -// deviceSpeedLimit = device.getSpeedLimit(); -// try { -// Group group = Context.getDataManager().getGroupById(device.getGroupId()); -// if (group != null) { -// groupSpeedLimit = group.getSpeedLimit(); -// } -// } catch (SQLException error) { -// Log.warning(error); -// } -// } - if (valid && globalSpeedLimit != 0 && speed > globalSpeedLimit - || valid && groupSpeedLimit != 0 && speed > groupSpeedLimit - || valid && deviceSpeedLimit != 0 && speed > deviceSpeedLimit) { + if (valid && globalSpeedLimit != 0 && speed > globalSpeedLimit) { try { if (Context.getDataManager().getLastEvents( - position.getDeviceId(), Event.DEVICE_OVERSPEED, suppressRepeated).isEmpty()) { - event = new Event(Event.DEVICE_OVERSPEED, position.getDeviceId(), position.getId()); + position.getDeviceId(), Event.TYPE_DEVICE_OVERSPEED, suppressRepeated).isEmpty()) { + event = new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId()); } } catch (SQLException error) { Log.warning(error); diff --git a/src/org/traccar/model/Event.java b/src/org/traccar/model/Event.java index 27c750c49..75049c433 100644 --- a/src/org/traccar/model/Event.java +++ b/src/org/traccar/model/Event.java @@ -30,18 +30,18 @@ public class Event extends Message { this.id = id; } - public static final String COMMAND_RESULT = "command-result"; + public static final String TYPE_COMMAND_RESULT = "commandResult"; - public static final String DEVICE_ONLINE = "device-online"; - public static final String DEVICE_OFFLINE = "device-offline"; + public static final String TYPE_DEVICE_ONLINE = "deviceOnline"; + public static final String TYPE_DEVICE_OFFLINE = "deviceOffline"; - public static final String DEVICE_MOVING = "device-moving"; - public static final String DEVICE_STOPPED = "device-stopped"; + public static final String TYPE_DEVICE_MOVING = "deviceMoving"; + public static final String TYPE_DEVICE_STOPPED = "deviceStopped"; - public static final String DEVICE_OVERSPEED = "device-overspeed"; + public static final String TYPE_DEVICE_OVERSPEED = "deviceOverspeed"; - public static final String GEOFENCE_ENTER = "geofence-enter"; - public static final String GEOFENCE_EXIT = "geofence-exit"; + public static final String TYPE_GEOFENCE_ENTER = "geofenceEnter"; + public static final String TYPE_GEOFENCE_EXIT = "geofenceExit"; private Date eventTime; -- cgit v1.2.3 From 41c46575a92f746622c61b07004b7a0832712a54 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 31 May 2016 08:56:10 +0500 Subject: - Renamed eventTime to serverTime - Fixed bracket indentation --- database/changelog-3.6.xml | 2 +- setup/unix/traccar.xml | 6 +++--- setup/windows/traccar.xml | 6 +++--- src/org/traccar/api/resource/EventResource.java | 2 +- src/org/traccar/model/Event.java | 20 ++++++++++---------- 5 files changed, 18 insertions(+), 18 deletions(-) (limited to 'src/org/traccar') diff --git a/database/changelog-3.6.xml b/database/changelog-3.6.xml index f61755f36..378ec741f 100644 --- a/database/changelog-3.6.xml +++ b/database/changelog-3.6.xml @@ -14,7 +14,7 @@ - + diff --git a/setup/unix/traccar.xml b/setup/unix/traccar.xml index 22b71e204..a0233e601 100644 --- a/setup/unix/traccar.xml +++ b/setup/unix/traccar.xml @@ -177,12 +177,12 @@ - INSERT INTO events (type, eventTime, deviceId, positionId, attributes) - VALUES (:type, :eventTime, :deviceId, :positionId, :attributes); + INSERT INTO events (type, serverTime, deviceId, positionId, attributes) + VALUES (:type, :serverTime, :deviceId, :positionId, :attributes); - SELECT * FROM events WHERE deviceId = :deviceId AND type LIKE :type AND eventTime BETWEEN :from AND :to ORDER BY eventTime DESC; + SELECT * FROM events WHERE deviceId = :deviceId AND type LIKE :type AND serverTime BETWEEN :from AND :to ORDER BY serverTime DESC; diff --git a/setup/windows/traccar.xml b/setup/windows/traccar.xml index 649c3bb15..5748f3993 100644 --- a/setup/windows/traccar.xml +++ b/setup/windows/traccar.xml @@ -177,12 +177,12 @@ - INSERT INTO events (type, eventTime, deviceId, positionId, attributes) - VALUES (:type, :eventTime, :deviceId, :positionId, :attributes); + INSERT INTO events (type, serverTime, deviceId, positionId, attributes) + VALUES (:type, :serverTime, :deviceId, :positionId, :attributes); - SELECT * FROM events WHERE deviceId = :deviceId AND type LIKE :type AND eventTime BETWEEN :from AND :to ORDER BY eventTime DESC; + SELECT * FROM events WHERE deviceId = :deviceId AND type LIKE :type AND serverTime BETWEEN :from AND :to ORDER BY serverTime DESC; diff --git a/src/org/traccar/api/resource/EventResource.java b/src/org/traccar/api/resource/EventResource.java index 4ea3e3b49..74a748ea5 100644 --- a/src/org/traccar/api/resource/EventResource.java +++ b/src/org/traccar/api/resource/EventResource.java @@ -35,5 +35,5 @@ public class EventResource extends BaseResource { @QueryParam("interval") int interval) throws SQLException { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); return Context.getDataManager().getLastEvents(deviceId, type, interval); - } + } } diff --git a/src/org/traccar/model/Event.java b/src/org/traccar/model/Event.java index 75049c433..6de885c70 100644 --- a/src/org/traccar/model/Event.java +++ b/src/org/traccar/model/Event.java @@ -8,13 +8,13 @@ public class Event extends Message { this.setType(type); this.setDeviceId(deviceId); this.setPositionId(positionId); - this.eventTime = new Date(); + this.serverTime = new Date(); } public Event(String type, long deviceId) { this.setType(type); this.setDeviceId(deviceId); - this.eventTime = new Date(); + this.serverTime = new Date(); } public Event() { @@ -43,21 +43,21 @@ public class Event extends Message { public static final String TYPE_GEOFENCE_ENTER = "geofenceEnter"; public static final String TYPE_GEOFENCE_EXIT = "geofenceExit"; - private Date eventTime; + private Date serverTime; - public Date getEventTime() { - if (eventTime != null) { - return new Date(eventTime.getTime()); + public Date getServerTime() { + if (serverTime != null) { + return new Date(serverTime.getTime()); } else { return null; } } - public void setEventTime(Date eventTime) { - if (eventTime != null) { - this.eventTime = new Date(eventTime.getTime()); + public void setServerTime(Date serverTime) { + if (serverTime != null) { + this.serverTime = new Date(serverTime.getTime()); } else { - this.eventTime = null; + this.serverTime = null; } } -- cgit v1.2.3