diff options
Diffstat (limited to 'src/org/traccar/protocol/DmtProtocolDecoder.java')
-rw-r--r-- | src/org/traccar/protocol/DmtProtocolDecoder.java | 263 |
1 files changed, 172 insertions, 91 deletions
diff --git a/src/org/traccar/protocol/DmtProtocolDecoder.java b/src/org/traccar/protocol/DmtProtocolDecoder.java index 74db5a6f7..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,6 +43,7 @@ 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; @@ -61,142 +63,221 @@ public class DmtProtocolDecoder extends BaseProtocolDecoder { } } - @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()); - ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); - response.writeInt((int) ((System.currentTimeMillis() - 1356998400000L) / 1000)); - response.writeInt(deviceSession != null ? 0 : 1); // flags - sendResponse(channel, MSG_HELLO_RESPONSE, response); + buf.readUnsignedShort(); // position accuracy + buf.readUnsignedByte(); // speed accuracy - } else if (type == MSG_COMMIT) { + position.set(Position.KEY_EVENT, buf.readUnsignedByte()); - ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); - response.writeByte(1); // flags (success) - sendResponse(channel, MSG_COMMIT_RESPONSE, response); + position.setValid(BitUtil.check(buf.readByte(), 0)); - } else if (type == MSG_CANNED_REQUEST_1) { + position.set(Position.KEY_INPUT, buf.readUnsignedInt()); + position.set(Position.KEY_OUTPUT, buf.readUnsignedShort()); - ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); - response.writeBytes(new byte[12]); - sendResponse(channel, MSG_CANNED_RESPONSE_1, response); + for (int i = 1; i <= 5; i++) { + position.set(Position.PREFIX_ADC + i, buf.readShort()); + } - } else if (type == MSG_CANNED_REQUEST_2) { + position.set(Position.KEY_DEVICE_TEMP, buf.readByte()); - sendResponse(channel, MSG_CANNED_RESPONSE_2, null); + buf.readShort(); // accelerometer x + buf.readShort(); // accelerometer y + buf.readShort(); // accelerometer z - } else if (type == MSG_DATA_RECORD) { + buf.skipBytes(8); // device id - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); - if (deviceSession == null) { - return null; - } + position.set(Position.KEY_PDOP, buf.readUnsignedShort() * 0.01); - List<Position> positions = new LinkedList<>(); + buf.skipBytes(2); // reserved - while (buf.readable()) { + buf.readUnsignedShort(); // checksum - int recordEnd = buf.readerIndex() + buf.readUnsignedShort(); + positions.add(position); + } - Position position = new Position(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); + return positions; + } - position.set(Position.KEY_INDEX, buf.readUnsignedInt()); + private List<Position> decodeStandard(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { - position.setDeviceTime(new Date(1356998400000L + buf.readUnsignedInt() * 1000)); // since 1 Jan 2013 + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } - position.set(Position.KEY_EVENT, buf.readUnsignedByte()); + List<Position> positions = new LinkedList<>(); - while (buf.readerIndex() < recordEnd) { + while (buf.readable()) { + int recordEnd = buf.readerIndex() + buf.readUnsignedShort(); - int fieldId = buf.readUnsignedByte(); - int fieldLength = buf.readUnsignedByte(); - int fieldEnd = buf.readerIndex() + (fieldLength == 255 ? buf.readUnsignedShort() : fieldLength); + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); - if (fieldId == 0) { + position.set(Position.KEY_INDEX, buf.readUnsignedInt()); - 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())); + position.setDeviceTime(new Date(1356998400000L + buf.readUnsignedInt() * 1000)); // since 1 Jan 2013 - buf.readUnsignedByte(); // speed accuracy + position.set(Position.KEY_EVENT, buf.readUnsignedByte()); - position.setCourse(buf.readUnsignedByte() * 2); + while (buf.readerIndex() < recordEnd) { - position.set(Position.KEY_PDOP, buf.readUnsignedByte() * 0.1); + int fieldId = buf.readUnsignedByte(); + int fieldLength = buf.readUnsignedByte(); + int fieldEnd = buf.readerIndex() + (fieldLength == 255 ? buf.readUnsignedShort() : fieldLength); - position.setAccuracy(buf.readUnsignedByte()); - position.setValid(buf.readUnsignedByte() != 0); + if (fieldId == 0) { - } else if (fieldId == 2) { + 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())); - int input = buf.readInt(); - int output = buf.readUnsignedShort(); - int status = buf.readUnsignedShort(); + buf.readUnsignedByte(); // speed accuracy - position.set(Position.KEY_IGNITION, BitUtil.check(input, 0)); + position.setCourse(buf.readUnsignedByte() * 2); - position.set(Position.KEY_INPUT, input); - position.set(Position.KEY_OUTPUT, output); - position.set(Position.KEY_STATUS, status); + position.set(Position.KEY_PDOP, buf.readUnsignedByte() * 0.1); - } else if (fieldId == 6) { + position.setAccuracy(buf.readUnsignedByte()); + position.setValid(buf.readUnsignedByte() != 0); - 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; - } - } + } else if (fieldId == 2) { - } + int input = buf.readInt(); + int output = buf.readUnsignedShort(); + int status = buf.readUnsignedShort(); - buf.readerIndex(fieldEnd); + 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()); + } + + 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 } - return positions; + 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); } |