diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2011-11-09 12:07:58 +0000 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2011-11-09 12:07:58 +0000 |
commit | 139177767ab256033a5c3b71ea84b92c1dcbe504 (patch) | |
tree | 0294e0a962329d0bdc5b95da7dd6ffa514906322 | |
parent | cc1f8e376ddbc7a5db0256e3ae7697774fa76dbd (diff) | |
download | trackermap-server-139177767ab256033a5c3b71ea84b92c1dcbe504.tar.gz trackermap-server-139177767ab256033a5c3b71ea84b92c1dcbe504.tar.bz2 trackermap-server-139177767ab256033a5c3b71ea84b92c1dcbe504.zip |
5 files changed, 158 insertions, 72 deletions
diff --git a/src/net/sourceforge/opentracking/Server.java b/src/net/sourceforge/opentracking/Server.java index b0fb8d96a..66d0ba3d9 100644 --- a/src/net/sourceforge/opentracking/Server.java +++ b/src/net/sourceforge/opentracking/Server.java @@ -93,6 +93,7 @@ public class Server implements DataManager { initGps103Server(properties); initTk103Server(properties); initGl200Server(properties); + initT55Server(properties); } /** @@ -470,7 +471,7 @@ public class Server implements DataManager { if (serverCreator.isLoggerEnabled()) { pipeline.addLast("logger", new LoggingHandler("logger")); } - byte delimiter[] = { (byte) '\r' }; + byte delimiter[] = { (byte) '\r', (byte) '\n' }; pipeline.addLast("frameDecoder", new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); pipeline.addLast("stringDecoder", new StringDecoder()); diff --git a/src/net/sourceforge/opentracking/protocol/t55/T55ProtocolDecoder.java b/src/net/sourceforge/opentracking/protocol/t55/T55ProtocolDecoder.java index 967165bcb..01954b474 100644 --- a/src/net/sourceforge/opentracking/protocol/t55/T55ProtocolDecoder.java +++ b/src/net/sourceforge/opentracking/protocol/t55/T55ProtocolDecoder.java @@ -45,6 +45,11 @@ public class T55ProtocolDecoder extends OneToOneDecoder { * Reset connection delay */ private Integer resetDelay; + + /** + * Device ID + */ + private Long deviceId; /** * Init device table @@ -58,21 +63,17 @@ public class T55ProtocolDecoder extends OneToOneDecoder { * Regular expressions pattern */ static private Pattern pattern = Pattern.compile( - "\\(" + - "(\\d+)" + // Device ID - "(.{4})" + // Command - "(\\d{15})" + // IMEI (?) - "(\\d{2})(\\d{2})(\\d{2})" + // Date (YYMMDD) - "([AV])" + // Validity - "(\\d{2})(\\d{2}.\\d{4})" + // Latitude (DDMM.MMMM) - "([NS])" + - "(\\d{3})(\\d{2}.\\d{4})" + // Longitude (DDDMM.MMMM) - "([EW])" + - "(\\d+.\\d)" + // Speed - "(\\d{2})(\\d{2})(\\d{2})" + // Time (HHMMSS) - "(\\d+.\\d{2})" + // Course - "(\\d+)" + // State - ".+"); // Mileage (?) + "\\$GPRMC," + + "(\\d{2})(\\d{2})(\\d{2}).(\\d{3})," + // Time (HHMMSS.SSS) + "([AV])," + // Validity + "(\\d{2})(\\d{2}.\\d{4})," + // Latitude (DDMM.MMMM) + "([NS])," + + "(\\d{3})(\\d{2}.\\d{4})," + // Longitude (DDDMM.MMMM) + "([EW])," + + "(\\d+.\\d{2})?," + // Speed + "(\\d+.\\d{2})?," + // Course + "(\\d{2})(\\d{2})(\\d{2})" + // Date (DDMMYY) + ".+"); // Other (Checksumm) /** * Decode message @@ -82,65 +83,87 @@ public class T55ProtocolDecoder extends OneToOneDecoder { throws Exception { String sentence = (String) msg; + + //System.out.println(sentence); - System.out.println(sentence); // DELME - - // TODO: Send response (?) - - // Parse message - Matcher parser = pattern.matcher(sentence); - if (!parser.matches()) { - return null; + // Detect device ID + if (sentence.contains("$PGID")) { + String imei = sentence.substring(6, 6 + 15); + deviceId = dataManager.getDeviceByImei(imei).getId(); } - // Create new position - Position position = new Position(); - - Integer index = 1; - index += 2; // Skip Device ID and command - - // Get device by IMEI - String imei = parser.group(index++); - position.setDeviceId(dataManager.getDeviceByImei(imei).getId()); - - // Date - Calendar time = new GregorianCalendar(TimeZone.getTimeZone("UTC")); - time.clear(); - time.set(Calendar.YEAR, 2000 + Integer.valueOf(parser.group(index++))); - time.set(Calendar.MONTH, Integer.valueOf(parser.group(index++)) - 1); - time.set(Calendar.DAY_OF_MONTH, Integer.valueOf(parser.group(index++))); - - // 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); - - // Altitude - position.setAltitude(0.0); + // Parse message + else if (sentence.contains("$GPRMC") && deviceId != null) { - // Speed - position.setSpeed(Double.valueOf(parser.group(index++))); + // Send response + if (channel != null) { + channel.write("OK1\r\n"); + } - // Time - 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++))); - position.setTime(time.getTime()); + // Parse message + Matcher parser = pattern.matcher(sentence); + if (!parser.matches()) { + return null; + } + + // Create new position + Position position = new Position(); + position.setDeviceId(deviceId); + + Integer index = 1; + + // Time + Calendar time = new GregorianCalendar(TimeZone.getTimeZone("UTC")); + time.clear(); + 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 + position.setTime(time.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 - position.setCourse(Double.valueOf(parser.group(index++))); + // Course + String course = parser.group(index++); + if (course != null) { + position.setCourse(Double.valueOf(course)); + } else { + position.setCourse(0.0); + } + + // Date + time.set(Calendar.DAY_OF_MONTH, Integer.valueOf(parser.group(index++))); + time.set(Calendar.MONTH, Integer.valueOf(parser.group(index++)) - 1); + time.set(Calendar.YEAR, 2000 + Integer.valueOf(parser.group(index++))); + + // Altitude + position.setAltitude(0.0); + + return position; + } - return position; + return null; } /** diff --git a/src/net/sourceforge/opentracking/protocol/tk103/Tk103ProtocolDecoder.java b/src/net/sourceforge/opentracking/protocol/tk103/Tk103ProtocolDecoder.java index ff7c41fb2..12108b121 100644 --- a/src/net/sourceforge/opentracking/protocol/tk103/Tk103ProtocolDecoder.java +++ b/src/net/sourceforge/opentracking/protocol/tk103/Tk103ProtocolDecoder.java @@ -83,8 +83,6 @@ public class Tk103ProtocolDecoder extends OneToOneDecoder { String sentence = (String) msg; - System.out.println(sentence); // DELME - // TODO: Send response (?) // Parse message diff --git a/src/net/sourceforge/opentracking/protocol/xexun/XexunProtocolDecoder.java b/src/net/sourceforge/opentracking/protocol/xexun/XexunProtocolDecoder.java index 73b7cef11..872569e1a 100644 --- a/src/net/sourceforge/opentracking/protocol/xexun/XexunProtocolDecoder.java +++ b/src/net/sourceforge/opentracking/protocol/xexun/XexunProtocolDecoder.java @@ -66,8 +66,8 @@ public class XexunProtocolDecoder extends OneToOneDecoder { "([NS])," + "([\\d]{3})([\\d]{2}.[\\d]{4})," + // Longitude (DDDMM.MMMM) "([EW])," + - "([\\d]+.[\\d]+)," + // Speed - "([\\d]+.[\\d]+)?," + // Course + "([\\d]+.[\\d]+)," + // Speed + "([\\d]+.[\\d]+)?," + // Course "([\\d]{2})([\\d]{2})([\\d]{2})," + // Date (DDMMYY) ".*imei:" + "([\\d]+),"); // IMEI diff --git a/test/net/sourceforge/opentracking/protocol/t55/T55ProtocolDecoderTest.java b/test/net/sourceforge/opentracking/protocol/t55/T55ProtocolDecoderTest.java new file mode 100644 index 000000000..b63f91524 --- /dev/null +++ b/test/net/sourceforge/opentracking/protocol/t55/T55ProtocolDecoderTest.java @@ -0,0 +1,64 @@ +package net.sourceforge.opentracking.protocol.t55; + +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.List; +import org.junit.Test; +import net.sourceforge.opentracking.Device; +import net.sourceforge.opentracking.Position; +import net.sourceforge.opentracking.DataManager; +import static org.junit.Assert.*; + +public class T55ProtocolDecoderTest { + + private class TestDataManager implements DataManager { + public List getDevices() { + return null; + } + + public Device getDeviceByImei(String imei) { + Device device = new Device(); + device.setId(new Long(1)); + device.setImei("10000000000000"); + return device; + } + + public void setPosition(Position position) { + } + } + + @Test + public void testDecode() throws Exception { + + String test1 = "$PGID,359853000144328*0F"; + + String test2 = "$GPRMC,094907.000,A,6000.5332,N,03020.5192,E,1.17,60.26,091111,,*33"; + //$GPRMC,115528.000,A,6000.5432,N,03020.4948,E,,,091111,,*06 + + T55ProtocolDecoder decoder = new T55ProtocolDecoder(new TestDataManager(), 0); + decoder.decode(null, null, test1); + + Position position = (Position) decoder.decode(null, null, test2); + + Date time = new GregorianCalendar(2003, 1, 1, 1, 2, 3).getTime(); + assertEquals(time, position.getTime()); + + assertEquals(true, position.getValid()); + + Double latitude = 1.0 + 2.0003 / 60.0; + assertEquals(latitude, position.getLatitude()); + + Double longitude = 1.0 + 2.0003 / 60.0; + assertEquals(longitude, position.getLongitude()); + + Double speed = 1.02; + assertEquals(speed, position.getSpeed()); + + Double course = 1.02; + assertEquals(course, position.getCourse()); + + Long deviceId = new Long(1); + assertEquals(deviceId, position.getDeviceId()); + } + +} |