From 315c359711b37b1afd90117564332f0770033057 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 6 Nov 2015 19:39:49 +1300 Subject: Replace Calendar with DateBuilder --- src/org/traccar/protocol/ApelProtocolDecoder.java | 43 ++----------- src/org/traccar/protocol/MxtProtocolDecoder.java | 17 ++--- src/org/traccar/protocol/NoranProtocolDecoder.java | 31 ++++----- src/org/traccar/protocol/OrionProtocolDecoder.java | 30 +++------ .../traccar/protocol/ProgressProtocolDecoder.java | 75 ++++++---------------- .../traccar/protocol/TramigoProtocolDecoder.java | 4 +- 6 files changed, 50 insertions(+), 150 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/ApelProtocolDecoder.java b/src/org/traccar/protocol/ApelProtocolDecoder.java index a04aa01af..e346e7d88 100644 --- a/src/org/traccar/protocol/ApelProtocolDecoder.java +++ b/src/org/traccar/protocol/ApelProtocolDecoder.java @@ -18,16 +18,14 @@ package org.traccar.protocol; import java.net.SocketAddress; import java.nio.ByteOrder; import java.nio.charset.Charset; -import java.util.Calendar; +import java.util.Date; import java.util.LinkedList; import java.util.List; -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.Checksum; -import org.traccar.helper.Log; import org.traccar.helper.UnitsConverter; import org.traccar.model.Event; import org.traccar.model.Position; @@ -90,8 +88,7 @@ public class ApelProtocolDecoder 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; int type = buf.readUnsignedShort(); @@ -104,8 +101,7 @@ public class ApelProtocolDecoder extends BaseProtocolDecoder { } if (type == MSG_TRACKER_ID) { - Log.warning("Unsupported authentication type"); - return null; + return null; // unsupported authentication type } if (type == MSG_TRACKER_ID_EXT) { @@ -139,7 +135,6 @@ public class ApelProtocolDecoder extends BaseProtocolDecoder { position.setProtocol(getProtocolName()); position.setDeviceId(getDeviceId()); - // Message index int subtype = type; if (type == MSG_LOG_RECORDS) { position.set(Event.KEY_ARCHIVE, true); @@ -154,19 +149,10 @@ public class ApelProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedShort(); // length } - // Time - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - time.setTimeInMillis(buf.readUnsignedInt() * 1000); - position.setTime(time.getTime()); - - // Latitude + position.setTime(new Date(buf.readUnsignedInt() * 1000)); position.setLatitude(buf.readInt() * 180.0 / 0x7FFFFFFF); - - // Longitude position.setLongitude(buf.readInt() * 180.0 / 0x7FFFFFFF); - // Speed and Validity if (subtype == MSG_STATE_FULL_INFO_T104) { int speed = buf.readUnsignedByte(); position.setValid(speed != 255); @@ -175,39 +161,25 @@ public class ApelProtocolDecoder extends BaseProtocolDecoder { } else { int speed = buf.readShort(); position.setValid(speed != -1); - position.setSpeed(UnitsConverter.knotsFromKph(speed / 100.0)); + position.setSpeed(UnitsConverter.knotsFromKph(speed * 0.01)); } - // Course - position.setCourse(buf.readShort() / 100.0); - - // Altitude + position.setCourse(buf.readShort() * 0.01); position.setAltitude(buf.readShort()); if (subtype == MSG_STATE_FULL_INFO_T104) { - // Satellites position.set(Event.KEY_SATELLITES, buf.readUnsignedByte()); - - // Cell signal position.set(Event.KEY_GSM, buf.readUnsignedByte()); - - // Event type position.set(Event.KEY_EVENT, buf.readUnsignedShort()); - - // Odometer position.set(Event.KEY_ODOMETER, buf.readUnsignedInt()); - - // Input/Output position.set(Event.KEY_INPUT, buf.readUnsignedByte()); position.set(Event.KEY_OUTPUT, buf.readUnsignedByte()); - // Analog sensors for (int i = 1; i <= 8; i++) { position.set(Event.PREFIX_ADC + i, buf.readUnsignedShort()); } - // Counters position.set(Event.PREFIX_COUNT + 1, buf.readUnsignedInt()); position.set(Event.PREFIX_COUNT + 2, buf.readUnsignedInt()); position.set(Event.PREFIX_COUNT + 3, buf.readUnsignedInt()); @@ -216,8 +188,7 @@ public class ApelProtocolDecoder extends BaseProtocolDecoder { positions.add(position); } - // Skip CRC - buf.readUnsignedInt(); + buf.readUnsignedInt(); // crc if (type == MSG_LOG_RECORDS) { requestArchive(channel); diff --git a/src/org/traccar/protocol/MxtProtocolDecoder.java b/src/org/traccar/protocol/MxtProtocolDecoder.java index ba97694d3..dc1a95d32 100644 --- a/src/org/traccar/protocol/MxtProtocolDecoder.java +++ b/src/org/traccar/protocol/MxtProtocolDecoder.java @@ -16,13 +16,11 @@ package org.traccar.protocol; import java.net.SocketAddress; -import java.util.Calendar; -import java.util.Date; -import java.util.TimeZone; import org.jboss.netty.buffer.ChannelBuffer; 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; @@ -64,12 +62,7 @@ public class MxtProtocolDecoder extends BaseProtocolDecoder { position.set(Event.KEY_INDEX, buf.readUnsignedShort()); - // Date and time - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - time.set(Calendar.YEAR, 2000); - time.set(Calendar.MONTH, 0); - time.set(Calendar.DAY_OF_MONTH, 1); + DateBuilder dateBuilder = new DateBuilder().setDate(2000, 1, 1); long date = buf.readUnsignedInt(); @@ -78,12 +71,10 @@ public class MxtProtocolDecoder extends BaseProtocolDecoder { long minutes = BitUtil.between(date, 6, 6 + 6); long seconds = BitUtil.to(date, 6); - long millis = time.getTimeInMillis(); - millis += (((days * 24 + hours) * 60 + minutes) * 60 + seconds) * 1000; + dateBuilder.addMillis((((days * 24 + hours) * 60 + minutes) * 60 + seconds) * 1000); - position.setTime(new Date(millis)); + position.setTime(dateBuilder.getDate()); - // Location position.setValid(true); position.setLatitude(buf.readInt() / 1000000.0); position.setLongitude(buf.readInt() / 1000000.0); diff --git a/src/org/traccar/protocol/NoranProtocolDecoder.java b/src/org/traccar/protocol/NoranProtocolDecoder.java index 11408b1ed..35924c5b2 100644 --- a/src/org/traccar/protocol/NoranProtocolDecoder.java +++ b/src/org/traccar/protocol/NoranProtocolDecoder.java @@ -20,12 +20,12 @@ import java.nio.ByteOrder; import java.nio.charset.Charset; import java.text.DateFormat; import java.text.SimpleDateFormat; -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.model.Event; import org.traccar.model.Position; @@ -87,14 +87,10 @@ public class NoranProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedInt(); // GIS port } - // Flags - int flags = buf.readUnsignedByte(); - position.setValid((flags & 0x01) != 0); + position.setValid(BitUtil.check(buf.readUnsignedByte(), 0)); - // Alarm type position.set(Event.KEY_ALARM, buf.readUnsignedByte()); - // Location if (newFormat) { position.setSpeed(buf.readUnsignedInt()); position.setCourse(buf.readFloat()); @@ -105,21 +101,18 @@ public class NoranProtocolDecoder extends BaseProtocolDecoder { position.setLongitude(buf.readFloat()); position.setLatitude(buf.readFloat()); - // Time if (!newFormat) { long timeValue = buf.readUnsignedInt(); - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - time.set(Calendar.YEAR, 2000 + (int) (timeValue >> 26)); - time.set(Calendar.MONTH, (int) (timeValue >> 22 & 0x0f) - 1); - time.set(Calendar.DAY_OF_MONTH, (int) (timeValue >> 17 & 0x1f)); - time.set(Calendar.HOUR_OF_DAY, (int) (timeValue >> 12 & 0x1f)); - time.set(Calendar.MINUTE, (int) (timeValue >> 6 & 0x3f)); - time.set(Calendar.SECOND, (int) (timeValue & 0x3f)); - position.setTime(time.getTime()); + DateBuilder dateBuilder = new DateBuilder() + .setYear((int) BitUtil.from(timeValue, 26)) + .setMonth((int) BitUtil.between(timeValue, 22, 26)) + .setDay((int) BitUtil.between(timeValue, 17, 22)) + .setHour((int) BitUtil.between(timeValue, 12, 17)) + .setMinute((int) BitUtil.between(timeValue, 6, 12)) + .setSecond((int) BitUtil.to(timeValue, 6)); + position.setTime(dateBuilder.getDate()); } - // Identification ChannelBuffer rawId; if (newFormat) { rawId = buf.readBytes(12); @@ -132,14 +125,12 @@ public class NoranProtocolDecoder extends BaseProtocolDecoder { } position.setDeviceId(getDeviceId()); - // Time if (newFormat) { DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); position.setTime(dateFormat.parse(buf.readBytes(17).toString(Charset.defaultCharset()))); buf.readByte(); } - // Other data if (!newFormat) { position.set(Event.PREFIX_IO + 1, buf.readUnsignedByte()); position.set(Event.KEY_FUEL, buf.readUnsignedByte()); diff --git a/src/org/traccar/protocol/OrionProtocolDecoder.java b/src/org/traccar/protocol/OrionProtocolDecoder.java index b2e1699c3..a208d83e4 100644 --- a/src/org/traccar/protocol/OrionProtocolDecoder.java +++ b/src/org/traccar/protocol/OrionProtocolDecoder.java @@ -16,16 +16,13 @@ package org.traccar.protocol; import java.net.SocketAddress; -import java.util.Calendar; import java.util.LinkedList; import java.util.List; -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.DateBuilder; import org.traccar.model.Event; import org.traccar.model.Position; @@ -56,8 +53,7 @@ public class OrionProtocolDecoder 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; @@ -80,7 +76,6 @@ public class OrionProtocolDecoder extends BaseProtocolDecoder { for (int i = 0; i < (header & 0x0f); i++) { - // Create new position Position position = new Position(); position.setDeviceId(getDeviceId()); position.setProtocol(getProtocolName()); @@ -89,28 +84,21 @@ public class OrionProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedByte(); // length position.set(Event.KEY_FLAGS, buf.readUnsignedShort()); - // Location position.setLatitude(convertCoordinate(buf.readInt())); position.setLongitude(convertCoordinate(buf.readInt())); position.setAltitude(buf.readShort() / 10.0); position.setCourse(buf.readUnsignedShort()); position.setSpeed(buf.readUnsignedShort() * 0.0539957); - // 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()); - - // Accuracy + DateBuilder dateBuilder = new DateBuilder() + .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) + .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); + position.setTime(dateBuilder.getDate()); + int satellites = buf.readUnsignedByte(); - position.set(Event.KEY_SATELLITES, satellites); position.setValid(satellites >= 3); + position.set(Event.KEY_SATELLITES, satellites); + positions.add(position); } diff --git a/src/org/traccar/protocol/ProgressProtocolDecoder.java b/src/org/traccar/protocol/ProgressProtocolDecoder.java index 9fbd601d5..93091e0a1 100644 --- a/src/org/traccar/protocol/ProgressProtocolDecoder.java +++ b/src/org/traccar/protocol/ProgressProtocolDecoder.java @@ -18,14 +18,14 @@ package org.traccar.protocol; import java.net.SocketAddress; import java.nio.ByteOrder; import java.nio.charset.Charset; -import java.util.Calendar; +import java.util.Date; import java.util.LinkedList; import java.util.List; -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.model.Event; import org.traccar.model.Position; @@ -48,8 +48,6 @@ public class ProgressProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_ALARM = 200; public static final int MSG_ALARM_RECIEVED = 201; - private static final String HEX_CHARS = "0123456789ABCDEF"; - private void requestArchive(Channel channel) { if (lastIndex == 0) { lastIndex = newIndex; @@ -64,7 +62,8 @@ public class ProgressProtocolDecoder extends BaseProtocolDecoder { } @Override - protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ChannelBuffer buf = (ChannelBuffer) msg; int type = buf.readUnsignedShort(); @@ -95,7 +94,6 @@ public class ProgressProtocolDecoder extends BaseProtocolDecoder { position.setProtocol(getProtocolName()); position.setDeviceId(getDeviceId()); - // Message index if (type == MSG_LOGMSG) { position.set(Event.KEY_ARCHIVE, true); int subtype = buf.readUnsignedShort(); @@ -112,84 +110,47 @@ public class ProgressProtocolDecoder extends BaseProtocolDecoder { newIndex = buf.readUnsignedInt(); } - // Time - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - time.setTimeInMillis(buf.readUnsignedInt() * 1000); - position.setTime(time.getTime()); - - // Latitude + position.setTime(new Date(buf.readUnsignedInt() * 1000)); position.setLatitude(buf.readInt() * 180.0 / 0x7FFFFFFF); - - // Longitude position.setLongitude(buf.readInt() * 180.0 / 0x7FFFFFFF); + position.setSpeed(buf.readUnsignedInt() * 0.01); + position.setCourse(buf.readUnsignedShort() * 0.01); + position.setAltitude(buf.readUnsignedShort() * 0.01); - // Speed - position.setSpeed(buf.readUnsignedInt() / 100.0); - - // Course - position.setCourse(buf.readUnsignedShort() / 100.0); - - // Altitude - position.setAltitude(buf.readUnsignedShort() / 100.0); + int satellites = buf.readUnsignedByte(); + position.setValid(satellites >= 3); + position.set(Event.KEY_SATELLITES, satellites); - // Satellites - int satellitesNumber = buf.readUnsignedByte(); - position.set(Event.KEY_SATELLITES, satellitesNumber); - - // Validity - position.setValid(satellitesNumber >= 3); - - // Cell signal position.set(Event.KEY_GSM, buf.readUnsignedByte()); - - // Odometer position.set(Event.KEY_ODOMETER, buf.readUnsignedInt()); long extraFlags = buf.readLong(); - // Analog inputs - if ((extraFlags & 0x1) == 0x1) { + if (BitUtil.check(extraFlags, 0)) { int count = buf.readUnsignedShort(); for (int i = 1; i <= count; i++) { position.set(Event.PREFIX_ADC + i, buf.readUnsignedShort()); } } - // CAN adapter - if ((extraFlags & 0x2) == 0x2) { + if (BitUtil.check(extraFlags, 1)) { int size = buf.readUnsignedShort(); position.set("can", buf.toString(buf.readerIndex(), size, Charset.defaultCharset())); buf.skipBytes(size); } - // Passenger sensor - if ((extraFlags & 0x4) == 0x4) { - int size = buf.readUnsignedShort(); - - // Convert binary data to hex - StringBuilder hex = new StringBuilder(); - for (int i = buf.readerIndex(); i < buf.readerIndex() + size; i++) { - byte b = buf.getByte(i); - hex.append(HEX_CHARS.charAt((b & 0xf0) >> 4)); - hex.append(HEX_CHARS.charAt(b & 0x0F)); - } - - position.set("passenger", hex.toString()); - - buf.skipBytes(size); + if (BitUtil.check(extraFlags, 2)) { + position.set("passenger", + ChannelBuffers.hexDump(buf.readBytes(buf.readUnsignedShort()))); } - // Send response for alarm message if (type == MSG_ALARM) { + position.set(Event.KEY_ALARM, true); byte[] response = {(byte) 0xC9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; channel.write(ChannelBuffers.wrappedBuffer(response)); - - position.set(Event.KEY_ALARM, true); } - // Skip CRC - buf.readUnsignedInt(); + buf.readUnsignedInt(); // crc positions.add(position); } diff --git a/src/org/traccar/protocol/TramigoProtocolDecoder.java b/src/org/traccar/protocol/TramigoProtocolDecoder.java index be97f24fd..456086fe2 100644 --- a/src/org/traccar/protocol/TramigoProtocolDecoder.java +++ b/src/org/traccar/protocol/TramigoProtocolDecoder.java @@ -105,7 +105,6 @@ public class TramigoProtocolDecoder extends BaseProtocolDecoder { String sentence = buf.toString(Charset.defaultCharset()); - // Coordinates Pattern pattern = Pattern.compile("(-?\\d+\\.\\d+), (-?\\d+\\.\\d+)"); Matcher matcher = pattern.matcher(sentence); if (!matcher.find()) { @@ -114,7 +113,6 @@ public class TramigoProtocolDecoder extends BaseProtocolDecoder { position.setLatitude(Double.parseDouble(matcher.group(1))); position.setLongitude(Double.parseDouble(matcher.group(2))); - // Speed and Course pattern = Pattern.compile("([NSWE]{1,2}) with speed (\\d+) km/h"); matcher = pattern.matcher(sentence); if (matcher.find()) { @@ -122,7 +120,6 @@ public class TramigoProtocolDecoder extends BaseProtocolDecoder { position.setCourse(0); // matcher.group(1) for course } - // Time pattern = Pattern.compile("(\\d{1,2}:\\d{2} \\w{3} \\d{1,2})"); matcher = pattern.matcher(sentence); if (!matcher.find()) { @@ -130,6 +127,7 @@ public class TramigoProtocolDecoder extends BaseProtocolDecoder { } DateFormat dateFormat = new SimpleDateFormat("HH:mm MMM d yyyy", Locale.ENGLISH); position.setTime(dateFormat.parse(matcher.group(1) + " " + Calendar.getInstance().get(Calendar.YEAR))); + return position; } -- cgit v1.2.3