diff options
author | Christoph Krey <c@ckrey.de> | 2018-04-16 10:45:11 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-16 10:45:11 +0200 |
commit | 3a602dd133b533cc69d5986d64a00fb3ed670f75 (patch) | |
tree | 97ff753db310a49ae7e53240b1db07fb8475679e /src/org/traccar/protocol/DmtProtocolDecoder.java | |
parent | 232de5f0daef98f31b28d177d991fdbfa191f195 (diff) | |
parent | 6d4b8df25c7e942b9ad594db9444fe15bcb16be9 (diff) | |
download | trackermap-server-3a602dd133b533cc69d5986d64a00fb3ed670f75.tar.gz trackermap-server-3a602dd133b533cc69d5986d64a00fb3ed670f75.tar.bz2 trackermap-server-3a602dd133b533cc69d5986d64a00fb3ed670f75.zip |
Merge pull request #4 from traccar/master
upgrade to current master
Diffstat (limited to 'src/org/traccar/protocol/DmtProtocolDecoder.java')
-rw-r--r-- | src/org/traccar/protocol/DmtProtocolDecoder.java | 288 |
1 files changed, 193 insertions, 95 deletions
diff --git a/src/org/traccar/protocol/DmtProtocolDecoder.java b/src/org/traccar/protocol/DmtProtocolDecoder.java index 39462a469..30a6ae13a 100644 --- a/src/org/traccar/protocol/DmtProtocolDecoder.java +++ b/src/org/traccar/protocol/DmtProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2018 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; +import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -42,144 +43,241 @@ public class DmtProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_DATA_RECORD = 0x04; public static final int MSG_COMMIT = 0x05; public static final int MSG_COMMIT_RESPONSE = 0x06; + public static final int MSG_DATA_RECORD_64 = 0x10; + + public static final int MSG_CANNED_REQUEST_1 = 0x14; + public static final int MSG_CANNED_RESPONSE_1 = 0x15; + public static final int MSG_CANNED_REQUEST_2 = 0x22; + public static final int MSG_CANNED_RESPONSE_2 = 0x23; + + private void sendResponse(Channel channel, int type, ChannelBuffer content) { + if (channel != null) { + ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + response.writeByte(0x02); response.writeByte(0x55); // header + response.writeByte(type); + response.writeShort(content != null ? content.readableBytes() : 0); + if (content != null) { + response.writeBytes(content); + } + channel.write(response); + } + } - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + private List<Position> decodeFixed64(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { - ChannelBuffer buf = (ChannelBuffer) msg; + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } - buf.skipBytes(2); // header + List<Position> positions = new LinkedList<>(); - int type = buf.readUnsignedByte(); + while (buf.readableBytes() >= 64) { + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); - buf.readUnsignedShort(); // length + buf.readByte(); // type - if (type == MSG_HELLO) { + position.set(Position.KEY_INDEX, buf.readUnsignedInt()); - buf.readUnsignedInt(); // device serial number + long time = buf.readUnsignedInt(); + position.setTime(new DateBuilder() + .setYear((int) (2000 + (time & 0x3F))) + .setMonth((int) (time >> 6) & 0xF) + .setDay((int) (time >> 10) & 0x1F) + .setHour((int) (time >> 15) & 0x1F) + .setMinute((int) (time >> 20) & 0x3F) + .setSecond((int) (time >> 26) & 0x3F) + .getDate()); - DeviceSession deviceSession = getDeviceSession( - channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII)); + position.setLongitude(buf.readInt() * 0.0000001); + position.setLatitude(buf.readInt() * 0.0000001); + position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedShort())); + position.setCourse(buf.readUnsignedByte() * 2); + position.setAltitude(buf.readShort()); - if (channel != null) { - ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); - response.writeByte(0x02); response.writeByte(0x55); // header - response.writeByte(MSG_HELLO_RESPONSE); - response.writeShort(4 + 4); - response.writeInt((int) (System.currentTimeMillis() / 1000)); - response.writeInt(deviceSession != null ? 0 : 1); // flags - channel.write(response); - } + buf.readUnsignedShort(); // position accuracy + buf.readUnsignedByte(); // speed accuracy - } else if (type == MSG_COMMIT) { + position.set(Position.KEY_EVENT, buf.readUnsignedByte()); - if (channel != null) { - ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); - response.writeByte(0x02); response.writeByte(0x55); // header - response.writeByte(MSG_COMMIT_RESPONSE); - response.writeShort(1); - response.writeByte(1); // flags (success) - channel.write(response); - } + position.setValid(BitUtil.check(buf.readByte(), 0)); - } else if (type == MSG_DATA_RECORD) { + position.set(Position.KEY_INPUT, buf.readUnsignedInt()); + position.set(Position.KEY_OUTPUT, buf.readUnsignedShort()); - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); - if (deviceSession == null) { - return null; + for (int i = 1; i <= 5; i++) { + position.set(Position.PREFIX_ADC + i, buf.readShort()); } - List<Position> positions = new LinkedList<>(); + position.set(Position.KEY_DEVICE_TEMP, buf.readByte()); - while (buf.readable()) { + buf.readShort(); // accelerometer x + buf.readShort(); // accelerometer y + buf.readShort(); // accelerometer z - int recordEnd = buf.readerIndex() + buf.readUnsignedShort(); + buf.skipBytes(8); // device id - Position position = new Position(); - position.setProtocol(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); + position.set(Position.KEY_PDOP, buf.readUnsignedShort() * 0.01); - position.set(Position.KEY_INDEX, buf.readUnsignedInt()); + buf.skipBytes(2); // reserved - position.setDeviceTime(new Date(1356998400000L + buf.readUnsignedInt() * 1000)); // since 1 Jan 2013 + buf.readUnsignedShort(); // checksum - position.set(Position.KEY_EVENT, buf.readUnsignedByte()); + positions.add(position); + } - while (buf.readerIndex() < recordEnd) { + return positions; + } - int fieldId = buf.readUnsignedByte(); - int fieldLength = buf.readUnsignedByte(); - int fieldEnd = buf.readerIndex() + (fieldLength == 255 ? buf.readUnsignedShort() : fieldLength); + private List<Position> decodeStandard(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { - if (fieldId == 0) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } - position.setFixTime(new Date(1356998400000L + buf.readUnsignedInt() * 1000)); - position.setLatitude(buf.readInt() * 0.0000001); - position.setLongitude(buf.readInt() * 0.0000001); - position.setAltitude(buf.readShort()); - position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedShort())); + List<Position> positions = new LinkedList<>(); - buf.readUnsignedByte(); // speed accuracy + while (buf.readable()) { + int recordEnd = buf.readerIndex() + buf.readUnsignedShort(); - position.setCourse(buf.readUnsignedByte() * 2); + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); - position.set(Position.KEY_PDOP, buf.readUnsignedByte() * 0.1); + position.set(Position.KEY_INDEX, buf.readUnsignedInt()); - position.setAccuracy(buf.readUnsignedByte()); - position.setValid(buf.readUnsignedByte() != 0); + position.setDeviceTime(new Date(1356998400000L + buf.readUnsignedInt() * 1000)); // since 1 Jan 2013 - } else if (fieldId == 2) { + position.set(Position.KEY_EVENT, buf.readUnsignedByte()); - int input = buf.readInt(); - int output = buf.readUnsignedShort(); - int status = buf.readUnsignedShort(); + while (buf.readerIndex() < recordEnd) { - position.set(Position.KEY_IGNITION, BitUtil.check(input, 0)); + int fieldId = buf.readUnsignedByte(); + int fieldLength = buf.readUnsignedByte(); + int fieldEnd = buf.readerIndex() + (fieldLength == 255 ? buf.readUnsignedShort() : fieldLength); - position.set(Position.KEY_INPUT, input); - position.set(Position.KEY_OUTPUT, output); - position.set(Position.KEY_STATUS, status); + if (fieldId == 0) { - } else if (fieldId == 6) { + position.setFixTime(new Date(1356998400000L + buf.readUnsignedInt() * 1000)); + position.setLatitude(buf.readInt() * 0.0000001); + position.setLongitude(buf.readInt() * 0.0000001); + position.setAltitude(buf.readShort()); + position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedShort())); - while (buf.readerIndex() < fieldEnd) { - switch (buf.readUnsignedByte()) { - case 1: - position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001); - break; - case 2: - position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01); - break; - case 3: - position.set(Position.KEY_DEVICE_TEMP, buf.readShort() * 0.01); - break; - case 4: - position.set(Position.KEY_RSSI, buf.readUnsignedShort()); - break; - case 5: - position.set("solarPower", buf.readUnsignedShort() * 0.001); - break; - default: - break; - } - } + buf.readUnsignedByte(); // speed accuracy - } + position.setCourse(buf.readUnsignedByte() * 2); - buf.readerIndex(fieldEnd); + position.set(Position.KEY_PDOP, buf.readUnsignedByte() * 0.1); - } + position.setAccuracy(buf.readUnsignedByte()); + position.setValid(buf.readUnsignedByte() != 0); + + } else if (fieldId == 2) { + + int input = buf.readInt(); + int output = buf.readUnsignedShort(); + int status = buf.readUnsignedShort(); + + position.set(Position.KEY_IGNITION, BitUtil.check(input, 0)); + + position.set(Position.KEY_INPUT, input); + position.set(Position.KEY_OUTPUT, output); + position.set(Position.KEY_STATUS, status); + + } else if (fieldId == 6) { + + while (buf.readerIndex() < fieldEnd) { + switch (buf.readUnsignedByte()) { + case 1: + position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001); + break; + case 2: + position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01); + break; + case 3: + position.set(Position.KEY_DEVICE_TEMP, buf.readShort() * 0.01); + break; + case 4: + position.set(Position.KEY_RSSI, buf.readUnsignedShort()); + break; + case 5: + position.set("solarPower", buf.readUnsignedShort() * 0.001); + break; + default: + break; + } + } - if (position.getFixTime() == null) { - getLastLocation(position, position.getDeviceTime()); } - positions.add(position); + buf.readerIndex(fieldEnd); + + } + if (position.getFixTime() == null) { + getLastLocation(position, position.getDeviceTime()); } - return positions; + positions.add(position); + } + + return positions; + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ChannelBuffer buf = (ChannelBuffer) msg; + + buf.skipBytes(2); // header + + int type = buf.readUnsignedByte(); + int length = buf.readUnsignedShort(); + + if (type == MSG_HELLO) { + + buf.readUnsignedInt(); // device serial number + + DeviceSession deviceSession = getDeviceSession( + channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII)); + + ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + if (length == 51) { + response.writeByte(0); // reserved + response.writeInt(0); // reserved + } else { + response.writeInt((int) ((System.currentTimeMillis() - 1356998400000L) / 1000)); + response.writeInt(deviceSession != null ? 0 : 1); // flags + } + + sendResponse(channel, MSG_HELLO_RESPONSE, response); + + } else if (type == MSG_COMMIT) { + + ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + response.writeByte(1); // flags (success) + sendResponse(channel, MSG_COMMIT_RESPONSE, response); + + } else if (type == MSG_CANNED_REQUEST_1) { + + ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + response.writeBytes(new byte[12]); + sendResponse(channel, MSG_CANNED_RESPONSE_1, response); + + } else if (type == MSG_CANNED_REQUEST_2) { + + sendResponse(channel, MSG_CANNED_RESPONSE_2, null); + + } else if (type == MSG_DATA_RECORD_64) { + + return decodeFixed64(channel, remoteAddress, buf); + + } else if (type == MSG_DATA_RECORD) { + + return decodeStandard(channel, remoteAddress, buf); } |