From e0a4ed2a60fd1a38a904fa7156afba6c0aee1cdb Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 20 Sep 2016 17:14:13 +0500 Subject: Add parameter "report.ignoreOdometer" --- src/org/traccar/reports/ReportUtils.java | 2 ++ src/org/traccar/reports/Summary.java | 14 ++++++++++++-- src/org/traccar/reports/Trips.java | 15 ++++++++++++--- 3 files changed, 26 insertions(+), 5 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index 818920ad5..c973c3ff3 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -30,6 +30,8 @@ public final class ReportUtils { private ReportUtils() { } + public static final String IGNORE_ODOMETER = "report.ignoreOdometer"; + public static Collection getDeviceList(Collection deviceIds, Collection groupIds) { Collection result = new ArrayList<>(); result.addAll(deviceIds); diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index 44fb1dd4c..79e1a7c05 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -24,6 +24,7 @@ import javax.json.Json; import javax.json.JsonArrayBuilder; import org.traccar.Context; +import org.traccar.model.Device; import org.traccar.model.Position; import org.traccar.reports.model.SummaryReport; import org.traccar.web.CsvBuilder; @@ -35,9 +36,11 @@ public final class Summary { } private static SummaryReport calculateSummaryResult(long deviceId, Date from, Date to) throws SQLException { + boolean ignoreOdometerConfig = Context.getConfig().getBoolean(ReportUtils.IGNORE_ODOMETER); SummaryReport result = new SummaryReport(); + Device device = Context.getDeviceManager().getDeviceById(deviceId); result.setDeviceId(deviceId); - result.setDeviceName(Context.getDeviceManager().getDeviceById(deviceId).getName()); + result.setDeviceName(device.getName()); Collection positions = Context.getDataManager().getPositions(deviceId, from, to); if (positions != null && !positions.isEmpty()) { Position firstPosition = null; @@ -60,7 +63,14 @@ public final class Summary { speedSum += position.getSpeed(); result.setMaxSpeed(position.getSpeed()); } - result.setDistance(ReportUtils.calculateDistance(firstPosition, previousPosition)); + boolean ignoreOdometer = false; + if (device.getAttributes().containsKey(ReportUtils.IGNORE_ODOMETER)) { + ignoreOdometer = Boolean.parseBoolean(device.getAttributes() + .get(ReportUtils.IGNORE_ODOMETER).toString()); + } else { + ignoreOdometer = ignoreOdometerConfig; + } + result.setDistance(ReportUtils.calculateDistance(firstPosition, previousPosition, !ignoreOdometer)); result.setAverageSpeed(speedSum / positions.size()); } return result; diff --git a/src/org/traccar/reports/Trips.java b/src/org/traccar/reports/Trips.java index 2171d5f93..514c5d1c4 100644 --- a/src/org/traccar/reports/Trips.java +++ b/src/org/traccar/reports/Trips.java @@ -25,6 +25,7 @@ import javax.json.Json; import javax.json.JsonArrayBuilder; import org.traccar.Context; +import org.traccar.model.Device; import org.traccar.model.Position; import org.traccar.reports.model.TripReport; import org.traccar.web.CsvBuilder; @@ -36,6 +37,7 @@ public final class Trips { } private static TripReport calculateTrip(ArrayList positions, int startIndex, int endIndex) { + boolean ignoreOdometerConfig = Context.getConfig().getBoolean(ReportUtils.IGNORE_ODOMETER); Position startTrip = positions.get(startIndex); Position endTrip = positions.get(endIndex); @@ -53,15 +55,22 @@ public final class Trips { long tripDuration = endTrip.getFixTime().getTime() - positions.get(startIndex).getFixTime().getTime(); long deviceId = startTrip.getDeviceId(); trip.setDeviceId(deviceId); - String deviceName = Context.getDeviceManager().getDeviceById(deviceId).getName(); - trip.setDeviceName(deviceName); + Device device = Context.getDeviceManager().getDeviceById(deviceId); + trip.setDeviceName(device.getName()); trip.setStartPositionId(startTrip.getId()); trip.setStartTime(startTrip.getFixTime()); trip.setStartAddress(startTrip.getAddress()); trip.setEndPositionId(endTrip.getId()); trip.setEndTime(endTrip.getFixTime()); trip.setEndAddress(endTrip.getAddress()); - trip.setDistance(ReportUtils.calculateDistance(startTrip, endTrip)); + boolean ignoreOdometer = false; + if (device.getAttributes().containsKey(ReportUtils.IGNORE_ODOMETER)) { + ignoreOdometer = Boolean.parseBoolean(device.getAttributes() + .get(ReportUtils.IGNORE_ODOMETER).toString()); + } else { + ignoreOdometer = ignoreOdometerConfig; + } + trip.setDistance(ReportUtils.calculateDistance(startTrip, endTrip, !ignoreOdometer)); trip.setDuration(tripDuration); trip.setAverageSpeed(speedSum / (endIndex - startIndex)); trip.setMaxSpeed(speedMax); -- cgit v1.2.3 From b298aef26f01154e84e9a00b29ff62921484a220 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 21 Sep 2016 09:29:42 +0500 Subject: - Add two functions to lookup attributes from device to server and from device to config - Removed constant --- src/org/traccar/database/DeviceManager.java | 19 ++++++++++++++++--- src/org/traccar/events/OverspeedEventHandler.java | 3 ++- src/org/traccar/reports/ReportUtils.java | 2 -- src/org/traccar/reports/Summary.java | 14 +++++--------- src/org/traccar/reports/Trips.java | 14 +++++--------- 5 files changed, 28 insertions(+), 24 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 5f68df831..21e370051 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -316,7 +316,16 @@ public class DeviceManager implements IdentityManager { groupsById.remove(groupId); } - public String lookupAttribute(long deviceId, String attributeName) { + public String lookupServerAttribute(long deviceId, String attributeName) { + return lookupAttribute(deviceId, attributeName, true); + } + + public String lookupConfigAttribute(long deviceId, String attributeName) { + return lookupAttribute(deviceId, attributeName, false); + } + + + private String lookupAttribute(long deviceId, String attributeName, boolean lookupServer) { String result = null; Device device = getDeviceById(deviceId); if (device != null) { @@ -338,8 +347,12 @@ public class DeviceManager implements IdentityManager { } } if (result == null) { - Server server = Context.getPermissionsManager().getServer(); - result = (String) server.getAttributes().get(attributeName); + if (lookupServer) { + Server server = Context.getPermissionsManager().getServer(); + result = (String) server.getAttributes().get(attributeName); + } else { + result = Context.getConfig().getString(attributeName); + } } } return result; diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index 302fa87cd..a32dfae7e 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -48,7 +48,8 @@ public class OverspeedEventHandler extends BaseEventHandler { Collection events = new ArrayList<>(); double speed = position.getSpeed(); double speedLimit = 0; - String speedLimitAttribute = Context.getDeviceManager().lookupAttribute(device.getId(), ATTRIBUTE_SPEED_LIMIT); + String speedLimitAttribute = Context.getDeviceManager() + .lookupServerAttribute(device.getId(), ATTRIBUTE_SPEED_LIMIT); if (speedLimitAttribute != null) { speedLimit = Double.parseDouble(speedLimitAttribute); } diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index c973c3ff3..818920ad5 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -30,8 +30,6 @@ public final class ReportUtils { private ReportUtils() { } - public static final String IGNORE_ODOMETER = "report.ignoreOdometer"; - public static Collection getDeviceList(Collection deviceIds, Collection groupIds) { Collection result = new ArrayList<>(); result.addAll(deviceIds); diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index 79e1a7c05..763ddb600 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -24,7 +24,6 @@ import javax.json.Json; import javax.json.JsonArrayBuilder; import org.traccar.Context; -import org.traccar.model.Device; import org.traccar.model.Position; import org.traccar.reports.model.SummaryReport; import org.traccar.web.CsvBuilder; @@ -36,11 +35,9 @@ public final class Summary { } private static SummaryReport calculateSummaryResult(long deviceId, Date from, Date to) throws SQLException { - boolean ignoreOdometerConfig = Context.getConfig().getBoolean(ReportUtils.IGNORE_ODOMETER); SummaryReport result = new SummaryReport(); - Device device = Context.getDeviceManager().getDeviceById(deviceId); result.setDeviceId(deviceId); - result.setDeviceName(device.getName()); + result.setDeviceName(Context.getDeviceManager().getDeviceById(deviceId).getName()); Collection positions = Context.getDataManager().getPositions(deviceId, from, to); if (positions != null && !positions.isEmpty()) { Position firstPosition = null; @@ -64,11 +61,10 @@ public final class Summary { result.setMaxSpeed(position.getSpeed()); } boolean ignoreOdometer = false; - if (device.getAttributes().containsKey(ReportUtils.IGNORE_ODOMETER)) { - ignoreOdometer = Boolean.parseBoolean(device.getAttributes() - .get(ReportUtils.IGNORE_ODOMETER).toString()); - } else { - ignoreOdometer = ignoreOdometerConfig; + String ignoreOdometerAttribute = Context.getDeviceManager() + .lookupConfigAttribute(deviceId, "report.ignoreOdometer"); + if (ignoreOdometerAttribute != null) { + ignoreOdometer = Boolean.parseBoolean(ignoreOdometerAttribute); } result.setDistance(ReportUtils.calculateDistance(firstPosition, previousPosition, !ignoreOdometer)); result.setAverageSpeed(speedSum / positions.size()); diff --git a/src/org/traccar/reports/Trips.java b/src/org/traccar/reports/Trips.java index 514c5d1c4..c4a7f2c8f 100644 --- a/src/org/traccar/reports/Trips.java +++ b/src/org/traccar/reports/Trips.java @@ -25,7 +25,6 @@ import javax.json.Json; import javax.json.JsonArrayBuilder; import org.traccar.Context; -import org.traccar.model.Device; import org.traccar.model.Position; import org.traccar.reports.model.TripReport; import org.traccar.web.CsvBuilder; @@ -37,7 +36,6 @@ public final class Trips { } private static TripReport calculateTrip(ArrayList positions, int startIndex, int endIndex) { - boolean ignoreOdometerConfig = Context.getConfig().getBoolean(ReportUtils.IGNORE_ODOMETER); Position startTrip = positions.get(startIndex); Position endTrip = positions.get(endIndex); @@ -55,8 +53,7 @@ public final class Trips { long tripDuration = endTrip.getFixTime().getTime() - positions.get(startIndex).getFixTime().getTime(); long deviceId = startTrip.getDeviceId(); trip.setDeviceId(deviceId); - Device device = Context.getDeviceManager().getDeviceById(deviceId); - trip.setDeviceName(device.getName()); + trip.setDeviceName(Context.getDeviceManager().getDeviceById(deviceId).getName()); trip.setStartPositionId(startTrip.getId()); trip.setStartTime(startTrip.getFixTime()); trip.setStartAddress(startTrip.getAddress()); @@ -64,11 +61,10 @@ public final class Trips { trip.setEndTime(endTrip.getFixTime()); trip.setEndAddress(endTrip.getAddress()); boolean ignoreOdometer = false; - if (device.getAttributes().containsKey(ReportUtils.IGNORE_ODOMETER)) { - ignoreOdometer = Boolean.parseBoolean(device.getAttributes() - .get(ReportUtils.IGNORE_ODOMETER).toString()); - } else { - ignoreOdometer = ignoreOdometerConfig; + String ignoreOdometerAttribute = Context.getDeviceManager() + .lookupConfigAttribute(deviceId, "report.ignoreOdometer"); + if (ignoreOdometerAttribute != null) { + ignoreOdometer = Boolean.parseBoolean(ignoreOdometerAttribute); } trip.setDistance(ReportUtils.calculateDistance(startTrip, endTrip, !ignoreOdometer)); trip.setDuration(tripDuration); -- cgit v1.2.3 From d0ce4c7e8069fc4663e2d04f942d8f8989be2998 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 21 Sep 2016 09:37:45 +0500 Subject: PMD fix --- src/org/traccar/web/CsvBuilder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/web/CsvBuilder.java b/src/org/traccar/web/CsvBuilder.java index a25b839a9..f59aabc7f 100644 --- a/src/org/traccar/web/CsvBuilder.java +++ b/src/org/traccar/web/CsvBuilder.java @@ -37,10 +37,10 @@ public class CsvBuilder { SortedSet methods = new TreeSet(new Comparator() { @Override public int compare(Method m1, Method m2) { - if (m1.getName().equals("getAttributes") & !m1.getName().equals(m2.getName())) { + if (m1.getName().equals("getAttributes") && !m1.getName().equals(m2.getName())) { return 1; } - if (m2.getName().equals("getAttributes") & !m1.getName().equals(m2.getName())) { + if (m2.getName().equals("getAttributes") && !m1.getName().equals(m2.getName())) { return -1; } return m1.getName().compareTo(m2.getName()); -- cgit v1.2.3 From fbe0190e65ee0646f6521cfca31ecfa3efa10472 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 21 Sep 2016 14:10:58 +0500 Subject: Implement lookup attribute helpers for primitives --- src/org/traccar/database/DeviceManager.java | 79 +++++++++++++++++++++-- src/org/traccar/events/OverspeedEventHandler.java | 8 +-- src/org/traccar/reports/Summary.java | 10 +-- src/org/traccar/reports/Trips.java | 10 +-- 4 files changed, 83 insertions(+), 24 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 21e370051..f32c7edd2 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -316,14 +316,85 @@ public class DeviceManager implements IdentityManager { groupsById.remove(groupId); } - public String lookupServerAttribute(long deviceId, String attributeName) { - return lookupAttribute(deviceId, attributeName, true); + public boolean lookupServerBoolean(long deviceId, String attributeName, boolean defaultValue) { + String result = lookupAttribute(deviceId, attributeName, true); + if (result != null) { + return Boolean.parseBoolean(result); + } + return defaultValue; } - public String lookupConfigAttribute(long deviceId, String attributeName) { - return lookupAttribute(deviceId, attributeName, false); + public String lookupServerString(long deviceId, String attributeName, String defaultValue) { + String result = lookupAttribute(deviceId, attributeName, true); + if (result != null) { + return result; + } + return defaultValue; } + public int lookupServerInteger(long deviceId, String attributeName, int defaultValue) { + String result = lookupAttribute(deviceId, attributeName, true); + if (result != null) { + return Integer.parseInt(result); + } + return defaultValue; + } + + public long lookupServerLong(long deviceId, String attributeName, long defaultValue) { + String result = lookupAttribute(deviceId, attributeName, true); + if (result != null) { + return Long.parseLong(result); + } + return defaultValue; + } + + public double lookupServerDouble(long deviceId, String attributeName, double defaultValue) { + String result = lookupAttribute(deviceId, attributeName, true); + if (result != null) { + return Double.parseDouble(result); + } + return defaultValue; + } + + public boolean lookupConfigBoolean(long deviceId, String attributeName, boolean defaultValue) { + String result = lookupAttribute(deviceId, attributeName, false); + if (result != null) { + return Boolean.parseBoolean(result); + } + return defaultValue; + } + + public String lookupConfigString(long deviceId, String attributeName, String defaultValue) { + String result = lookupAttribute(deviceId, attributeName, false); + if (result != null) { + return result; + } + return defaultValue; + } + + public int lookupConfigInteger(long deviceId, String attributeName, int defaultValue) { + String result = lookupAttribute(deviceId, attributeName, false); + if (result != null) { + return Integer.parseInt(result); + } + return defaultValue; + } + + public long lookupConfigLong(long deviceId, String attributeName, long defaultValue) { + String result = lookupAttribute(deviceId, attributeName, false); + if (result != null) { + return Long.parseLong(result); + } + return defaultValue; + } + + public double lookupConfigDouble(long deviceId, String attributeName, double defaultValue) { + String result = lookupAttribute(deviceId, attributeName, false); + if (result != null) { + return Double.parseDouble(result); + } + return defaultValue; + } private String lookupAttribute(long deviceId, String attributeName, boolean lookupServer) { String result = null; diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index a32dfae7e..57f60d864 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -47,12 +47,8 @@ public class OverspeedEventHandler extends BaseEventHandler { Collection events = new ArrayList<>(); double speed = position.getSpeed(); - double speedLimit = 0; - String speedLimitAttribute = Context.getDeviceManager() - .lookupServerAttribute(device.getId(), ATTRIBUTE_SPEED_LIMIT); - if (speedLimitAttribute != null) { - speedLimit = Double.parseDouble(speedLimitAttribute); - } + double speedLimit = Context.getDeviceManager() + .lookupServerDouble(device.getId(), ATTRIBUTE_SPEED_LIMIT, 0); if (speedLimit == 0) { return null; } diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index 763ddb600..d4171f644 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -37,7 +37,7 @@ public final class Summary { private static SummaryReport calculateSummaryResult(long deviceId, Date from, Date to) throws SQLException { SummaryReport result = new SummaryReport(); result.setDeviceId(deviceId); - result.setDeviceName(Context.getDeviceManager().getDeviceById(deviceId).getName()); + result.setDeviceName(Context.getIdentityManager().getDeviceById(deviceId).getName()); Collection positions = Context.getDataManager().getPositions(deviceId, from, to); if (positions != null && !positions.isEmpty()) { Position firstPosition = null; @@ -60,12 +60,8 @@ public final class Summary { speedSum += position.getSpeed(); result.setMaxSpeed(position.getSpeed()); } - boolean ignoreOdometer = false; - String ignoreOdometerAttribute = Context.getDeviceManager() - .lookupConfigAttribute(deviceId, "report.ignoreOdometer"); - if (ignoreOdometerAttribute != null) { - ignoreOdometer = Boolean.parseBoolean(ignoreOdometerAttribute); - } + boolean ignoreOdometer = Context.getDeviceManager() + .lookupConfigBoolean(deviceId, "report.ignoreOdometer", false); result.setDistance(ReportUtils.calculateDistance(firstPosition, previousPosition, !ignoreOdometer)); result.setAverageSpeed(speedSum / positions.size()); } diff --git a/src/org/traccar/reports/Trips.java b/src/org/traccar/reports/Trips.java index c4a7f2c8f..f0a10edbd 100644 --- a/src/org/traccar/reports/Trips.java +++ b/src/org/traccar/reports/Trips.java @@ -53,19 +53,15 @@ public final class Trips { long tripDuration = endTrip.getFixTime().getTime() - positions.get(startIndex).getFixTime().getTime(); long deviceId = startTrip.getDeviceId(); trip.setDeviceId(deviceId); - trip.setDeviceName(Context.getDeviceManager().getDeviceById(deviceId).getName()); + trip.setDeviceName(Context.getIdentityManager().getDeviceById(deviceId).getName()); trip.setStartPositionId(startTrip.getId()); trip.setStartTime(startTrip.getFixTime()); trip.setStartAddress(startTrip.getAddress()); trip.setEndPositionId(endTrip.getId()); trip.setEndTime(endTrip.getFixTime()); trip.setEndAddress(endTrip.getAddress()); - boolean ignoreOdometer = false; - String ignoreOdometerAttribute = Context.getDeviceManager() - .lookupConfigAttribute(deviceId, "report.ignoreOdometer"); - if (ignoreOdometerAttribute != null) { - ignoreOdometer = Boolean.parseBoolean(ignoreOdometerAttribute); - } + boolean ignoreOdometer = Context.getDeviceManager() + .lookupConfigBoolean(deviceId, "report.ignoreOdometer", false); trip.setDistance(ReportUtils.calculateDistance(startTrip, endTrip, !ignoreOdometer)); trip.setDuration(tripDuration); trip.setAverageSpeed(speedSum / (endIndex - startIndex)); -- cgit v1.2.3