Example usage for io.netty.buffer ByteBuf readMediumLE

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

Introduction

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

Prototype

public abstract int readMediumLE();

Source Link

Document

Gets a 24-bit medium integer at the current readerIndex in the Little Endian Byte Order and increases the readerIndex by 3 in this buffer.

Usage

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int index = buf.getUnsignedShort(buf.readerIndex() + 5 + 2);
    buf.skipBytes(buf.getUnsignedByte(buf.readerIndex() + 3));

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

    while (buf.readableBytes() > 2) {

        int length = buf.readUnsignedShortLE();
        int recordIndex = buf.readUnsignedShortLE();
        int recordFlags = buf.readUnsignedByte();

        if (BitUtil.check(recordFlags, 0)) {
            buf.readUnsignedIntLE(); // object id
        }// www  .j  a v a  2  s . c  o m

        if (BitUtil.check(recordFlags, 1)) {
            buf.readUnsignedIntLE(); // event id
        }
        if (BitUtil.check(recordFlags, 2)) {
            buf.readUnsignedIntLE(); // time
        }

        int serviceType = buf.readUnsignedByte();
        buf.readUnsignedByte(); // recipient service type

        int recordEnd = buf.readerIndex() + length;

        Position position = new Position(getProtocolName());
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession != null) {
            position.setDeviceId(deviceSession.getDeviceId());
        }

        ByteBuf response = Unpooled.buffer();
        response.writeShortLE(recordIndex);
        response.writeByte(0); // success
        sendResponse(channel, PT_RESPONSE, index, serviceType, MSG_RECORD_RESPONSE, response);

        while (buf.readerIndex() < recordEnd) {
            int type = buf.readUnsignedByte();
            int end = buf.readUnsignedShortLE() + buf.readerIndex();

            if (type == MSG_TERM_IDENTITY) {

                buf.readUnsignedIntLE(); // object id
                int flags = buf.readUnsignedByte();

                if (BitUtil.check(flags, 0)) {
                    buf.readUnsignedShortLE(); // home dispatcher identifier
                }
                if (BitUtil.check(flags, 1)) {
                    getDeviceSession(channel, remoteAddress,
                            buf.readSlice(15).toString(StandardCharsets.US_ASCII).trim());
                }
                if (BitUtil.check(flags, 2)) {
                    getDeviceSession(channel, remoteAddress,
                            buf.readSlice(16).toString(StandardCharsets.US_ASCII).trim());
                }
                if (BitUtil.check(flags, 3)) {
                    buf.skipBytes(3); // language identifier
                }
                if (BitUtil.check(flags, 5)) {
                    buf.skipBytes(3); // network identifier
                }
                if (BitUtil.check(flags, 6)) {
                    buf.readUnsignedShortLE(); // buffer size
                }
                if (BitUtil.check(flags, 7)) {
                    getDeviceSession(channel, remoteAddress,
                            buf.readSlice(15).toString(StandardCharsets.US_ASCII).trim());
                }

                response = Unpooled.buffer();
                response.writeByte(0); // success
                sendResponse(channel, PT_APPDATA, 0, serviceType, MSG_RESULT_CODE, response);

            } else if (type == MSG_POS_DATA) {

                position.setTime(new Date((buf.readUnsignedIntLE() + 1262304000) * 1000)); // since 2010-01-01
                position.setLatitude(buf.readUnsignedIntLE() * 90.0 / 0xFFFFFFFFL);
                position.setLongitude(buf.readUnsignedIntLE() * 180.0 / 0xFFFFFFFFL);

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

                int speed = buf.readUnsignedShortLE();
                position.setSpeed(UnitsConverter.knotsFromKph(BitUtil.to(speed, 14) * 0.1));
                position.setCourse(buf.readUnsignedByte() + (BitUtil.check(speed, 15) ? 0x100 : 0));

                position.set(Position.KEY_ODOMETER, buf.readUnsignedMediumLE() * 100);
                position.set(Position.KEY_INPUT, buf.readUnsignedByte());
                position.set(Position.KEY_EVENT, buf.readUnsignedByte());

                if (BitUtil.check(flags, 7)) {
                    position.setAltitude(buf.readMediumLE());
                }

            } else if (type == MSG_EXT_POS_DATA) {

                int flags = buf.readUnsignedByte();

                if (BitUtil.check(flags, 0)) {
                    position.set(Position.KEY_VDOP, buf.readUnsignedShortLE());
                }
                if (BitUtil.check(flags, 1)) {
                    position.set(Position.KEY_HDOP, buf.readUnsignedShortLE());
                }
                if (BitUtil.check(flags, 2)) {
                    position.set(Position.KEY_PDOP, buf.readUnsignedShortLE());
                }
                if (BitUtil.check(flags, 3)) {
                    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
                }

            } else if (type == MSG_AD_SENSORS_DATA) {

                buf.readUnsignedByte(); // inputs flags

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

                buf.readUnsignedByte(); // adc flags

            }

            buf.readerIndex(end);
        }

        if (serviceType == SERVICE_TELEDATA && deviceSession != null) {
            positions.add(position);
        }
    }

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

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

License:Apache License

private Position parsePositionReport(DeviceSession deviceSession, ByteBuf buf, int sequenceNumber,
        long timestamp) {
    Position position = new Position(getProtocolName());

    position.set(Position.KEY_INDEX, sequenceNumber);
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(convertTimestamp(timestamp));

    position.setLatitude(buf.readMediumLE() * 0.00002);
    position.setLongitude(buf.readMediumLE() * 0.00002);

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

    short flags = buf.readUnsignedByte();
    position.setValid((flags & 0x80) == 0x80 && (flags & 0x40) == 0x40);

    buf.readUnsignedByte(); // reserved

    return position;
}