diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-11-24 16:49:54 +1300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-24 16:49:54 +1300 |
commit | ebe64e810bd8e57a805a571cfd6110fbc3cd57c1 (patch) | |
tree | 90d8bb347c6081116df225d5c1e08f9469458675 /src | |
parent | 04f33e6ba29b3a73a65b081d2a8b6ac3e0e0ebee (diff) | |
parent | 03fb12d4a9453d6845198a167a70e720726df2e2 (diff) | |
download | trackermap-server-ebe64e810bd8e57a805a571cfd6110fbc3cd57c1.tar.gz trackermap-server-ebe64e810bd8e57a805a571cfd6110fbc3cd57c1.tar.bz2 trackermap-server-ebe64e810bd8e57a805a571cfd6110fbc3cd57c1.zip |
Merge pull request #2593 from Abyss777/attributes_helpers
Attributes helpers
Diffstat (limited to 'src')
21 files changed, 76 insertions, 60 deletions
diff --git a/src/org/traccar/BaseProtocol.java b/src/org/traccar/BaseProtocol.java index 4cbb5bd7c..59331d7cc 100644 --- a/src/org/traccar/BaseProtocol.java +++ b/src/org/traccar/BaseProtocol.java @@ -56,7 +56,7 @@ public abstract class BaseProtocol implements Protocol { if (supportedCommands.contains(command.getType())) { activeDevice.write(command); } else if (command.getType().equals(Command.TYPE_CUSTOM)) { - String data = (String) command.getAttributes().get(Command.KEY_DATA); + String data = command.getString(Command.KEY_DATA); if (activeDevice.getChannel().getPipeline().get(StringEncoder.class) != null) { activeDevice.write(data); } else { diff --git a/src/org/traccar/BaseProtocolEncoder.java b/src/org/traccar/BaseProtocolEncoder.java index 5c2c503e4..3c2d08471 100644 --- a/src/org/traccar/BaseProtocolEncoder.java +++ b/src/org/traccar/BaseProtocolEncoder.java @@ -31,8 +31,8 @@ public abstract class BaseProtocolEncoder extends OneToOneEncoder { protected void initDevicePassword(Command command, String defaultPassword) { if (!command.getAttributes().containsKey(Command.KEY_DEVICE_PASSWORD)) { Device device = Context.getIdentityManager().getDeviceById(command.getDeviceId()); - if (device.getAttributes().containsKey(Command.KEY_DEVICE_PASSWORD)) { - String password = (String) device.getAttributes().get(Command.KEY_DEVICE_PASSWORD); + String password = device.getString(Command.KEY_DEVICE_PASSWORD); + if (password != null) { command.set(Command.KEY_DEVICE_PASSWORD, password); } else { command.set(Command.KEY_DEVICE_PASSWORD, defaultPassword); diff --git a/src/org/traccar/DistanceHandler.java b/src/org/traccar/DistanceHandler.java index 138fc85d6..fdf9847a7 100644 --- a/src/org/traccar/DistanceHandler.java +++ b/src/org/traccar/DistanceHandler.java @@ -38,9 +38,7 @@ public class DistanceHandler extends BaseDataHandler { Position last = getLastPosition(position.getDeviceId()); if (last != null) { - if (last.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) { - totalDistance = ((Number) last.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue(); - } + totalDistance = last.getDouble(Position.KEY_TOTAL_DISTANCE); if (!position.getAttributes().containsKey(Position.KEY_DISTANCE)) { distance = DistanceCalculator.distance( @@ -49,7 +47,7 @@ public class DistanceHandler extends BaseDataHandler { distance = BigDecimal.valueOf(distance).setScale(2, RoundingMode.HALF_EVEN).doubleValue(); } else { - distance = ((Number) position.getAttributes().get(Position.KEY_DISTANCE)).doubleValue(); + distance = position.getDouble(Position.KEY_DISTANCE); } } if (!position.getAttributes().containsKey(Position.KEY_DISTANCE)) { diff --git a/src/org/traccar/FilterHandler.java b/src/org/traccar/FilterHandler.java index 40676c72d..898651837 100644 --- a/src/org/traccar/FilterHandler.java +++ b/src/org/traccar/FilterHandler.java @@ -111,7 +111,7 @@ public class FilterHandler extends BaseDataHandler { } private boolean filterApproximate(Position position) { - Boolean approximate = (Boolean) position.getAttributes().get(Position.KEY_APPROXIMATE); + Boolean approximate = position.getBoolean(Position.KEY_APPROXIMATE); return filterApproximate && approximate != null && approximate; } diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 5681602fe..600669579 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -401,14 +401,12 @@ public class DeviceManager implements IdentityManager { String result = null; Device device = getDeviceById(deviceId); if (device != null) { - if (device.getAttributes().containsKey(attributeName)) { - result = (String) device.getAttributes().get(attributeName); - } + result = device.getString(attributeName); if (result == null && lookupGroupsAttribute) { long groupId = device.getGroupId(); while (groupId != 0) { if (getGroupById(groupId) != null) { - result = (String) getGroupById(groupId).getAttributes().get(attributeName); + result = getGroupById(groupId).getString(attributeName); if (result != null) { break; } @@ -421,7 +419,7 @@ public class DeviceManager implements IdentityManager { if (result == null) { if (lookupServer) { Server server = Context.getPermissionsManager().getServer(); - result = (String) server.getAttributes().get(attributeName); + result = server.getString(attributeName); } else { result = Context.getConfig().getString(attributeName); } diff --git a/src/org/traccar/events/IgnitionEventHandler.java b/src/org/traccar/events/IgnitionEventHandler.java index b52083f57..3086adb13 100644 --- a/src/org/traccar/events/IgnitionEventHandler.java +++ b/src/org/traccar/events/IgnitionEventHandler.java @@ -39,20 +39,12 @@ public class IgnitionEventHandler extends BaseEventHandler { Collection<Event> result = null; - boolean ignition = false; - Object ignitionObject = position.getAttributes().get(Position.KEY_IGNITION); - if (ignitionObject != null && ignitionObject instanceof Boolean) { - ignition = (Boolean) ignitionObject; - } + boolean ignition = position.getBoolean(Position.KEY_IGNITION); boolean oldIgnition = false; - Object oldIgnitionObject = null; Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId()); if (lastPosition != null) { - oldIgnitionObject = lastPosition.getAttributes().get(Position.KEY_IGNITION); - } - if (oldIgnitionObject != null && oldIgnitionObject instanceof Boolean) { - oldIgnition = (Boolean) oldIgnitionObject; + oldIgnition = lastPosition.getBoolean(Position.KEY_IGNITION); } if (ignition && !oldIgnition) { diff --git a/src/org/traccar/events/MaintenanceEventHandler.java b/src/org/traccar/events/MaintenanceEventHandler.java index 466e0b617..f423e035b 100644 --- a/src/org/traccar/events/MaintenanceEventHandler.java +++ b/src/org/traccar/events/MaintenanceEventHandler.java @@ -50,13 +50,10 @@ public class MaintenanceEventHandler extends BaseEventHandler { double newTotalDistance = 0.0; Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId()); - if (lastPosition != null && lastPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) { - oldTotalDistance = ((Number) lastPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue(); - } - - if (position.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) { - newTotalDistance = ((Number) position.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue(); + if (lastPosition != null) { + oldTotalDistance = lastPosition.getDouble(Position.KEY_TOTAL_DISTANCE); } + newTotalDistance = position.getDouble(Position.KEY_TOTAL_DISTANCE); oldTotalDistance -= maintenanceStart; newTotalDistance -= maintenanceStart; diff --git a/src/org/traccar/model/Extensible.java b/src/org/traccar/model/Extensible.java index efe60fb75..b8c83bb21 100644 --- a/src/org/traccar/model/Extensible.java +++ b/src/org/traccar/model/Extensible.java @@ -68,4 +68,44 @@ public class Extensible { } } + public String getString(String key) { + if (attributes.containsKey(key)) { + return (String) attributes.get(key); + } else { + return null; + } + } + + public double getDouble(String key) { + if (attributes.containsKey(key)) { + return ((Number) attributes.get(key)).doubleValue(); + } else { + return 0.0; + } + } + + public boolean getBoolean(String key) { + if (attributes.containsKey(key)) { + return Boolean.parseBoolean(attributes.get(key).toString()); + } else { + return false; + } + } + + public int getInteger(String key) { + if (attributes.containsKey(key)) { + return ((Number) attributes.get(key)).intValue(); + } else { + return 0; + } + } + + public long getLong(String key) { + if (attributes.containsKey(key)) { + return ((Number) attributes.get(key)).longValue(); + } else { + return 0; + } + } + } diff --git a/src/org/traccar/notification/PropertiesProvider.java b/src/org/traccar/notification/PropertiesProvider.java index 9136288f8..e7cac8d0f 100644 --- a/src/org/traccar/notification/PropertiesProvider.java +++ b/src/org/traccar/notification/PropertiesProvider.java @@ -36,7 +36,7 @@ public class PropertiesProvider { if (config != null) { return config.getString(key); } else { - return (String) extensible.getAttributes().get(key); + return extensible.getString(key); } } diff --git a/src/org/traccar/protocol/CityeasyProtocolEncoder.java b/src/org/traccar/protocol/CityeasyProtocolEncoder.java index 0c149c1c9..c800131d6 100644 --- a/src/org/traccar/protocol/CityeasyProtocolEncoder.java +++ b/src/org/traccar/protocol/CityeasyProtocolEncoder.java @@ -50,13 +50,13 @@ public class CityeasyProtocolEncoder extends BaseProtocolEncoder { case Command.TYPE_POSITION_SINGLE: return encodeContent(CityeasyProtocolDecoder.MSG_LOCATION_REQUEST, content); case Command.TYPE_POSITION_PERIODIC: - content.writeShort(((Number) command.getAttributes().get(Command.KEY_FREQUENCY)).intValue()); + content.writeShort(command.getInteger(Command.KEY_FREQUENCY)); return encodeContent(CityeasyProtocolDecoder.MSG_LOCATION_INTERVAL, content); case Command.TYPE_POSITION_STOP: content.writeShort(0); return encodeContent(CityeasyProtocolDecoder.MSG_LOCATION_INTERVAL, content); case Command.TYPE_SET_TIMEZONE: - int timezone = ((Number) command.getAttributes().get(Command.KEY_TIMEZONE)).intValue(); + int timezone = command.getInteger(Command.KEY_TIMEZONE); if (timezone < 0) { content.writeByte(1); } else { diff --git a/src/org/traccar/protocol/EelinkProtocolEncoder.java b/src/org/traccar/protocol/EelinkProtocolEncoder.java index b9d6b6f97..5a28733f7 100644 --- a/src/org/traccar/protocol/EelinkProtocolEncoder.java +++ b/src/org/traccar/protocol/EelinkProtocolEncoder.java @@ -47,7 +47,7 @@ public class EelinkProtocolEncoder extends BaseProtocolEncoder { switch (command.getType()) { case Command.TYPE_CUSTOM: - return encodeContent((String) command.getAttributes().get(Command.KEY_DATA)); + return encodeContent(command.getString(Command.KEY_DATA)); case Command.TYPE_ENGINE_STOP: return encodeContent("RELAY,1#"); case Command.TYPE_ENGINE_RESUME: diff --git a/src/org/traccar/protocol/Gt06ProtocolEncoder.java b/src/org/traccar/protocol/Gt06ProtocolEncoder.java index b6ec5a0d1..8e00522a7 100644 --- a/src/org/traccar/protocol/Gt06ProtocolEncoder.java +++ b/src/org/traccar/protocol/Gt06ProtocolEncoder.java @@ -59,7 +59,7 @@ public class Gt06ProtocolEncoder extends BaseProtocolEncoder { boolean alternative; Device device = Context.getIdentityManager().getDeviceById(command.getDeviceId()); if (device.getAttributes().containsKey("gt06.alternative")) { - alternative = Boolean.parseBoolean((String) device.getAttributes().get("gt06.alternative")); + alternative = device.getBoolean("gt06.alternative"); } else { alternative = Context.getConfig().getBoolean("gt06.alternative"); } diff --git a/src/org/traccar/protocol/Jt600ProtocolEncoder.java b/src/org/traccar/protocol/Jt600ProtocolEncoder.java index 3eb0566a4..0bf389460 100644 --- a/src/org/traccar/protocol/Jt600ProtocolEncoder.java +++ b/src/org/traccar/protocol/Jt600ProtocolEncoder.java @@ -29,7 +29,7 @@ public class Jt600ProtocolEncoder extends StringProtocolEncoder { case Command.TYPE_ENGINE_RESUME: return "(S07,1)"; case Command.TYPE_SET_TIMEZONE: - int offset = ((Number) command.getAttributes().get(Command.KEY_TIMEZONE)).intValue() / 60; + int offset = command.getInteger(Command.KEY_TIMEZONE) / 60; return "(S09,1," + offset + ")"; case Command.TYPE_REBOOT_DEVICE: return "(S17)"; diff --git a/src/org/traccar/protocol/MeiligaoProtocolEncoder.java b/src/org/traccar/protocol/MeiligaoProtocolEncoder.java index a91b33a60..268bae392 100644 --- a/src/org/traccar/protocol/MeiligaoProtocolEncoder.java +++ b/src/org/traccar/protocol/MeiligaoProtocolEncoder.java @@ -24,7 +24,6 @@ import org.traccar.model.Command; import javax.xml.bind.DatatypeConverter; import java.nio.charset.StandardCharsets; -import java.util.Map; public class MeiligaoProtocolEncoder extends BaseProtocolEncoder { @@ -62,13 +61,12 @@ public class MeiligaoProtocolEncoder extends BaseProtocolEncoder { protected Object encodeCommand(Command command) { ChannelBuffer content = ChannelBuffers.dynamicBuffer(); - Map<String, Object> attributes = command.getAttributes(); switch (command.getType()) { case Command.TYPE_POSITION_SINGLE: return encodeContent(command.getDeviceId(), MSG_TRACK_ON_DEMAND, content); case Command.TYPE_POSITION_PERIODIC: - content.writeShort(((Number) attributes.get(Command.KEY_FREQUENCY)).intValue() / 10); + content.writeShort(command.getInteger(Command.KEY_FREQUENCY) / 10); return encodeContent(command.getDeviceId(), MSG_TRACK_BY_INTERVAL, content); case Command.TYPE_ENGINE_STOP: content.writeByte(0x01); @@ -77,10 +75,10 @@ public class MeiligaoProtocolEncoder extends BaseProtocolEncoder { content.writeByte(0x00); return encodeContent(command.getDeviceId(), MSG_OUTPUT_CONTROL, content); case Command.TYPE_ALARM_GEOFENCE: - content.writeShort(((Number) attributes.get(Command.KEY_RADIUS)).intValue()); + content.writeShort(command.getInteger(Command.KEY_RADIUS)); return encodeContent(command.getDeviceId(), MSG_MOVEMENT_ALARM, content); case Command.TYPE_SET_TIMEZONE: - int offset = ((Number) attributes.get(Command.KEY_TIMEZONE)).intValue() / 60; + int offset = command.getInteger(Command.KEY_TIMEZONE) / 60; content.writeBytes(String.valueOf(offset).getBytes(StandardCharsets.US_ASCII)); return encodeContent(command.getDeviceId(), MSG_TIME_ZONE, content); case Command.TYPE_REBOOT_DEVICE: diff --git a/src/org/traccar/protocol/NoranProtocolEncoder.java b/src/org/traccar/protocol/NoranProtocolEncoder.java index 5bd069218..25b510a73 100644 --- a/src/org/traccar/protocol/NoranProtocolEncoder.java +++ b/src/org/traccar/protocol/NoranProtocolEncoder.java @@ -52,7 +52,7 @@ public class NoranProtocolEncoder extends BaseProtocolEncoder { case Command.TYPE_POSITION_SINGLE: return encodeContent("*KW,000,000,000000#"); case Command.TYPE_POSITION_PERIODIC: - int interval = ((Number) command.getAttributes().get(Command.KEY_FREQUENCY)).intValue(); + int interval = command.getInteger(Command.KEY_FREQUENCY); return encodeContent("*KW,000,002,000000," + interval + "#"); case Command.TYPE_POSITION_STOP: return encodeContent("*KW,000,002,000000,0#"); diff --git a/src/org/traccar/protocol/RuptelaProtocolEncoder.java b/src/org/traccar/protocol/RuptelaProtocolEncoder.java index c3b3a3619..0abfa18ce 100644 --- a/src/org/traccar/protocol/RuptelaProtocolEncoder.java +++ b/src/org/traccar/protocol/RuptelaProtocolEncoder.java @@ -43,7 +43,7 @@ public class RuptelaProtocolEncoder extends BaseProtocolEncoder { switch (command.getType()) { case Command.TYPE_CUSTOM: - return encodeContent((String) command.getAttributes().get(Command.KEY_DATA)); + return encodeContent(command.getString(Command.KEY_DATA)); default: Log.warning(new UnsupportedOperationException(command.getType())); break; diff --git a/src/org/traccar/protocol/T800xProtocolEncoder.java b/src/org/traccar/protocol/T800xProtocolEncoder.java index 7b02ed10b..6ed5dbccd 100644 --- a/src/org/traccar/protocol/T800xProtocolEncoder.java +++ b/src/org/traccar/protocol/T800xProtocolEncoder.java @@ -51,7 +51,7 @@ public class T800xProtocolEncoder extends BaseProtocolEncoder { switch (command.getType()) { case Command.TYPE_CUSTOM: - return encodeContent(command, (String) command.getAttributes().get(Command.KEY_DATA)); + return encodeContent(command, command.getString(Command.KEY_DATA)); default: Log.warning(new UnsupportedOperationException(command.getType())); break; diff --git a/src/org/traccar/protocol/TeltonikaProtocolEncoder.java b/src/org/traccar/protocol/TeltonikaProtocolEncoder.java index eb83477b8..fd6eae744 100644 --- a/src/org/traccar/protocol/TeltonikaProtocolEncoder.java +++ b/src/org/traccar/protocol/TeltonikaProtocolEncoder.java @@ -50,7 +50,7 @@ public class TeltonikaProtocolEncoder extends BaseProtocolEncoder { switch (command.getType()) { case Command.TYPE_CUSTOM: - return encodeContent((String) command.getAttributes().get(Command.KEY_DATA)); + return encodeContent(command.getString(Command.KEY_DATA)); default: Log.warning(new UnsupportedOperationException(command.getType())); break; diff --git a/src/org/traccar/protocol/WatchProtocolEncoder.java b/src/org/traccar/protocol/WatchProtocolEncoder.java index f15c0f0c0..0b6a83ce9 100644 --- a/src/org/traccar/protocol/WatchProtocolEncoder.java +++ b/src/org/traccar/protocol/WatchProtocolEncoder.java @@ -33,7 +33,7 @@ public class WatchProtocolEncoder extends StringProtocolEncoder { } private int getEnableFlag(Command command) { - if ((Boolean) command.getAttributes().get(Command.KEY_ENABLE)) { + if (command.getBoolean(Command.KEY_ENABLE)) { return 1; } else { return 0; @@ -51,7 +51,7 @@ public class WatchProtocolEncoder extends StringProtocolEncoder { } private String getBinaryData(Command command) { - byte[] data = DatatypeConverter.parseHexBinary((String) command.getAttributes().get(Command.KEY_DATA)); + byte[] data = DatatypeConverter.parseHexBinary(command.getString(Command.KEY_DATA)); int encodedLength = data.length; for (byte b : data) { diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index 7fcc767e9..032925a4a 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -63,18 +63,15 @@ public final class ReportUtils { double distance = 0.0; double firstOdometer = 0.0; double lastOdometer = 0.0; - if (firstPosition.getAttributes().containsKey(Position.KEY_ODOMETER)) { - firstOdometer = ((Number) firstPosition.getAttributes().get(Position.KEY_ODOMETER)).doubleValue(); - } - if (lastPosition.getAttributes().containsKey(Position.KEY_ODOMETER)) { - lastOdometer = ((Number) lastPosition.getAttributes().get(Position.KEY_ODOMETER)).doubleValue(); - } + firstOdometer = firstPosition.getDouble(Position.KEY_ODOMETER); + lastOdometer = lastPosition.getDouble(Position.KEY_ODOMETER); + if (useOdometer && (firstOdometer != 0.0 || lastOdometer != 0.0)) { distance = lastOdometer - firstOdometer; } else if (firstPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE) && lastPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) { - distance = ((Number) lastPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue() - - ((Number) firstPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue(); + distance = lastPosition.getDouble(Position.KEY_TOTAL_DISTANCE) + - firstPosition.getDouble(Position.KEY_TOTAL_DISTANCE); } return distance; } diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index 95a3737a1..b404b0ed5 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -49,12 +49,8 @@ public final class Summary { if (firstPosition == null) { firstPosition = position; } - if (previousPosition != null - && position.getAttributes().get(Position.KEY_IGNITION) != null - && Boolean.parseBoolean(position.getAttributes().get(Position.KEY_IGNITION).toString()) - && previousPosition.getAttributes().get(Position.KEY_IGNITION) != null - && Boolean.parseBoolean(previousPosition.getAttributes() - .get(Position.KEY_IGNITION).toString())) { + if (previousPosition != null && position.getBoolean(Position.KEY_IGNITION) + && previousPosition.getBoolean(Position.KEY_IGNITION)) { result.addEngineHours(position.getFixTime().getTime() - previousPosition.getFixTime().getTime()); } |