diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2017-09-07 23:03:45 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-07 23:03:45 +1200 |
commit | 7294d12706466840562b073a5a58a3696c0e54da (patch) | |
tree | a3c474d75b2393149bd89a2a9b9580444ba00a61 | |
parent | f0578c7b5f8737e019a9cf05935d2caaa940f0ca (diff) | |
parent | ee28d68243892623df7f718d50cd3ea5791f35ee (diff) | |
download | trackermap-server-7294d12706466840562b073a5a58a3696c0e54da.tar.gz trackermap-server-7294d12706466840562b073a5a58a3696c0e54da.tar.bz2 trackermap-server-7294d12706466840562b073a5a58a3696c0e54da.zip |
Merge pull request #3511 from Abyss777/attributes_types
Handle correct and incorrect attribute types
-rw-r--r-- | src/org/traccar/database/DeviceManager.java | 33 | ||||
-rw-r--r-- | src/org/traccar/model/ExtendedModel.java | 2 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationMail.java | 2 | ||||
-rw-r--r-- | src/org/traccar/notification/PropertiesProvider.java | 13 | ||||
-rw-r--r-- | src/org/traccar/protocol/OsmAndProtocolDecoder.java | 12 | ||||
-rw-r--r-- | test/org/traccar/ProtocolTest.java | 4 | ||||
-rw-r--r-- | test/org/traccar/protocol/OsmAndProtocolDecoderTest.java | 3 |
7 files changed, 48 insertions, 21 deletions
diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index a485d6dc6..2157e738d 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -262,59 +262,56 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity public boolean lookupAttributeBoolean( long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { - String result = lookupAttribute(deviceId, attributeName, lookupConfig); + Object result = lookupAttribute(deviceId, attributeName, lookupConfig); if (result != null) { - return Boolean.parseBoolean(result); + return result instanceof String ? Boolean.parseBoolean((String) result) : (Boolean) result; } return defaultValue; } public String lookupAttributeString( long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { - String result = lookupAttribute(deviceId, attributeName, lookupConfig); - if (result != null) { - return result; - } - return defaultValue; + Object result = lookupAttribute(deviceId, attributeName, lookupConfig); + return result != null ? (String) result : defaultValue; } public int lookupAttributeInteger(long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { - String result = lookupAttribute(deviceId, attributeName, lookupConfig); + Object result = lookupAttribute(deviceId, attributeName, lookupConfig); if (result != null) { - return Integer.parseInt(result); + return result instanceof String ? Integer.parseInt((String) result) : ((Number) result).intValue(); } return defaultValue; } public long lookupAttributeLong( long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { - String result = lookupAttribute(deviceId, attributeName, lookupConfig); + Object result = lookupAttribute(deviceId, attributeName, lookupConfig); if (result != null) { - return Long.parseLong(result); + return result instanceof String ? Long.parseLong((String) result) : ((Number) result).longValue(); } return defaultValue; } public double lookupAttributeDouble( long deviceId, String attributeName, double defaultValue, boolean lookupConfig) { - String result = lookupAttribute(deviceId, attributeName, lookupConfig); + Object result = lookupAttribute(deviceId, attributeName, lookupConfig); if (result != null) { - return Double.parseDouble(result); + return result instanceof String ? Double.parseDouble((String) result) : ((Number) result).doubleValue(); } return defaultValue; } - private String lookupAttribute(long deviceId, String attributeName, boolean lookupConfig) { - String result = null; + private Object lookupAttribute(long deviceId, String attributeName, boolean lookupConfig) { + Object result = null; Device device = getById(deviceId); if (device != null) { - result = device.getString(attributeName); + result = device.getAttributes().get(attributeName); if (result == null && lookupGroupsAttribute) { long groupId = device.getGroupId(); while (groupId != 0) { Group group = Context.getGroupsManager().getById(groupId); if (group != null) { - result = group.getString(attributeName); + result = group.getAttributes().get(attributeName); if (result != null) { break; } @@ -329,7 +326,7 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity result = Context.getConfig().getString(attributeName); } else { Server server = Context.getPermissionsManager().getServer(); - result = server.getString(attributeName); + result = server.getAttributes().get(attributeName); } } } diff --git a/src/org/traccar/model/ExtendedModel.java b/src/org/traccar/model/ExtendedModel.java index a4bf00e70..2ce503eea 100644 --- a/src/org/traccar/model/ExtendedModel.java +++ b/src/org/traccar/model/ExtendedModel.java @@ -100,7 +100,7 @@ public class ExtendedModel extends BaseModel { public boolean getBoolean(String key) { if (attributes.containsKey(key)) { - return Boolean.parseBoolean(attributes.get(key).toString()); + return (Boolean) attributes.get(key); } else { return false; } diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java index d7f3bf64c..fc70a6115 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -43,7 +43,7 @@ public final class NotificationMail { if (host != null) { properties.put("mail.transport.protocol", provider.getString("mail.transport.protocol", "smtp")); properties.put("mail.smtp.host", host); - properties.put("mail.smtp.port", provider.getString("mail.smtp.port", "25")); + properties.put("mail.smtp.port", String.valueOf(provider.getInteger("mail.smtp.port", 25))); String starttlsEnable = provider.getString("mail.smtp.starttls.enable"); if (starttlsEnable != null) { diff --git a/src/org/traccar/notification/PropertiesProvider.java b/src/org/traccar/notification/PropertiesProvider.java index 2fea901af..c5ba688e8 100644 --- a/src/org/traccar/notification/PropertiesProvider.java +++ b/src/org/traccar/notification/PropertiesProvider.java @@ -48,4 +48,17 @@ public class PropertiesProvider { return value; } + public int getInteger(String key, int defaultValue) { + if (config != null) { + return config.getInteger(key, defaultValue); + } else { + Object result = extendedModel.getAttributes().get(key); + if (result != null) { + return result instanceof String ? Integer.parseInt((String) result) : (Integer) result; + } else { + return defaultValue; + } + } + } + } diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java index c06582a15..15a71c88b 100644 --- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java +++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java @@ -135,7 +135,17 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder { try { position.set(entry.getKey(), Double.parseDouble(value)); } catch (NumberFormatException e) { - position.set(entry.getKey(), value); + switch (value) { + case "true": + position.set(entry.getKey(), true); + break; + case "false": + position.set(entry.getKey(), false); + break; + default: + position.set(entry.getKey(), value); + break; + } } break; } diff --git a/test/org/traccar/ProtocolTest.java b/test/org/traccar/ProtocolTest.java index a2c7e4f21..7bff41acd 100644 --- a/test/org/traccar/ProtocolTest.java +++ b/test/org/traccar/ProtocolTest.java @@ -241,6 +241,10 @@ public class ProtocolTest extends BaseTest { Assert.assertTrue(attributes.get(Position.KEY_CHARGE) instanceof Boolean); } + if (attributes.containsKey(Position.KEY_IGNITION)) { + Assert.assertTrue(attributes.get(Position.KEY_IGNITION) instanceof Boolean); + } + if (attributes.containsKey(Position.KEY_MOTION)) { Assert.assertTrue(attributes.get(Position.KEY_MOTION) instanceof Boolean); } diff --git a/test/org/traccar/protocol/OsmAndProtocolDecoderTest.java b/test/org/traccar/protocol/OsmAndProtocolDecoderTest.java index 2d0ef0adf..af860f371 100644 --- a/test/org/traccar/protocol/OsmAndProtocolDecoderTest.java +++ b/test/org/traccar/protocol/OsmAndProtocolDecoderTest.java @@ -37,6 +37,9 @@ public class OsmAndProtocolDecoderTest extends ProtocolTest { verifyPosition(decoder, request( "/?id=123456×tamp=1377177267&location=60.0,30.0")); + verifyPosition(decoder, request( + "/?id=123456789012345×tamp=1504763810&lat=40.7232948571&lon=-74.0061408571&bearing=7.19889788244&speed=40&ignition=true&rpm=933&fuel=24")); + } } |