diff options
-rw-r--r-- | src/org/traccar/protocol/T55ProtocolDecoder.java | 158 | ||||
-rw-r--r-- | test/org/traccar/protocol/T55ProtocolDecoderTest.java | 11 |
2 files changed, 156 insertions, 13 deletions
diff --git a/src/org/traccar/protocol/T55ProtocolDecoder.java b/src/org/traccar/protocol/T55ProtocolDecoder.java index b95b9fc5a..ba85b8f1a 100644 --- a/src/org/traccar/protocol/T55ProtocolDecoder.java +++ b/src/org/traccar/protocol/T55ProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 2013 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ import org.jboss.netty.channel.ChannelHandlerContext; import org.traccar.BaseProtocolDecoder; import org.traccar.ServerManager; import org.traccar.helper.Log; +import org.traccar.model.ExtendedInfoFormatter; import org.traccar.model.Position; public class T55ProtocolDecoder extends BaseProtocolDecoder { @@ -34,12 +35,9 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { super(serverManager); } - /** - * Regular expressions pattern - */ - static private Pattern pattern = Pattern.compile( + static private Pattern patternGPRMC = Pattern.compile( "\\$GPRMC," + - "(\\d{2})(\\d{2})(\\d{2})\\.(\\d+)," + // Time (HHMMSS.SSS) + "(\\d{2})(\\d{2})(\\d{2})\\.?\\d*," + // Time (HHMMSS.SSS) "([AV])," + // Validity "(\\d{2})(\\d{2}\\.\\d+)," + // Latitude (DDMM.MMMM) "([NS])," + @@ -48,8 +46,28 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { "(\\d+\\.?\\d*)?," + // Speed "(\\d+\\.?\\d*)?," + // Course "(\\d{2})(\\d{2})(\\d{2})" + // Date (DDMMYY) - ".+"); // Other (Checksumm) + ".+"); + static private Pattern patternGPGGA = Pattern.compile( + "\\$GPGGA," + + "(\\d{2})(\\d{2})(\\d{2})\\.?\\d*," + // Time + "(\\d{2})(\\d{2}\\.\\d+)," + // Latitude + "([NS])," + + "(\\d{3})(\\d{2}\\.\\d+)," + // Longitude + "([EW])," + + ".+"); + + static private Pattern patternGPRMA = Pattern.compile( + "\\$GPRMA," + + "([AV])," + // Validity + "(\\d{2})(\\d{2}\\.\\d+)," + // Latitude + "([NS])," + + "(\\d{3})(\\d{2}\\.\\d+)," + // Longitude + "([EW]),,," + + "(\\d+\\.?\\d*)?," + // Speed + "(\\d+\\.?\\d*)?," + // Course + ".+"); + @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, Object msg) @@ -57,8 +75,8 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { String sentence = (String) msg; - // Detect device ID - if (sentence.contains("$PGID")) { + // Identification + if (sentence.startsWith("$PGID")) { String imei = sentence.substring(6, sentence.length() - 3); try { deviceId = getDataManager().getDeviceByImei(imei).getId(); @@ -67,8 +85,18 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { } } - // Parse message - else if (sentence.contains("$GPRMC") && deviceId != null) { + // Identification + else if (sentence.startsWith("$PCPTI")) { + String id = sentence.substring(7, sentence.indexOf(",", 7)); + try { + deviceId = getDataManager().getDeviceByImei(id).getId(); + } catch(Exception error) { + Log.warning("Unknown device - " + id); + } + } + + // Location + else if (sentence.startsWith("$GPRMC") && deviceId != null) { // Send response if (channel != null) { @@ -76,13 +104,14 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { } // Parse message - Matcher parser = pattern.matcher(sentence); + Matcher parser = patternGPRMC.matcher(sentence); if (!parser.matches()) { return null; } // Create new position Position position = new Position(); + ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("t55"); position.setDeviceId(deviceId); Integer index = 1; @@ -93,7 +122,6 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { time.set(Calendar.HOUR, Integer.valueOf(parser.group(index++))); time.set(Calendar.MINUTE, Integer.valueOf(parser.group(index++))); time.set(Calendar.SECOND, Integer.valueOf(parser.group(index++))); - index += 1; // Skip milliseconds // Validity position.setValid(parser.group(index++).compareTo("A") == 0 ? true : false); @@ -135,9 +163,113 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { // Altitude position.setAltitude(0.0); + position.setExtendedInfo(extendedInfo.toString()); + return position; + } + + // Location + else if (sentence.startsWith("$GPGGA") && deviceId != null) { + + // Parse message + Matcher parser = patternGPGGA.matcher(sentence); + if (!parser.matches()) { + return null; + } + + // Create new position + Position position = new Position(); + ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("t55"); + position.setDeviceId(deviceId); + + Integer index = 1; + + // Time + Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + time.set(Calendar.HOUR, Integer.valueOf(parser.group(index++))); + time.set(Calendar.MINUTE, Integer.valueOf(parser.group(index++))); + time.set(Calendar.SECOND, Integer.valueOf(parser.group(index++))); + time.set(Calendar.MILLISECOND, 0); + position.setTime(time.getTime()); + + // Validity + position.setValid(true); + + // Latitude + Double latitude = Double.valueOf(parser.group(index++)); + latitude += Double.valueOf(parser.group(index++)) / 60; + if (parser.group(index++).compareTo("S") == 0) latitude = -latitude; + position.setLatitude(latitude); + + // Longitude + Double lonlitude = Double.valueOf(parser.group(index++)); + lonlitude += Double.valueOf(parser.group(index++)) / 60; + if (parser.group(index++).compareTo("W") == 0) lonlitude = -lonlitude; + position.setLongitude(lonlitude); + + // Altitude + position.setAltitude(0.0); + + position.setExtendedInfo(extendedInfo.toString()); return position; } + // Location + else if (sentence.startsWith("$GPRMA") && deviceId != null) { + + // Parse message + Matcher parser = patternGPRMA.matcher(sentence); + if (!parser.matches()) { + return null; + } + + // Create new position + Position position = new Position(); + ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("t55"); + position.setDeviceId(deviceId); + + Integer index = 1; + + // Time + position.setTime(Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime()); + + // Validity + position.setValid(parser.group(index++).compareTo("A") == 0 ? true : false); + + // Latitude + Double latitude = Double.valueOf(parser.group(index++)); + latitude += Double.valueOf(parser.group(index++)) / 60; + if (parser.group(index++).compareTo("S") == 0) latitude = -latitude; + position.setLatitude(latitude); + + // Longitude + Double lonlitude = Double.valueOf(parser.group(index++)); + lonlitude += Double.valueOf(parser.group(index++)) / 60; + if (parser.group(index++).compareTo("W") == 0) lonlitude = -lonlitude; + position.setLongitude(lonlitude); + + // Speed + String speed = parser.group(index++); + if (speed != null) { + position.setSpeed(Double.valueOf(speed)); + } else { + position.setSpeed(0.0); + } + + // Course + String course = parser.group(index++); + if (course != null) { + position.setCourse(Double.valueOf(course)); + } else { + position.setCourse(0.0); + } + + // Altitude + position.setAltitude(0.0); + + position.setExtendedInfo(extendedInfo.toString()); + return position; + } + return null; } diff --git a/test/org/traccar/protocol/T55ProtocolDecoderTest.java b/test/org/traccar/protocol/T55ProtocolDecoderTest.java index f7aac7030..691474043 100644 --- a/test/org/traccar/protocol/T55ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/T55ProtocolDecoderTest.java @@ -14,6 +14,8 @@ public class T55ProtocolDecoderTest { assertNull(decoder.decode(null, null, "$PGID,359853000144328*0F")); + assertNull(decoder.decode(null, null, "$PCPTI,CradlePoint Test,184453,184453.0,6F*57")); + assertNotNull(decoder.decode(null, null, "$GPRMC,094907.000,A,6000.5332,N,03020.5192,E,1.17,60.26,091111,,*33")); @@ -22,6 +24,15 @@ public class T55ProtocolDecoderTest { assertNotNull(decoder.decode(null, null, "$GPRMC,064411.000,A,3717.240078,N,00603.046984,W,0.000,1,010313,,,A*6C")); + + assertNotNull(decoder.decode(null, null, + "$GPGGA,184453.0,4337.200755,N,11611.955704,W,1,05,3.5,825.5,M,-11.0,M,,*6F")); + + assertNotNull(decoder.decode(null, null, + "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47")); + + assertNotNull(decoder.decode(null, null, + "$GPRMA,V,0000.00,S,00000.00,E,,,00.0,000.,11.,E*7")); } |