diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-11-04 12:10:19 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-11-04 12:10:19 +1300 |
commit | 79b7b8145b69163d555d0be47f5c590a9ff3257b (patch) | |
tree | 2c7e7402e777e169631b4a750ab4961d04b1fc1f /src/org/traccar | |
parent | 3e30b12b995c2f219625b957bfaac4e8bf92c8fc (diff) | |
download | trackermap-server-79b7b8145b69163d555d0be47f5c590a9ff3257b.tar.gz trackermap-server-79b7b8145b69163d555d0be47f5c590a9ff3257b.tar.bz2 trackermap-server-79b7b8145b69163d555d0be47f5c590a9ff3257b.zip |
Refactor GT02 protocol decoder
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/protocol/Gt02ProtocolDecoder.java | 45 |
1 files changed, 17 insertions, 28 deletions
diff --git a/src/org/traccar/protocol/Gt02ProtocolDecoder.java b/src/org/traccar/protocol/Gt02ProtocolDecoder.java index 76756f41d..81a04be0a 100644 --- a/src/org/traccar/protocol/Gt02ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gt02ProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 2015 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,12 +16,12 @@ package org.traccar.protocol; import java.net.SocketAddress; -import java.util.Calendar; -import java.util.TimeZone; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.helper.BitUtil; +import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; import org.traccar.model.Event; import org.traccar.model.Position; @@ -49,8 +49,7 @@ public class Gt02ProtocolDecoder extends BaseProtocolDecoder { @Override protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) - throws Exception { + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ChannelBuffer buf = (ChannelBuffer) msg; @@ -74,50 +73,40 @@ public class Gt02ProtocolDecoder extends BaseProtocolDecoder { } else if (type == MSG_DATA) { - // Create new position Position position = new Position(); position.setProtocol(getProtocolName()); position.set(Event.KEY_INDEX, index); - // Get device id if (!identify(imei, channel)) { return null; } position.setDeviceId(getDeviceId()); - // Date and time - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - 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()); - - // Latitude - double latitude = buf.readUnsignedInt() / (60.0 * 30000.0); + DateBuilder dateBuilder = new DateBuilder() + .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) + .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); + position.setTime(dateBuilder.getDate()); - // Longitude + double latitude = buf.readUnsignedInt() / (60.0 * 30000.0); double longitude = buf.readUnsignedInt() / (60.0 * 30000.0); - // Speed position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); - - // Course position.setCourse(buf.readUnsignedShort()); buf.skipBytes(3); // reserved - // Flags long flags = buf.readUnsignedInt(); - position.setValid((flags & 0x1) == 0x1); - if ((flags & 0x2) == 0) latitude = -latitude; - if ((flags & 0x4) == 0) longitude = -longitude; + position.setValid(BitUtil.check(flags, 0)); + if (!BitUtil.check(flags, 1)) { + latitude = -latitude; + } + if (!BitUtil.check(flags, 2)) { + longitude = -longitude; + } position.setLatitude(latitude); position.setLongitude(longitude); + return position; } |