From 111ea9bbec5516c36939df8eec4f7ba3edb268bf Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 5 Nov 2015 14:23:43 +1300 Subject: Refacor Gotop protocol decoder --- src/org/traccar/protocol/GotopProtocolDecoder.java | 87 ++++++++-------------- 1 file changed, 31 insertions(+), 56 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/GotopProtocolDecoder.java b/src/org/traccar/protocol/GotopProtocolDecoder.java index 16348cb7f..cfae1b0f6 100644 --- a/src/org/traccar/protocol/GotopProtocolDecoder.java +++ b/src/org/traccar/protocol/GotopProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2014 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2013 - 2015 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. @@ -16,12 +16,12 @@ package org.traccar.protocol; import java.net.SocketAddress; -import java.util.Calendar; -import java.util.TimeZone; -import java.util.regex.Matcher; 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.helper.UnitsConverter; import org.traccar.model.Event; import org.traccar.model.Position; @@ -32,77 +32,52 @@ public class GotopProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - "(\\d+)," + // IMEI - "[^,]+," + // Type - "([AV])," + // Validity - "DATE:(\\d{2})(\\d{2})(\\d{2})," + // Date (YYMMDD) - "TIME:(\\d{2})(\\d{2})(\\d{2})," + // Time (HHMMSS) - "LAT:(\\d+.\\d+)([NS])," + // Latitude - "LOT:(\\d+.\\d+)([EW])," + // Longitude - "Speed:(\\d+.\\d+)," + // Speed - "([^,]+)," + // Status - "(\\d+)?" + // Course - ".*"); + private static final Pattern PATTERN = new PatternBuilder() + .number("(d+),") // imei + .expression("[^,]+,") // type + .expression("([AV]),") // validity + .number("DATE:(dd)(dd)(dd),") // date (yyddmm) + .number("TIME:(dd)(dd)(dd),") // time + .number("LAT:(d+.d+)([NS]),") // latitude + .number("LOT:(d+.d+)([EW]),") // longitude + .text("Speed:").number("(d+.d+),") // speed + .expression("([^,]+),") // status + .number("(d+)?") // course + .any() + .compile(); @Override protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) - throws Exception { + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - // Parse message - String sentence = (String) msg; - Matcher parser = PATTERN.matcher(sentence); - if (sentence.isEmpty() || !parser.matches()) { + 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; - // Get device by IMEI - if (!identify(parser.group(index++), channel)) { + if (!identify(parser.next(), channel)) { return null; } position.setDeviceId(getDeviceId()); - // Validity - position.setValid(parser.group(index++).compareTo("A") == 0); + position.setValid(parser.next().equals("A")); - // 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()); + DateBuilder dateBuilder = new DateBuilder() + .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); - // Latitude - Double latitude = Double.parseDouble(parser.group(index++)); - if (parser.group(index++).compareTo("S") == 0) latitude = -latitude; - position.setLatitude(latitude); + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble())); - // Longitude - Double longitude = Double.parseDouble(parser.group(index++)); - if (parser.group(index++).compareTo("W") == 0) longitude = -longitude; - position.setLongitude(longitude); + position.set(Event.KEY_STATUS, parser.next()); - // Speed - position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(parser.group(index++)))); + position.setCourse(parser.nextDouble()); - // Status - position.set(Event.KEY_STATUS, parser.group(index++)); - - // Course - String course = parser.group(index++); - if (course != null) { - position.setCourse(Double.parseDouble(course)); - } return position; } -- cgit v1.2.3