Skip to content

Commit

Permalink
Merge pull request traccar#3449 from Abyss777/load_position
Browse files Browse the repository at this point in the history
Map correct position to delayed event notification
  • Loading branch information
tananaev authored Aug 17, 2017
2 parents e8739ed + 1324c00 commit 9e9f97a
Show file tree
Hide file tree
Showing 18 changed files with 113 additions and 109 deletions.
8 changes: 4 additions & 4 deletions src/org/traccar/BaseEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.traccar;

import java.util.Collection;
import java.util.Map;

import org.traccar.model.Event;
import org.traccar.model.Position;
Expand All @@ -25,13 +25,13 @@ public abstract class BaseEventHandler extends BaseDataHandler {
@Override
protected Position handlePosition(Position position) {

Collection<Event> events = analyzePosition(position);
Map<Event, Position> events = analyzePosition(position);
if (events != null && Context.getNotificationManager() != null) {
Context.getNotificationManager().updateEvents(events, position);
Context.getNotificationManager().updateEvents(events);
}
return position;
}

protected abstract Collection<Event> analyzePosition(Position position);
protected abstract Map<Event, Position> analyzePosition(Position position);

}
21 changes: 11 additions & 10 deletions src/org/traccar/database/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.SocketAddress;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -84,26 +85,26 @@ public void updateDevice(final long deviceId, String status, Date time) {

if (enableStatusEvents && !status.equals(oldStatus)) {
String eventType;
Set<Event> events = new HashSet<>();
Map<Event, Position> events = new HashMap<>();
switch (status) {
case Device.STATUS_ONLINE:
eventType = Event.TYPE_DEVICE_ONLINE;
break;
case Device.STATUS_UNKNOWN:
eventType = Event.TYPE_DEVICE_UNKNOWN;
if (updateDeviceState) {
events.addAll(updateDeviceState(deviceId));
events.putAll(updateDeviceState(deviceId));
}
break;
default:
eventType = Event.TYPE_DEVICE_OFFLINE;
if (updateDeviceState) {
events.addAll(updateDeviceState(deviceId));
events.putAll(updateDeviceState(deviceId));
}
break;
}
events.add(new Event(eventType, deviceId));
Context.getNotificationManager().updateEvents(events, null);
events.put(new Event(eventType, deviceId), null);
Context.getNotificationManager().updateEvents(events);
}

Timeout timeout = timeouts.remove(deviceId);
Expand Down Expand Up @@ -135,19 +136,19 @@ public void run(Timeout timeout) throws Exception {
updateDevice(device);
}

public Set<Event> updateDeviceState(long deviceId) {
public Map<Event, Position> updateDeviceState(long deviceId) {
DeviceState deviceState = Context.getDeviceManager().getDeviceState(deviceId);
Set<Event> result = new HashSet<>();
Map<Event, Position> result = new HashMap<>();

Event event = Context.getMotionEventHandler().updateMotionState(deviceState);
Map<Event, Position> event = Context.getMotionEventHandler().updateMotionState(deviceState);
if (event != null) {
result.add(event);
result.putAll(event);
}

event = Context.getOverspeedEventHandler().updateOverspeedState(deviceState, Context.getDeviceManager().
lookupAttributeDouble(deviceId, OverspeedEventHandler.ATTRIBUTE_SPEED_LIMIT, 0, false));
if (event != null) {
result.add(event);
result.putAll(event);
}

return result;
Expand Down
7 changes: 4 additions & 3 deletions src/org/traccar/database/NotificationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
Expand Down Expand Up @@ -77,9 +78,9 @@ public void updateEvent(Event event, Position position) {
}
}

public void updateEvents(Collection<Event> events, Position position) {
for (Event event : events) {
updateEvent(event, position);
public void updateEvents(Map<Event, Position> events) {
for (Entry<Event, Position> event : events.entrySet()) {
updateEvent(event.getKey(), event.getValue());
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/org/traccar/events/AlertEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package org.traccar.events;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import org.traccar.BaseEventHandler;
import org.traccar.model.Event;
Expand All @@ -25,12 +25,12 @@
public class AlertEventHandler extends BaseEventHandler {

@Override
protected Collection<Event> analyzePosition(Position position) {
protected Map<Event, Position> analyzePosition(Position position) {
Object alarm = position.getAttributes().get(Position.KEY_ALARM);
if (alarm != null) {
Event event = new Event(Event.TYPE_ALARM, position.getDeviceId(), position.getId());
event.set(Position.KEY_ALARM, (String) alarm);
return Collections.singleton(event);
return Collections.singletonMap(event, position);
}
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions src/org/traccar/events/CommandResultEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package org.traccar.events;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import org.traccar.BaseEventHandler;
import org.traccar.model.Event;
Expand All @@ -25,12 +25,12 @@
public class CommandResultEventHandler extends BaseEventHandler {

@Override
protected Collection<Event> analyzePosition(Position position) {
protected Map<Event, Position> analyzePosition(Position position) {
Object commandResult = position.getAttributes().get(Position.KEY_RESULT);
if (commandResult != null) {
Event event = new Event(Event.TYPE_COMMAND_RESULT, position.getDeviceId(), position.getId());
event.set(Position.KEY_RESULT, (String) commandResult);
return Collections.singleton(event);
return Collections.singletonMap(event, position);
}
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions src/org/traccar/events/DriverEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/
package org.traccar.events;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import org.traccar.BaseEventHandler;
import org.traccar.Context;
Expand All @@ -27,7 +27,7 @@
public class DriverEventHandler extends BaseEventHandler {

@Override
protected Collection<Event> analyzePosition(Position position) {
protected Map<Event, Position> analyzePosition(Position position) {
if (!Context.getIdentityManager().isLatestPosition(position)) {
return null;
}
Expand All @@ -41,7 +41,7 @@ protected Collection<Event> analyzePosition(Position position) {
if (!driverUniqueId.equals(oldDriverUniqueId)) {
Event event = new Event(Event.TYPE_DRIVER_CHANGED, position.getDeviceId(), position.getId());
event.set(Position.KEY_DRIVER_UNIQUE_ID, driverUniqueId);
return Collections.singleton(event);
return Collections.singletonMap(event, position);
}
}
return null;
Expand Down
6 changes: 3 additions & 3 deletions src/org/traccar/events/FuelDropEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
import org.traccar.model.Event;
import org.traccar.model.Position;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

public class FuelDropEventHandler extends BaseEventHandler {

public static final String ATTRIBUTE_FUEL_DROP_THRESHOLD = "fuelDropThreshold";

@Override
protected Collection<Event> analyzePosition(Position position) {
protected Map<Event, Position> analyzePosition(Position position) {

Device device = Context.getIdentityManager().getById(position.getDeviceId());
if (device == null) {
Expand All @@ -52,7 +52,7 @@ protected Collection<Event> analyzePosition(Position position) {
if (drop >= fuelDropThreshold) {
Event event = new Event(Event.TYPE_DEVICE_FUEL_DROP, position.getDeviceId(), position.getId());
event.set(ATTRIBUTE_FUEL_DROP_THRESHOLD, fuelDropThreshold);
return Collections.singleton(event);
return Collections.singletonMap(event, position);
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/org/traccar/events/GeofenceEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
package org.traccar.events;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.traccar.BaseEventHandler;
import org.traccar.Context;
Expand All @@ -36,7 +37,7 @@ public GeofenceEventHandler() {
}

@Override
protected Collection<Event> analyzePosition(Position position) {
protected Map<Event, Position> analyzePosition(Position position) {
Device device = Context.getIdentityManager().getById(position.getDeviceId());
if (device == null) {
return null;
Expand All @@ -56,14 +57,14 @@ protected Collection<Event> analyzePosition(Position position) {

device.setGeofenceIds(currentGeofences);

Collection<Event> events = new ArrayList<>();
Map<Event, Position> events = new HashMap<>();
for (long geofenceId : newGeofences) {
long calendarId = geofenceManager.getById(geofenceId).getCalendarId();
Calendar calendar = calendarId != 0 ? Context.getCalendarManager().getById(calendarId) : null;
if (calendar == null || calendar.checkMoment(position.getFixTime())) {
Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId());
event.setGeofenceId(geofenceId);
events.add(event);
events.put(event, position);
}
}
for (long geofenceId : oldGeofences) {
Expand All @@ -72,7 +73,7 @@ protected Collection<Event> analyzePosition(Position position) {
if (calendar == null || calendar.checkMoment(position.getFixTime())) {
Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId());
event.setGeofenceId(geofenceId);
events.add(event);
events.put(event, position);
}
}
return events;
Expand Down
14 changes: 7 additions & 7 deletions src/org/traccar/events/IgnitionEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/
package org.traccar.events;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import org.traccar.BaseEventHandler;
import org.traccar.Context;
Expand All @@ -28,13 +28,13 @@
public class IgnitionEventHandler extends BaseEventHandler {

@Override
protected Collection<Event> analyzePosition(Position position) {
protected Map<Event, Position> analyzePosition(Position position) {
Device device = Context.getIdentityManager().getById(position.getDeviceId());
if (device == null || !Context.getIdentityManager().isLatestPosition(position)) {
return null;
}

Collection<Event> result = null;
Map<Event, Position> result = null;

if (position.getAttributes().containsKey(Position.KEY_IGNITION)) {
boolean ignition = position.getBoolean(Position.KEY_IGNITION);
Expand All @@ -44,11 +44,11 @@ protected Collection<Event> analyzePosition(Position position) {
boolean oldIgnition = lastPosition.getBoolean(Position.KEY_IGNITION);

if (ignition && !oldIgnition) {
result = Collections.singleton(
new Event(Event.TYPE_IGNITION_ON, position.getDeviceId(), position.getId()));
result = Collections.singletonMap(
new Event(Event.TYPE_IGNITION_ON, position.getDeviceId(), position.getId()), position);
} else if (!ignition && oldIgnition) {
result = Collections.singleton(
new Event(Event.TYPE_IGNITION_OFF, position.getDeviceId(), position.getId()));
result = Collections.singletonMap(
new Event(Event.TYPE_IGNITION_OFF, position.getDeviceId(), position.getId()), position);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/org/traccar/events/MaintenanceEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/
package org.traccar.events;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import org.traccar.BaseEventHandler;
import org.traccar.Context;
Expand All @@ -31,7 +31,7 @@ public class MaintenanceEventHandler extends BaseEventHandler {
public static final String ATTRIBUTE_MAINTENANCE_INTERVAL = "maintenance.interval";

@Override
protected Collection<Event> analyzePosition(Position position) {
protected Map<Event, Position> analyzePosition(Position position) {
Device device = Context.getIdentityManager().getById(position.getDeviceId());
if (device == null || !Context.getIdentityManager().isLatestPosition(position)) {
return null;
Expand Down Expand Up @@ -60,7 +60,7 @@ protected Collection<Event> analyzePosition(Position position) {
if ((long) (oldTotalDistance / maintenanceInterval) < (long) (newTotalDistance / maintenanceInterval)) {
Event event = new Event(Event.TYPE_MAINTENANCE, position.getDeviceId(), position.getId());
event.set(Position.KEY_TOTAL_DISTANCE, newTotalDistance);
return Collections.singleton(event);
return Collections.singletonMap(event, position);
}

return null;
Expand Down
Loading

0 comments on commit 9e9f97a

Please sign in to comment.