From 51f0299aefe728163ac69f36cf9223c08ecd97aa Mon Sep 17 00:00:00 2001 From: Alexandre Truppel Date: Wed, 22 Feb 2023 14:27:07 +0100 Subject: Start writing boolean special case --- .../traccar/protocol/WialonProtocolDecoder.java | 60 ++++++++++++++-------- 1 file changed, 39 insertions(+), 21 deletions(-) (limited to 'src/main/java/org/traccar/protocol/WialonProtocolDecoder.java') diff --git a/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java b/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java index b87ba2b53..1fc434ed8 100644 --- a/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java @@ -39,31 +39,31 @@ public class WialonProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN_ANY = new PatternBuilder() - .expression("([^#]+)?") // imei - .text("#") // start byte - .expression("([^#]+)") // type - .text("#") // separator - .expression("(.*)") // message + .expression("([^#]+)?") // imei + .text("#") // start byte + .expression("([^#]+)") // type + .text("#") // separator + .expression("(.*)") // message .compile(); private static final Pattern PATTERN = new PatternBuilder() - .number("(?:NA|(dd)(dd)(dd));") // date (ddmmyy) - .number("(?:NA|(dd)(dd)(dd));") // time (hhmmss) - .number("(?:NA|(dd)(dd.d+));") // latitude + .number("(?:NA|(dd)(dd)(dd));") // date (ddmmyy) + .number("(?:NA|(dd)(dd)(dd));") // time (hhmmss) + .number("(?:NA|(dd)(dd.d+));") // latitude .expression("(?:NA|([NS]));") - .number("(?:NA|(ddd)(dd.d+));") // longitude + .number("(?:NA|(ddd)(dd.d+));") // longitude .expression("(?:NA|([EW]));") - .number("(?:NA|(d+.?d*))?;") // speed - .number("(?:NA|(d+.?d*))?;") // course - .number("(?:NA|(-?d+.?d*));") // altitude - .number("(?:NA|(d+))") // satellites + .number("(?:NA|(d+.?d*))?;") // speed + .number("(?:NA|(d+.?d*))?;") // course + .number("(?:NA|(-?d+.?d*));") // altitude + .number("(?:NA|(d+))") // satellites .groupBegin().text(";") - .number("(?:NA|(d+.?d*));") // hdop - .number("(?:NA|(d+));") // inputs - .number("(?:NA|(d+));") // outputs - .expression("(?:NA|([^;]*));") // adc - .expression("(?:NA|([^;]*));") // ibutton - .expression("(?:NA|(.*))") // params + .number("(?:NA|(d+.?d*));") // hdop + .number("(?:NA|(d+));") // inputs + .number("(?:NA|(d+));") // outputs + .expression("(?:NA|([^;]*));") // adc + .expression("(?:NA|([^;]*));") // ibutton + .expression("(?:NA|(.*))") // params .groupEnd("?") .compile(); @@ -135,10 +135,28 @@ public class WialonProtocolDecoder extends BaseProtocolDecoder { for (String param : values) { Matcher paramParser = Pattern.compile("(.*):[1-3]:(.*)").matcher(param); if (paramParser.matches()) { + // Parsing gives a (string,string) key-value pair + String key = paramParser.group(1).toLowerCase(); + String value = paramParser.group(2); + + // Key is already in correct type (string) + // If we can parse the value as a double, then we use that as the value's type + // If not, value type is a string unless it is equal to some specific cases (true, yes, etc), in which case we convert into a boolean + try { - position.set(paramParser.group(1).toLowerCase(), Double.parseDouble(paramParser.group(2))); + position.set(key, Double.parseDouble(value)); } catch (NumberFormatException e) { - position.set(paramParser.group(1).toLowerCase(), paramParser.group(2)); + if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("t") + || value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("y") + || value.equalsIgnoreCase("on")) { + position.set(key, true); + } else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("f") + || value.equalsIgnoreCase("no") || value.equalsIgnoreCase("n") + || value.equalsIgnoreCase("off")) { + position.set(key, false); + } else { + position.set(key, value); + } } } } -- cgit v1.2.3 From 345262a970292541c07aee9982c6a26a29c11f9c Mon Sep 17 00:00:00 2001 From: Alexandre Truppel Date: Wed, 22 Feb 2023 15:36:45 +0100 Subject: Boolean special case for Wialon done, added tests --- src/main/java/org/traccar/protocol/WialonProtocolDecoder.java | 10 +++++++--- .../java/org/traccar/protocol/WialonProtocolDecoderTest.java | 6 ++++++ 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'src/main/java/org/traccar/protocol/WialonProtocolDecoder.java') diff --git a/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java b/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java index 1fc434ed8..4c4ff7a63 100644 --- a/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java @@ -140,9 +140,13 @@ public class WialonProtocolDecoder extends BaseProtocolDecoder { String value = paramParser.group(2); // Key is already in correct type (string) - // If we can parse the value as a double, then we use that as the value's type - // If not, value type is a string unless it is equal to some specific cases (true, yes, etc), in which case we convert into a boolean - + + // If we can parse the value as a double, then we use that as the value's type. + // This covers both integer (x:1:y) and double (x:2:y) types in the Wialon protocol + + // If not, value type is a string unless it is equal to some specific cases + // (true, yes, etc), in which case we convert into a boolean + try { position.set(key, Double.parseDouble(value)); } catch (NumberFormatException e) { diff --git a/src/test/java/org/traccar/protocol/WialonProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/WialonProtocolDecoderTest.java index 6953784fb..45fbfbb1d 100644 --- a/src/test/java/org/traccar/protocol/WialonProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/WialonProtocolDecoderTest.java @@ -19,6 +19,12 @@ public class WialonProtocolDecoderTest extends ProtocolTest { verifyAttributes(decoder, text( "#D#120319;112003;NA;NA;NA;NA;0.000;NA;NA;0;NA;NA;NA;NA;NA;101_521347:1:521246,101_158:1:510,101_521055:1:510,101_521055_2.9:1:509,101_521056:1:3;626B")); + verifyAttributes(decoder, text( + "#D#120319;112003;NA;NA;NA;NA;0.000;NA;NA;0;NA;NA;NA;NA;NA;motion:3:true")); + + verifyAttributes(decoder, text( + "#D#120319;112003;NA;NA;NA;NA;0.000;NA;NA;0;NA;NA;NA;NA;NA;motion:3:false")); + verifyNull(decoder, text( "#L#123456789012345;test")); -- cgit v1.2.3 From ba6a9d72e62282d64781b856b8e094e1fb612f9e Mon Sep 17 00:00:00 2001 From: Alexandre Truppel Date: Wed, 22 Feb 2023 15:50:19 +0100 Subject: Added "setAccuracy" special case, with tests --- .../org/traccar/protocol/WialonProtocolDecoder.java | 11 ++++++++++- .../traccar/protocol/WialonProtocolDecoderTest.java | 18 ++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) (limited to 'src/main/java/org/traccar/protocol/WialonProtocolDecoder.java') diff --git a/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java b/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java index 4c4ff7a63..e2ccfe9d8 100644 --- a/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java @@ -148,7 +148,16 @@ public class WialonProtocolDecoder extends BaseProtocolDecoder { // (true, yes, etc), in which case we convert into a boolean try { - position.set(key, Double.parseDouble(value)); + double double_value = Double.parseDouble(value); + + // Since accuracy is not part of the general parameter list, + // we need to handle it separately by calling setAccuracy directly + if (key.equals("accuracy")) { + position.setAccuracy(double_value); + } + else { + position.set(key, double_value); + } } catch (NumberFormatException e) { if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("t") || value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("y") diff --git a/src/test/java/org/traccar/protocol/WialonProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/WialonProtocolDecoderTest.java index 45fbfbb1d..e25ff7f9e 100644 --- a/src/test/java/org/traccar/protocol/WialonProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/WialonProtocolDecoderTest.java @@ -1,8 +1,11 @@ package org.traccar.protocol; +import org.traccar.model.Position; import org.junit.Test; import org.traccar.ProtocolTest; +import static org.junit.Assert.assertEquals; + public class WialonProtocolDecoderTest extends ProtocolTest { @Test @@ -19,12 +22,6 @@ public class WialonProtocolDecoderTest extends ProtocolTest { verifyAttributes(decoder, text( "#D#120319;112003;NA;NA;NA;NA;0.000;NA;NA;0;NA;NA;NA;NA;NA;101_521347:1:521246,101_158:1:510,101_521055:1:510,101_521055_2.9:1:509,101_521056:1:3;626B")); - verifyAttributes(decoder, text( - "#D#120319;112003;NA;NA;NA;NA;0.000;NA;NA;0;NA;NA;NA;NA;NA;motion:3:true")); - - verifyAttributes(decoder, text( - "#D#120319;112003;NA;NA;NA;NA;0.000;NA;NA;0;NA;NA;NA;NA;NA;motion:3:false")); - verifyNull(decoder, text( "#L#123456789012345;test")); @@ -81,6 +78,15 @@ public class WialonProtocolDecoderTest extends ProtocolTest { verifyPositions(decoder, text( "#B#110315;045857;5364.0167;N;06127.8262;E;0;155;965;7;2.40;4;0;14.77,0.02,3.6;AB45DF01145;")); + verifyAttributes(decoder, text( + "#D#120319;112003;NA;NA;NA;NA;0.000;NA;NA;0;NA;NA;NA;NA;NA;motion:3:true")); + + verifyAttributes(decoder, text( + "#D#120319;112003;NA;NA;NA;NA;0.000;NA;NA;0;NA;NA;NA;NA;NA;motion:3:false")); + + Position p = (Position)decoder.decode(null, null, text( + "#D#120319;112003;NA;NA;NA;NA;0.000;NA;NA;0;NA;NA;NA;NA;NA;accuracy:2:12.3")); + assertEquals(p.getAccuracy(), 12.3, 0.001); } } -- cgit v1.2.3 From 6c4f99117bbd3645f3b55852b2148beb28c1e0f9 Mon Sep 17 00:00:00 2001 From: Alexandre Truppel Date: Wed, 22 Feb 2023 15:58:43 +0100 Subject: Corrected styling --- src/main/java/org/traccar/protocol/WialonProtocolDecoder.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/main/java/org/traccar/protocol/WialonProtocolDecoder.java') diff --git a/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java b/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java index e2ccfe9d8..baf3847a1 100644 --- a/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java @@ -148,15 +148,14 @@ public class WialonProtocolDecoder extends BaseProtocolDecoder { // (true, yes, etc), in which case we convert into a boolean try { - double double_value = Double.parseDouble(value); + double doubleValue = Double.parseDouble(value); // Since accuracy is not part of the general parameter list, // we need to handle it separately by calling setAccuracy directly if (key.equals("accuracy")) { - position.setAccuracy(double_value); - } - else { - position.set(key, double_value); + position.setAccuracy(doubleValue); + } else { + position.set(key, doubleValue); } } catch (NumberFormatException e) { if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("t") -- cgit v1.2.3 From 47398e91b1450c744cf3ed31de46f7ea02bbede1 Mon Sep 17 00:00:00 2001 From: Alexandre Truppel Date: Wed, 22 Feb 2023 16:05:21 +0100 Subject: Review --- src/main/java/org/traccar/protocol/WialonProtocolDecoder.java | 10 +++------- .../java/org/traccar/protocol/WialonProtocolDecoderTest.java | 11 ++--------- 2 files changed, 5 insertions(+), 16 deletions(-) (limited to 'src/main/java/org/traccar/protocol/WialonProtocolDecoder.java') diff --git a/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java b/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java index baf3847a1..1166d0708 100644 --- a/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java @@ -145,7 +145,7 @@ public class WialonProtocolDecoder extends BaseProtocolDecoder { // This covers both integer (x:1:y) and double (x:2:y) types in the Wialon protocol // If not, value type is a string unless it is equal to some specific cases - // (true, yes, etc), in which case we convert into a boolean + // (true, false), in which case we convert into a boolean try { double doubleValue = Double.parseDouble(value); @@ -158,13 +158,9 @@ public class WialonProtocolDecoder extends BaseProtocolDecoder { position.set(key, doubleValue); } } catch (NumberFormatException e) { - if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("t") - || value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("y") - || value.equalsIgnoreCase("on")) { + if (value.equalsIgnoreCase("true")) { position.set(key, true); - } else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("f") - || value.equalsIgnoreCase("no") || value.equalsIgnoreCase("n") - || value.equalsIgnoreCase("off")) { + } else if (value.equalsIgnoreCase("false")) { position.set(key, false); } else { position.set(key, value); diff --git a/src/test/java/org/traccar/protocol/WialonProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/WialonProtocolDecoderTest.java index e25ff7f9e..0a47edcb4 100644 --- a/src/test/java/org/traccar/protocol/WialonProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/WialonProtocolDecoderTest.java @@ -78,15 +78,8 @@ public class WialonProtocolDecoderTest extends ProtocolTest { verifyPositions(decoder, text( "#B#110315;045857;5364.0167;N;06127.8262;E;0;155;965;7;2.40;4;0;14.77,0.02,3.6;AB45DF01145;")); - verifyAttributes(decoder, text( - "#D#120319;112003;NA;NA;NA;NA;0.000;NA;NA;0;NA;NA;NA;NA;NA;motion:3:true")); - - verifyAttributes(decoder, text( - "#D#120319;112003;NA;NA;NA;NA;0.000;NA;NA;0;NA;NA;NA;NA;NA;motion:3:false")); - - Position p = (Position)decoder.decode(null, null, text( - "#D#120319;112003;NA;NA;NA;NA;0.000;NA;NA;0;NA;NA;NA;NA;NA;accuracy:2:12.3")); - assertEquals(p.getAccuracy(), 12.3, 0.001); + verifyAttribute(decoder, text( + "#D#120319;112003;NA;NA;NA;NA;0.000;NA;NA;0;NA;NA;NA;NA;NA;motion:3:false"), "motion", false); } } -- cgit v1.2.3 From a2d9daf4b21313ebaed862f126355becf2b6d4ec Mon Sep 17 00:00:00 2001 From: Alexandre Truppel Date: Wed, 22 Feb 2023 16:07:05 +0100 Subject: Formatting --- .../traccar/protocol/WialonProtocolDecoder.java | 38 +++++++++++----------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'src/main/java/org/traccar/protocol/WialonProtocolDecoder.java') diff --git a/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java b/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java index 1166d0708..b319a5947 100644 --- a/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/WialonProtocolDecoder.java @@ -39,31 +39,31 @@ public class WialonProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN_ANY = new PatternBuilder() - .expression("([^#]+)?") // imei - .text("#") // start byte - .expression("([^#]+)") // type - .text("#") // separator - .expression("(.*)") // message + .expression("([^#]+)?") // imei + .text("#") // start byte + .expression("([^#]+)") // type + .text("#") // separator + .expression("(.*)") // message .compile(); private static final Pattern PATTERN = new PatternBuilder() - .number("(?:NA|(dd)(dd)(dd));") // date (ddmmyy) - .number("(?:NA|(dd)(dd)(dd));") // time (hhmmss) - .number("(?:NA|(dd)(dd.d+));") // latitude + .number("(?:NA|(dd)(dd)(dd));") // date (ddmmyy) + .number("(?:NA|(dd)(dd)(dd));") // time (hhmmss) + .number("(?:NA|(dd)(dd.d+));") // latitude .expression("(?:NA|([NS]));") - .number("(?:NA|(ddd)(dd.d+));") // longitude + .number("(?:NA|(ddd)(dd.d+));") // longitude .expression("(?:NA|([EW]));") - .number("(?:NA|(d+.?d*))?;") // speed - .number("(?:NA|(d+.?d*))?;") // course - .number("(?:NA|(-?d+.?d*));") // altitude - .number("(?:NA|(d+))") // satellites + .number("(?:NA|(d+.?d*))?;") // speed + .number("(?:NA|(d+.?d*))?;") // course + .number("(?:NA|(-?d+.?d*));") // altitude + .number("(?:NA|(d+))") // satellites .groupBegin().text(";") - .number("(?:NA|(d+.?d*));") // hdop - .number("(?:NA|(d+));") // inputs - .number("(?:NA|(d+));") // outputs - .expression("(?:NA|([^;]*));") // adc - .expression("(?:NA|([^;]*));") // ibutton - .expression("(?:NA|(.*))") // params + .number("(?:NA|(d+.?d*));") // hdop + .number("(?:NA|(d+));") // inputs + .number("(?:NA|(d+));") // outputs + .expression("(?:NA|([^;]*));") // adc + .expression("(?:NA|([^;]*));") // ibutton + .expression("(?:NA|(.*))") // params .groupEnd("?") .compile(); -- cgit v1.2.3