diff options
-rw-r--r-- | src/org/traccar/protocol/Xexun2ProtocolDecoder.java | 150 | ||||
-rw-r--r-- | src/org/traccar/protocol/XexunProtocol.java | 12 | ||||
-rw-r--r-- | src/org/traccar/protocol/XexunProtocolDecoder.java | 62 | ||||
-rw-r--r-- | test/org/traccar/protocol/Xexun2ProtocolDecoderTest.java | 62 | ||||
-rw-r--r-- | test/org/traccar/protocol/XexunProtocolDecoderTest.java | 63 |
5 files changed, 112 insertions, 237 deletions
diff --git a/src/org/traccar/protocol/Xexun2ProtocolDecoder.java b/src/org/traccar/protocol/Xexun2ProtocolDecoder.java deleted file mode 100644 index aba913ed3..000000000 --- a/src/org/traccar/protocol/Xexun2ProtocolDecoder.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright 2012 - 2014 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.protocol; - -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.jboss.netty.channel.ChannelHandlerContext; - -import org.traccar.BaseProtocolDecoder; -import org.traccar.model.Event; -import org.traccar.model.Position; - -public class Xexun2ProtocolDecoder extends BaseProtocolDecoder { - - public Xexun2ProtocolDecoder(XexunProtocol protocol) { - super(protocol); - } - - static private Pattern pattern = Pattern.compile( - "[\r\n]*" + - "(\\d+)," + // Serial - "([^,]+)?," + // Number - "G[PN]RMC," + - "(\\d{2})(\\d{2})(\\d{2})\\.(\\d+)," + // Time (HHMMSS.SSS) - "([AV])," + // Validity - "(\\d{2})(\\d{2}\\.\\d+)," + // Latitude (DDMM.MMMM) - "([NS])," + - "(\\d{3})(\\d{2}\\.\\d+)," + // Longitude (DDDMM.MMMM) - "([EW])," + - "(\\d+\\.\\d+)," + // Speed - "(\\d+\\.\\d+)?," + // Course - "(\\d{2})(\\d{2})(\\d{2})," + // Date (DDMMYY) - "[^\\*]*\\*..," + // Checksum - "([FL])," + // Signal - "(?:([^,]*),)?" + // Alarm - ".*imei:" + - "(\\d+)," + // IMEI - "(\\d+)," + // Satellites - "(-?\\d+\\.\\d+)?," + // Altitude - "[FL]:(\\d+\\.\\d+)V" + // Power - ".*" + - "[\r\n]*"); - - @Override - protected Object decode( - ChannelHandlerContext ctx, Channel channel, Object msg) - throws Exception { - - // Parse message - String sentence = (String) msg; - Matcher parser = pattern.matcher(sentence); - if (!parser.matches()) { - return null; - } - - // Create new position - Position position = new Position(); - position.setProtocol(getProtocolName()); - - Integer index = 1; - - // Serial - position.set("serial", parser.group(index++)); - - // Number - position.set("number", parser.group(index++)); - - // Time - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - time.set(Calendar.HOUR_OF_DAY, Integer.valueOf(parser.group(index++))); - time.set(Calendar.MINUTE, Integer.valueOf(parser.group(index++))); - time.set(Calendar.SECOND, Integer.valueOf(parser.group(index++))); - time.set(Calendar.MILLISECOND, Integer.valueOf(parser.group(index++))); - - // Validity - position.setValid(parser.group(index++).compareTo("A") == 0); - - // Latitude - Double latitude = Double.valueOf(parser.group(index++)); - latitude += Double.valueOf(parser.group(index++)) / 60; - if (parser.group(index++).compareTo("S") == 0) latitude = -latitude; - position.setLatitude(latitude); - - // Longitude - Double longitude = Double.valueOf(parser.group(index++)); - longitude += Double.valueOf(parser.group(index++)) / 60; - if (parser.group(index++).compareTo("W") == 0) longitude = -longitude; - position.setLongitude(longitude); - - // Speed - position.setSpeed(Double.valueOf(parser.group(index++))); - - // Course - String course = parser.group(index++); - if (course != null) { - position.setCourse(Double.valueOf(course)); - } - - // Date - time.set(Calendar.DAY_OF_MONTH, Integer.valueOf(parser.group(index++))); - time.set(Calendar.MONTH, Integer.valueOf(parser.group(index++)) - 1); - time.set(Calendar.YEAR, 2000 + Integer.valueOf(parser.group(index++))); - position.setTime(time.getTime()); - - // Signal - position.set("signal", parser.group(index++)); - - // Alarm - position.set(Event.KEY_ALARM, parser.group(index++)); - - // Get device by IMEI - if (!identify(parser.group(index++), channel)) { - return null; - } - position.setDeviceId(getDeviceId()); - - // Satellites - position.set(Event.KEY_SATELLITES, parser.group(index++).replaceFirst ("^0*(?![\\.$])", "")); - - // Altitude - String altitude = parser.group(index++); - if (altitude != null) { - position.setAltitude(Double.valueOf(altitude)); - } - - // Power - position.set(Event.KEY_POWER, Double.valueOf(parser.group(index++))); - - return position; - } - -} diff --git a/src/org/traccar/protocol/XexunProtocol.java b/src/org/traccar/protocol/XexunProtocol.java index 0d4502a3a..52d022b05 100644 --- a/src/org/traccar/protocol/XexunProtocol.java +++ b/src/org/traccar/protocol/XexunProtocol.java @@ -22,11 +22,8 @@ import org.jboss.netty.handler.codec.string.StringDecoder; import org.traccar.BaseProtocol; import org.traccar.Context; import org.traccar.TrackerServer; -import org.traccar.protocol.commands.CommandTemplate; -import org.traccar.http.commands.CommandType; import java.util.List; -import java.util.Map; public class XexunProtocol extends BaseProtocol { @@ -39,15 +36,14 @@ public class XexunProtocol extends BaseProtocol { serverList.add(new TrackerServer(new ServerBootstrap(), this.getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { - if (Boolean.valueOf(Context.getProps().getProperty(getName() + ".extended"))) { + boolean full = Boolean.valueOf(Context.getProps().getProperty(getName() + ".extended")); + if (full) { pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); // tracker bug \n\r - pipeline.addLast("stringDecoder", new StringDecoder()); - pipeline.addLast("objectDecoder", new Xexun2ProtocolDecoder(XexunProtocol.this)); } else { pipeline.addLast("frameDecoder", new XexunFrameDecoder()); - pipeline.addLast("stringDecoder", new StringDecoder()); - pipeline.addLast("objectDecoder", new XexunProtocolDecoder(XexunProtocol.this)); } + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectDecoder", new XexunProtocolDecoder(XexunProtocol.this, full)); } }); } diff --git a/src/org/traccar/protocol/XexunProtocolDecoder.java b/src/org/traccar/protocol/XexunProtocolDecoder.java index 4f01faa0c..2f5628a9d 100644 --- a/src/org/traccar/protocol/XexunProtocolDecoder.java +++ b/src/org/traccar/protocol/XexunProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2013 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 2014 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. @@ -15,7 +15,6 @@ */ package org.traccar.protocol; -import java.text.ParseException; import java.util.Calendar; import java.util.TimeZone; import java.util.regex.Matcher; @@ -25,17 +24,21 @@ import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.traccar.BaseProtocolDecoder; +import org.traccar.model.Event; import org.traccar.model.Position; public class XexunProtocolDecoder extends BaseProtocolDecoder { - public XexunProtocolDecoder(XexunProtocol protocol) { + private boolean full; + + public XexunProtocolDecoder(XexunProtocol protocol, boolean full) { super(protocol); + this.full = full; } - private static final Pattern pattern = Pattern.compile( + static private Pattern patternBasic = Pattern.compile( "G[PN]RMC," + - "(\\d{2})(\\d{2})(\\d{2}).(\\d+)," + // Time (HHMMSS.SSS) + "(\\d{2})(\\d{2})(\\d{2})\\.(\\d+)," + // Time (HHMMSS.SSS) "([AV])," + // Validity "(\\d+)(\\d{2}\\.\\d+)," + // Latitude (DDMM.MMMM) "([NS])," + @@ -44,19 +47,33 @@ public class XexunProtocolDecoder extends BaseProtocolDecoder { "(\\d+\\.?\\d*)," + // Speed "(\\d+\\.?\\d*)?," + // Course "(\\d{2})(\\d{2})(\\d{2})," + // Date (DDMMYY) - ".*\r?\n?.*imei:" + + "[^\\*]*\\*..," + // Checksum + "([FL])," + // Signal + "(?:([^,]*),)?" + // Alarm + ".*imei:" + "(\\d+),"); // IMEI + static private Pattern patternFull = Pattern.compile( + "[\r\n]*" + + "(\\d+)," + // Serial + "([^,]+)?," + // Number + patternBasic.pattern() + + "(\\d+)," + // Satellites + "(-?\\d+\\.\\d+)?," + // Altitude + "[FL]:(\\d+\\.\\d+)V" + // Power + ".*" + + "[\r\n]*"); + @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { // Parse message - String sentence = (String) msg; - Matcher parser = pattern.matcher(sentence); + Pattern pattern = full ? patternFull : patternBasic; + Matcher parser = pattern.matcher((String) msg); if (!parser.matches()) { - throw new ParseException(null, 0); + return null; } // Create new position @@ -65,6 +82,11 @@ public class XexunProtocolDecoder extends BaseProtocolDecoder { Integer index = 1; + if (full) { + position.set("serial", parser.group(index++)); + position.set("number", parser.group(index++)); + } + // Time Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); time.clear(); @@ -106,11 +128,33 @@ public class XexunProtocolDecoder extends BaseProtocolDecoder { time.set(Calendar.YEAR, 2000 + Integer.valueOf(parser.group(index++))); position.setTime(time.getTime()); + // Signal + position.set("signal", parser.group(index++)); + + // Alarm + position.set(Event.KEY_ALARM, parser.group(index++)); + // Get device by IMEI if (!identify(parser.group(index++), channel)) { return null; } position.setDeviceId(getDeviceId()); + + if (full) { + + // Satellites + position.set(Event.KEY_SATELLITES, parser.group(index++).replaceFirst ("^0*(?![\\.$])", "")); + + // Altitude + String altitude = parser.group(index++); + if (altitude != null) { + position.setAltitude(Double.valueOf(altitude)); + } + + // Power + position.set(Event.KEY_POWER, Double.valueOf(parser.group(index++))); + } + return position; } diff --git a/test/org/traccar/protocol/Xexun2ProtocolDecoderTest.java b/test/org/traccar/protocol/Xexun2ProtocolDecoderTest.java deleted file mode 100644 index 6fa23943f..000000000 --- a/test/org/traccar/protocol/Xexun2ProtocolDecoderTest.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.traccar.protocol; - -import static org.junit.Assert.assertNull; -import org.traccar.helper.TestDataManager; -import static org.traccar.helper.DecoderVerifier.verify; -import org.junit.Test; - -public class Xexun2ProtocolDecoderTest extends ProtocolDecoderTest { - - @Test - public void testDecode() throws Exception { - - Xexun2ProtocolDecoder decoder = new Xexun2ProtocolDecoder(new XexunProtocol()); - - assertNull(decoder.decode(null, null, - ",+48606717068,,L,, imei:012207005047292,,,F:4.28V,1,52,11565,247,01,000E,1FC5")); - - verify(decoder.decode(null, null, - "130302125349,+79604870506,GPRMC,085349.000,A,4503.2392,N,03858.5660,E,6.95,154.65,020313,,,A*6C,F,, imei:012207007744243,03,-1.5,F:4.15V,1,139,28048,250,01,278A,5072")); - - verify(decoder.decode(null, null, - "111111120009,+436763737552,GPRMC,120009.590,A,4639.6774,N,01418.5737,E,0.00,0.00,111111,,,A*68,F,, imei:359853000144328,04,481.2,F:4.15V,0,139,2689,232,03,2725,0576")); - - verify(decoder.decode(null, null, - "111111120009,+436763737552,GPRMC,120600.000,A,6000.0000,N,13000.0000,E,0.00,0.00,010112,,,A*68,F,help me!, imei:123456789012345,04,481.2,F:4.15V,0,139,2689,232,03,2725,0576")); - - verify(decoder.decode(null, null, - "111111120009,+436763737552,GPRMC,120600.000,A,6000.0000,N,13000.0000,E,0.00,0.00,010112,,,A*68,F,help me!, imei:123456789012345,04,481.2,L:3.5V,0,139,2689,232,03,2725,0576")); - - verify(decoder.decode(null, null, - "111111120009,436763737552,GPRMC,120600.000,A,6000.0000,N,13000.0000,E,0.00,0.00,010112,,,A*68,F,help me!, imei:123456789012345,04,481.2,L:3.5V,0,139,2689,232,03,2725,0576")); - - verify(decoder.decode(null, null, - "111111120009,+1234,GPRMC,204530.4,A,6000.0000,N,13000.0000,E,0.0,,010112,0.0,E,A*68,F,imei:123456789012345,04,123.5,F:3.55V,0,139,,232,03,272CE1,0576")); - - verify(decoder.decode(null, null, - "111111120009,+1234,GPRMC,204530.4,A,6000.000,N,01000.6288,E,0.0,0.00,230713,0.0,E,A*3C,F,imei:123456789012345,00,,F:3.88V,0,125,,262,01,224CE1,379B")); - - verify(decoder.decode(null, null, - "111111120009,+1234,GPRMC,215840.7,A,6000.000,N,01000.6253,E,0.0,0.00,230713,0.0,E,A*34,F,imei:123456789012345,00,,F:3.9V,0,124,,262,01,224CE1,379B")); - - verify(decoder.decode(null, null, - "130725134142,,GPRMC,134142.591,A,3845.6283,N,00909.8876,W,2.08,287.33,250713,,,A*71,F,, imei:013227000526784,03,-50.7,L:3.69V,0,128,65337,268,03,177A,119F")); - - verify(decoder.decode(null, null, - "140602152533,TESCO_INFO,GPRMC,152533.000,A,5145.4275,N,00000.3448,E,0.00,0.00,020614,,,A*66,F,, imei:013227002781643,06,35.1,F:4.15V,1,135,38950,234,10,10B4,5235")); - - verify(decoder.decode(null, null, - "150216154418,5277,GNRMC,134418.000,A,5533.8973,N,03745.4398,E,0.00,308.85,160215,,,A*7A,F,, imei:864244028033115,10,169.8,F:4.28V,1,132,48269,250,99,6D0D,8572")); - - verify(decoder.decode(null, null, - "150224173341,+66961544651,GPRMC,093341.000,A,1344.5716,N,10033.6648,E,0.00,0.00,240215,,,A*68,F,,imei:865328028306149,05,106.4,F:4.01V/ADC1=0.20V/ADC2=0.00V,0,159,955,520,01,5DE8,0399,6.21km")); - - verify(decoder.decode(null, null, - "150316182840,07872167745,GPRMC,182840.000,A,5126.1310,N,00055.5573,W,0.00,0.00,160315,,,A*7C,F,,imei:865328023469306,06,54.3,F:4.10V/ADC1=0.76V/ADC2=0.00V,0,157,38486,234,10,34DC,48A6,3.70km")); - - verify(decoder.decode(null, null, - "150615123731,+33647384611,GPRMC,103731.636,A,4545.5266,N,00448.8259,E,21.12,276.01,150615,,,A*57,L,, imei:013949002026675,04,3522.9,F:3.72V,0,142,21744,208,01,0702,9C8C")); - - } - -} diff --git a/test/org/traccar/protocol/XexunProtocolDecoderTest.java b/test/org/traccar/protocol/XexunProtocolDecoderTest.java index 879771126..85f7921fb 100644 --- a/test/org/traccar/protocol/XexunProtocolDecoderTest.java +++ b/test/org/traccar/protocol/XexunProtocolDecoderTest.java @@ -1,6 +1,6 @@ package org.traccar.protocol; -import org.traccar.helper.TestDataManager; +import static org.junit.Assert.assertNull; import static org.traccar.helper.DecoderVerifier.verify; import org.junit.Test; @@ -9,10 +9,7 @@ public class XexunProtocolDecoderTest extends ProtocolDecoderTest { @Test public void testDecode() throws Exception { - XexunProtocolDecoder decoder = new XexunProtocolDecoder(new XexunProtocol()); - - verify(decoder.decode(null, null, - "GPRMC,043435.000,A,811.299200,S,11339.9500,E,0.93,29.52,160313,00,0000.0,A*65,F,,imei:359585014597923,")); + XexunProtocolDecoder decoder = new XexunProtocolDecoder(new XexunProtocol(), false); verify(decoder.decode(null, null, "GPRMC,150120.000,A,3346.4463,S,15057.3083,E,0.0,117.4,010911,,,A*76,F,imei:351525010943661,")); @@ -27,9 +24,6 @@ public class XexunProtocolDecoderTest extends ProtocolDecoderTest { "GPRMC,080303.000,A,5546.7313,N,03738.6005,E,0.56,160.13,100311,,,A*6A,L,imei:354778030461167,")); verify(decoder.decode(null, null, - "GPRMC,014623.000,A,4710.8260,N,1948.1220,E,0.11,105.40,111212,00,0000.0,A*49,F,,imei:357713002048962,")); - - verify(decoder.decode(null, null, "GPRMC,220828.678,A,5206.1446,N,02038.2403,,0,0,160912,,,E*23,L,imei:358948012501019,")); verify(decoder.decode(null, null, @@ -41,6 +35,59 @@ public class XexunProtocolDecoderTest extends ProtocolDecoderTest { verify(decoder.decode(null, null, "GPRMC,103731.636,A,4545.5266,N,00448.8259,E,21.12,276.01,150615,,,A*57,L,, imei:013949002026675,")); + verify(decoder.decode(null, null, + "GPRMC,014623.000,A,4710.8260,N,1948.1220,E,0.11,105.40,111212,00,0000.0,A*49,F,,imei:357713002048962,")); + + verify(decoder.decode(null, null, + "GPRMC,043435.000,A,811.299200,S,11339.9500,E,0.93,29.52,160313,00,0000.0,A*65,F,,imei:359585014597923,")); + + decoder = new XexunProtocolDecoder(new XexunProtocol(), true); + + assertNull(decoder.decode(null, null, + ",+48606717068,,L,, imei:012207005047292,,,F:4.28V,1,52,11565,247,01,000E,1FC5")); + + verify(decoder.decode(null, null, + "130302125349,+79604870506,GPRMC,085349.000,A,4503.2392,N,03858.5660,E,6.95,154.65,020313,,,A*6C,F,, imei:012207007744243,03,-1.5,F:4.15V,1,139,28048,250,01,278A,5072")); + + verify(decoder.decode(null, null, + "111111120009,+436763737552,GPRMC,120009.590,A,4639.6774,N,01418.5737,E,0.00,0.00,111111,,,A*68,F,, imei:359853000144328,04,481.2,F:4.15V,0,139,2689,232,03,2725,0576")); + + verify(decoder.decode(null, null, + "111111120009,+436763737552,GPRMC,120600.000,A,6000.0000,N,13000.0000,E,0.00,0.00,010112,,,A*68,F,help me!, imei:123456789012345,04,481.2,F:4.15V,0,139,2689,232,03,2725,0576")); + + verify(decoder.decode(null, null, + "111111120009,+436763737552,GPRMC,120600.000,A,6000.0000,N,13000.0000,E,0.00,0.00,010112,,,A*68,F,help me!, imei:123456789012345,04,481.2,L:3.5V,0,139,2689,232,03,2725,0576")); + + verify(decoder.decode(null, null, + "111111120009,436763737552,GPRMC,120600.000,A,6000.0000,N,13000.0000,E,0.00,0.00,010112,,,A*68,F,help me!, imei:123456789012345,04,481.2,L:3.5V,0,139,2689,232,03,2725,0576")); + + verify(decoder.decode(null, null, + "111111120009,+1234,GPRMC,204530.4,A,6000.0000,N,13000.0000,E,0.0,,010112,0.0,E,A*68,F,imei:123456789012345,04,123.5,F:3.55V,0,139,,232,03,272CE1,0576")); + + verify(decoder.decode(null, null, + "111111120009,+1234,GPRMC,204530.4,A,6000.000,N,01000.6288,E,0.0,0.00,230713,0.0,E,A*3C,F,imei:123456789012345,00,,F:3.88V,0,125,,262,01,224CE1,379B")); + + verify(decoder.decode(null, null, + "111111120009,+1234,GPRMC,215840.7,A,6000.000,N,01000.6253,E,0.0,0.00,230713,0.0,E,A*34,F,imei:123456789012345,00,,F:3.9V,0,124,,262,01,224CE1,379B")); + + verify(decoder.decode(null, null, + "130725134142,,GPRMC,134142.591,A,3845.6283,N,00909.8876,W,2.08,287.33,250713,,,A*71,F,, imei:013227000526784,03,-50.7,L:3.69V,0,128,65337,268,03,177A,119F")); + + verify(decoder.decode(null, null, + "140602152533,TESCO_INFO,GPRMC,152533.000,A,5145.4275,N,00000.3448,E,0.00,0.00,020614,,,A*66,F,, imei:013227002781643,06,35.1,F:4.15V,1,135,38950,234,10,10B4,5235")); + + verify(decoder.decode(null, null, + "150216154418,5277,GNRMC,134418.000,A,5533.8973,N,03745.4398,E,0.00,308.85,160215,,,A*7A,F,, imei:864244028033115,10,169.8,F:4.28V,1,132,48269,250,99,6D0D,8572")); + + verify(decoder.decode(null, null, + "150224173341,+66961544651,GPRMC,093341.000,A,1344.5716,N,10033.6648,E,0.00,0.00,240215,,,A*68,F,,imei:865328028306149,05,106.4,F:4.01V/ADC1=0.20V/ADC2=0.00V,0,159,955,520,01,5DE8,0399,6.21km")); + + verify(decoder.decode(null, null, + "150316182840,07872167745,GPRMC,182840.000,A,5126.1310,N,00055.5573,W,0.00,0.00,160315,,,A*7C,F,,imei:865328023469306,06,54.3,F:4.10V/ADC1=0.76V/ADC2=0.00V,0,157,38486,234,10,34DC,48A6,3.70km")); + + verify(decoder.decode(null, null, + "150615123731,+33647384611,GPRMC,103731.636,A,4545.5266,N,00448.8259,E,21.12,276.01,150615,,,A*57,L,, imei:013949002026675,04,3522.9,F:3.72V,0,142,21744,208,01,0702,9C8C")); + } } |