From 5538a83ac7eda6ad5775e582688dd0199e6c538c Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 24 Oct 2015 22:57:38 +1300 Subject: Refactor pattern builder class --- src/org/traccar/helper/PatternBuilder.java | 85 +++++-------- .../traccar/protocol/AquilaProtocolDecoder.java | 66 +++++----- src/org/traccar/protocol/BoxProtocolDecoder.java | 22 ++-- src/org/traccar/protocol/Gl200ProtocolDecoder.java | 108 ++++++++--------- .../traccar/protocol/GlobalSatProtocolDecoder.java | 30 ++--- .../traccar/protocol/GoSafeProtocolDecoder.java | 96 +++++++-------- .../traccar/protocol/Gps103ProtocolDecoder.java | 54 ++++----- .../traccar/protocol/GpsmtaProtocolDecoder.java | 24 ++-- .../traccar/protocol/MegastekProtocolDecoder.java | 134 ++++++++++----------- src/org/traccar/protocol/MtxProtocolDecoder.java | 36 +++--- src/org/traccar/protocol/T55ProtocolDecoder.java | 68 +++++------ src/org/traccar/protocol/Tk102ProtocolDecoder.java | 30 ++--- .../protocol/TopflytechProtocolDecoder.java | 18 +-- src/org/traccar/protocol/TotemProtocolDecoder.java | 109 +++++++++-------- src/org/traccar/protocol/V680ProtocolDecoder.java | 37 +++--- src/org/traccar/protocol/XexunProtocolDecoder.java | 41 ++++--- test/org/traccar/helper/PatternBuilderTest.java | 5 +- 17 files changed, 470 insertions(+), 493 deletions(-) diff --git a/src/org/traccar/helper/PatternBuilder.java b/src/org/traccar/helper/PatternBuilder.java index 5092200be..5d9d3afa8 100644 --- a/src/org/traccar/helper/PatternBuilder.java +++ b/src/org/traccar/helper/PatternBuilder.java @@ -15,104 +15,81 @@ */ package org.traccar.helper; +import java.util.ArrayList; import java.util.regex.Pattern; public class PatternBuilder { - private final StringBuilder pattern = new StringBuilder(); + private final ArrayList fragments = new ArrayList<>(); - public interface Builder { - void build(PatternBuilder builder); + public PatternBuilder optional() { + return optional(1); } - // eXPRession - public PatternBuilder xpr(String s) { - pattern.append(s); + public PatternBuilder optional(int count) { + fragments.add(fragments.size() - count, "(?:"); + fragments.add(")?"); return this; } - // GRouP - public PatternBuilder grp(String s) { - return xpr("(?:").xpr(s).xpr(")"); - } - - // OPtional eXpression - public PatternBuilder opx(String s) { - return xpr("(?:").xpr(s).xpr(")?"); - } - - // TeXT - public PatternBuilder txt(String s) { - pattern.append(s.replaceAll("([\\\\\\.\\[\\{\\(\\)\\*\\+\\?\\^\\$\\|])", "\\\\$1")); + public PatternBuilder expression(String s) { + fragments.add(s); return this; } - // OPtional Text - public PatternBuilder opt(String s) { - return xpr("(?:").txt(s).xpr(")?"); + public PatternBuilder text(String s) { + fragments.add(s.replaceAll("([\\\\\\.\\[\\{\\(\\)\\*\\+\\?\\^\\$\\|])", "\\\\$1")); + return this; } - // NUMber - public PatternBuilder num(String s) { + public PatternBuilder number(String s) { s = s.replace("dddd", "d{4}").replace("ddd", "d{3}").replace("dd", "d{2}"); s = s.replace("xxxx", "x{4}").replace("xxx", "x{3}").replace("xx", "x{2}"); s = s.replace("d", "\\d").replace("x", "\\p{XDigit}").replaceAll("([\\.])", "\\\\$1"); s = s.replaceAll("\\|$", "\\\\|"); // special case for delimiter - pattern.append(s); - + fragments.add(s); return this; } - // OPtional Number - public PatternBuilder opn(String s) { - return xpr("(?:").num(s).xpr(")?"); - } - public PatternBuilder any() { - pattern.append(".*"); + fragments.add(".*"); return this; } - public PatternBuilder not(String s) { - return xpr("[^").txt(s).xpr("]*"); - } - - // NeXT - public PatternBuilder nxt(String s) { - return not(s).txt(s); - } - - // BINary - public PatternBuilder bin(String s) { - pattern.append(s.replaceAll("(\\p{XDigit}{2})", "\\\\$1")); + public PatternBuilder binary(String s) { + fragments.add(s.replaceAll("(\\p{XDigit}{2})", "\\\\$1")); return this; } public PatternBuilder groupBegin() { - return xpr("(?:"); + return expression("(?:"); } - public PatternBuilder groupEnd(boolean optional) { - if (optional) { - return xpr(")?"); - } else { - return xpr(")"); - } + public PatternBuilder groupEnd() { + return expression(")"); + } + + public PatternBuilder groupEnd(String s) { + return expression(")" + s); } public PatternBuilder or() { - return xpr("|"); + return expression("|"); } public Pattern compile() { - return Pattern.compile(pattern.toString(), Pattern.DOTALL); + return Pattern.compile(toString(), Pattern.DOTALL); } @Override public String toString() { - return pattern.toString(); + StringBuilder builder = new StringBuilder(); + for (String fragment : fragments) { + builder.append(fragment); + } + return builder.toString(); } } diff --git a/src/org/traccar/protocol/AquilaProtocolDecoder.java b/src/org/traccar/protocol/AquilaProtocolDecoder.java index 9bdaeac10..577a7bbef 100644 --- a/src/org/traccar/protocol/AquilaProtocolDecoder.java +++ b/src/org/traccar/protocol/AquilaProtocolDecoder.java @@ -33,39 +33,39 @@ public class AquilaProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN = new PatternBuilder() - .txt("$$") - .nxt(",") // client - .num("(d+),") // device serial number - .num("(d+),") // event - .num("(-?d+.d+),") // latitude - .num("(-?d+.d+),") // longitude - .num("(dd)(dd)(dd)") // date (yymmdd) - .num("(dd)(dd)(dd),") // time (hhmmss) - .xpr("([AV]),") // validity - .num("(d+),") // gsm - .num("(d+),") // speed - .num("(d+),") // distance - .num("d+,") // driver code - .num("(d+),") // fuel - .num("([01]),") // io 1 - .num("[01],") // case open switch - .num("[01],") // over speed start - .num("[01],") // over speed end - .num("(?:d+,){3}") // reserved - .num("([01]),") // power status - .num("([01]),") // io 2 - .num("d+,") // reserved - .num("([01]),") // ignition - .num("[01],") // ignition off event - .num("(?:d+,){7}") // reserved - .num("[01],") // corner packet - .num("(?:d+,){8}") // reserved - .num("([01]),") // course bit 0 - .num("([01]),") // course bit 1 - .num("([01]),") // course bit 2 - .num("([01]),") // course bit 3 - .txt("*") - .num("(xx)") // checksum + .text("$$") + .expression("[^,]*,") // client + .number("(d+),") // device serial number + .number("(d+),") // event + .number("(-?d+.d+),") // latitude + .number("(-?d+.d+),") // longitude + .number("(dd)(dd)(dd)") // date (yymmdd) + .number("(dd)(dd)(dd),") // time (hhmmss) + .expression("([AV]),") // validity + .number("(d+),") // gsm + .number("(d+),") // speed + .number("(d+),") // distance + .number("d+,") // driver code + .number("(d+),") // fuel + .number("([01]),") // io 1 + .number("[01],") // case open switch + .number("[01],") // over speed start + .number("[01],") // over speed end + .number("(?:d+,){3}") // reserved + .number("([01]),") // power status + .number("([01]),") // io 2 + .number("d+,") // reserved + .number("([01]),") // ignition + .number("[01],") // ignition off event + .number("(?:d+,){7}") // reserved + .number("[01],") // corner packet + .number("(?:d+,){8}") // reserved + .number("([01]),") // course bit 0 + .number("([01]),") // course bit 1 + .number("([01]),") // course bit 2 + .number("([01]),") // course bit 3 + .text("*") + .number("(xx)") // checksum .compile(); @Override diff --git a/src/org/traccar/protocol/BoxProtocolDecoder.java b/src/org/traccar/protocol/BoxProtocolDecoder.java index f0586520c..24f8b1d9d 100644 --- a/src/org/traccar/protocol/BoxProtocolDecoder.java +++ b/src/org/traccar/protocol/BoxProtocolDecoder.java @@ -33,17 +33,17 @@ public class BoxProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN = new PatternBuilder() - .txt("L,") - .num("(dd)(dd)(dd)") // date - .num("(dd)(dd)(dd),") // time - .txt("G,") - .num("(-?d+.d+),") // latitude - .num("(-?d+.d+),") // longitude - .num("(d+.?d*),") // speed - .num("(d+.?d*),") // course - .num("(d+.?d*),") // distance - .num("(d+),") // event - .num("(d+)") // status + .text("L,") + .number("(dd)(dd)(dd)") // date + .number("(dd)(dd)(dd),") // time + .text("G,") + .number("(-?d+.d+),") // latitude + .number("(-?d+.d+),") // longitude + .number("(d+.?d*),") // speed + .number("(d+.?d*),") // course + .number("(d+.?d*),") // distance + .number("(d+),") // event + .number("(d+)") // status .any() .compile(); diff --git a/src/org/traccar/protocol/Gl200ProtocolDecoder.java b/src/org/traccar/protocol/Gl200ProtocolDecoder.java index 20db06f9f..2863fbaf8 100644 --- a/src/org/traccar/protocol/Gl200ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gl200ProtocolDecoder.java @@ -34,69 +34,69 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN_HEARTBEAT = new PatternBuilder() - .txt("+ACK:GTHBD,") - .num("([0-9A-Z]{2}xxxx),") - .any().txt(",") - .num("(xxxx)") - .opt("$") + .text("+ACK:GTHBD,") + .number("([0-9A-Z]{2}xxxx),") + .any().text(",") + .number("(xxxx)") + .text("$").optional() .compile(); private static final Pattern PATTERN = new PatternBuilder() .groupBegin() - .txt("+").grp("RESP|BUFF").txt(":") + .text("+").expression("(?:RESP|BUFF)").text(":") .or() - .bin("00?04,") - .num("xxxx,") - .xpr("[01],") - .groupEnd(false) - .xpr("GT...,") - .opn("[0-9A-Z]{2}xxxx").txt(",") // protocol version - .xpr("([^,]+),") // imei + .binary("00?04,") + .number("xxxx,") + .expression("[01],") + .groupEnd() + .expression("GT...,") + .number("(?:[0-9A-Z]{2}xxxx)?,") // protocol version + .expression("([^,]+),") // imei .groupBegin() - .xpr("[0-9A-Z]{17},") // vin - .xpr("[^,]{0,20},") // device name - .xpr("[01],") // report type - .num("x{1,8},") // report mask - .xpr("[0-9A-Z]{17},") // vin - .num("[01],") // obd connect - .num("d{1,5},") // obd voltage - .num("x{8},") // support pids - .num("(d{1,5}),") // engine rpm - .num("(d{1,3}),") // speed - .num("(-?d{1,3}),") // coolant temp - .num("(d+.?d*|Inf|NaN)?,") // fuel consumption - .num("(d{1,5}),") // dtcs cleared distance - .num("d{1,5},") - .xpr("([01]),") // obd connect - .num("(d{1,3}),") // number of dtcs - .num("(x*),") // dtcs - .num("(d{1,3}),") // throttle - .num("d{1,3},") // engine load - .num("(d{1,3})?,") // fuel level - .num("(d+)") // odometer + .expression("[0-9A-Z]{17},") // vin + .expression("[^,]{0,20},") // device name + .expression("[01],") // report type + .number("x{1,8},") // report mask + .expression("[0-9A-Z]{17},") // vin + .number("[01],") // obd connect + .number("d{1,5},") // obd voltage + .number("x{8},") // support pids + .number("(d{1,5}),") // engine rpm + .number("(d{1,3}),") // speed + .number("(-?d{1,3}),") // coolant temp + .number("(d+.?d*|Inf|NaN)?,") // fuel consumption + .number("(d{1,5}),") // dtcs cleared distance + .number("d{1,5},") + .expression("([01]),") // obd connect + .number("(d{1,3}),") // number of dtcs + .number("(x*),") // dtcs + .number("(d{1,3}),") // throttle + .number("d{1,3},") // engine load + .number("(d{1,3})?,") // fuel level + .number("(d+)") // odometer .or().any() - .groupEnd(false).txt(",") - - .num("(d*),") // gps accuracy - .num("(d+.d)?,") // speed - .num("(d+)?,") // course - .num("(-?d+.d)?,") // altitude - .num("(-?d+.d+),") // longitude - .num("(-?d+.d+),") // latitude - .num("(dddd)(dd)(dd)") // date - .num("(dd)(dd)(dd),") // time - .num("(dddd)?,") // mcc - .num("(dddd)?,") // mnc - .num("(xxxx|x{8})?,") // loc - .num("(xxxx)?,") // cell + .groupEnd().text(",") + + .number("(d*),") // gps accuracy + .number("(d+.d)?,") // speed + .number("(d+)?,") // course + .number("(-?d+.d)?,") // altitude + .number("(-?d+.d+),") // longitude + .number("(-?d+.d+),") // latitude + .number("(dddd)(dd)(dd)") // date + .number("(dd)(dd)(dd),") // time + .number("(dddd)?,") // mcc + .number("(dddd)?,") // mnc + .number("(xxxx|x{8})?,") // loc + .number("(xxxx)?,") // cell .groupBegin() - .num("(d+.d)?,") // odometer - .num("(d{1,3})?,") // battery - .groupEnd(true) - .any().txt(",") - .num("(xxxx)\\$?") - .opt("$") + .number("(d+.d)?,") // odometer + .number("(d{1,3})?,") // battery + .groupEnd("?") + .any().text(",") + .number("(xxxx)\\$?") + .text("$").optional() .compile(); @Override diff --git a/src/org/traccar/protocol/GlobalSatProtocolDecoder.java b/src/org/traccar/protocol/GlobalSatProtocolDecoder.java index 05c4dec47..049bb2f7f 100644 --- a/src/org/traccar/protocol/GlobalSatProtocolDecoder.java +++ b/src/org/traccar/protocol/GlobalSatProtocolDecoder.java @@ -177,21 +177,21 @@ public class GlobalSatProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN = new PatternBuilder() - .txt("$") - .num("(d+),") // imei - .num("d+,") // mode - .num("(d),") // fix - .num("(dd)(dd)(dd),") // date (ddmmyy) - .num("(dd)(dd)(dd),") // time (hhmmss) - .xpr("([EW])") - .num("(ddd)(dd.d+),") // longitude (dddmm.mmmm) - .xpr("([NS])") - .num("(dd)(dd.d+),") // latitude (ddmm.mmmm) - .num("(d+.?d*),") // altitude - .num("(d+.?d*),") // speed - .num("(d+.?d*),") // course - .num("(d+),") // satellites - .num("(d+.?d*)") // hdop + .text("$") + .number("(d+),") // imei + .number("d+,") // mode + .number("(d),") // fix + .number("(dd)(dd)(dd),") // date (ddmmyy) + .number("(dd)(dd)(dd),") // time (hhmmss) + .expression("([EW])") + .number("(ddd)(dd.d+),") // longitude (dddmm.mmmm) + .expression("([NS])") + .number("(dd)(dd.d+),") // latitude (ddmm.mmmm) + .number("(d+.?d*),") // altitude + .number("(d+.?d*),") // speed + .number("(d+.?d*),") // course + .number("(d+),") // satellites + .number("(d+.?d*)") // hdop .compile(); private Position decodeAlternative(Channel channel, String sentence) { diff --git a/src/org/traccar/protocol/GoSafeProtocolDecoder.java b/src/org/traccar/protocol/GoSafeProtocolDecoder.java index cba045122..e1321633e 100644 --- a/src/org/traccar/protocol/GoSafeProtocolDecoder.java +++ b/src/org/traccar/protocol/GoSafeProtocolDecoder.java @@ -36,67 +36,67 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN = new PatternBuilder() - .txt("*GS") // header - .num("d+,") // protocol version - .num("(d+),") // imei - .num("(dd)(dd)(dd)") // time - .num("(dd)(dd)(dd),") // date - .xpr("(.*)#?") // data + .text("*GS") // header + .number("d+,") // protocol version + .number("(d+),") // imei + .number("(dd)(dd)(dd)") // time + .number("(dd)(dd)(dd),") // date + .expression("(.*)#?") // data .compile(); private static final Pattern PATTERN_ITEM = new PatternBuilder() - .num("(x+)?,") // event + .number("(x+)?,") // event .groupBegin() - .txt("SYS:") - .nxt(",") - .groupEnd(true) + .text("SYS:") + .expression("[^,]*,") + .groupEnd("?") .groupBegin() - .txt("GPS:") - .xpr("([AV]);") // validity - .num("(d+);") // satellites - .num("([NS])(d+.d+);") // latitude - .num("([EW])(d+.d+);") // longitude - .num("(d+);") // speed - .num("(d+);") // course - .num("(d+);") // altitude - .num("(d+.d+)") // hdop - .opn(";d+.d+") // vdop - .xpr(",?") - .groupEnd(false) + .text("GPS:") + .expression("([AV]);") // validity + .number("(d+);") // satellites + .number("([NS])(d+.d+);") // latitude + .number("([EW])(d+.d+);") // longitude + .number("(d+);") // speed + .number("(d+);") // course + .number("(d+);") // altitude + .number("(d+.d+)") // hdop + .number("(?:;d+.d+)?") // vdop + .expression(",?") + .groupEnd() .groupBegin() - .txt("GSM:").not(",").xpr(",?") - .groupEnd(true) + .text("GSM:").expression("[^,],?") + .groupEnd("?") .groupBegin() - .txt("COT:") - .num("(d+)") // odometer - .opn(";d+:d+:d+") // engine hours - .xpr(",?") - .groupEnd(true) + .text("COT:") + .number("(d+)") // odometer + .number("(?:;d+:d+:d+)?") // engine hours + .expression(",?") + .groupEnd("?") .groupBegin() - .txt("ADC:") - .num("(d+.d+);") // power - .num("(d+.d+),?") // battery - .groupEnd(true) + .text("ADC:") + .number("(d+.d+);") // power + .number("(d+.d+),?") // battery + .groupEnd("?") .groupBegin() - .txt("DTT:") - .num("(x+);") // status - .nxt(";") - .num("x+;") // geo-fence 0-119 - .num("x+;") // geo-fence 120-155 - .num("x+,?") // event status - .groupEnd(true) + .text("DTT:") + .number("(x+);") // status + .expression("[^;]*;") + .number("x+;") // geo-fence 0-119 + .number("x+;") // geo-fence 120-155 + .number("x+,?") // event status + .groupEnd("?") .groupBegin() - .txt("ETD:").not(",").xpr(",?") - .groupEnd(true) + .text("ETD:").expression("[^,],?") + .groupEnd("?") .groupBegin() - .txt("OBD:").not(",").xpr(",?") - .groupEnd(true) + .text("OBD:").expression("[^,],?") + .groupEnd("?") .groupBegin() - .txt("FUL:").not(",").xpr(",?") - .groupEnd(true) + .text("FUL:").expression("[^,],?") + .groupEnd("?") .groupBegin() - .txt("TRU:").not(",").xpr(",?") - .groupEnd(true) + .text("TRU:").expression("[^,],?") + .groupEnd("?") .compile(); private Position decodePosition(Parser parser, Date time) { diff --git a/src/org/traccar/protocol/Gps103ProtocolDecoder.java b/src/org/traccar/protocol/Gps103ProtocolDecoder.java index 9bd508e92..9fa71ad58 100644 --- a/src/org/traccar/protocol/Gps103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gps103ProtocolDecoder.java @@ -32,39 +32,39 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN = new PatternBuilder() - .txt("imei:") - .num("(d+),") // imei - .xpr("([^,]+),") // alarm - .num("(dd)/?(dd)/?(dd) ?") // local date - .num("(dd):?(dd)(?:dd)?,") // local time - .nxt(",") - .xpr("[FL],") // full / low + .text("imei:") + .number("(d+),") // imei + .expression("([^,]+),") // alarm + .number("(dd)/?(dd)/?(dd) ?") // local date + .number("(dd):?(dd)(?:dd)?,") // local time + .expression("[^,]*,") + .expression("[FL],") // full / low .groupBegin() - .num("(dd)(dd)(dd).(d+)") // time utc (hhmmss.sss) + .number("(dd)(dd)(dd).(d+)") // time utc (hhmmss.sss) .or() - .opn("d{1,5}.d+") - .groupEnd(false) - .txt(",") - .xpr("([AV]),") // validity - .opx("([NS]),") - .num("(d+)(dd.d+),") // latitude (ddmm.mmmm) - .opx("([NS]),") - .opx("([EW]),") - .num("(d+)(dd.d+),") // longitude (dddmm.mmmm) - .opx("([EW])?,") - .num("(d+.?d*)?,?") // speed - .num("(d+.?d*)?,?") // course - .num("(d+.?d*)?,?") // altitude - .xpr("([^,;]+)?,?") - .xpr("([^,;]+)?,?") - .xpr("([^,;]+)?,?") - .xpr("([^,;]+)?,?") - .xpr("([^,;]+)?,?") + .number("(?:d{1,5}.d+)?") + .groupEnd() + .text(",") + .expression("([AV]),") // validity + .expression("([NS]),").optional() + .number("(d+)(dd.d+),") // latitude (ddmm.mmmm) + .expression("([NS]),").optional() + .expression("([EW]),").optional() + .number("(d+)(dd.d+),") // longitude (dddmm.mmmm) + .expression("([EW])?,").optional() + .number("(d+.?d*)?,?") // speed + .number("(d+.?d*)?,?") // course + .number("(d+.?d*)?,?") // altitude + .expression("([^,;]+)?,?") + .expression("([^,;]+)?,?") + .expression("([^,;]+)?,?") + .expression("([^,;]+)?,?") + .expression("([^,;]+)?,?") .any() .compile(); private static final Pattern PATTERN_HANDSHAKE = new PatternBuilder() - .num("##,imei:(d+),A") + .number("##,imei:(d+),A") .compile(); @Override diff --git a/src/org/traccar/protocol/GpsmtaProtocolDecoder.java b/src/org/traccar/protocol/GpsmtaProtocolDecoder.java index 7c7db9aa0..37023181e 100644 --- a/src/org/traccar/protocol/GpsmtaProtocolDecoder.java +++ b/src/org/traccar/protocol/GpsmtaProtocolDecoder.java @@ -32,18 +32,18 @@ public class GpsmtaProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN = new PatternBuilder() - .num("(d+) ") // uid - .num("(d+) ") // time - .num("(d+.d+) ") // latitude - .num("(d+.d+) ") // longitude - .num("(d+) ") // speed - .num("(d+) ") // course - .num("(d+) ") // accuracy - .num("(d+) ") // altitude - .num("(d+) ") // flags - .num("(d+) ") // battery - .num("(d+) ") // temperature - .num("(d)") // changing status + .number("(d+) ") // uid + .number("(d+) ") // time + .number("(d+.d+) ") // latitude + .number("(d+.d+) ") // longitude + .number("(d+) ") // speed + .number("(d+) ") // course + .number("(d+) ") // accuracy + .number("(d+) ") // altitude + .number("(d+) ") // flags + .number("(d+) ") // battery + .number("(d+) ") // temperature + .number("(d)") // changing status .any() .compile(); diff --git a/src/org/traccar/protocol/MegastekProtocolDecoder.java b/src/org/traccar/protocol/MegastekProtocolDecoder.java index 4b957b879..fd6b2be83 100644 --- a/src/org/traccar/protocol/MegastekProtocolDecoder.java +++ b/src/org/traccar/protocol/MegastekProtocolDecoder.java @@ -32,46 +32,46 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN_GPRMC = new PatternBuilder() - .txt("$GPRMC,") - .num("(dd)(dd)(dd).d+,") // time - .xpr("([AV]),") // validity - .num("(d+)(dd.d+),([NS]),") // latitude - .num("(d+)(dd.d+),([EW]),") // longitude - .num("(d+.d+)?,") // speed - .num("(d+.d+)?,") // course - .num("(dd)(dd)(dd)") // date (ddmmyy) + .text("$GPRMC,") + .number("(dd)(dd)(dd).d+,") // time + .expression("([AV]),") // validity + .number("(d+)(dd.d+),([NS]),") // latitude + .number("(d+)(dd.d+),([EW]),") // longitude + .number("(d+.d+)?,") // speed + .number("(d+.d+)?,") // course + .number("(dd)(dd)(dd)") // date (ddmmyy) .any() // checksum .compile(); private static final Pattern PATTERN_SIMPLE = new PatternBuilder() - .xpr("[FL],") // flag - .xpr("([^,]*),") // alarm - .num("imei:(d+),") // imei - .num("(d+/?d*)?,") // satellites - .num("(d+.d+)?,") // altitude - .num("Battery=(d+)%,,") // battery - .num("(d)?,") // charger - .num("(d+)?,") // mcc - .num("(d+)?,") // mnc - .num("(xxxx,xxxx);") // location code + .expression("[FL],") // flag + .expression("([^,]*),") // alarm + .number("imei:(d+),") // imei + .number("(d+/?d*)?,") // satellites + .number("(d+.d+)?,") // altitude + .number("Battery=(d+)%,,") // battery + .number("(d)?,") // charger + .number("(d+)?,") // mcc + .number("(d+)?,") // mnc + .number("(xxxx,xxxx);") // location code .any() // checksum .compile(); private static final Pattern PATTERN_ALTERNATIVE = new PatternBuilder() - .num("(d+),") // mcc - .num("(d+),") // mnc - .num("(xxxx,xxxx),") // location code - .num("(d+),") // gsm signal - .num("(d+),") // battery - .num("(d+),") // flags - .num("(d+),") // inputs - .num("(?:(d+),)?") // outputs - .num("(d.?d*),") // adc 1 + .number("(d+),") // mcc + .number("(d+),") // mnc + .number("(xxxx,xxxx),") // location code + .number("(d+),") // gsm signal + .number("(d+),") // battery + .number("(d+),") // flags + .number("(d+),") // inputs + .number("(?:(d+),)?") // outputs + .number("(d.?d*),") // adc 1 .groupBegin() - .num("(d.dd),") // adc 2 - .num("(d.dd),") // adc 3 - .groupEnd(true) - .xpr("([^;]+);") // alarm + .number("(d.dd),") // adc 2 + .number("(d.dd),") // adc 3 + .groupEnd("?") + .expression("([^;]+);") // alarm .any() // checksum .compile(); @@ -216,44 +216,44 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN_NEW = new PatternBuilder() - .txt("$MGV") - .num("ddd,") - .num("(d+),") // imei - .nxt(",") // name - .xpr("([RS]),") - .num("(dd)(dd)(dd),") // date (ddmmyy) - .num("(dd)(dd)(dd),") // time - .xpr("([AV]),") // validity - .num("(d+)(dd.d+),([NS]),") // latitude - .num("(d+)(dd.d+),([EW]),") // longitude - .num("dd,") - .num("(dd),") // satellites - .num("dd,") - .num("(d+.d+),") // hdop - .num("(d+.d+),") // speed - .num("(d+.d+),") // course - .num("(d+.d+),") // altitude - .num("(d+.d+),") // odometer - .num("(d+),") // mcc - .num("(d+),") // mnc - .num("(xxxx,xxxx),") // cell - .num("(d+)?,") // gsm - .xpr("([01]+),") // input - .xpr("([01]+),") // output - .num("(d+),") // adc1 - .num("(d+),") // adc2 - .num("(d+),") // adc3 + .text("$MGV") + .number("ddd,") + .number("(d+),") // imei + .expression("[^,]*,") // name + .expression("([RS]),") + .number("(dd)(dd)(dd),") // date (ddmmyy) + .number("(dd)(dd)(dd),") // time + .expression("([AV]),") // validity + .number("(d+)(dd.d+),([NS]),") // latitude + .number("(d+)(dd.d+),([EW]),") // longitude + .number("dd,") + .number("(dd),") // satellites + .number("dd,") + .number("(d+.d+),") // hdop + .number("(d+.d+),") // speed + .number("(d+.d+),") // course + .number("(d+.d+),") // altitude + .number("(d+.d+),") // odometer + .number("(d+),") // mcc + .number("(d+),") // mnc + .number("(xxxx,xxxx),") // cell + .number("(d+)?,") // gsm + .expression("([01]+),") // input + .expression("([01]+),") // output + .number("(d+),") // adc1 + .number("(d+),") // adc2 + .number("(d+),") // adc3 .groupBegin() - .num("(-?d+.?d*)") // temperature 1 - .or().txt(" ") - .groupEnd(false).txt(",") + .number("(-?d+.?d*)") // temperature 1 + .or().text(" ") + .groupEnd().text(",") .groupBegin() - .num("(-?d+.?d*)") // temperature 2 - .or().txt(" ") - .groupEnd(false).txt(",") - .num("(d+)?,,") // rfid - .num("(d+)?,") // battery - .xpr("([^,]*);") // alert + .number("(-?d+.?d*)") // temperature 2 + .or().text(" ") + .groupEnd().text(",") + .number("(d+)?,,") // rfid + .number("(d+)?,") // battery + .expression("([^,]*);") // alert .any() .compile(); diff --git a/src/org/traccar/protocol/MtxProtocolDecoder.java b/src/org/traccar/protocol/MtxProtocolDecoder.java index bcf77ff4a..802875345 100644 --- a/src/org/traccar/protocol/MtxProtocolDecoder.java +++ b/src/org/traccar/protocol/MtxProtocolDecoder.java @@ -32,26 +32,26 @@ public class MtxProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN = new PatternBuilder() - .txt("#MTX,") - .num("(d+),") // IMEI - .num("(dddd)(dd)(dd),") // Date - .num("(dd)(dd)(dd),") // Time - .num("(-?d+.d+),") // Latitude - .num("(-?d+.d+),") // Longitude - .num("(d+.?d*),") // Speed - .num("(d+),") // Course - .num("(d+.?d*),") // Odometer + .text("#MTX,") + .number("(d+),") // imei + .number("(dddd)(dd)(dd),") // date + .number("(dd)(dd)(dd),") // time + .number("(-?d+.d+),") // latitude + .number("(-?d+.d+),") // longitude + .number("(d+.?d*),") // speed + .number("(d+),") // course + .number("(d+.?d*),") // odometer .groupBegin() - .num("d+") + .number("d+") .or() - .txt("X") - .groupEnd(false) - .txt(",") - .xpr("(?:[01]|X),") - .xpr("([01]+),") // Input - .xpr("([01]+),") // Output - .num("(d+),") // ADC1 - .num("(d+)") // ADC2 + .text("X") + .groupEnd() + .text(",") + .expression("(?:[01]|X),") + .expression("([01]+),") // input + .expression("([01]+),") // output + .number("(d+),") // adc1 + .number("(d+)") // adc2 .any() .compile(); diff --git a/src/org/traccar/protocol/T55ProtocolDecoder.java b/src/org/traccar/protocol/T55ProtocolDecoder.java index 73c5ab73c..42db4c753 100644 --- a/src/org/traccar/protocol/T55ProtocolDecoder.java +++ b/src/org/traccar/protocol/T55ProtocolDecoder.java @@ -33,52 +33,52 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN_GPRMC = new PatternBuilder() - .txt("$GPRMC,") - .num("(dd)(dd)(dd).?d*,") // time - .xpr("([AV]),") // validity - .num("(dd)(dd.d+),") // latitude - .xpr("([NS]),") - .num("(d{2,3})(dd.d+),") // longitude - .xpr("([EW]),") - .num("(d+.?d*)?,") // speed - .num("(d+.?d*)?,") // course - .num("(dd)(dd)(dd)") // date + .text("$GPRMC,") + .number("(dd)(dd)(dd).?d*,") // time + .expression("([AV]),") // validity + .number("(dd)(dd.d+),") // latitude + .expression("([NS]),") + .number("(d{2,3})(dd.d+),") // longitude + .expression("([EW]),") + .number("(d+.?d*)?,") // speed + .number("(d+.?d*)?,") // course + .number("(dd)(dd)(dd)") // date .any() .compile(); private static final Pattern PATTERN_GPGGA = new PatternBuilder() - .txt("$GPGGA,") - .num("(dd)(dd)(dd).?d*,") // time - .num("(d+)(dd.d+),") // latitude - .xpr("([NS]),") - .num("(d+)(dd.d+),") // longitude - .xpr("([EW]),") + .text("$GPGGA,") + .number("(dd)(dd)(dd).?d*,") // time + .number("(d+)(dd.d+),") // latitude + .expression("([NS]),") + .number("(d+)(dd.d+),") // longitude + .expression("([EW]),") .any() .compile(); private static final Pattern PATTERN_GPRMA = new PatternBuilder() - .txt("$GPRMA,") - .xpr("([AV]),") // validity - .num("(dd)(dd.d+),") // latitude - .xpr("([NS]),") - .num("(ddd)(dd.d+),") // longitude - .xpr("([EW]),,,") - .num("(d+.?d*)?,") // speed - .num("(d+.?d*)?,") // course + .text("$GPRMA,") + .expression("([AV]),") // validity + .number("(dd)(dd.d+),") // latitude + .expression("([NS]),") + .number("(ddd)(dd.d+),") // longitude + .expression("([EW]),,,") + .number("(d+.?d*)?,") // speed + .number("(d+.?d*)?,") // course .any() .compile(); private static final Pattern PATTERN_TRCCR = new PatternBuilder() - .txt("$TRCCR,") - .num("(dddd)(dd)(dd)") // date - .num("(dd)(dd)(dd).?d*,") // time - .xpr("([AV]),") // validity - .num("(-?d+.d+),") // latitude - .num("(-?d+.d+),") // longitude - .num("(d+.d+),") // speed - .num("(d+.d+),") // course - .num("(-?d+.d+),") // altitude - .num("(d+.?d*),") // battery + .text("$TRCCR,") + .number("(dddd)(dd)(dd)") // date + .number("(dd)(dd)(dd).?d*,") // time + .expression("([AV]),") // validity + .number("(-?d+.d+),") // latitude + .number("(-?d+.d+),") // longitude + .number("(d+.d+),") // speed + .number("(d+.d+),") // course + .number("(-?d+.d+),") // altitude + .number("(d+.?d*),") // battery .any() .compile(); diff --git a/src/org/traccar/protocol/Tk102ProtocolDecoder.java b/src/org/traccar/protocol/Tk102ProtocolDecoder.java index b02a424e0..e9fb86cc2 100644 --- a/src/org/traccar/protocol/Tk102ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tk102ProtocolDecoder.java @@ -31,22 +31,22 @@ public class Tk102ProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN = new PatternBuilder() - .txt("[") - .xpr(".") - .num("d{10}") - .xpr(".") - .txt("(") - .xpr("[A-Z]+") - .num("(dd)(dd)(dd)") // Time (HHMMSS) - .xpr("([AV])") // Validity - .num("(dd)(dd.dddd)([NS])") // Latitude (DDMM.MMMM) - .num("(ddd)(dd.dddd)([EW])") // Longitude (DDDMM.MMMM) - .num("(ddd.ddd)") // Speed - .num("(dd)(dd)(dd)") // Date (DDMMYY) - .num("d+") + .text("[") + .expression(".") + .number("d{10}") + .expression(".") + .text("(") + .expression("[A-Z]+") + .number("(dd)(dd)(dd)") // time + .expression("([AV])") // validity + .number("(dd)(dd.dddd)([NS])") // latitude + .number("(ddd)(dd.dddd)([EW])") // longitude + .number("(ddd.ddd)") // Speed + .number("(dd)(dd)(dd)") // date (ddmmyy) + .number("d+") .any() - .txt(")") - .opt("]") + .text(")") + .text("]").optional() .compile(); @Override diff --git a/src/org/traccar/protocol/TopflytechProtocolDecoder.java b/src/org/traccar/protocol/TopflytechProtocolDecoder.java index 5fa4b3456..768de3463 100644 --- a/src/org/traccar/protocol/TopflytechProtocolDecoder.java +++ b/src/org/traccar/protocol/TopflytechProtocolDecoder.java @@ -33,16 +33,16 @@ public class TopflytechProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN = new PatternBuilder() - .txt("(") - .num("(d+)") // imei + .text("(") + .number("(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 + .number("(dd)(dd)(dd)") // date (yymmdd) + .number("(dd)(dd)(dd)") // time + .expression("([AV])") + .number("(dd)(dd.dddd)([NS])") // latitude + .number("(ddd)(dd.dddd)([EW])") // longitude + .number("(ddd.d)") // speed + .number("(d+.d+)") // course .compile(); @Override diff --git a/src/org/traccar/protocol/TotemProtocolDecoder.java b/src/org/traccar/protocol/TotemProtocolDecoder.java index 6ab2f1a27..60285d360 100644 --- a/src/org/traccar/protocol/TotemProtocolDecoder.java +++ b/src/org/traccar/protocol/TotemProtocolDecoder.java @@ -33,35 +33,35 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN1 = new PatternBuilder() - .txt("$$") // header - .num("xx") // length - .num("(d+)|") // imei - .xpr("(..)") // alarm - .txt("$GPRMC,") - .num("(dd)(dd)(dd).d+,") // time - .xpr("([AV]),") // validity - .num("(d+)(dd.d+),([NS]),") // latitude - .num("(d+)(dd.d+),([EW]),") // longitude - .num("(d+.?d*)?,") // speed - .num("(d+.?d*)?,") // course - .num("(dd)(dd)(dd)") // date - .nxt("*") - .num("xx|") // checksum - .num("d+.d+|") // pdop - .num("(d+.d+)|") // hdop - .num("d+.d+|") // vdop - .num("(d+)|") // io status - .num("d+|") // time - .num("d") // charged - .num("(ddd)") // battery - .num("(dddd)|") // power - .opn("(d+)|") // adc - .num("(x+)|") // location code - .num("(d+)|") // temperature - .num("(d+.d+)|") // odometer - .num("d+|") // serial number + .text("$$") // header + .number("xx") // length + .number("(d+)|") // imei + .expression("(..)") // alarm + .text("$GPRMC,") + .number("(dd)(dd)(dd).d+,") // time + .expression("([AV]),") // validity + .number("(d+)(dd.d+),([NS]),") // latitude + .number("(d+)(dd.d+),([EW]),") // longitude + .number("(d+.?d*)?,") // speed + .number("(d+.?d*)?,") // course + .number("(dd)(dd)(dd)") // date + .expression("[^*]*").text("*") + .number("xx|") // checksum + .number("d+.d+|") // pdop + .number("(d+.d+)|") // hdop + .number("d+.d+|") // vdop + .number("(d+)|") // io status + .number("d+|") // time + .number("d") // charged + .number("(ddd)") // battery + .number("(dddd)|") // power + .number("(d+)|").optional() // adc + .number("(x+)|") // location code + .number("(d+)|") // temperature + .number("(d+.d+)|") // odometer + .number("d+|") // serial number .any() - .num("xxxx") // checksum + .number("xxxx") // checksum .any() .compile(); @@ -93,38 +93,37 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { "\r?\n?"); private static final Pattern PATTERN3 = new PatternBuilder() - .txt("$$") // header - .num("xx") // length - .num("(d+)|") // imei - .xpr("(..)") // alarm type - .num("(dd)(dd)(dd)") // date (yymmdd) - .num("(dd)(dd)(dd)") // time (hhmmss) - .num("(xxxx)") // io status - .xpr("[01]") // charging - .num("(dd)") // battery - .num("(dd)") // external power - .num("(dddd)") // adc 1 - .num("(dddd)") // adc 2 - .num("(ddd)") // temperature 1 - .num("(ddd)") // temperature 2 - .num("(x{8})") // location code - .xpr("([AV])") // validity - .num("(dd)") // satellites - .num("(ddd)") // course - .num("(ddd)") // speed - .num("(dd.d)") // pdop - .num("(d{7})") // odometer - .num("(dd)(dd.dddd)([NS])") // latitude - .num("(ddd)(dd.dddd)([EW])") // longitude - .num("dddd") // serial number - .num("xxxx") // checksum + .text("$$") // header + .number("xx") // length + .number("(d+)|") // imei + .expression("(..)") // alarm type + .number("(dd)(dd)(dd)") // date (yymmdd) + .number("(dd)(dd)(dd)") // time + .number("(xxxx)") // io status + .expression("[01]") // charging + .number("(dd)") // battery + .number("(dd)") // external power + .number("(dddd)") // adc 1 + .number("(dddd)") // adc 2 + .number("(ddd)") // temperature 1 + .number("(ddd)") // temperature 2 + .number("(x{8})") // location code + .expression("([AV])") // validity + .number("(dd)") // satellites + .number("(ddd)") // course + .number("(ddd)") // speed + .number("(dd.d)") // pdop + .number("(d{7})") // odometer + .number("(dd)(dd.dddd)([NS])") // latitude + .number("(ddd)(dd.dddd)([EW])") // longitude + .number("dddd") // serial number + .number("xxxx") // checksum .any() .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; diff --git a/src/org/traccar/protocol/V680ProtocolDecoder.java b/src/org/traccar/protocol/V680ProtocolDecoder.java index dc0dcd2f6..229861c4f 100644 --- a/src/org/traccar/protocol/V680ProtocolDecoder.java +++ b/src/org/traccar/protocol/V680ProtocolDecoder.java @@ -33,30 +33,29 @@ public class V680ProtocolDecoder extends BaseProtocolDecoder { private static final Pattern PATTERN = new PatternBuilder() .groupBegin() - .num("#(d+)#") // imei - .xpr("([^#]*)#") // user - .groupEnd(true) - .num("(d+)#") // fix - .xpr("([^#]+)#") // password - .xpr("([^#]+)#") // event - .num("(d+)#") // packet number - .xpr("([^#]+)?#?") // gsm base station - .xpr("(?:[^#]+#)?") - .num("(d+)?(dd.d+),") // longitude - .xpr("([EW]),") - .num("(d+)?(dd.d+),") // latitude - .xpr("([NS]),") - .num("(d+.d+),") // speed - .num("(d+.?d*)?#") // course - .num("(dd)(dd)(dd)#") // date - .num("(dd)(dd)(dd)") // time + .number("#(d+)#") // imei + .expression("([^#]*)#") // user + .groupEnd("?") + .number("(d+)#") // fix + .expression("([^#]+)#") // password + .expression("([^#]+)#") // event + .number("(d+)#") // packet number + .expression("([^#]+)?#?") // gsm base station + .expression("(?:[^#]+#)?") + .number("(d+)?(dd.d+),") // longitude + .expression("([EW]),") + .number("(d+)?(dd.d+),") // latitude + .expression("([NS]),") + .number("(d+.d+),") // speed + .number("(d+.?d*)?#") // course + .number("(dd)(dd)(dd)#") // date + .number("(dd)(dd)(dd)") // time .any() .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; sentence = sentence.trim(); diff --git a/src/org/traccar/protocol/XexunProtocolDecoder.java b/src/org/traccar/protocol/XexunProtocolDecoder.java index 5c719ece7..89ea954bc 100644 --- a/src/org/traccar/protocol/XexunProtocolDecoder.java +++ b/src/org/traccar/protocol/XexunProtocolDecoder.java @@ -36,35 +36,36 @@ public class XexunProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN_BASIC = new PatternBuilder() - .xpr("G[PN]RMC,") - .num("(dd)(dd)(dd).(d+),") // time - .xpr("([AV]),") // validity - .num("(d+)(dd.d+),([NS]),") // latitude - .num("(d+)(dd.d+),([EW])?,") // longitude - .num("(d+.?d*),") // speed - .num("(d+.?d*)?,") // course - .num("(dd)(dd)(dd),") // date - .nxt("*") - .num("xx,") // checksum - .xpr("([FL]),") // signal - .opx("([^,]*),") // alarm + .expression("G[PN]RMC,") + .number("(dd)(dd)(dd).(d+),") // time + .expression("([AV]),") // validity + .number("(d+)(dd.d+),([NS]),") // latitude + .number("(d+)(dd.d+),([EW])?,") // longitude + .number("(d+.?d*),") // speed + .number("(d+.?d*)?,") // course + .number("(dd)(dd)(dd),") // date + .expression("[^*]*").text("*") + .number("xx,") // checksum + .expression("([FL]),") // signal + .expression("([^,]*),").optional() // alarm .any() - .num("imei:(d+),") // imei + .number("imei:(d+),") // imei .compile(); private static final Pattern PATTERN_FULL = new PatternBuilder() .any() - .num("(d+),") // serial - .xpr("([^,]+)?,") // phone number - .xpr(PATTERN_BASIC.pattern()) - .num("(d+),") // satellites - .num("(-?d+.d+)?,") // altitude - .num("[FL]:(d+.d+)V") // power + .number("(d+),") // serial + .expression("([^,]+)?,") // phone number + .expression(PATTERN_BASIC.pattern()) + .number("(d+),") // satellites + .number("(-?d+.d+)?,") // altitude + .number("[FL]:(d+.d+)V") // power .any() .compile(); @Override - protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { Pattern pattern = full ? PATTERN_FULL : PATTERN_BASIC; Matcher parser = pattern.matcher((String) msg); diff --git a/test/org/traccar/helper/PatternBuilderTest.java b/test/org/traccar/helper/PatternBuilderTest.java index a36481934..427161758 100644 --- a/test/org/traccar/helper/PatternBuilderTest.java +++ b/test/org/traccar/helper/PatternBuilderTest.java @@ -7,8 +7,9 @@ public class PatternBuilderTest { @Test public void testPatternBuilder() { - Assert.assertEquals("\\$GPRMC", new PatternBuilder().txt("$GPRMC").toString()); - Assert.assertEquals("(\\d{2}\\.\\p{XDigit}+)", new PatternBuilder().num("(dd.x+)").toString()); + Assert.assertEquals("\\$GPRMC", new PatternBuilder().text("$GPRMC").toString()); + Assert.assertEquals("(\\d{2}\\.\\p{XDigit}+)", new PatternBuilder().number("(dd.x+)").toString()); + Assert.assertEquals("a(?:bc)?", new PatternBuilder().text("a").text("b").text("c").optional(2).toString()); } } -- cgit v1.2.3