diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2017-04-01 14:11:20 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2017-04-01 14:11:20 +1300 |
commit | 571078422b3e1162870d777124a3844da56583b0 (patch) | |
tree | 74aff22ab9f71e7170d2e618af70745c207ad92c | |
parent | 8972c89df0efd530197dcc3d33cb928124a799c7 (diff) | |
download | trackermap-server-571078422b3e1162870d777124a3844da56583b0.tar.gz trackermap-server-571078422b3e1162870d777124a3844da56583b0.tar.bz2 trackermap-server-571078422b3e1162870d777124a3844da56583b0.zip |
Refactor GL200 protocol decoder
3 files changed, 55 insertions, 114 deletions
diff --git a/src/org/traccar/protocol/Gl200ProtocolDecoder.java b/src/org/traccar/protocol/Gl200ProtocolDecoder.java index 886c3d08d..1c81ca5d6 100644 --- a/src/org/traccar/protocol/Gl200ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gl200ProtocolDecoder.java @@ -115,16 +115,14 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { .number("(dddd)(dd)(dd)") // date (yyyymmdd) .number("(dd)(dd)(dd)").optional(2) // time (hhmmss) .text(",") - .groupBegin() - .number("(0ddd)?,") // mcc - .number("(0ddd)?,") // mnc - .number("(xxxx)?,") // lac - .number("(xxxx)?,") // cell - .or() .number("(d+)?,") // mcc .number("(d+)?,") // mnc - .number("(d+)?,") // lac - .number("(d+)?,") // cell + .groupBegin() + .number("(d+),") // lac + .number("(d+),") // cid + .or() + .number("(x+)?,") // lac + .number("(x+)?,") // cid .groupEnd() .number("(?:d+|(d+.d))?,") // odometer .compile(); @@ -296,10 +294,10 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { .number("(dddd)(dd)(dd)") // date (yyyymmdd) .number("(dd)(dd)(dd)").optional(2) // time (hhmmss) .text(",") - .number("(0ddd),") // mcc - .number("(0ddd),") // mnc - .number("(xxxx),") // lac - .number("(xxxx),").optional(4) // cell + .number("(d+),") // mcc + .number("(d+),") // mnc + .number("(x+),") // lac + .number("(x+),").optional(4) // cell .any() .number("(dddd)(dd)(dd)") // date (yyyymmdd) .number("(dd)(dd)(dd)").optional(2) // time (hhmmss) @@ -334,22 +332,26 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { return null; } - private Object decodeInf(Channel channel, SocketAddress remoteAddress, String sentence) { - - Parser parser = new Parser(PATTERN_INF, sentence); - if (!parser.matches()) { - return null; + private Position initPosition(Parser parser, Channel channel, SocketAddress remoteAddress) { + if (parser.matches()) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession != null) { + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + return position; + } } + return null; + } - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); - if (deviceSession == null) { + private Object decodeInf(Channel channel, SocketAddress remoteAddress, String sentence) { + Parser parser = new Parser(PATTERN_INF, sentence); + Position position = initPosition(parser, channel, remoteAddress); + if (position == null) { return null; } - Position position = new Position(); - position.setProtocol(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - position.set(Position.KEY_STATUS, parser.next()); parser.next(); // odometer or external power @@ -378,19 +380,11 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { private Object decodeVer(Channel channel, SocketAddress remoteAddress, String sentence) { Parser parser = new Parser(PATTERN_VER, sentence); - if (!parser.matches()) { + Position position = initPosition(parser, channel, remoteAddress); + if (position == null) { return null; } - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); - if (deviceSession == null) { - return null; - } - - Position position = new Position(); - position.setProtocol(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - position.set("deviceType", parser.next()); position.set(Position.KEY_VERSION_FW, parser.nextInt(16)); position.set(Position.KEY_VERSION_HW, parser.nextInt(16)); @@ -418,31 +412,26 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, null); } - if (parser.hasNext(4)) { - position.setNetwork(new Network(CellTower.from( - parser.nextInt(), parser.nextInt(), parser.nextInt(16), parser.nextInt(16)))); + if (parser.hasNext(6)) { + int mcc = parser.nextInt(); + int mnc = parser.nextInt(); + if (parser.hasNext(2)) { + position.setNetwork(new Network(CellTower.from(mcc, mnc, parser.nextInt(), parser.nextInt()))); + } else { + position.setNetwork(new Network(CellTower.from(mcc, mnc, parser.nextInt(16), parser.nextInt(16)))); + } } - parser.skip(4); // alternative networks - position.set(Position.KEY_ODOMETER, parser.nextDouble() * 1000); } private Object decodeObd(Channel channel, SocketAddress remoteAddress, String sentence) { Parser parser = new Parser(PATTERN_OBD, sentence); - if (!parser.matches()) { - return null; - } - - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); - if (deviceSession == null) { + Position position = initPosition(parser, channel, remoteAddress); + if (position == null) { return null; } - Position position = new Position(); - position.setProtocol(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - position.set(Position.KEY_RPM, parser.next()); position.set(Position.KEY_OBD_SPEED, parser.next()); position.set(Position.PREFIX_TEMP + 1, parser.next()); @@ -536,19 +525,11 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { private Object decodeIgn(Channel channel, SocketAddress remoteAddress, String sentence) { Parser parser = new Parser(PATTERN_IGN, sentence); - if (!parser.matches()) { + Position position = initPosition(parser, channel, remoteAddress); + if (position == null) { return null; } - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); - if (deviceSession == null) { - return null; - } - - Position position = new Position(); - position.setProtocol(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - decodeLocation(position, parser); position.set(Position.KEY_HOURS, parser.next()); @@ -563,19 +544,11 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { private Object decodeIda(Channel channel, SocketAddress remoteAddress, String sentence) { Parser parser = new Parser(PATTERN_IDA, sentence); - if (!parser.matches()) { - return null; - } - - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); - if (deviceSession == null) { + Position position = initPosition(parser, channel, remoteAddress); + if (position == null) { return null; } - Position position = new Position(); - position.setProtocol(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - position.set(Position.KEY_RFID, parser.next()); decodeLocation(position, parser); @@ -591,19 +564,11 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { private Object decodeWif(Channel channel, SocketAddress remoteAddress, String sentence) { Parser parser = new Parser(PATTERN_WIF, sentence); - if (!parser.matches()) { + Position position = initPosition(parser, channel, remoteAddress); + if (position == null) { return null; } - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); - if (deviceSession == null) { - return null; - } - - Position position = new Position(); - position.setProtocol(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - getLastLocation(position, null); Network network = new Network(); @@ -625,19 +590,11 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { private Object decodeGsm(Channel channel, SocketAddress remoteAddress, String sentence) { Parser parser = new Parser(PATTERN_GSM, sentence); - if (!parser.matches()) { - return null; - } - - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); - if (deviceSession == null) { + Position position = initPosition(parser, channel, remoteAddress); + if (position == null) { return null; } - Position position = new Position(); - position.setProtocol(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - getLastLocation(position, null); Network network = new Network(); @@ -659,19 +616,11 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { private Object decodeOther(Channel channel, SocketAddress remoteAddress, String sentence, String type) { Parser parser = new Parser(PATTERN, sentence); - if (!parser.matches()) { + Position position = initPosition(parser, channel, remoteAddress); + if (position == null) { return null; } - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); - if (deviceSession == null) { - return null; - } - - Position position = new Position(); - position.setProtocol(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - int reportType = parser.nextInt(); if (type.equals("NMR")) { position.set(Position.KEY_MOTION, reportType); @@ -699,19 +648,11 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { private Object decodeBasic(Channel channel, SocketAddress remoteAddress, String sentence, String type) { Parser parser = new Parser(PATTERN_BASIC, sentence); - if (!parser.matches()) { - return null; - } - - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); - if (deviceSession == null) { + Position position = initPosition(parser, channel, remoteAddress); + if (position == null) { return null; } - Position position = new Position(); - position.setProtocol(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - int hdop = parser.nextInt(); position.setValid(hdop > 0); position.set(Position.KEY_HDOP, hdop); diff --git a/test/org/traccar/protocol/Gl200ProtocolDecoderTest.java b/test/org/traccar/protocol/Gl200ProtocolDecoderTest.java index ee7bf84a7..811c0f62c 100644 --- a/test/org/traccar/protocol/Gl200ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Gl200ProtocolDecoderTest.java @@ -13,7 +13,7 @@ public class Gl200ProtocolDecoderTest extends ProtocolTest { verifyPositions(decoder, text( "+RESP:GTFRI,210102,354524044925825,,1,1,1,29,2.8,0,133.7,-90.203063,32.265473,20170318005208,,,,,10800,4,20170318005208,0002$")); - verifyAttributes(decoder, text( + verifyPositions(decoder, false, text( "+RESP:GTFRI,210102,354524044925825,,1,1,1,,,,,,,,310,410,51bc,ca1dae6,10800,1,20170318214333,0002$")); verifyAttributes(decoder, text( @@ -112,7 +112,7 @@ public class Gl200ProtocolDecoderTest extends ProtocolTest { verifyPositions(decoder, text( "+BUFF:GTFRI,060402,862894021808798,,,10,1,1,0.0,349,394.3,-63.287717,-17.662410,20160116234031,0736,0003,6ABA,8305,00,3326.8,,,,94,220100,,,,20160116194035,4D83")); - verifyPosition(decoder, text( + verifyPositions(decoder, text( "+RESP:GTFRI,2C0204,867162020003125,GL300W,0,0,2,1,1.7,205,2867.0,-78.481127,-0.206828,20160215210433,0740,0000,7596,5891C,0.0,1,1.7,205,2867.0,-78.481127,-0.206828,20160215210503,0740,0000,7596,5891C,0.0,88,20160215210506,1E78$")); verifyPositions(decoder, text( @@ -121,7 +121,7 @@ public class Gl200ProtocolDecoderTest extends ProtocolTest { verifyPositions(decoder, text( "+RESP:GTFRI,110100,A5868800000015,,0,0,1,1,4.3,92,70.0,121.354335,31.222073,20110214013254,0460,0000,18d8,6141,00,80,20110214013254,000C")); - verifyPositions(decoder, text( + verifyNotNull(decoder, text( "+RESP:GTFRI,210102,A10000458356CE,,0,1,1,15,1.4,0,190.6,-85.765763,42.894896,20160208164505,4126,210,0,18673,00,92,20160208164507,00A6")); verifyPositions(decoder, text( @@ -208,10 +208,10 @@ public class Gl200ProtocolDecoderTest extends ProtocolTest { verifyPositions(decoder, text( "+RESP:GTFRI,240100,135790246811220,,,10,2,1,4.3,92,70.0,121.354335,31.222073,20090214013254,0460,0000,18d8,6141,00,0,4.3,92,70.0,121.354335,31.222073,20090101000000,0460,0000,18d8,6141,00,2000.0,12345:12:34,,,80,,,,,20090214093254,11F0$")); - verifyPosition(decoder, text( + verifyNotNull(decoder, text( "+RESP:GTSTT,280100,A1000043D20139,,42,0,0.1,321,228.6,-76.660884,39.832552,20150615120628,0310,0484,00600019,0A52,,20150615085741,0320$")); - verifyPosition(decoder, text( + verifyNotNull(decoder, text( "+RESP:GTRTL,280100,A1000043D20139,,0,0,1,1,0.1,321,239.1,-76.661047,39.832501,20150615114455,0310,0484,00600019,0A52,,87,20150615074456,031E$")); verifyAttributes(decoder, text( diff --git a/test/org/traccar/protocol/RuptelaProtocolDecoderTest.java b/test/org/traccar/protocol/RuptelaProtocolDecoderTest.java index 1074b7eb3..dcb1d73ca 100644 --- a/test/org/traccar/protocol/RuptelaProtocolDecoderTest.java +++ b/test/org/traccar/protocol/RuptelaProtocolDecoderTest.java @@ -10,7 +10,7 @@ public class RuptelaProtocolDecoderTest extends ProtocolTest { RuptelaProtocolDecoder decoder = new RuptelaProtocolDecoder(new RuptelaProtocol()); - verifyAttributes(decoder, binary( + verifyNothing(decoder, binary( "002e000315bc70d3e2ff0f4f42443130302e30312e30382e30300000c2b30ea77e430000601b000001f40000003c00144aa0")); verifyAttributes(decoder, binary( |