Skip to content

Commit

Permalink
Merge pull request traccar#2149 from Abyss777/lastposition_reorganiza…
Browse files Browse the repository at this point in the history
…tion

Move lastPosition update to the end of pipeline
  • Loading branch information
tananaev authored Jul 25, 2016
2 parents 8c4eb2e + 80847cc commit 7e20896
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 37 deletions.
2 changes: 1 addition & 1 deletion debug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
</entry>

<entry key='database.updateDeviceStatus'>
UPDATE devices SET status = :status, lastUpdate = :lastUpdate, motion = :motion WHERE id = :id;
UPDATE devices SET status = :status, lastUpdate = :lastUpdate WHERE id = :id;
</entry>

<entry key='database.deleteDevice'>
Expand Down
2 changes: 2 additions & 0 deletions schema/changelog-3.7.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<column name="attributes" type="VARCHAR(4096)" />
</addColumn>

<dropColumn tableName="devices" columnName="motion" />

</changeSet>

<changeSet author="author" id="changelog-3.7-notmssql">
Expand Down
2 changes: 1 addition & 1 deletion setup/unix/traccar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
</entry>

<entry key='database.updateDeviceStatus'>
UPDATE devices SET status = :status, lastUpdate = :lastUpdate, motion = :motion WHERE id = :id;
UPDATE devices SET status = :status, lastUpdate = :lastUpdate WHERE id = :id;
</entry>

<entry key='database.deleteDevice'>
Expand Down
2 changes: 1 addition & 1 deletion setup/windows/traccar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
</entry>

<entry key='database.updateDeviceStatus'>
UPDATE devices SET status = :status, lastUpdate = :lastUpdate, motion = :motion WHERE id = :id;
UPDATE devices SET status = :status, lastUpdate = :lastUpdate WHERE id = :id;
</entry>

<entry key='database.deleteDevice'>
Expand Down
1 change: 0 additions & 1 deletion src/org/traccar/DefaultDataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ protected Position handlePosition(Position position) {

try {
Context.getDataManager().addPosition(position);
Context.getDeviceManager().updateLatestPosition(position);
} catch (Exception error) {
Log.warning(error);
}
Expand Down
7 changes: 7 additions & 0 deletions src/org/traccar/MainEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.traccar.helper.Log;
import org.traccar.model.Position;

import java.sql.SQLException;
import java.text.SimpleDateFormat;

public class MainEventHandler extends IdleStateAwareChannelHandler {
Expand All @@ -36,6 +37,11 @@ public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
if (e.getMessage() != null && e.getMessage() instanceof Position) {

Position position = (Position) e.getMessage();
try {
Context.getDeviceManager().updateLatestPosition(position);
} catch (SQLException error) {
Log.warning(error);
}

String uniqueId = Context.getIdentityManager().getDeviceById(position.getDeviceId()).getUniqueId();

Expand All @@ -54,6 +60,7 @@ public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
s.append(", result: ").append(cmdResult);
}
Log.info(s.toString());

}
}

Expand Down
10 changes: 6 additions & 4 deletions src/org/traccar/database/DeviceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ private void updateDeviceCache(boolean force) throws SQLException {
}
}
device.setStatus(Device.STATUS_OFFLINE);
device.setMotion(Device.STATUS_STOPPED);
}
}
for (Long cachedDeviceId : devicesById.keySet()) {
Expand Down Expand Up @@ -176,7 +175,6 @@ public void updateDeviceStatus(Device device) throws SQLException {
if (devicesById.containsKey(device.getId())) {
Device cachedDevice = devicesById.get(device.getId());
cachedDevice.setStatus(device.getStatus());
cachedDevice.setMotion(device.getMotion());
}
}

Expand All @@ -191,10 +189,14 @@ public void removeDevice(long deviceId) throws SQLException {
positions.remove(deviceId);
}

public boolean isLatestPosition(Position position) {
Position lastPosition = getLastPosition(position.getDeviceId());
return lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) > 0;
}

public void updateLatestPosition(Position position) throws SQLException {

Position lastPosition = getLastPosition(position.getDeviceId());
if (lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) > 0) {
if (isLatestPosition(position)) {

dataManager.updateLatestPosition(position);

Expand Down
2 changes: 1 addition & 1 deletion src/org/traccar/events/GeofenceEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected Collection<Event> analyzePosition(Position position) {
if (device == null) {
return null;
}
if (position.getId() != device.getPositionId() || !position.getValid()) {
if (!Context.getDeviceManager().isLatestPosition(position) || !position.getValid()) {
return null;
}

Expand Down
20 changes: 8 additions & 12 deletions src/org/traccar/events/MotionEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public class MotionEventHandler extends BaseEventHandler {

private static final double SPEED_THRESHOLD = 0.01;
private static final double SPEED_THRESHOLD = 0.01;
private int suppressRepeated;

public MotionEventHandler() {
Expand All @@ -42,26 +42,22 @@ protected Collection<Event> analyzePosition(Position position) {
if (device == null) {
return null;
}
if (position.getId() != device.getPositionId() || !position.getValid()) {
if (!Context.getDeviceManager().isLatestPosition(position) || !position.getValid()) {
return null;
}

Collection<Event> result = null;
double speed = position.getSpeed();
boolean valid = position.getValid();
String motion = device.getMotion();
if (motion == null) {
motion = Device.STATUS_STOPPED;
double oldSpeed = 0;
Position lastPosition = Context.getDeviceManager().getLastPosition(position.getDeviceId());
if (lastPosition != null) {
oldSpeed = lastPosition.getSpeed();
}
try {
if (valid && speed > SPEED_THRESHOLD && !motion.equals(Device.STATUS_MOVING)) {
device.setMotion(Device.STATUS_MOVING);
Context.getDeviceManager().updateDeviceStatus(device);
if (speed > SPEED_THRESHOLD && oldSpeed <= SPEED_THRESHOLD) {
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)) {
device.setMotion(Device.STATUS_STOPPED);
Context.getDeviceManager().updateDeviceStatus(device);
} else if (speed <= SPEED_THRESHOLD && oldSpeed > SPEED_THRESHOLD) {
result = new ArrayList<>();
result.add(new Event(Event.TYPE_DEVICE_STOPPED, position.getDeviceId(), position.getId()));
}
Expand Down
5 changes: 2 additions & 3 deletions src/org/traccar/events/OverspeedEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ protected Collection<Event> analyzePosition(Position position) {
if (device == null) {
return null;
}
if (position.getId() != device.getPositionId() || !position.getValid()) {
if (!Context.getDeviceManager().isLatestPosition(position) || !position.getValid()) {
return null;
}

Collection<Event> events = new ArrayList<>();
double speed = position.getSpeed();
boolean valid = position.getValid();

if (valid && globalSpeedLimit != 0 && speed > globalSpeedLimit) {
if (globalSpeedLimit != 0 && speed > globalSpeedLimit) {
try {
if (Context.getDataManager().getLastEvents(
position.getDeviceId(), Event.TYPE_DEVICE_OVERSPEED, suppressRepeated).isEmpty()) {
Expand Down
13 changes: 0 additions & 13 deletions src/org/traccar/model/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,6 @@ public void setGroupId(long groupId) {
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;
}

private List<Long> geofenceIds;

public List<Long> getGeofenceIds() {
Expand Down

0 comments on commit 7e20896

Please sign in to comment.