diff options
-rw-r--r-- | src/org/traccar/protocol/XexunProtocolDecoder.java | 86 | ||||
-rw-r--r-- | test/org/traccar/protocol/XexunProtocolDecoderTest.java | 6 |
2 files changed, 29 insertions, 63 deletions
diff --git a/src/org/traccar/protocol/XexunProtocolDecoder.java b/src/org/traccar/protocol/XexunProtocolDecoder.java index 89ea954bc..7a9e880a8 100644 --- a/src/org/traccar/protocol/XexunProtocolDecoder.java +++ b/src/org/traccar/protocol/XexunProtocolDecoder.java @@ -22,6 +22,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.helper.DateBuilder; +import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; import org.traccar.model.Event; import org.traccar.model.Position; @@ -67,8 +69,12 @@ public class XexunProtocolDecoder extends BaseProtocolDecoder { protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - Pattern pattern = full ? PATTERN_FULL : PATTERN_BASIC; - Matcher parser = pattern.matcher((String) msg); + Pattern pattern = PATTERN_BASIC; + if (full) { + pattern = PATTERN_FULL; + } + + Parser parser = new Parser(pattern, (String) msg); if (!parser.matches()) { return null; } @@ -76,79 +82,37 @@ public class XexunProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - Integer index = 1; - if (full) { - position.set("serial", parser.group(index++)); - position.set("number", parser.group(index++)); + position.set("serial", parser.next()); + position.set("number", parser.next()); } - // Time - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - time.set(Calendar.HOUR_OF_DAY, Integer.parseInt(parser.group(index++))); - time.set(Calendar.MINUTE, Integer.parseInt(parser.group(index++))); - time.set(Calendar.SECOND, Integer.parseInt(parser.group(index++))); - time.set(Calendar.MILLISECOND, Integer.parseInt(parser.group(index++))); - - // Validity - position.setValid(parser.group(index++).compareTo("A") == 0); - - // Latitude - Double latitude = Double.parseDouble(parser.group(index++)); - latitude += Double.parseDouble(parser.group(index++)) / 60; - if (parser.group(index++).compareTo("S") == 0) latitude = -latitude; - position.setLatitude(latitude); - - // Longitude - Double longitude = Double.parseDouble(parser.group(index++)); - longitude += Double.parseDouble(parser.group(index++)) / 60; - String hemisphere = parser.group(index++); - if (hemisphere != null && hemisphere.compareTo("W") == 0) { - longitude = -longitude; - } - position.setLongitude(longitude); + DateBuilder dateBuilder = new DateBuilder() + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt()); - // Speed - position.setSpeed(Double.parseDouble(parser.group(index++))); + position.setValid(parser.next().equals("A")); + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); + position.setSpeed(parser.nextDouble()); + position.setCourse(parser.nextDouble()); - // Course - String course = parser.group(index++); - if (course != null) { - position.setCourse(Double.parseDouble(course)); - } - - // Date - time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(parser.group(index++))); - time.set(Calendar.MONTH, Integer.parseInt(parser.group(index++)) - 1); - time.set(Calendar.YEAR, 2000 + Integer.parseInt(parser.group(index++))); - position.setTime(time.getTime()); + dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); - // Signal - position.set("signal", parser.group(index++)); + position.set("signal", parser.next()); + position.set(Event.KEY_ALARM, parser.next()); - // Alarm - position.set(Event.KEY_ALARM, parser.group(index++)); - - // Get device by IMEI - if (!identify(parser.group(index++), channel)) { + if (!identify(parser.next(), channel)) { return null; } position.setDeviceId(getDeviceId()); if (full) { + position.set(Event.KEY_SATELLITES, parser.next().replaceFirst("^0*(?![\\.$])", "")); - // Satellites - position.set(Event.KEY_SATELLITES, parser.group(index++).replaceFirst("^0*(?![\\.$])", "")); - - // Altitude - String altitude = parser.group(index++); - if (altitude != null) { - position.setAltitude(Double.parseDouble(altitude)); - } + position.setAltitude(parser.nextDouble()); - // Power - position.set(Event.KEY_POWER, Double.parseDouble(parser.group(index++))); + position.set(Event.KEY_POWER, parser.nextDouble()); } return position; diff --git a/test/org/traccar/protocol/XexunProtocolDecoderTest.java b/test/org/traccar/protocol/XexunProtocolDecoderTest.java index a228a1e2a..db112f1ea 100644 --- a/test/org/traccar/protocol/XexunProtocolDecoderTest.java +++ b/test/org/traccar/protocol/XexunProtocolDecoderTest.java @@ -12,7 +12,8 @@ public class XexunProtocolDecoderTest extends ProtocolDecoderTest { XexunProtocolDecoder decoder = new XexunProtocolDecoder(new XexunProtocol(), false); verifyPosition(decoder, text( - "GPRMC,150120.000,A,3346.4463,S,15057.3083,E,0.0,117.4,010911,,,A*76,F,imei:351525010943661,")); + "GPRMC,150120.000,A,3346.4463,S,15057.3083,E,0.0,117.4,010911,,,A*76,F,imei:351525010943661,"), + position("2011-09-01 15:01:20.000", true, -33.77411, 150.95514)); verifyPosition(decoder, text( "GPRMC,010203.000,A,0102.0003,N,00102.0003,E,1.02,1.02,010203,,,A*00,F,,imei:10000000000000,")); @@ -47,7 +48,8 @@ public class XexunProtocolDecoderTest extends ProtocolDecoderTest { ",+48606717068,,L,, imei:012207005047292,,,F:4.28V,1,52,11565,247,01,000E,1FC5")); verifyPosition(decoder, text( - "130302125349,+79604870506,GPRMC,085349.000,A,4503.2392,N,03858.5660,E,6.95,154.65,020313,,,A*6C,F,, imei:012207007744243,03,-1.5,F:4.15V,1,139,28048,250,01,278A,5072")); + "130302125349,+79604870506,GPRMC,085349.000,A,4503.2392,N,03858.5660,E,6.95,154.65,020313,,,A*6C,F,, imei:012207007744243,03,-1.5,F:4.15V,1,139,28048,250,01,278A,5072"), + position("2013-03-02 08:53:49.000", true, 45.05399, 38.97610)); verifyPosition(decoder, text( "111111120009,+436763737552,GPRMC,120009.590,A,4639.6774,N,01418.5737,E,0.00,0.00,111111,,,A*68,F,, imei:359853000144328,04,481.2,F:4.15V,0,139,2689,232,03,2725,0576")); |