diff options
Diffstat (limited to 'src/org/traccar/protocol/TopflytechProtocolDecoder.java')
-rw-r--r-- | src/org/traccar/protocol/TopflytechProtocolDecoder.java | 79 |
1 files changed, 27 insertions, 52 deletions
diff --git a/src/org/traccar/protocol/TopflytechProtocolDecoder.java b/src/org/traccar/protocol/TopflytechProtocolDecoder.java index 5ea431709..1bde4f399 100644 --- a/src/org/traccar/protocol/TopflytechProtocolDecoder.java +++ b/src/org/traccar/protocol/TopflytechProtocolDecoder.java @@ -24,6 +24,9 @@ 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.Position; public class TopflytechProtocolDecoder extends BaseProtocolDecoder { @@ -32,75 +35,47 @@ public class TopflytechProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern pattern = Pattern.compile( - "\\(" + - "(\\d+)" + // IMEI - ".*" + - "(\\d{2})(\\d{2})(\\d{2})" + // Date (YYMMDD) - "(\\d{2})(\\d{2})(\\d{2})" + // Time (HHMMSS) - "([AV])" + // Validity - "(\\d{2})(\\d{2}\\.\\d{4})" + // Latitude (DDMM.MMMM) - "([NS])" + - "(\\d{3})(\\d{2}\\.\\d{4})" + // Longitude (DDDMM.MMMM) - "([EW])" + - "(\\d{3}\\.\\d)" + // Speed - "(\\d+\\.\\d+)"); // Course + private static final Pattern PATTERN = new PatternBuilder() + .txt("(") + .num("(d+)") // imei + .any() + .num("(dd)(dd)(dd)") // date (yymmdd) + .num("(dd)(dd)(dd)") // time + .xpr("([AV])") + .num("(dd)(dd.dddd)([NS])") // latitude + .num("(ddd)(dd.dddd)([EW])") // longitude + .num("(ddd.d)") // speed + .num("(d+.d+)") // course + .compile(); @Override protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) - throws Exception { + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - String sentence = (String) msg; - - // Parse message - Matcher parser = pattern.matcher(sentence); + Parser parser = new Parser(PATTERN, (String) msg); if (!parser.matches()) { return null; } - // Create new position Position position = new Position(); position.setProtocol(getProtocolName()); - Integer index = 1; - // Identifier - if (!identify(parser.group(index++), channel)) { + if (!identify(parser.next(), channel)) { return null; } position.setDeviceId(getDeviceId()); - // Time - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - time.set(Calendar.YEAR, 2000 + 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()); - - // 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; - if (parser.group(index++).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()); - // 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 - position.setCourse(Double.parseDouble(parser.group(index++))); return position; } |