Example usage for io.netty.buffer ByteBuf setShortLE

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

Introduction

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

Prototype

public abstract ByteBuf setShortLE(int index, int value);

Source Link

Document

Sets the specified 16-bit short integer at the specified absolute index in this buffer with the Little Endian Byte Order.

Usage

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;//from w w  w.j a  v  a 2s .  c  o m

    buf.readUnsignedShortLE(); // checksum
    buf.readUnsignedShortLE(); // preamble
    long id = buf.readUnsignedIntLE();
    buf.readUnsignedShortLE(); // length

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

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

    while (buf.isReadable()) {

        buf.readUnsignedShortLE(); // checksum
        buf.readUnsignedShortLE(); // checksum
        buf.readUnsignedShortLE(); // type
        int length = buf.readUnsignedShortLE();

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

        position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
        position.setLatitude(buf.readUnsignedIntLE() * 0.000001);
        position.setLongitude(buf.readUnsignedIntLE() * 0.000001);
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE() * 0.01));
        position.setAltitude(buf.readShortLE());
        position.setCourse(buf.readUnsignedShortLE());
        position.setValid(buf.readUnsignedByte() > 0);

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

        if (BitUtil.check(buf.readUnsignedByte(), 0)) {
            position.set(Position.KEY_ARCHIVE, true);
        }

        positions.add(position);

        buf.skipBytes(length);

    }

    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeShortLE(0);
        response.writeShortLE(0x4CBF); // preamble
        response.writeIntLE((int) id);
        response.writeShortLE(0);
        response.setShortLE(0, Checksum.crc16(Checksum.CRC16_CCITT_FALSE,
                response.nioBuffer(2, response.readableBytes() - 2)));
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }

    return positions;
}