Example usage for io.netty.buffer ByteBuf readUnsignedInt

List of usage examples for io.netty.buffer ByteBuf readUnsignedInt

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readUnsignedInt.

Prototype

public abstract long readUnsignedInt();

Source Link

Document

Gets an unsigned 32-bit integer at the current readerIndex and increases the readerIndex by 4 in this buffer.

Usage

From source file:org.traccar.protocol.TeltonikaProtocolDecoder.java

License:Apache License

private void decodeLocation(Position position, ByteBuf buf, int codec) {

    int globalMask = 0x0f;

    if (codec == CODEC_GH3000) {

        long time = buf.readUnsignedInt() & 0x3fffffff;
        time += 1167609600; // 2007-01-01 00:00:00

        globalMask = buf.readUnsignedByte();
        if (BitUtil.check(globalMask, 0)) {

            position.setTime(new Date(time * 1000));

            int locationMask = buf.readUnsignedByte();

            if (BitUtil.check(locationMask, 0)) {
                position.setLatitude(buf.readFloat());
                position.setLongitude(buf.readFloat());
            }//w  ww  .j a  v a 2  s .  c o  m

            if (BitUtil.check(locationMask, 1)) {
                position.setAltitude(buf.readUnsignedShort());
            }

            if (BitUtil.check(locationMask, 2)) {
                position.setCourse(buf.readUnsignedByte() * 360.0 / 256);
            }

            if (BitUtil.check(locationMask, 3)) {
                position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
            }

            if (BitUtil.check(locationMask, 4)) {
                position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
            }

            if (BitUtil.check(locationMask, 5)) {
                CellTower cellTower = CellTower.fromLacCid(buf.readUnsignedShort(), buf.readUnsignedShort());

                if (BitUtil.check(locationMask, 6)) {
                    cellTower.setSignalStrength((int) buf.readUnsignedByte());
                }

                if (BitUtil.check(locationMask, 7)) {
                    cellTower.setOperator(buf.readUnsignedInt());
                }

                position.setNetwork(new Network(cellTower));

            } else {
                if (BitUtil.check(locationMask, 6)) {
                    position.set(Position.KEY_RSSI, buf.readUnsignedByte());
                }
                if (BitUtil.check(locationMask, 7)) {
                    position.set(Position.KEY_OPERATOR, buf.readUnsignedInt());
                }
            }

        } else {

            getLastLocation(position, new Date(time * 1000));

        }

    } else {

        position.setTime(new Date(buf.readLong()));

        position.set("priority", buf.readUnsignedByte());

        position.setLongitude(buf.readInt() / 10000000.0);
        position.setLatitude(buf.readInt() / 10000000.0);
        position.setAltitude(buf.readShort());
        position.setCourse(buf.readUnsignedShort());

        int satellites = buf.readUnsignedByte();
        position.set(Position.KEY_SATELLITES, satellites);

        position.setValid(satellites != 0);

        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));

        position.set(Position.KEY_EVENT, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16));
        if (codec == CODEC_16) {
            buf.readUnsignedByte(); // generation type
        }

        readExtByte(buf, codec, CODEC_8_EXT); // total IO data records

    }

    // Read 1 byte data
    if (BitUtil.check(globalMask, 1)) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            decodeParameter(position, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16), buf, 1, codec);
        }
    }

    // Read 2 byte data
    if (BitUtil.check(globalMask, 2)) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            decodeParameter(position, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16), buf, 2, codec);
        }
    }

    // Read 4 byte data
    if (BitUtil.check(globalMask, 3)) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            decodeParameter(position, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16), buf, 4, codec);
        }
    }

    // Read 8 byte data
    if (codec == CODEC_8 || codec == CODEC_8_EXT || codec == CODEC_16) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            decodeOtherParameter(position, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16), buf, 8);
        }
    }

    // Read 16 byte data
    if (extended) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            int id = readExtByte(buf, codec, CODEC_8_EXT, CODEC_16);
            position.set(Position.PREFIX_IO + id, ByteBufUtil.hexDump(buf.readSlice(16)));
        }
    }

    // Read X byte data
    if (codec == CODEC_8_EXT) {
        int cnt = buf.readUnsignedShort();
        for (int j = 0; j < cnt; j++) {
            int id = buf.readUnsignedShort();
            int length = buf.readUnsignedShort();
            if (id == 256) {
                position.set(Position.KEY_VIN, buf.readSlice(length).toString(StandardCharsets.US_ASCII));
            } else {
                position.set(Position.PREFIX_IO + id, ByteBufUtil.hexDump(buf.readSlice(length)));
            }
        }
    }

    decodeNetwork(position);

}

From source file:org.traccar.protocol.TeltonikaProtocolDecoder.java

License:Apache License

private List<Position> parseData(Channel channel, SocketAddress remoteAddress, ByteBuf buf,
        int locationPacketId, String... imei) {
    List<Position> positions = new LinkedList<>();

    if (!connectionless) {
        buf.readUnsignedInt(); // data length
    }/*from   w w w . j  ava2 s  .  c o m*/

    int codec = buf.readUnsignedByte();
    int count = buf.readUnsignedByte();

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);

    if (deviceSession == null) {
        return null;
    }

    for (int i = 0; i < count; i++) {
        Position position = new Position(getProtocolName());

        position.setDeviceId(deviceSession.getDeviceId());
        position.setValid(true);

        if (codec == CODEC_12) {
            decodeSerial(channel, remoteAddress, position, buf);
        } else {
            decodeLocation(position, buf, codec);
        }

        if (!position.getOutdated() || !position.getAttributes().isEmpty()) {
            positions.add(position);
        }
    }

    if (channel != null) {
        if (connectionless) {
            ByteBuf response = Unpooled.buffer();
            response.writeShort(5);
            response.writeShort(0);
            response.writeByte(0x01);
            response.writeByte(locationPacketId);
            response.writeByte(count);
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        } else {
            ByteBuf response = Unpooled.buffer();
            response.writeInt(count);
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
    }

    return positions.isEmpty() ? null : positions;
}

From source file:org.traccar.protocol.ThinkRaceProtocolDecoder.java

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;

    buf.skipBytes(2); // header
    ByteBuf id = buf.readSlice(12);/*w  w w  .j  ava  2s . co m*/
    buf.readUnsignedByte(); // separator
    int type = buf.readUnsignedByte();
    buf.readUnsignedShort(); // length

    if (type == MSG_LOGIN) {

        int command = buf.readUnsignedByte(); // 0x00 - heartbeat

        if (command == 0x01) {
            String imei = buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII);
            DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
            if (deviceSession != null && channel != null) {
                ByteBuf response = Unpooled.buffer();
                response.writeByte(0x48);
                response.writeByte(0x52); // header
                response.writeBytes(id);
                response.writeByte(0x2c); // separator
                response.writeByte(type);
                response.writeShort(0x0002); // length
                response.writeShort(0x8000);
                response.writeShort(0x0000); // checksum
                channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
            }
        }

    } else if (type == MSG_GPS) {

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        position.setTime(new Date(buf.readUnsignedInt() * 1000));

        int flags = buf.readUnsignedByte();

        position.setValid(true);
        position.setLatitude(convertCoordinate(buf.readUnsignedInt(), !BitUtil.check(flags, 0)));
        position.setLongitude(convertCoordinate(buf.readUnsignedInt(), !BitUtil.check(flags, 1)));

        position.setSpeed(buf.readUnsignedByte());
        position.setCourse(buf.readUnsignedByte());

        position.setNetwork(
                new Network(CellTower.fromLacCid(buf.readUnsignedShort(), buf.readUnsignedShort())));

        return position;

    }

    return null;
}

From source file:org.traccar.protocol.TramigoProtocolDecoder.java

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;

    int protocol = buf.readUnsignedByte();
    boolean legacy = protocol == 0x80;

    buf.readUnsignedByte(); // version id
    int index = legacy ? buf.readUnsignedShort() : buf.readUnsignedShortLE();
    int type = legacy ? buf.readUnsignedShort() : buf.readUnsignedShortLE();
    buf.readUnsignedShort(); // length
    buf.readUnsignedShort(); // mask
    buf.readUnsignedShort(); // checksum
    long id = legacy ? buf.readUnsignedInt() : buf.readUnsignedIntLE();
    buf.readUnsignedInt(); // time

    Position position = new Position(getProtocolName());
    position.set(Position.KEY_INDEX, index);
    position.setValid(true);//from ww w  .j ava  2s.co  m

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(id));
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());

    if (protocol == 0x01 && (type == MSG_COMPACT || type == MSG_FULL)) {

        // need to send ack?

        buf.readUnsignedShortLE(); // report trigger
        buf.readUnsignedShortLE(); // state flag

        position.setLatitude(buf.readUnsignedIntLE() * 0.0000001);
        position.setLongitude(buf.readUnsignedIntLE() * 0.0000001);

        position.set(Position.KEY_RSSI, buf.readUnsignedShortLE());
        position.set(Position.KEY_SATELLITES, buf.readUnsignedShortLE());
        position.set(Position.KEY_SATELLITES_VISIBLE, buf.readUnsignedShortLE());
        position.set("gpsAntennaStatus", buf.readUnsignedShortLE());

        position.setSpeed(buf.readUnsignedShortLE() * 0.194384);
        position.setCourse(buf.readUnsignedShortLE());

        position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());

        position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE());

        position.set(Position.KEY_CHARGE, buf.readUnsignedShortLE());

        position.setTime(new Date(buf.readUnsignedIntLE() * 1000));

        // parse other data

        return position;

    } else if (legacy) {

        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(
                    Unpooled.copiedBuffer("gprs,ack," + index, StandardCharsets.US_ASCII), remoteAddress));
        }

        String sentence = buf.toString(StandardCharsets.US_ASCII);

        Pattern pattern = Pattern.compile("(-?\\d+\\.\\d+), (-?\\d+\\.\\d+)");
        Matcher matcher = pattern.matcher(sentence);
        if (!matcher.find()) {
            return null;
        }
        position.setLatitude(Double.parseDouble(matcher.group(1)));
        position.setLongitude(Double.parseDouble(matcher.group(2)));

        pattern = Pattern.compile("([NSWE]{1,2}) with speed (\\d+) km/h");
        matcher = pattern.matcher(sentence);
        if (matcher.find()) {
            for (int i = 0; i < DIRECTIONS.length; i++) {
                if (matcher.group(1).equals(DIRECTIONS[i])) {
                    position.setCourse(i * 45.0);
                    break;
                }
            }
            position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(matcher.group(2))));
        }

        pattern = Pattern.compile("(\\d{1,2}:\\d{2}(:\\d{2})? \\w{3} \\d{1,2})");
        matcher = pattern.matcher(sentence);
        if (!matcher.find()) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat(
                matcher.group(2) != null ? "HH:mm:ss MMM d yyyy" : "HH:mm MMM d yyyy", Locale.ENGLISH);
        position.setTime(DateUtil.correctYear(
                dateFormat.parse(matcher.group(1) + " " + Calendar.getInstance().get(Calendar.YEAR))));

        if (sentence.contains("Ignition on detected")) {
            position.set(Position.KEY_IGNITION, true);
        } else if (sentence.contains("Ignition off detected")) {
            position.set(Position.KEY_IGNITION, false);
        }

        return position;

    }

    return null;
}

From source file:org.traccar.protocol.TytanProtocolDecoder.java

License:Apache License

private void decodeExtraData(Position position, ByteBuf buf, int end) {
    while (buf.readerIndex() < end) {

        int type = buf.readUnsignedByte();
        int length = buf.readUnsignedByte();
        if (length == 255) {
            length += buf.readUnsignedByte();
        }// w  ww  .  j  a  v  a 2  s .  co m

        int n;

        switch (type) {
        case 2:
            position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedMedium());
            break;
        case 5:
            position.set(Position.KEY_INPUT, buf.readUnsignedByte());
            break;
        case 6:
            n = buf.readUnsignedByte() >> 4;
            if (n < 2) {
                position.set(Position.PREFIX_ADC + n, buf.readFloat());
            } else {
                position.set("di" + (n - 2), buf.readFloat());
            }
            break;
        case 7:
            int alarm = buf.readUnsignedByte();
            buf.readUnsignedByte();
            if (BitUtil.check(alarm, 5)) {
                position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
            }
            break;
        case 8:
            position.set("antihijack", buf.readUnsignedByte());
            break;
        case 9:
            position.set("unauthorized", ByteBufUtil.hexDump(buf.readSlice(8)));
            break;
        case 10:
            position.set("authorized", ByteBufUtil.hexDump(buf.readSlice(8)));
            break;
        case 24:
            for (int i = 0; i < length / 2; i++) {
                position.set(Position.PREFIX_TEMP + buf.readUnsignedByte(), buf.readByte());
            }
            break;
        case 28:
            position.set(Position.KEY_AXLE_WEIGHT, buf.readUnsignedShort());
            buf.readUnsignedByte();
            break;
        case 90:
            position.set(Position.KEY_POWER, buf.readFloat());
            break;
        case 101:
            position.set(Position.KEY_OBD_SPEED, buf.readUnsignedByte());
            break;
        case 102:
            position.set(Position.KEY_RPM, buf.readUnsignedByte() * 50);
            break;
        case 107:
            int fuel = buf.readUnsignedShort();
            int fuelFormat = fuel >> 14;
            if (fuelFormat == 1) {
                position.set("fuelValue", (fuel & 0x3fff) * 0.4 + "%");
            } else if (fuelFormat == 2) {
                position.set("fuelValue", (fuel & 0x3fff) * 0.5 + " l");
            } else if (fuelFormat == 3) {
                position.set("fuelValue", (fuel & 0x3fff) * -0.5 + " l");
            }
            break;
        case 108:
            position.set(Position.KEY_OBD_ODOMETER, buf.readUnsignedInt() * 5);
            break;
        case 150:
            position.set(Position.KEY_DOOR, buf.readUnsignedByte());
            break;
        default:
            buf.skipBytes(length);
            break;
        }
    }
}

From source file:org.traccar.protocol.TytanProtocolDecoder.java

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedByte(); // protocol
    buf.readUnsignedShort(); // length
    int index = buf.readUnsignedByte() >> 3;

    if (channel != null) {
        ByteBuf response = Unpooled.copiedBuffer("^" + index, StandardCharsets.US_ASCII);
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }/*from w  w  w .  j av  a  2  s.  c  o  m*/

    String id = String.valueOf(buf.readUnsignedInt());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }

    List<Position> positions = new LinkedList<>();

    while (buf.readableBytes() > 2) {

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        int end = buf.readerIndex() + buf.readUnsignedByte();

        position.setTime(new Date(buf.readUnsignedInt() * 1000));

        int flags = buf.readUnsignedByte();
        position.set(Position.KEY_SATELLITES, BitUtil.from(flags, 2));
        position.setValid(BitUtil.to(flags, 2) > 0);

        // Latitude
        double lat = buf.readUnsignedMedium();
        lat = lat * -180 / 16777216 + 90;
        position.setLatitude(lat);

        // Longitude
        double lon = buf.readUnsignedMedium();
        lon = lon * 360 / 16777216 - 180;
        position.setLongitude(lon);

        // Status
        flags = buf.readUnsignedByte();
        position.set(Position.KEY_IGNITION, BitUtil.check(flags, 0));
        position.set(Position.KEY_RSSI, BitUtil.between(flags, 2, 5));
        position.setCourse((BitUtil.from(flags, 5) * 45 + 180) % 360);

        // Speed
        int speed = buf.readUnsignedByte();
        if (speed < 250) {
            position.setSpeed(UnitsConverter.knotsFromKph(speed));
        }

        decodeExtraData(position, buf, end);

        positions.add(position);
    }

    return positions;
}

From source file:org.traccar.protocol.TzoneProtocolDecoder.java

License:Apache License

private boolean decodeGps(Position position, ByteBuf buf, int hardware) {

    int blockLength = buf.readUnsignedShort();
    int blockEnd = buf.readerIndex() + blockLength;

    if (blockLength < 22) {
        return false;
    }//  ww w . j av  a 2s .  co  m

    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());

    double lat;
    double lon;

    if (hardware == 0x10A || hardware == 0x10B) {
        lat = buf.readUnsignedInt() / 600000.0;
        lon = buf.readUnsignedInt() / 600000.0;
    } else {
        lat = buf.readUnsignedInt() / 100000.0 / 60.0;
        lon = buf.readUnsignedInt() / 100000.0 / 60.0;
    }

    position.setFixTime(
            new DateBuilder().setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
                    .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).getDate());

    position.setSpeed(buf.readUnsignedShort() * 0.01);

    position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium());

    int flags = buf.readUnsignedShort();
    position.setCourse(BitUtil.to(flags, 9));
    if (!BitUtil.check(flags, 10)) {
        lat = -lat;
    }
    position.setLatitude(lat);
    if (BitUtil.check(flags, 9)) {
        lon = -lon;
    }
    position.setLongitude(lon);
    position.setValid(BitUtil.check(flags, 11));

    buf.readerIndex(blockEnd);

    return true;
}

From source file:org.traccar.protocol.TzoneProtocolDecoder.java

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;

    buf.skipBytes(2); // header
    buf.readUnsignedShort(); // length
    if (buf.readUnsignedShort() != 0x2424) {
        return null;
    }//  w  w w. j  a v  a 2s.c  o  m
    int hardware = buf.readUnsignedShort();
    long firmware = buf.readUnsignedInt();

    String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }

    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());

    position.set(Position.KEY_VERSION_HW, hardware);
    position.set(Position.KEY_VERSION_FW, firmware);

    position.setDeviceTime(
            new DateBuilder().setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
                    .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).getDate());

    // GPS info

    if (hardware == 0x406 || !decodeGps(position, buf, hardware)) {

        getLastLocation(position, position.getDeviceTime());

    }

    // LBS info

    int blockLength = buf.readUnsignedShort();
    int blockEnd = buf.readerIndex() + blockLength;

    if (blockLength > 0 && (hardware == 0x10A || hardware == 0x10B || hardware == 0x406)) {
        position.setNetwork(
                new Network(CellTower.fromLacCid(buf.readUnsignedShort(), buf.readUnsignedShort())));
    }

    buf.readerIndex(blockEnd);

    // Status info

    blockLength = buf.readUnsignedShort();
    blockEnd = buf.readerIndex() + blockLength;

    if (blockLength >= 13) {
        position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedByte()));
        position.set("terminalInfo", buf.readUnsignedByte());

        int status = buf.readUnsignedByte();
        position.set(Position.PREFIX_OUT + 1, BitUtil.check(status, 0));
        position.set(Position.PREFIX_OUT + 2, BitUtil.check(status, 1));
        status = buf.readUnsignedByte();
        position.set(Position.PREFIX_IN + 1, BitUtil.check(status, 4));
        if (BitUtil.check(status, 0)) {
            position.set(Position.KEY_ALARM, Position.ALARM_SOS);
        }

        position.set(Position.KEY_RSSI, buf.readUnsignedByte());
        position.set("gsmStatus", buf.readUnsignedByte());
        position.set(Position.KEY_BATTERY, buf.readUnsignedShort());
        position.set(Position.KEY_POWER, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort());
    }

    if (blockLength >= 15) {
        position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedShort());
    }

    buf.readerIndex(blockEnd);

    if (hardware == 0x10B) {

        decodeCards(position, buf);

        buf.skipBytes(buf.readUnsignedShort()); // temperature
        buf.skipBytes(buf.readUnsignedShort()); // lock

        decodePassengers(position, buf);

    }

    if (hardware == 0x406) {

        decodeTags(position, buf);

    }

    return position;
}

From source file:org.traccar.protocol.UlbotechProtocolDecoder.java

License:Apache License

private Object decodeBinary(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

    buf.readUnsignedByte(); // header
    buf.readUnsignedByte(); // version
    buf.readUnsignedByte(); // type

    String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }//w ww.  j  av  a 2s .com

    if (deviceSession.getTimeZone() == null) {
        deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId()));
    }

    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());

    long seconds = buf.readUnsignedInt() & 0x7fffffffL;
    seconds += 946684800L; // 2000-01-01 00:00
    seconds -= deviceSession.getTimeZone().getRawOffset() / 1000;
    Date time = new Date(seconds * 1000);

    boolean hasLocation = false;

    while (buf.readableBytes() > 3) {

        int type = buf.readUnsignedByte();
        int length = type == DATA_CANBUS ? buf.readUnsignedShort() : buf.readUnsignedByte();

        switch (type) {

        case DATA_GPS:
            hasLocation = true;
            position.setValid(true);
            position.setLatitude(buf.readInt() / 1000000.0);
            position.setLongitude(buf.readInt() / 1000000.0);
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
            position.setCourse(buf.readUnsignedShort());
            position.set(Position.KEY_HDOP, buf.readUnsignedShort());
            break;

        case DATA_LBS:
            if (length == 11) {
                position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(),
                        buf.readUnsignedShort(), buf.readUnsignedInt(), -buf.readUnsignedByte())));
            } else {
                position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(),
                        buf.readUnsignedShort(), buf.readUnsignedShort(), -buf.readUnsignedByte())));
            }
            if (length > 9 && length != 11) {
                buf.skipBytes(length - 9);
            }
            break;

        case DATA_STATUS:
            int status = buf.readUnsignedShort();
            position.set(Position.KEY_IGNITION, BitUtil.check(status, 9));
            position.set(Position.KEY_STATUS, status);
            position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedShort()));
            break;

        case DATA_ODOMETER:
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
            break;

        case DATA_ADC:
            decodeAdc(position, buf, length);
            break;

        case DATA_GEOFENCE:
            position.set("geofenceIn", buf.readUnsignedInt());
            position.set("geofenceAlarm", buf.readUnsignedInt());
            break;

        case DATA_OBD2:
            decodeObd(position, buf, length);
            break;

        case DATA_FUEL:
            position.set(Position.KEY_FUEL_CONSUMPTION, buf.readUnsignedInt() / 10000.0);
            break;

        case DATA_OBD2_ALARM:
            decodeObd(position, buf, length);
            break;

        case DATA_HARSH_DRIVER:
            decodeDriverBehavior(position, buf);
            break;

        case DATA_CANBUS:
            position.set("can", ByteBufUtil.hexDump(buf.readSlice(length)));
            break;

        case DATA_J1708:
            decodeJ1708(position, buf, length);
            break;

        case DATA_VIN:
            position.set(Position.KEY_VIN, buf.readSlice(length).toString(StandardCharsets.US_ASCII));
            break;

        case DATA_RFID:
            position.set(Position.KEY_DRIVER_UNIQUE_ID,
                    buf.readSlice(length - 1).toString(StandardCharsets.US_ASCII));
            position.set("authorized", buf.readUnsignedByte() != 0);
            break;

        case DATA_EVENT:
            position.set(Position.KEY_EVENT, buf.readUnsignedByte());
            if (length > 1) {
                position.set("eventMask", buf.readUnsignedInt());
            }
            break;

        default:
            buf.skipBytes(length);
            break;
        }
    }

    if (!hasLocation) {
        getLastLocation(position, time);
    } else {
        position.setTime(time);
    }

    return position;
}

From source file:org.traccar.protocol.Vt200ProtocolDecoder.java

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;

    buf.skipBytes(1); // header

    String id = ByteBufUtil.hexDump(buf.readSlice(6));
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }/*from   w w  w .  j av a2s.c  o m*/

    int type = buf.readUnsignedShort();
    buf.readUnsignedShort(); // length

    if (type == 0x2086 || type == 0x2084 || type == 0x2082) {

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        buf.readUnsignedByte(); // data type
        buf.readUnsignedShort(); // trip id

        position.setTime(decodeDate(buf));

        position.setLatitude(decodeCoordinate(BcdUtil.readInteger(buf, 8)));
        position.setLongitude(decodeCoordinate(BcdUtil.readInteger(buf, 9)));

        int flags = buf.readUnsignedByte();
        position.setValid(BitUtil.check(flags, 0));
        if (!BitUtil.check(flags, 1)) {
            position.setLatitude(-position.getLatitude());
        }
        if (!BitUtil.check(flags, 2)) {
            position.setLongitude(-position.getLongitude());
        }

        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
        position.setCourse(buf.readUnsignedByte() * 2);

        position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
        position.set(Position.KEY_RSSI, buf.readUnsignedByte());
        position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
        position.set(Position.KEY_STATUS, buf.readUnsignedInt());

        // additional data

        return position;

    } else if (type == 0x3088) {

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        getLastLocation(position, null);

        buf.readUnsignedShort(); // trip id
        buf.skipBytes(8); // imei
        buf.skipBytes(8); // imsi

        position.set("tripStart", decodeDate(buf).getTime());
        position.set("tripEnd", decodeDate(buf).getTime());
        position.set("drivingTime", buf.readUnsignedShort());

        position.set(Position.KEY_FUEL_CONSUMPTION, buf.readUnsignedInt());
        position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt());

        position.set("maxSpeed", UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
        position.set("maxRpm", buf.readUnsignedShort());
        position.set("maxTemp", buf.readUnsignedByte() - 40);
        position.set("hardAccelerationCount", buf.readUnsignedByte());
        position.set("hardBrakingCount", buf.readUnsignedByte());

        for (String speedType : Arrays.asList("over", "high", "normal", "low")) {
            position.set(speedType + "SpeedTime", buf.readUnsignedShort());
            position.set(speedType + "SpeedDistance", buf.readUnsignedInt());
            position.set(speedType + "SpeedFuel", buf.readUnsignedInt());
        }

        position.set("idleTime", buf.readUnsignedShort());
        position.set("idleFuel", buf.readUnsignedInt());

        position.set("hardCorneringCount", buf.readUnsignedByte());
        position.set("overspeedCount", buf.readUnsignedByte());
        position.set("overheatCount", buf.readUnsignedShort());
        position.set("laneChangeCount", buf.readUnsignedByte());
        position.set("emergencyRefueling", buf.readUnsignedByte());

        return position;

    }

    return null;
}