From 6f7493e600316fd672162aa3f2b22b41c2642ebb Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 8 Apr 2013 23:47:53 +1200 Subject: Add trimble taip protocol (fix #152) --- src/org/traccar/protocol/SyrusProtocolDecoder.java | 41 ++++++++++++++++------ .../traccar/protocol/SyrusProtocolDecoderTest.java | 3 ++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/org/traccar/protocol/SyrusProtocolDecoder.java b/src/org/traccar/protocol/SyrusProtocolDecoder.java index 10aec4f26..7e809212a 100644 --- a/src/org/traccar/protocol/SyrusProtocolDecoder.java +++ b/src/org/traccar/protocol/SyrusProtocolDecoder.java @@ -36,15 +36,14 @@ public class SyrusProtocolDecoder extends BaseProtocolDecoder { /** * Regular expressions pattern */ - //"REV691615354941+3570173+1397742703203212;ID=Test" static private Pattern pattern = Pattern.compile( - "REV" + // Type - "\\d{2}" + // Event index + "R[EP]V" + // Type + "(?:\\d{2}" + // Event index "(\\d{4})" + // Week - "(\\d)" + // Day + "(\\d))?" + // Day "(\\d{5})" + // Seconds - "([\\+\\-]\\d{2})(\\d{5})" + // Latitude - "([\\+\\-]\\d{3})(\\d{5})" + // Longitude + "([\\+\\-]\\d{2})(\\d{5})" + // Latitude + "([\\+\\-]\\d{3})(\\d{5})" + // Longitude "(\\d{3})" + // Speed "(\\d{3})" + // Course "\\d" + // Fix mode @@ -64,6 +63,24 @@ public class SyrusProtocolDecoder extends BaseProtocolDecoder { return new Date(millis); } + private Date getTime(long seconds) { + Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + time.set(Calendar.HOUR, 0); + time.set(Calendar.MINUTE, 0); + time.set(Calendar.SECOND, 0); + time.set(Calendar.MILLISECOND, 0); + + long millis = time.getTimeInMillis() + seconds * 1000; + + long diff = new Date().getTime() - millis; + + if (diff > 12 * 60 * 60 * 1000) { + millis += 24 * 60 * 60 * 1000; + } + + return new Date(millis); + } + @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, Object msg) @@ -114,12 +131,16 @@ public class SyrusProtocolDecoder extends BaseProtocolDecoder { position.setDeviceId(deviceId); Integer index = 1; - + // Time - int week = Integer.valueOf(parser.group(index++)); - int day = Integer.valueOf(parser.group(index++)); + String week = parser.group(index++); + String day = parser.group(index++); int seconds = Integer.valueOf(parser.group(index++)); - position.setTime(getTime(week, day, seconds)); + if (week != null && day != null) { + position.setTime(getTime(Integer.valueOf(week), Integer.valueOf(day), seconds)); + } else { + position.setTime(getTime(seconds)); + } // Latitude String latitude = parser.group(index) + '.' + parser.group(index + 1); diff --git a/test/org/traccar/protocol/SyrusProtocolDecoderTest.java b/test/org/traccar/protocol/SyrusProtocolDecoderTest.java index d8a704dff..a7c542bf8 100644 --- a/test/org/traccar/protocol/SyrusProtocolDecoderTest.java +++ b/test/org/traccar/protocol/SyrusProtocolDecoderTest.java @@ -11,6 +11,9 @@ public class SyrusProtocolDecoderTest { SyrusProtocolDecoder decoder = new SyrusProtocolDecoder(null); decoder.setDataManager(new TestDataManager()); + assertNotNull(decoder.decode(null, null, + ">RPV15714+3739438-1220384601512612;ID=1234;*7F")); + assertNotNull(decoder.decode(null, null, "\r\n>REV691615354941+3570173+1397742703203212;ID=Test")); -- cgit v1.2.3