Example usage for io.netty.buffer ByteBuf writableBytes

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

Introduction

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

Prototype

public abstract int writableBytes();

Source Link

Document

Returns the number of writable bytes which is equal to (this.capacity - this.writerIndex) .

Usage

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

License:Apache License

private void sendPhotoRequest(Channel channel, int pictureId) {
    ByteBuf photo = photos.get(pictureId);
    ByteBuf content = Unpooled.buffer();
    content.writeInt(pictureId);/*www. ja va 2 s .c o  m*/
    content.writeInt(photo.writerIndex());
    content.writeShort(Math.min(photo.writableBytes(), 1024));
    sendResponse(channel, false, MSG_X1_PHOTO_DATA, 0, content);
}

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

License:Apache License

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

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }/*from w  w w . j  a v a2 s .  c om*/

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

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

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

    if (type == MSG_STRING_INFO) {

        buf.readUnsignedInt(); // server flag
        String data;
        if (buf.readUnsignedByte() == 1) {
            data = buf.readSlice(buf.readableBytes() - 6).toString(StandardCharsets.US_ASCII);
        } else {
            data = buf.readSlice(buf.readableBytes() - 6).toString(StandardCharsets.UTF_16BE);
        }

        if (decodeLocationString(position, data) == null) {
            getLastLocation(position, null);
            position.set(Position.KEY_RESULT, data);
        }

        return position;

    } else if (type == MSG_INFO) {

        int subType = buf.readUnsignedByte();

        getLastLocation(position, null);

        if (subType == 0x00) {
            position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01);
            return position;
        } else if (subType == 0x05) {
            int flags = buf.readUnsignedByte();
            position.set(Position.KEY_DOOR, BitUtil.check(flags, 0));
            position.set(Position.PREFIX_IO + 1, BitUtil.check(flags, 2));
            return position;
        } else if (subType == 0x0a) {
            buf.skipBytes(8); // imei
            buf.skipBytes(8); // imsi
            position.set("iccid", ByteBufUtil.hexDump(buf.readSlice(8)));
            return position;
        } else if (subType == 0x0d) {
            if (buf.getByte(buf.readerIndex()) != '!') {
                buf.skipBytes(6);
            }
            return decodeFuelData(position,
                    buf.toString(buf.readerIndex(), buf.readableBytes() - 4 - 2, StandardCharsets.US_ASCII));
        }

    } else if (type == MSG_X1_PHOTO_DATA) {

        int pictureId = buf.readInt();

        ByteBuf photo = photos.get(pictureId);

        buf.readUnsignedInt(); // offset
        buf.readBytes(photo, buf.readUnsignedShort());

        if (photo.writableBytes() > 0) {
            sendPhotoRequest(channel, pictureId);
        } else {
            Device device = Context.getDeviceManager().getById(deviceSession.getDeviceId());
            position.set(Position.KEY_IMAGE,
                    Context.getMediaManager().writeFile(device.getUniqueId(), photo, "jpg"));
            photos.remove(pictureId).release();
        }

    } else if (type == MSG_AZ735_GPS || type == MSG_AZ735_ALARM) {

        if (!decodeGps(position, buf, true, deviceSession.getTimeZone())) {
            getLastLocation(position, position.getDeviceTime());
        }

        if (decodeLbs(position, buf, true)) {
            position.set(Position.KEY_RSSI, buf.readUnsignedByte());
        }

        buf.skipBytes(buf.readUnsignedByte()); // additional cell towers
        buf.skipBytes(buf.readUnsignedByte()); // wifi access point

        int status = buf.readUnsignedByte();
        position.set(Position.KEY_STATUS, status);

        if (type == MSG_AZ735_ALARM) {
            switch (status) {
            case 0xA0:
                position.set(Position.KEY_ARMED, true);
                break;
            case 0xA1:
                position.set(Position.KEY_ARMED, false);
                break;
            case 0xA2:
            case 0xA3:
                position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY);
                break;
            case 0xA4:
                position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
                break;
            case 0xA5:
                position.set(Position.KEY_ALARM, Position.ALARM_DOOR);
                break;
            default:
                break;
            }
        }

        buf.skipBytes(buf.readUnsignedByte()); // reserved extension

        sendResponse(channel, true, type, buf.getShort(buf.writerIndex() - 6), null);

        return position;

    } else if (type == MSG_OBD) {

        DateBuilder dateBuilder = new DateBuilder(deviceSession.getTimeZone())
                .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
                .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());

        getLastLocation(position, dateBuilder.getDate());

        position.set(Position.KEY_IGNITION, buf.readUnsignedByte() > 0);

        String data = buf.readCharSequence(buf.readableBytes() - 18, StandardCharsets.US_ASCII).toString();
        for (String pair : data.split(",")) {
            String[] values = pair.split("=");
            switch (Integer.parseInt(values[0].substring(0, 2), 16)) {
            case 40:
                position.set(Position.KEY_ODOMETER, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 43:
                position.set(Position.KEY_FUEL_LEVEL, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 45:
                position.set(Position.KEY_COOLANT_TEMP, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 53:
                position.set(Position.KEY_OBD_SPEED, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 54:
                position.set(Position.KEY_RPM, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 71:
                position.set(Position.KEY_FUEL_USED, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 73:
                position.set(Position.KEY_HOURS, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 74:
                position.set(Position.KEY_VIN, values[1]);
                break;
            default:
                break;
            }
        }

        return position;

    }

    return null;
}

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

License:Apache License

private void decodeSerial(Channel channel, SocketAddress remoteAddress, Position position, ByteBuf buf) {

    getLastLocation(position, null);/*from  w w w.  jav a 2  s.  c  om*/

    int type = buf.readUnsignedByte();
    if (type == 0x0D) {

        buf.readInt(); // length
        int subtype = buf.readUnsignedByte();
        if (subtype == 0x01) {

            long photoId = buf.readUnsignedInt();
            ByteBuf photo = Unpooled.buffer(buf.readInt());
            photos.put(photoId, photo);
            sendImageRequest(channel, remoteAddress, photoId, 0, Math.min(IMAGE_PACKET_MAX, photo.capacity()));

        } else if (subtype == 0x02) {

            long photoId = buf.readUnsignedInt();
            buf.readInt(); // offset
            ByteBuf photo = photos.get(photoId);
            photo.writeBytes(buf, buf.readUnsignedShort());
            if (photo.writableBytes() > 0) {
                sendImageRequest(channel, remoteAddress, photoId, photo.writerIndex(),
                        Math.min(IMAGE_PACKET_MAX, photo.writableBytes()));
            } else {
                String uniqueId = Context.getIdentityManager().getById(position.getDeviceId()).getUniqueId();
                photos.remove(photoId);
                try {
                    position.set(Position.KEY_IMAGE,
                            Context.getMediaManager().writeFile(uniqueId, photo, "jpg"));
                } finally {
                    photo.release();
                }
            }

        }

    } else {

        position.set(Position.KEY_TYPE, type);

        int length = buf.readInt();
        boolean readable = true;
        for (int i = 0; i < length; i++) {
            byte b = buf.getByte(buf.readerIndex() + i);
            if (b < 32 && b != '\r' && b != '\n') {
                readable = false;
                break;
            }
        }

        if (readable) {
            position.set(Position.KEY_RESULT, buf.readSlice(length).toString(StandardCharsets.US_ASCII));
        } else {
            position.set(Position.KEY_RESULT, ByteBufUtil.hexDump(buf.readSlice(length)));
        }
    }
}