diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-01-08 16:24:59 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-01-08 16:24:59 +1300 |
commit | 85ee9ee4f04a61e8c9244f3446b9cdf38c318e41 (patch) | |
tree | 70271313c932ac10f90858f0e4e2653551327b4a /src | |
parent | 640effaf6080c95e1142daa26e864f4baafa460a (diff) | |
download | traccar-server-85ee9ee4f04a61e8c9244f3446b9cdf38c318e41.tar.gz traccar-server-85ee9ee4f04a61e8c9244f3446b9cdf38c318e41.tar.bz2 traccar-server-85ee9ee4f04a61e8c9244f3446b9cdf38c318e41.zip |
Support Tramigo legacy protocol
Diffstat (limited to 'src')
-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 |
3 files changed, 124 insertions, 21 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; |