diff options
-rw-r--r-- | src/org/traccar/ServerManager.java | 8 | ||||
-rw-r--r-- | src/org/traccar/protocol/TramigoFrameDecoder.java | 52 | ||||
-rw-r--r-- | src/org/traccar/protocol/TramigoProtocolDecoder.java | 85 | ||||
-rw-r--r-- | test/org/traccar/protocol/TramigoProtocolDecoderTest.java | 11 |
4 files changed, 133 insertions, 23 deletions
diff --git a/src/org/traccar/ServerManager.java b/src/org/traccar/ServerManager.java index f6504fd27..ca698f977 100644 --- a/src/org/traccar/ServerManager.java +++ b/src/org/traccar/ServerManager.java @@ -1290,13 +1290,15 @@ public class ServerManager { private void initTramigoServer(final String protocol) throws SQLException { if (isProtocolEnabled(properties, protocol)) { - serverList.add(new TrackerServer(this, new ServerBootstrap(), protocol) { + TrackerServer server = new TrackerServer(this, new ServerBootstrap(), protocol) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { - pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024, 6, 2, -8, 0)); + pipeline.addLast("frameDecoder", new TramigoFrameDecoder()); pipeline.addLast("objectDecoder", new TramigoProtocolDecoder(dataManager, protocol, properties)); } - }); + }; + server.setEndianness(ByteOrder.LITTLE_ENDIAN); + serverList.add(server); } } diff --git a/src/org/traccar/protocol/TramigoFrameDecoder.java b/src/org/traccar/protocol/TramigoFrameDecoder.java new file mode 100644 index 000000000..687c5ba04 --- /dev/null +++ b/src/org/traccar/protocol/TramigoFrameDecoder.java @@ -0,0 +1,52 @@ +/* + * Copyright 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. + * 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 org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.frame.FrameDecoder; +import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; + +public class TramigoFrameDecoder extends LengthFieldBasedFrameDecoder { + + public TramigoFrameDecoder() { + super(1024, 6, 2, -8, 0); + } + + @Override + protected Object decode( + ChannelHandlerContext ctx, + Channel channel, + ChannelBuffer buf) throws Exception { + + if (buf.readableBytes() < 20) { + return null; + } + + // Swap byte order for legacy protocol + if (buf.getUnsignedByte(buf.readerIndex()) == 0x80) { + int length = buf.readableBytes(); + byte bytes[] = new byte[length]; + buf.getBytes(buf.readerIndex(), bytes); + buf = ChannelBuffers.wrappedBuffer(bytes); + } + + return decode(ctx, channel, buf); + } + +} diff --git a/src/org/traccar/protocol/TramigoProtocolDecoder.java b/src/org/traccar/protocol/TramigoProtocolDecoder.java index 9bc8a5ae8..70eec208e 100644 --- a/src/org/traccar/protocol/TramigoProtocolDecoder.java +++ b/src/org/traccar/protocol/TramigoProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2014 - 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,6 +16,7 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.traccar.BaseProtocolDecoder; @@ -24,8 +25,15 @@ import org.traccar.helper.Log; import org.traccar.model.ExtendedInfoFormatter; import org.traccar.model.Position; +import java.nio.charset.Charset; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; +import java.util.Locale; import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class TramigoProtocolDecoder extends BaseProtocolDecoder { @@ -43,10 +51,7 @@ public class TramigoProtocolDecoder extends BaseProtocolDecoder { ChannelBuffer buf = (ChannelBuffer) msg; - if (buf.readUnsignedByte() != 1) { - return null; // wrong protocol version - } - + int protocol = buf.readUnsignedByte(); buf.readUnsignedByte(); // version id int index = buf.readUnsignedShort(); int type = buf.readUnsignedShort(); @@ -56,25 +61,27 @@ public class TramigoProtocolDecoder extends BaseProtocolDecoder { long id = buf.readUnsignedInt(); buf.readUnsignedInt(); // time - if (type == MSG_COMPACT || type == MSG_FULL) { + // Create new position + Position position = new Position(); + ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter(getProtocol()); + extendedInfo.set("index", index); + position.setValid(true); + + // Get device id + try { + position.setDeviceId(getDataManager().getDeviceByImei(String.valueOf(id)).getId()); + } catch(Exception error) { + Log.warning("Unknown device - " + id); + return null; + } - // Create new position - Position position = new Position(); - ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter(getProtocol()); - extendedInfo.set("index", index); + if (protocol == 0x01 && (type == MSG_COMPACT || type == MSG_FULL)) { - // Get device id - try { - position.setDeviceId(getDataManager().getDeviceByImei(String.valueOf(id)).getId()); - } catch(Exception error) { - Log.warning("Unknown device - " + id); - return null; - } + // TODO: send ack buf.readUnsignedShort(); // report trigger buf.readUnsignedShort(); // state flag - position.setValid(true); position.setLatitude(buf.readUnsignedInt() * 0.0000001); position.setLongitude(buf.readUnsignedInt() * 0.0000001); position.setAltitude(0.0); @@ -99,6 +106,48 @@ public class TramigoProtocolDecoder extends BaseProtocolDecoder { position.setExtendedInfo(extendedInfo.toString()); return position; + + } else if (protocol == 0x80) { + + if (channel != null) { + channel.write(ChannelBuffers.copiedBuffer("gprs,ack," + index, Charset.defaultCharset())); + } + + String sentence = buf.toString(Charset.defaultCharset()); + + // Coordinates + Pattern pattern = Pattern.compile("(-?\\d+\\.\\d+), (-?\\d+\\.\\d+)"); + Matcher matcher = pattern.matcher(sentence); + if (!matcher.find()) { + return null; + } + position.setLatitude(Double.valueOf(matcher.group(1))); + position.setLongitude(Double.valueOf(matcher.group(2))); + position.setAltitude(0.0); + + // Speed and Course + pattern = Pattern.compile("([NSWE]{1,2}) with speed (\\d+) km/h"); + matcher = pattern.matcher(sentence); + if (matcher.find()) { + position.setSpeed(Double.valueOf(matcher.group(2)) * 0.539957); + position.setCourse(0.0); // matcher.group(1) for course + } else { + position.setSpeed(0.0); + position.setCourse(0.0); + } + + // Time + pattern = Pattern.compile("(\\d{1,2}:\\d{2} \\w{3} \\d{1,2})"); + matcher = pattern.matcher(sentence); + if (!matcher.find()) { + return null; + } + DateFormat dateFormat = new SimpleDateFormat("HH:mm MMM d yyyy"); + position.setTime(dateFormat.parse(matcher.group(1) + " " + Calendar.getInstance().get(Calendar.YEAR))); + + position.setExtendedInfo(extendedInfo.toString()); + return position; + } return null; diff --git a/test/org/traccar/protocol/TramigoProtocolDecoderTest.java b/test/org/traccar/protocol/TramigoProtocolDecoderTest.java index eedabe965..2009c8072 100644 --- a/test/org/traccar/protocol/TramigoProtocolDecoderTest.java +++ b/test/org/traccar/protocol/TramigoProtocolDecoderTest.java @@ -5,6 +5,8 @@ import org.junit.Test; import org.traccar.helper.ChannelBufferTools; import org.traccar.helper.TestDataManager; +import java.nio.ByteOrder; + import static org.junit.Assert.assertNull; import static org.traccar.helper.DecoderVerifier.verify; @@ -14,11 +16,16 @@ public class TramigoProtocolDecoderTest { public void testDecode() throws Exception { TramigoProtocolDecoder decoder = new TramigoProtocolDecoder(new TestDataManager(), null, null); - + + verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, ChannelBufferTools.convertHexString( + "8000011bb0009e0001015b93032ef6f35994a9545472616d69676f3a204d6f76696e672c20302e3930206b6d205345206f66204372616e6562726f6f6b20466972652053746174696f6e2c2050656e726974682c205379646e65792c2041552c202d33332e37303732322c203135302e37313735392c2053452077697468207370656564203337206b6d2f682c2031393a3438204a616e20342020454f46")))); + + // Tramigo: Parked, 0.12 km E of McDonald's H.V. dela Costa, Makati, 11:07 Mar 27 + // Tramigo: Moving, 0.90 km SE of Cranebrook Fire Station, Penrith, Sydney, AU, -33.70722, 150.71759, SE with speed 37 km/h, 19:48 Jan 4 EOF + //verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertArray( // new int[] {0x68,0x68,0x25,0x00,0x00,0x01,0x23,0x45,0x67,0x89,0x01,0x23,0x45,0x00,0x01,0x10,0x01,0x01,0x01,0x01,0x01,0x01,0x02,0x6B,0x3F,0x3E,0x02,0x6B,0x3F,0x3E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x0D,0x0A})))); - //8000011bb0009e0001015b93032ef6f35994a9545472616d69676f3a204d6f76696e672c20302e3930206b6d205345206f66204372616e6562726f6f6b20466972652053746174696f6e2c2050656e726974682c205379646e65792c2041552c202d33332e37303732322c203135302e37313735392c2053452077697468207370656564203337206b6d2f682c2031393a3438204a616e20342020454f46 } } |