diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2019-10-26 17:05:46 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2019-10-26 17:05:46 -0700 |
commit | 3e36fcdb303f4c7af2df8e859f8fc6b1c7ba3fd0 (patch) | |
tree | e43ba14f871b0ad4249d7f3ad079f09b8c989686 /src/main/java | |
parent | 73c676e4faa3a8d71c9e8880417bd387eed20c7e (diff) | |
download | trackermap-server-3e36fcdb303f4c7af2df8e859f8fc6b1c7ba3fd0.tar.gz trackermap-server-3e36fcdb303f4c7af2df8e859f8fc6b1c7ba3fd0.tar.bz2 trackermap-server-3e36fcdb303f4c7af2df8e859f8fc6b1c7ba3fd0.zip |
Support binary Suntech protocol
Diffstat (limited to 'src/main/java')
3 files changed, 129 insertions, 16 deletions
diff --git a/src/main/java/org/traccar/protocol/SuntechFrameDecoder.java b/src/main/java/org/traccar/protocol/SuntechFrameDecoder.java index de8de17f0..7b8337468 100644 --- a/src/main/java/org/traccar/protocol/SuntechFrameDecoder.java +++ b/src/main/java/org/traccar/protocol/SuntechFrameDecoder.java @@ -32,13 +32,24 @@ public class SuntechFrameDecoder extends BaseFrameDecoder { protected Object decode( ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { - int delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\r'); - while (delimiterIndex > 0) { - if (delimiterIndex + 1 < buf.writerIndex() && buf.getByte(delimiterIndex + 1) == '\n') { - delimiterIndex = buf.indexOf(delimiterIndex + 1, buf.writerIndex(), (byte) '\r'); - } else { - return readFrame(buf, delimiterIndex); + if (buf.getByte(buf.readerIndex()) == (byte) 0x81) { + + int length = 1 + 2 + buf.getShort(buf.readerIndex() + 1); + if (buf.readableBytes() >= length) { + return buf.readRetainedSlice(length); } + + } else { + + int delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\r'); + while (delimiterIndex > 0) { + if (delimiterIndex + 1 < buf.writerIndex() && buf.getByte(delimiterIndex + 1) == '\n') { + delimiterIndex = buf.indexOf(delimiterIndex + 1, buf.writerIndex(), (byte) '\r'); + } else { + return readFrame(buf, delimiterIndex); + } + } + } return null; diff --git a/src/main/java/org/traccar/protocol/SuntechProtocol.java b/src/main/java/org/traccar/protocol/SuntechProtocol.java index 7e2c20e6f..199885537 100644 --- a/src/main/java/org/traccar/protocol/SuntechProtocol.java +++ b/src/main/java/org/traccar/protocol/SuntechProtocol.java @@ -15,7 +15,6 @@ */ package org.traccar.protocol; -import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; import org.traccar.PipelineBuilder; @@ -38,7 +37,6 @@ public class SuntechProtocol extends BaseProtocol { protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast(new SuntechFrameDecoder()); pipeline.addLast(new StringEncoder()); - pipeline.addLast(new StringDecoder()); pipeline.addLast(new SuntechProtocolEncoder(SuntechProtocol.this)); pipeline.addLast(new SuntechProtocolDecoder(SuntechProtocol.this)); } diff --git a/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java b/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java index e40096a77..04113c51e 100644 --- a/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java @@ -15,18 +15,22 @@ */ package org.traccar.protocol; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; import org.traccar.DeviceSession; import org.traccar.Protocol; import org.traccar.helper.BitUtil; +import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; import org.traccar.model.CellTower; import org.traccar.model.Network; import org.traccar.model.Position; import java.net.SocketAddress; +import java.nio.charset.StandardCharsets; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -501,20 +505,120 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder { return position; } + private Position decodeBinary( + Channel channel, SocketAddress remoteAddress, ByteBuf buf) { + + buf.readUnsignedByte(); // header + buf.readUnsignedShort(); // length + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ByteBufUtil.hexDump(buf.readSlice(5))); + if (deviceSession == null) { + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + int mask = buf.readUnsignedMedium(); + + if (BitUtil.check(mask, 1)) { + buf.readUnsignedByte(); // model + } + + if (BitUtil.check(mask, 2)) { + position.set(Position.KEY_VERSION_FW, String.format("%d.%d.%d", + buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())); + } + + if (BitUtil.check(mask, 3) && buf.readUnsignedByte() == 0) { + position.set(Position.KEY_ARCHIVE, true); + } + + if (BitUtil.check(mask, 4) && BitUtil.check(mask, 5)) { + position.setTime(new DateBuilder() + .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) + .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) + .getDate()); + } + + if (BitUtil.check(mask, 6)) { + buf.readUnsignedInt(); // cell + } + + if (BitUtil.check(mask, 7)) { + buf.readUnsignedShort(); // mcc + } + + if (BitUtil.check(mask, 8)) { + buf.readUnsignedShort(); // mnc + } + + if (BitUtil.check(mask, 9)) { + buf.readUnsignedShort(); // lac + } + + if (BitUtil.check(mask, 10)) { + position.set(Position.KEY_RSSI, buf.readUnsignedByte()); + } + + if (BitUtil.check(mask, 11)) { + long value = buf.readUnsignedInt(); + if (BitUtil.check(value, 31)) { + value = -BitUtil.to(value, 31); + } + position.setLatitude(value / 1000000.0); + } + + if (BitUtil.check(mask, 12)) { + long value = buf.readUnsignedInt(); + if (BitUtil.check(value, 31)) { + value = -BitUtil.to(value, 31); + } + position.setLongitude(value / 1000000.0); + } + + if (BitUtil.check(mask, 13)) { + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort() / 100.0)); + } + + if (BitUtil.check(mask, 14)) { + position.setCourse(buf.readUnsignedShort() / 100.0); + } + + if (BitUtil.check(mask, 15)) { + position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); + } + + if (BitUtil.check(mask, 16)) { + position.setValid(buf.readUnsignedByte() > 0); + } + + return position; + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - String[] values = ((String) msg).split(";"); + ByteBuf buf = (ByteBuf) msg; + + if (buf.getByte(buf.readerIndex()) == (byte) 0x81) { + + return decodeBinary(channel, remoteAddress, buf); - if (values[0].length() < 5) { - return decodeUniversal(channel, remoteAddress, values); - } else if (values[0].startsWith("ST9")) { - return decode9(channel, remoteAddress, values); - } else if (values[0].startsWith("ST4")) { - return decode4(channel, remoteAddress, values); } else { - return decode2356(channel, remoteAddress, values[0].substring(0, 5), values); + + String[] values = buf.toString(StandardCharsets.US_ASCII).split(";"); + + if (values[0].length() < 5) { + return decodeUniversal(channel, remoteAddress, values); + } else if (values[0].startsWith("ST9")) { + return decode9(channel, remoteAddress, values); + } else if (values[0].startsWith("ST4")) { + return decode4(channel, remoteAddress, values); + } else { + return decode2356(channel, remoteAddress, values[0].substring(0, 5), values); + } } } |