diff options
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/helper/DateBuilder.java | 19 | ||||
-rw-r--r-- | src/org/traccar/helper/PatternBuilder.java | 11 | ||||
-rw-r--r-- | src/org/traccar/protocol/TotemProtocolDecoder.java | 224 |
3 files changed, 81 insertions, 173 deletions
diff --git a/src/org/traccar/helper/DateBuilder.java b/src/org/traccar/helper/DateBuilder.java index 094dfff16..77c6821aa 100644 --- a/src/org/traccar/helper/DateBuilder.java +++ b/src/org/traccar/helper/DateBuilder.java @@ -60,16 +60,6 @@ public class DateBuilder { return setDate(year, month, day); } - @Deprecated - public DateBuilder setDate(String year, String month, String day) { - return setDate(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day)); - } - - @Deprecated - public DateBuilder setDateReverse(String day, String month, String year) { - return setDate(year, month, day); - } - public DateBuilder setCurrentDate() { Calendar now = Calendar.getInstance(calendar.getTimeZone()); return setYear(now.get(Calendar.YEAR)).setMonth(now.get(Calendar.MONTH)).setDay(now.get(Calendar.DAY_OF_MONTH)); @@ -104,19 +94,10 @@ public class DateBuilder { return setHour(hour).setMinute(minute).setSecond(second); } - @Deprecated - public DateBuilder setTime(String hour, String minute, String second) { - return setTime(Integer.parseInt(hour), Integer.parseInt(minute), Integer.parseInt(second)); - } - public DateBuilder setTime(int hour, int minute, int second, int millis) { return setHour(hour).setMinute(minute).setSecond(second).setMillis(millis); } - public DateBuilder setDateTime(int year, int month, int day, int hour, int minute, int second) { - return setDate(year, month, day).setTime(hour, minute, second); - } - public Date getDate() { return calendar.getTime(); } diff --git a/src/org/traccar/helper/PatternBuilder.java b/src/org/traccar/helper/PatternBuilder.java index 5d9d3afa8..4ce4295dd 100644 --- a/src/org/traccar/helper/PatternBuilder.java +++ b/src/org/traccar/helper/PatternBuilder.java @@ -33,6 +33,8 @@ public class PatternBuilder { } public PatternBuilder expression(String s) { + s = s.replaceAll("\\|$", "\\\\|"); // special case for delimiter + fragments.add(s); return this; } @@ -63,6 +65,11 @@ public class PatternBuilder { return this; } + public PatternBuilder or() { + fragments.add("|"); + return this; + } + public PatternBuilder groupBegin() { return expression("(?:"); } @@ -75,10 +82,6 @@ public class PatternBuilder { return expression(")" + s); } - public PatternBuilder or() { - return expression("|"); - } - public Pattern compile() { return Pattern.compile(toString(), Pattern.DOTALL); } diff --git a/src/org/traccar/protocol/TotemProtocolDecoder.java b/src/org/traccar/protocol/TotemProtocolDecoder.java index 60285d360..556a9fa1e 100644 --- a/src/org/traccar/protocol/TotemProtocolDecoder.java +++ b/src/org/traccar/protocol/TotemProtocolDecoder.java @@ -22,6 +22,8 @@ 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.model.Event; import org.traccar.model.Position; @@ -65,32 +67,33 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { .any() .compile(); - private static final Pattern PATTERN2 = Pattern.compile( - "\\$\\$" + // Header - "\\p{XDigit}{2}" + // Length - "(\\d+)\\|" + // IMEI - "(..)" + // Alarm Type - "(\\d{2})(\\d{2})(\\d{2})" + // Date (DDMMYY) - "(\\d{2})(\\d{2})(\\d{2})\\|" + // Time (HHMMSS) - "([AV])\\|" + // Validity - "(\\d+)(\\d{2}\\.\\d+)\\|" + // Latitude (DDMM.MMMM) - "([NS])\\|" + - "(\\d+)(\\d{2}\\.\\d+)\\|" + // Longitude (DDDMM.MMMM) - "([EW])\\|" + - "(\\d+\\.\\d+)?\\|" + // Speed - "(\\d+)?\\|" + // Course - "(\\d+\\.\\d+)\\|" + // HDOP - "(\\d+)\\|" + // IO Status - "\\d" + // Charged - "(\\d{2})" + // Battery - "(\\d{2})\\|" + // External Power - "(\\d+)\\|" + // ADC - "(\\p{XDigit}{8})\\|" + // Location Code - "(\\d+)\\|" + // Temperature - "(\\d+.\\d+)\\|" + // Odometer - "\\d+\\|" + // Serial Number - "\\p{XDigit}{4}" + // Checksum - "\r?\n?"); + private static final Pattern PATTERN2 = new PatternBuilder() + .text("$$") // header + .number("xx") // length + .number("(d+)|") // imei + .expression("(..)") // alarm type + .number("(dd)(dd)(dd)") // date (ddmmyy) + .number("(dd)(dd)(dd)|") // time + .expression("([AV])|") // validity + .number("(d+)(dd.d+)|") // latitude + .expression("([NS])|") + .number("(d+)(dd.d+)|") // longitude + .expression("([EW])|") + .number("(d+.d+)?|") // speed + .number("(d+)?|") // course + .number("(d+.d+)|") // hdop + .number("(d+)|") // io status + .number("d") // charged + .number("(dd)") // battery + .number("(dd)|") // external power + .number("(d+)|") // adc + .number("(x{8})|") // location code + .number("(d+)|") // temperature + .number("(d+.d+)|") // odometer + .number("d+|") // serial number + .number("xxxx") // checksum + .any() + .compile(); private static final Pattern PATTERN3 = new PatternBuilder() .text("$$") // header @@ -138,164 +141,85 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { } } - // Parse message - Matcher parser = pattern.matcher(sentence); + Parser parser = new Parser(pattern, sentence); 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()); - // Alarm type - position.set(Event.KEY_ALARM, parser.group(index++)); + position.set(Event.KEY_ALARM, parser.next()); if (pattern == PATTERN1 || pattern == PATTERN2) { - // Time - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); + DateBuilder dateBuilder = new DateBuilder(); int year = 0; if (pattern == PATTERN2) { - time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(parser.group(index++))); - time.set(Calendar.MONTH, Integer.parseInt(parser.group(index++)) - 1); - year = Integer.parseInt(parser.group(index++)); - time.set(Calendar.YEAR, 2000 + year); - } - 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++))); - - // 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); - - // Speed - String speed = parser.group(index++); - if (speed != null) { - position.setSpeed(Double.parseDouble(speed)); + dateBuilder.setDay(parser.nextInt()).setMonth(parser.nextInt()); + year = parser.nextInt(); + dateBuilder.setYear(year); } + dateBuilder.setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); - // Course - String course = parser.group(index++); - if (course != null) { - position.setCourse(Double.parseDouble(course)); - } + position.setValid(parser.next().equals("A")); + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); + position.setSpeed(parser.nextDouble()); + position.setCourse(parser.nextDouble()); - // Date if (pattern == PATTERN1) { - time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(parser.group(index++))); - time.set(Calendar.MONTH, Integer.parseInt(parser.group(index++)) - 1); - year = Integer.parseInt(parser.group(index++)); - time.set(Calendar.YEAR, 2000 + year); + dateBuilder.setDay(parser.nextInt()).setMonth(parser.nextInt()); + year = parser.nextInt(); + dateBuilder.setYear(year); } if (year == 0) { return null; // ignore invalid data } - position.setTime(time.getTime()); - - // Accuracy - position.set(Event.KEY_HDOP, parser.group(index++)); - - // IO Status - position.set(Event.PREFIX_IO + 1, parser.group(index++)); - - // Power - position.set(Event.KEY_BATTERY, parser.group(index++)); - position.set(Event.KEY_POWER, Double.parseDouble(parser.group(index++))); - - // ADC - position.set(Event.PREFIX_ADC + 1, parser.group(index++)); - - // Location Code - position.set(Event.KEY_LAC, parser.group(index++)); + position.setTime(dateBuilder.getDate()); - // Temperature - position.set(Event.PREFIX_TEMP + 1, parser.group(index++)); - - // Odometer - position.set(Event.KEY_ODOMETER, parser.group(index++)); + position.set(Event.KEY_HDOP, parser.next()); + position.set(Event.PREFIX_IO + 1, parser.next()); + position.set(Event.KEY_BATTERY, parser.next()); + position.set(Event.KEY_POWER, parser.nextDouble()); + position.set(Event.PREFIX_ADC + 1, parser.next()); + position.set(Event.KEY_LAC, parser.next()); + position.set(Event.PREFIX_TEMP + 1, parser.next()); + position.set(Event.KEY_ODOMETER, parser.next()); } else if (pattern == PATTERN3) { - // 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()); - - // IO Status - position.set(Event.PREFIX_IO + 1, parser.group(index++)); - - // Power - position.set(Event.KEY_BATTERY, Double.parseDouble(parser.group(index++)) / 10); - position.set(Event.KEY_POWER, Double.parseDouble(parser.group(index++))); - - // ADC - position.set(Event.PREFIX_ADC + 1, parser.group(index++)); - position.set(Event.PREFIX_ADC + 2, parser.group(index++)); - - // Temperature - position.set(Event.PREFIX_TEMP + 1, parser.group(index++)); - position.set(Event.PREFIX_TEMP + 2, parser.group(index++)); - - // Location Code - position.set(Event.KEY_LAC, parser.group(index++)); - - // Validity - position.setValid(parser.group(index++).compareTo("A") == 0); - - // Satellites - position.set(Event.KEY_SATELLITES, parser.group(index++)); + DateBuilder dateBuilder = new DateBuilder() + .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); - // Course - position.setCourse(Double.parseDouble(parser.group(index++))); + position.set(Event.PREFIX_IO + 1, parser.next()); + position.set(Event.KEY_BATTERY, parser.nextDouble() / 10); + position.set(Event.KEY_POWER, parser.nextDouble()); + position.set(Event.PREFIX_ADC + 1, parser.next()); + position.set(Event.PREFIX_ADC + 2, parser.next()); + position.set(Event.PREFIX_TEMP + 1, parser.next()); + position.set(Event.PREFIX_TEMP + 2, parser.next()); + position.set(Event.KEY_LAC, parser.next()); - // Speed - position.setSpeed(Double.parseDouble(parser.group(index++))); + position.setValid(parser.next().equals("A")); + position.set(Event.KEY_SATELLITES, parser.next()); - // PDOP - position.set("pdop", parser.group(index++)); + position.setCourse(parser.nextDouble()); + position.setSpeed(parser.nextDouble()); - // Odometer - position.set(Event.KEY_ODOMETER, parser.group(index++)); + position.set("pdop", parser.next()); - // 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); + position.set(Event.KEY_ODOMETER, parser.next()); - // 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); + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); } |