diff options
-rw-r--r-- | src/org/traccar/protocol/CityeasyProtocolDecoder.java | 105 | ||||
-rw-r--r-- | test/org/traccar/protocol/CityeasyProtocolDecoderTest.java | 12 |
2 files changed, 58 insertions, 59 deletions
diff --git a/src/org/traccar/protocol/CityeasyProtocolDecoder.java b/src/org/traccar/protocol/CityeasyProtocolDecoder.java index 67da05a8d..213dd90d0 100644 --- a/src/org/traccar/protocol/CityeasyProtocolDecoder.java +++ b/src/org/traccar/protocol/CityeasyProtocolDecoder.java @@ -17,15 +17,15 @@ package org.traccar.protocol; import java.net.SocketAddress; import java.nio.charset.Charset; -import java.util.Calendar; -import java.util.TimeZone; -import java.util.regex.Matcher; import java.util.regex.Pattern; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.helper.Checksum; +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; @@ -35,21 +35,24 @@ public class CityeasyProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - "(\\d{4})(\\d{2})(\\d{2})" + // Date - "(\\d{2})(\\d{2})(\\d{2})," + // Time - "([AV])," + // Validity - "(\\d+)," + // Satellites - "([NS]),(\\d+\\.\\d+)," + // Latitude - "([EW]),(\\d+\\.\\d+)," + // Longitude - "(\\d+\\.\\d)," + // Speed - "(\\d+\\.\\d)," + // HDOP - "(\\d+\\.\\d);" + // Altitude - "(\\d+)," + // MCC - "(\\d+)," + // MNC - "(\\d+)," + // LAC - "(\\d+)" + // Cell - ".*"); + private static final Pattern PATTERN = new PatternBuilder() + .groupBegin() + .number("(dddd)(dd)(dd)") // date + .number("(dd)(dd)(dd),") // time + .number("([AV]),") // validity + .number("(d+),") // satellites + .number("([NS]),(d+.d+),") // latitude + .number("([EW]),(d+.d+),") // longitude + .number("(d+.d),") // speed + .number("(d+.d),") // hdop + .number("(d+.d)") // altitude + .groupEnd("?").text(";") + .number("(d+),") // mcc + .number("(d+),") // mnc + .number("(d+),") // lac + .number("(d+)") // cell + .any() + .compile(); public static final int MSG_ADDRESS_REQUEST = 0x0001; public static final int MSG_STATUS = 0x0002; @@ -62,8 +65,7 @@ public class CityeasyProtocolDecoder extends BaseProtocolDecoder { @Override protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) - throws Exception { + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ChannelBuffer buf = (ChannelBuffer) msg; @@ -80,7 +82,7 @@ public class CityeasyProtocolDecoder extends BaseProtocolDecoder { if (type == MSG_LOCATION_REPORT || type == MSG_LOCATION_REQUEST) { String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 8, Charset.defaultCharset()); - Matcher parser = PATTERN.matcher(sentence); + Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; } @@ -89,46 +91,33 @@ public class CityeasyProtocolDecoder extends BaseProtocolDecoder { position.setProtocol(getProtocolName()); position.setDeviceId(getDeviceId()); - Integer index = 1; - - // Date and time - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - time.set(Calendar.YEAR, Integer.parseInt(parser.group(index++))); - time.set(Calendar.MONTH, Integer.parseInt(parser.group(index++)) - 1); - time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(parser.group(index++))); - 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++))); - position.setTime(time.getTime()); - - position.setValid(parser.group(index++).equals("A")); - position.set(Event.KEY_SATELLITES, parser.group(index++)); - - // Latitude - String hemisphere = parser.group(index++); - double latitude = Double.parseDouble(parser.group(index++)); - if (hemisphere.compareTo("S") == 0) { - latitude = -latitude; - } - position.setLatitude(latitude); + if (parser.hasNext(15)) { - // Longitude - hemisphere = parser.group(index++); - double longitude = Double.parseDouble(parser.group(index++)); - if (hemisphere.compareTo("W") == 0) { - longitude = -longitude; - } - position.setLongitude(longitude); + DateBuilder dateBuilder = new DateBuilder() + .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); + + position.setValid(parser.next().equals("A")); + position.set(Event.KEY_SATELLITES, parser.next()); + + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG)); - position.setSpeed(Double.parseDouble(parser.group(index++))); - position.set(Event.KEY_HDOP, Double.parseDouble(parser.group(index++))); - position.setAltitude(Double.parseDouble(parser.group(index++))); + position.setSpeed(parser.nextDouble()); + position.set(Event.KEY_HDOP, parser.nextDouble()); + position.setAltitude(parser.nextDouble()); + + } else { + + getLastLocation(position, null); + + } - position.set(Event.KEY_MCC, Integer.parseInt(parser.group(index++))); - position.set(Event.KEY_MNC, Integer.parseInt(parser.group(index++))); - position.set(Event.KEY_LAC, Integer.parseInt(parser.group(index++))); - position.set(Event.KEY_CELL, Integer.parseInt(parser.group(index++))); + position.set(Event.KEY_MCC, parser.nextInt()); + position.set(Event.KEY_MNC, parser.nextInt()); + position.set(Event.KEY_LAC, parser.nextInt()); + position.set(Event.KEY_CELL, parser.nextInt()); return position; } diff --git a/test/org/traccar/protocol/CityeasyProtocolDecoderTest.java b/test/org/traccar/protocol/CityeasyProtocolDecoderTest.java index 33ee51c4f..7f39e5dd6 100644 --- a/test/org/traccar/protocol/CityeasyProtocolDecoderTest.java +++ b/test/org/traccar/protocol/CityeasyProtocolDecoderTest.java @@ -13,6 +13,15 @@ public class CityeasyProtocolDecoderTest extends ProtocolDecoderTest { CityeasyProtocolDecoder decoder = new CityeasyProtocolDecoder(new CityeasyProtocol()); + verifyAttributes(decoder, binary( + "545400853575570249020100033b3430342c34352c31303638312c31313632312c33352c31303638312c31313632322c32332c31303638312c32383938332c32332c31303638312c31313632332c32312c31303638312c32333338312c31372c31303638312c32323538332c31372c31303638312c32363434312c31330000000d352e0d0a")); + + verifyNothing(decoder, binary( + "54540019357557024902010002520704100000000bbe700d0a")); + + verifyNothing(decoder, binary( + "5454001735755702490201434a01000000000c24280d0a")); + verifyNothing(decoder, binary( "545400153520000000000100010000000111000D0A")); @@ -20,7 +29,8 @@ public class CityeasyProtocolDecoderTest extends ProtocolDecoderTest { "54540019357557024902000002520704300000000376390d0a")); verifyPosition(decoder, binary( - "5454006135200000000001000332303134313131303039353430392C412C342C4E2C32322E3533373232382C452C3131342E3032323737342C302E312C312E392C35302E363B3436302C302C31303137332C343635322C34310000000B63130D0A")); + "5454006135200000000001000332303134313131303039353430392C412C342C4E2C32322E3533373232382C452C3131342E3032323737342C302E312C312E392C35302E363B3436302C302C31303137332C343635322C34310000000B63130D0A"), + position("2014-11-10 09:54:09.000", true, 22.53723, 114.02277)); verifyPosition(decoder, binary( "5454006135200000000001000432303134313131303039353330362C412C352C4E2C32322E3533373233352C452C3131342E3032323838312C302E322C312E362C35342E313B3436302C302C31303137332C343635322C343100000045EC620D0A")); |