From cc66af4ff6887c9ce2f594e87d91cff87f11098e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 6 May 2016 10:28:14 +1200 Subject: Refactor Teltonika protocol decoder --- .../traccar/protocol/TeltonikaProtocolDecoder.java | 210 +++++++++++---------- 1 file changed, 109 insertions(+), 101 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/TeltonikaProtocolDecoder.java b/src/org/traccar/protocol/TeltonikaProtocolDecoder.java index 9e2ef6a7a..4bf3678c0 100644 --- a/src/org/traccar/protocol/TeltonikaProtocolDecoder.java +++ b/src/org/traccar/protocol/TeltonikaProtocolDecoder.java @@ -57,147 +57,155 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder { private static final int CODEC_FM4X00 = 0x08; private static final int CODEC_12 = 0x0C; - private List parseData(Channel channel, ChannelBuffer buf) { - List positions = new LinkedList<>(); + private void decodeSerial(Position position, ChannelBuffer buf) { - buf.skipBytes(4); // marker - buf.readUnsignedInt(); // data length - int codec = buf.readUnsignedByte(); - int count = buf.readUnsignedByte(); + getLastLocation(position, null); - for (int i = 0; i < count; i++) { - Position position = new Position(); - position.setProtocol(getProtocolName()); + position.set(Event.KEY_TYPE, buf.readUnsignedByte()); - position.setDeviceId(getDeviceId()); + position.set("command", buf.readBytes(buf.readInt()).toString(StandardCharsets.US_ASCII)); - if (codec == CODEC_12) { + } - getLastLocation(position, null); + private void decodeLocation(Position position, ChannelBuffer buf, int codec) { - position.set(Event.KEY_TYPE, buf.readUnsignedByte()); + int globalMask = 0x0f; - position.set("command", buf.readBytes(buf.readInt()).toString(StandardCharsets.US_ASCII)); + if (codec == CODEC_GH3000) { - } else { + long time = buf.readUnsignedInt() & 0x3fffffff; + time += 1167609600; // 2007-01-01 00:00:00 + + globalMask = buf.readUnsignedByte(); + if (BitUtil.check(globalMask, 0)) { - int globalMask = 0x0f; + position.setTime(new Date(time * 1000)); - if (codec == CODEC_GH3000) { + int locationMask = buf.readUnsignedByte(); - long time = buf.readUnsignedInt() & 0x3fffffff; - time += 1167609600; // 2007-01-01 00:00:00 + if (BitUtil.check(locationMask, 0)) { + position.setLatitude(buf.readFloat()); + position.setLongitude(buf.readFloat()); + } - globalMask = buf.readUnsignedByte(); - if (BitUtil.check(globalMask, 0)) { + if (BitUtil.check(locationMask, 1)) { + position.setAltitude(buf.readUnsignedShort()); + } - position.setTime(new Date(time * 1000)); + if (BitUtil.check(locationMask, 2)) { + position.setCourse(buf.readUnsignedByte() * 360.0 / 256); + } - int locationMask = buf.readUnsignedByte(); + if (BitUtil.check(locationMask, 3)) { + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); + } - if (BitUtil.check(locationMask, 0)) { - position.setLatitude(buf.readFloat()); - position.setLongitude(buf.readFloat()); - } + if (BitUtil.check(locationMask, 4)) { + int satellites = buf.readUnsignedByte(); + position.set(Event.KEY_SATELLITES, satellites); + position.setValid(satellites >= 3); + } - if (BitUtil.check(locationMask, 1)) { - position.setAltitude(buf.readUnsignedShort()); - } + if (BitUtil.check(locationMask, 5)) { + position.set(Event.KEY_LAC, buf.readUnsignedShort()); + position.set(Event.KEY_CID, buf.readUnsignedShort()); + } - if (BitUtil.check(locationMask, 2)) { - position.setCourse(buf.readUnsignedByte() * 360.0 / 256); - } + if (BitUtil.check(locationMask, 6)) { + position.set(Event.KEY_GSM, buf.readUnsignedByte()); + } - if (BitUtil.check(locationMask, 3)) { - position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); - } + if (BitUtil.check(locationMask, 7)) { + position.set("operator", buf.readUnsignedInt()); + } - if (BitUtil.check(locationMask, 4)) { - int satellites = buf.readUnsignedByte(); - position.set(Event.KEY_SATELLITES, satellites); - position.setValid(satellites >= 3); - } + } else { - if (BitUtil.check(locationMask, 5)) { - position.set(Event.KEY_LAC, buf.readUnsignedShort()); - position.set(Event.KEY_CID, buf.readUnsignedShort()); - } + getLastLocation(position, new Date(time * 1000)); - if (BitUtil.check(locationMask, 6)) { - position.set(Event.KEY_GSM, buf.readUnsignedByte()); - } + } - if (BitUtil.check(locationMask, 7)) { - position.set("operator", buf.readUnsignedInt()); - } + } else { - } else { + position.setTime(new Date(buf.readLong())); - getLastLocation(position, new Date(time * 1000)); + position.set("priority", buf.readUnsignedByte()); - } + position.setLongitude(buf.readInt() / 10000000.0); + position.setLatitude(buf.readInt() / 10000000.0); + position.setAltitude(buf.readShort()); + position.setCourse(buf.readUnsignedShort()); - } else { + int satellites = buf.readUnsignedByte(); + position.set(Event.KEY_SATELLITES, satellites); - position.setTime(new Date(buf.readLong())); + position.setValid(satellites != 0); - position.set("priority", buf.readUnsignedByte()); + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); - position.setLongitude(buf.readInt() / 10000000.0); - position.setLatitude(buf.readInt() / 10000000.0); - position.setAltitude(buf.readShort()); - position.setCourse(buf.readUnsignedShort()); + position.set(Event.KEY_EVENT, buf.readUnsignedByte()); - int satellites = buf.readUnsignedByte(); - position.set(Event.KEY_SATELLITES, satellites); + buf.readUnsignedByte(); // total IO data records - position.setValid(satellites != 0); + } - position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); + // Read 1 byte data + if (BitUtil.check(globalMask, 1)) { + int cnt = buf.readUnsignedByte(); + for (int j = 0; j < cnt; j++) { + int id = buf.readUnsignedByte(); + if (id == 1) { + position.set(Event.KEY_POWER, buf.readUnsignedByte()); + } else { + position.set(Event.PREFIX_IO + id, buf.readUnsignedByte()); + } + } + } - position.set(Event.KEY_EVENT, buf.readUnsignedByte()); + // Read 2 byte data + if (BitUtil.check(globalMask, 2)) { + int cnt = buf.readUnsignedByte(); + for (int j = 0; j < cnt; j++) { + position.set(Event.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedShort()); + } + } - buf.readUnsignedByte(); // total IO data records + // Read 4 byte data + if (BitUtil.check(globalMask, 3)) { + int cnt = buf.readUnsignedByte(); + for (int j = 0; j < cnt; j++) { + position.set(Event.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedInt()); + } + } - } + // Read 8 byte data + if (codec == CODEC_FM4X00) { + int cnt = buf.readUnsignedByte(); + for (int j = 0; j < cnt; j++) { + position.set(Event.PREFIX_IO + buf.readUnsignedByte(), buf.readLong()); + } + } - // Read 1 byte data - if (BitUtil.check(globalMask, 1)) { - int cnt = buf.readUnsignedByte(); - for (int j = 0; j < cnt; j++) { - int id = buf.readUnsignedByte(); - if (id == 1) { - position.set(Event.KEY_POWER, buf.readUnsignedByte()); - } else { - position.set(Event.PREFIX_IO + id, buf.readUnsignedByte()); - } - } - } + } - // Read 2 byte data - if (BitUtil.check(globalMask, 2)) { - int cnt = buf.readUnsignedByte(); - for (int j = 0; j < cnt; j++) { - position.set(Event.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedShort()); - } - } + private List parseData(Channel channel, ChannelBuffer buf) { + List positions = new LinkedList<>(); - // Read 4 byte data - if (BitUtil.check(globalMask, 3)) { - int cnt = buf.readUnsignedByte(); - for (int j = 0; j < cnt; j++) { - position.set(Event.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedInt()); - } - } + buf.skipBytes(4); // marker + buf.readUnsignedInt(); // data length + int codec = buf.readUnsignedByte(); + int count = buf.readUnsignedByte(); - // Read 8 byte data - if (codec == CODEC_FM4X00) { - int cnt = buf.readUnsignedByte(); - for (int j = 0; j < cnt; j++) { - position.set(Event.PREFIX_IO + buf.readUnsignedByte(), buf.readLong()); - } - } + for (int i = 0; i < count; i++) { + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(getDeviceId()); + + if (codec == CODEC_12) { + decodeSerial(position, buf); + } else { + decodeLocation(position, buf, codec); } positions.add(position); -- cgit v1.2.3