From 06b5113f3d98a44530c2ade6810bc75092fccc31 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 20 Oct 2015 13:59:44 +1300 Subject: Refactor bit utililies class --- src/org/traccar/helper/BitUtil.java | 24 ++++++++++++++-------- src/org/traccar/protocol/AdmProtocolDecoder.java | 2 +- src/org/traccar/protocol/Gt06ProtocolDecoder.java | 7 +++++-- src/org/traccar/protocol/MxtProtocolDecoder.java | 14 ++++++------- src/org/traccar/protocol/TytanProtocolDecoder.java | 10 ++++----- src/org/traccar/protocol/TzoneProtocolDecoder.java | 5 ++--- .../traccar/protocol/UlbotechProtocolDecoder.java | 2 +- 7 files changed, 37 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/org/traccar/helper/BitUtil.java b/src/org/traccar/helper/BitUtil.java index 81c638a68..31271a691 100644 --- a/src/org/traccar/helper/BitUtil.java +++ b/src/org/traccar/helper/BitUtil.java @@ -24,20 +24,28 @@ public final class BitUtil { return (number & (1 << index)) != 0; } - public static int range(int number, int index) { - return number >> index; + public static int between(int number, int from, int to) { + return (number >> from) & ((1 << to - from) - 1); } - public static int range(int number, int index, int length) { - return (number >> index) & ((1 << length) - 1); + public static int from(int number, int from) { + return number >> from; } - public static long range(long number, int index) { - return number >> index; + public static int to(int number, int to) { + return between(number, 0, to); } - public static long range(long number, int index, int length) { - return (number >> index) & ((1L << length) - 1); + public static long between(long number, int from, int to) { + return (number >> from) & ((1L << to - from) - 1L); + } + + public static long from(long number, int from) { + return number >> from; + } + + public static long to(long number, int to) { + return between(number, 0, to); } } diff --git a/src/org/traccar/protocol/AdmProtocolDecoder.java b/src/org/traccar/protocol/AdmProtocolDecoder.java index a22cade99..a40b00e90 100644 --- a/src/org/traccar/protocol/AdmProtocolDecoder.java +++ b/src/org/traccar/protocol/AdmProtocolDecoder.java @@ -52,7 +52,7 @@ public class AdmProtocolDecoder extends BaseProtocolDecoder { identify(buf.toString(buf.readerIndex(), 15, Charset.defaultCharset()), channel); } - if (hasDeviceId() && BitUtil.range(type, 0, 2) == 0) { + if (hasDeviceId() && BitUtil.to(type, 2) == 0) { Position position = new Position(); position.setProtocol(getProtocolName()); diff --git a/src/org/traccar/protocol/Gt06ProtocolDecoder.java b/src/org/traccar/protocol/Gt06ProtocolDecoder.java index e319d9e81..396fe686a 100644 --- a/src/org/traccar/protocol/Gt06ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gt06ProtocolDecoder.java @@ -26,6 +26,7 @@ import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; +import org.traccar.helper.BitUtil; import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; @@ -155,7 +156,9 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { } else if (hasDeviceId()) { - if (MESSAGES_SUPPORTED.contains(type)) { + if (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) { Position position = new Position(); position.setDeviceId(getDeviceId()); @@ -168,7 +171,7 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { // GPS length and Satellites count int gpsLength = buf.readUnsignedByte(); - position.set(Event.KEY_SATELLITES, gpsLength & 0b0000_1111); + position.set(Event.KEY_SATELLITES, BitUtil.to(gpsLength, 4)); gpsLength >>= 4; // Latitude diff --git a/src/org/traccar/protocol/MxtProtocolDecoder.java b/src/org/traccar/protocol/MxtProtocolDecoder.java index 9151d9672..ba97694d3 100644 --- a/src/org/traccar/protocol/MxtProtocolDecoder.java +++ b/src/org/traccar/protocol/MxtProtocolDecoder.java @@ -73,10 +73,10 @@ public class MxtProtocolDecoder extends BaseProtocolDecoder { long date = buf.readUnsignedInt(); - long days = BitUtil.range(date, 6 + 6 + 5); - long hours = BitUtil.range(date, 6 + 6, 5); - long minutes = BitUtil.range(date, 6, 6); - long seconds = BitUtil.range(date, 0, 6); + long days = BitUtil.from(date, 6 + 6 + 5); + long hours = BitUtil.between(date, 6 + 6, 6 + 6 + 5); + 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; @@ -91,9 +91,9 @@ public class MxtProtocolDecoder extends BaseProtocolDecoder { long flags = buf.readUnsignedInt(); position.set(Event.KEY_IGNITION, BitUtil.check(flags, 0)); position.set(Event.KEY_ALARM, BitUtil.check(flags, 1)); - position.set(Event.KEY_INPUT, BitUtil.range(flags, 2, 5)); - position.set(Event.KEY_OUTPUT, BitUtil.range(flags, 7, 3)); - position.setCourse(BitUtil.range(flags, 10, 3) * 45); + position.set(Event.KEY_INPUT, BitUtil.between(flags, 2, 7)); + position.set(Event.KEY_OUTPUT, BitUtil.between(flags, 7, 10)); + position.setCourse(BitUtil.between(flags, 10, 13) * 45); //position.setValid(BitUtil.check(flags, 15)); position.set(Event.KEY_CHARGE, BitUtil.check(flags, 20)); diff --git a/src/org/traccar/protocol/TytanProtocolDecoder.java b/src/org/traccar/protocol/TytanProtocolDecoder.java index 20cfc8ace..ec5fb167d 100644 --- a/src/org/traccar/protocol/TytanProtocolDecoder.java +++ b/src/org/traccar/protocol/TytanProtocolDecoder.java @@ -76,7 +76,7 @@ public class TytanProtocolDecoder extends BaseProtocolDecoder { int alarm = buf.readUnsignedByte(); buf.readUnsignedByte(); if (BitUtil.check(alarm, 5)) { - position.set(Event.KEY_ALARM, BitUtil.range(alarm, 0, 4)); + position.set(Event.KEY_ALARM, BitUtil.to(alarm, 4)); } break; case 8: @@ -172,8 +172,8 @@ public class TytanProtocolDecoder extends BaseProtocolDecoder { position.setTime(new Date(buf.readUnsignedInt() * 1000)); int flags = buf.readUnsignedByte(); - position.set(Event.KEY_SATELLITES, BitUtil.range(flags, 2)); - position.setValid(BitUtil.range(flags, 0, 2) > 0); + position.set(Event.KEY_SATELLITES, BitUtil.from(flags, 2)); + position.setValid(BitUtil.to(flags, 2) > 0); // Latitude double lat = buf.readUnsignedMedium(); @@ -188,8 +188,8 @@ public class TytanProtocolDecoder extends BaseProtocolDecoder { // Status flags = buf.readUnsignedByte(); position.set(Event.KEY_IGNITION, BitUtil.check(flags, 0)); - position.set(Event.KEY_GSM, BitUtil.range(flags, 2, 3)); - position.setCourse((BitUtil.range(flags, 5) * 45 + 180) % 360); + position.set(Event.KEY_GSM, BitUtil.between(flags, 2, 5)); + position.setCourse((BitUtil.from(flags, 5) * 45 + 180) % 360); // Speed int speed = buf.readUnsignedByte(); diff --git a/src/org/traccar/protocol/TzoneProtocolDecoder.java b/src/org/traccar/protocol/TzoneProtocolDecoder.java index 1fe32af7a..8125f33cb 100644 --- a/src/org/traccar/protocol/TzoneProtocolDecoder.java +++ b/src/org/traccar/protocol/TzoneProtocolDecoder.java @@ -34,8 +34,7 @@ public class TzoneProtocolDecoder 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; @@ -87,7 +86,7 @@ public class TzoneProtocolDecoder extends BaseProtocolDecoder { position.set(Event.KEY_ODOMETER, buf.readUnsignedMedium()); int flags = buf.readUnsignedShort(); - position.setCourse(BitUtil.range(flags, 0, 9)); + position.setCourse(BitUtil.to(flags, 9)); position.setLatitude(BitUtil.check(flags, 10) ? lat : -lat); position.setLongitude(BitUtil.check(flags, 9) ? -lon : lon); position.setValid(BitUtil.check(flags, 11)); diff --git a/src/org/traccar/protocol/UlbotechProtocolDecoder.java b/src/org/traccar/protocol/UlbotechProtocolDecoder.java index a33077e5f..b7a00e3b5 100644 --- a/src/org/traccar/protocol/UlbotechProtocolDecoder.java +++ b/src/org/traccar/protocol/UlbotechProtocolDecoder.java @@ -123,7 +123,7 @@ public class UlbotechProtocolDecoder extends BaseProtocolDecoder { case DATA_ADC: for (int i = 0; i < length / 2; i++) { int value = buf.readUnsignedShort(); - position.set(Event.PREFIX_ADC + BitUtil.range(value, 12), BitUtil.range(value, 0, 12)); + position.set(Event.PREFIX_ADC + BitUtil.from(value, 12), BitUtil.to(value, 12)); } break; -- cgit v1.2.3