diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-13 10:06:49 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-13 10:06:49 +1300 |
commit | f95de7fdb6f9a47618b30a991949b86567d6deb9 (patch) | |
tree | d608b2557136848113bdc3b176ee61083d98753b /src/org/traccar | |
parent | aa289b8600d9a55fdd053e003705de116ab25f2e (diff) | |
download | traccar-server-f95de7fdb6f9a47618b30a991949b86567d6deb9.tar.gz traccar-server-f95de7fdb6f9a47618b30a991949b86567d6deb9.tar.bz2 traccar-server-f95de7fdb6f9a47618b30a991949b86567d6deb9.zip |
Clean up GT06 protocol decoder
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/protocol/Gt06ProtocolDecoder.java | 207 |
1 files changed, 111 insertions, 96 deletions
diff --git a/src/org/traccar/protocol/Gt06ProtocolDecoder.java b/src/org/traccar/protocol/Gt06ProtocolDecoder.java index 2a5cae6c4..6f62baf11 100644 --- a/src/org/traccar/protocol/Gt06ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gt06ProtocolDecoder.java @@ -16,8 +16,8 @@ package org.traccar.protocol; import java.net.SocketAddress; -import java.util.Calendar; -import java.util.TimeZone; +import java.util.*; + import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; @@ -74,6 +74,28 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_COMMAND_1 = 0x81; public static final int MSG_COMMAND_2 = 0x82; + private final static Set<Integer> MESSAGES_SUPPORTED = new HashSet<>(Arrays.asList( + MSG_GPS, + MSG_GPS_LBS_1, + MSG_GPS_LBS_2, + MSG_GPS_LBS_STATUS_1, + MSG_GPS_LBS_STATUS_2, + MSG_GPS_LBS_STATUS_3, + MSG_GPS_PHONE, + MSG_GPS_LBS_EXTEND)); + + private final static Set<Integer> MESSAGES_LBS = new HashSet<>(Arrays.asList( + MSG_GPS_LBS_1, + MSG_GPS_LBS_2, + MSG_GPS_LBS_STATUS_1, + MSG_GPS_LBS_STATUS_2, + MSG_GPS_LBS_STATUS_3)); + + private final static Set<Integer> MESSAGES_STATUS = new HashSet<>(Arrays.asList( + MSG_GPS_LBS_STATUS_1, + MSG_GPS_LBS_STATUS_2, + MSG_GPS_LBS_STATUS_3)); + private static void sendResponse(Channel channel, int type, int index) { if (channel != null) { ChannelBuffer response = ChannelBuffers.directBuffer(10); @@ -94,7 +116,6 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { ChannelBuffer buf = (ChannelBuffer) msg; - // Check header if (buf.readByte() != 0x78 || buf.readByte() != 0x78) { return null; } @@ -128,116 +149,110 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { sendResponse(channel, type, buf.readUnsignedShort()); } - } else if (hasDeviceId() && ( - type == MSG_GPS || - type == MSG_GPS_LBS_1 || - type == MSG_GPS_LBS_2 || - type == MSG_GPS_LBS_STATUS_1 || - type == MSG_GPS_LBS_STATUS_2 || - type == MSG_GPS_LBS_STATUS_3 || - type == MSG_GPS_PHONE || - type == MSG_GPS_LBS_EXTEND)) { - - // Create new position - Position position = new Position(); - position.setDeviceId(getDeviceId()); - position.setProtocol(getProtocolName()); - - // Date and time - Calendar time = Calendar.getInstance(timeZone); - time.clear(); - time.set(Calendar.YEAR, 2000 + buf.readUnsignedByte()); - time.set(Calendar.MONTH, buf.readUnsignedByte() - 1); - time.set(Calendar.DAY_OF_MONTH, buf.readUnsignedByte()); - time.set(Calendar.HOUR_OF_DAY, buf.readUnsignedByte()); - time.set(Calendar.MINUTE, buf.readUnsignedByte()); - time.set(Calendar.SECOND, buf.readUnsignedByte()); - position.setTime(time.getTime()); - - // GPS length and Satellites count - int gpsLength = buf.readUnsignedByte(); - position.set(Event.KEY_SATELLITES, gpsLength & 0b0000_1111); - gpsLength >>= 4; - - // Latitude - double latitude = buf.readUnsignedInt() / (60.0 * 30000.0); - - // Longitude - double longitude = buf.readUnsignedInt() / (60.0 * 30000.0); - - // Speed - position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); - - // Course and flags - int union = buf.readUnsignedShort(); - position.setCourse(union & 0b0000_0011_1111_1111); - position.setValid((union & 0b0001_0000_0000_0000) != 0); - if ((union & 0b0000_0100_0000_0000) == 0) latitude = -latitude; - if ((union & 0b0000_1000_0000_0000) != 0) longitude = -longitude; - - position.setLatitude(latitude); - position.setLongitude(longitude); - - if ((union & 0b0100_0000_0000_0000) != 0) { - position.set(Event.KEY_IGNITION, (union & 0b1000_0000_0000_0000) != 0); - } + } else if (hasDeviceId()) { - buf.skipBytes(gpsLength - 12); // skip reserved + if (MESSAGES_SUPPORTED.contains(type)) { - if (type == MSG_GPS_LBS_1 || type == MSG_GPS_LBS_2 || - type == MSG_GPS_LBS_STATUS_1 || type == MSG_GPS_LBS_STATUS_2 || type == MSG_GPS_LBS_STATUS_3) { + Position position = new Position(); + position.setDeviceId(getDeviceId()); + position.setProtocol(getProtocolName()); - int lbsLength = 0; - if (type == MSG_GPS_LBS_STATUS_1 || type == MSG_GPS_LBS_STATUS_2 || type == MSG_GPS_LBS_STATUS_3) { - lbsLength = buf.readUnsignedByte(); - } + // Date and time + Calendar time = Calendar.getInstance(timeZone); + time.clear(); + time.set(Calendar.YEAR, 2000 + buf.readUnsignedByte()); + time.set(Calendar.MONTH, buf.readUnsignedByte() - 1); + time.set(Calendar.DAY_OF_MONTH, buf.readUnsignedByte()); + time.set(Calendar.HOUR_OF_DAY, buf.readUnsignedByte()); + time.set(Calendar.MINUTE, buf.readUnsignedByte()); + time.set(Calendar.SECOND, buf.readUnsignedByte()); + position.setTime(time.getTime()); + + // GPS length and Satellites count + int gpsLength = buf.readUnsignedByte(); + position.set(Event.KEY_SATELLITES, gpsLength & 0b0000_1111); + gpsLength >>= 4; + + // Latitude + double latitude = buf.readUnsignedInt() / (60.0 * 30000.0); - // Cell information - position.set(Event.KEY_MCC, buf.readUnsignedShort()); - position.set(Event.KEY_MNC, buf.readUnsignedByte()); - position.set(Event.KEY_LAC, buf.readUnsignedShort()); - position.set(Event.KEY_CELL, (buf.readUnsignedShort() << 8) + buf.readUnsignedByte()); - if (lbsLength > 0) { - buf.skipBytes(lbsLength - 9); + // Longitude + double longitude = buf.readUnsignedInt() / (60.0 * 30000.0); + + // Speed + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); + + // Course and flags + int union = buf.readUnsignedShort(); + position.setCourse(union & 0b0000_0011_1111_1111); + position.setValid((union & 0b0001_0000_0000_0000) != 0); + if ((union & 0b0000_0100_0000_0000) == 0) latitude = -latitude; + if ((union & 0b0000_1000_0000_0000) != 0) longitude = -longitude; + + position.setLatitude(latitude); + position.setLongitude(longitude); + + if ((union & 0b0100_0000_0000_0000) != 0) { + position.set(Event.KEY_IGNITION, (union & 0b1000_0000_0000_0000) != 0); } - // Status - if (type == MSG_GPS_LBS_STATUS_1 || type == MSG_GPS_LBS_STATUS_2 || type == MSG_GPS_LBS_STATUS_3) { + buf.skipBytes(gpsLength - 12); // skip reserved + + if (MESSAGES_LBS.contains(type)) { + + int lbsLength = 0; + if (MESSAGES_STATUS.contains(type)) { + lbsLength = buf.readUnsignedByte(); + } - position.set(Event.KEY_ALARM, true); + // Cell information + position.set(Event.KEY_MCC, buf.readUnsignedShort()); + position.set(Event.KEY_MNC, buf.readUnsignedByte()); + position.set(Event.KEY_LAC, buf.readUnsignedShort()); + position.set(Event.KEY_CELL, (buf.readUnsignedShort() << 8) + buf.readUnsignedByte()); + if (lbsLength > 0) { + buf.skipBytes(lbsLength - 9); + } - int flags = buf.readUnsignedByte(); + if (MESSAGES_STATUS.contains(type)) { + position.set(Event.KEY_ALARM, true); - position.set(Event.KEY_IGNITION, (flags & 0x2) != 0); - // TODO parse other flags + int flags = buf.readUnsignedByte(); - // Voltage - position.set(Event.KEY_POWER, buf.readUnsignedByte()); + position.set(Event.KEY_IGNITION, (flags & 0x2) != 0); + // TODO parse other flags + + // Voltage + position.set(Event.KEY_POWER, buf.readUnsignedByte()); + + // GSM signal + position.set(Event.KEY_GSM, buf.readUnsignedByte()); + } - // GSM signal - position.set(Event.KEY_GSM, buf.readUnsignedByte()); } - } - if (type == MSG_GPS_LBS_1 && buf.readableBytes() == 4 + 6) { - position.set(Event.KEY_ODOMETER, buf.readUnsignedInt()); - } + if (type == MSG_GPS_LBS_1 && buf.readableBytes() == 4 + 6) { + position.set(Event.KEY_ODOMETER, buf.readUnsignedInt()); + } - // Index - if (buf.readableBytes() > 6) { - buf.skipBytes(buf.readableBytes() - 6); - } - int index = buf.readUnsignedShort(); - position.set(Event.KEY_INDEX, index); - sendResponse(channel, type, index); - return position; + // Index + if (buf.readableBytes() > 6) { + buf.skipBytes(buf.readableBytes() - 6); + } + int index = buf.readUnsignedShort(); + position.set(Event.KEY_INDEX, index); + sendResponse(channel, type, index); + return position; - } else { + } else { + + buf.skipBytes(dataLength); + if (type != MSG_COMMAND_0 && type != MSG_COMMAND_1 && type != MSG_COMMAND_2) { + sendResponse(channel, type, buf.readUnsignedShort()); + } - buf.skipBytes(dataLength); - if (type != MSG_COMMAND_0 && type != MSG_COMMAND_1 && type != MSG_COMMAND_2) { - sendResponse(channel, type, buf.readUnsignedShort()); } + } return null; |