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.OigoProtocolDecoder.java

License:Apache License

private Position decodeArMessage(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

    buf.skipBytes(1); // header
    buf.readUnsignedShort(); // length

    int type = buf.readUnsignedByte();

    int tag = buf.readUnsignedByte();

    DeviceSession deviceSession;/*w  ww . j a v a  2  s  . c o m*/
    switch (BitUtil.to(tag, 3)) {
    case 0:
        String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
        deviceSession = getDeviceSession(channel, remoteAddress, imei);
        break;
    case 1:
        buf.skipBytes(1);
        String meid = buf.readSlice(14).toString(StandardCharsets.US_ASCII);
        deviceSession = getDeviceSession(channel, remoteAddress, meid);
        break;
    default:
        deviceSession = getDeviceSession(channel, remoteAddress);
        break;
    }

    if (deviceSession == null || type != MSG_AR_LOCATION) {
        return null;
    }

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

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

    int mask = buf.readInt();

    if (BitUtil.check(mask, 0)) {
        position.set(Position.KEY_INDEX, buf.readUnsignedShort());
    }

    if (BitUtil.check(mask, 1)) {
        int date = buf.readUnsignedByte();
        DateBuilder dateBuilder = new DateBuilder()
                .setDate(BitUtil.between(date, 4, 8) + 2010, BitUtil.to(date, 4), buf.readUnsignedByte())
                .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
        position.setTime(dateBuilder.getDate());
    }

    if (BitUtil.check(mask, 2)) {
        buf.skipBytes(5); // device time
    }

    if (BitUtil.check(mask, 3)) {
        position.setLatitude(buf.readUnsignedInt() * 0.000001 - 90);
        position.setLongitude(buf.readUnsignedInt() * 0.000001 - 180.0);
    }

    if (BitUtil.check(mask, 4)) {
        int status = buf.readUnsignedByte();
        position.setValid(BitUtil.between(status, 4, 8) != 0);
        position.set(Position.KEY_SATELLITES, BitUtil.to(status, 4));
        position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
    }

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

    if (BitUtil.check(mask, 6)) {
        position.setCourse(buf.readUnsignedShort());
    }

    if (BitUtil.check(mask, 7)) {
        position.setAltitude(buf.readShort());
    }

    if (BitUtil.check(mask, 8)) {
        position.set(Position.KEY_RSSI, buf.readUnsignedByte());
    }

    if (BitUtil.check(mask, 9)) {
        position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
    }

    if (BitUtil.check(mask, 10)) {
        position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
    }

    if (BitUtil.check(mask, 11)) {
        buf.skipBytes(2); // gpio
    }

    if (BitUtil.check(mask, 12)) {
        position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
    }

    if (BitUtil.check(mask, 13)) {
        buf.skipBytes(6); // software version
    }

    if (BitUtil.check(mask, 14)) {
        buf.skipBytes(5); // hardware version
    }

    if (BitUtil.check(mask, 15)) {
        buf.readUnsignedShort(); // device config
    }

    return position;
}

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

License:Apache License

private Position decodeMgMessage(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

    buf.readUnsignedByte(); // tag
    int flags = buf.getUnsignedByte(buf.readerIndex());

    DeviceSession deviceSession;/*from ww w  .j av  a2s  .  c  o  m*/
    if (BitUtil.check(flags, 6)) {
        buf.readUnsignedByte(); // flags
        deviceSession = getDeviceSession(channel, remoteAddress);
    } else {
        String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
        deviceSession = getDeviceSession(channel, remoteAddress, imei);
    }

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

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

    buf.skipBytes(8); // imsi

    int date = buf.readUnsignedShort();

    DateBuilder dateBuilder = new DateBuilder()
            .setDate(2010 + BitUtil.from(date, 12), BitUtil.between(date, 8, 12), BitUtil.to(date, 8))
            .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), 0);

    position.setValid(true);
    position.setLatitude(convertCoordinate(buf.readInt()));
    position.setLongitude(convertCoordinate(buf.readInt()));

    position.setAltitude(UnitsConverter.metersFromFeet(buf.readShort()));
    position.setCourse(buf.readUnsignedShort());
    position.setSpeed(UnitsConverter.knotsFromMph(buf.readUnsignedByte()));

    position.set(Position.KEY_POWER, buf.readUnsignedByte() * 0.1);
    position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());

    dateBuilder.setSecond(buf.readUnsignedByte());
    position.setTime(dateBuilder.getDate());

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

    int index = buf.readUnsignedByte();

    position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
    position.set(Position.KEY_ODOMETER, (long) (buf.readUnsignedInt() * 1609.34));

    if (channel != null && BitUtil.check(flags, 7)) {
        ByteBuf response = Unpooled.buffer();
        response.writeByte(MSG_ACKNOWLEDGEMENT);
        response.writeByte(index);
        response.writeByte(0x00);
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }

    return position;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.skipBytes(2); // header
    int type = buf.readUnsignedByte() & 0x0f;

    if (type == MSG_USERLOG) {

        int header = buf.readUnsignedByte();

        if ((header & 0x40) != 0) {
            sendResponse(channel, buf);//from w w w.  jav a2s.  co  m
        }

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

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

        for (int i = 0; i < (header & 0x0f); i++) {

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

            position.set(Position.KEY_EVENT, buf.readUnsignedByte());
            buf.readUnsignedByte(); // length
            position.set(Position.KEY_FLAGS, buf.readUnsignedShortLE());

            position.setLatitude(convertCoordinate(buf.readIntLE()));
            position.setLongitude(convertCoordinate(buf.readIntLE()));
            position.setAltitude(buf.readShortLE() / 10.0);
            position.setCourse(buf.readUnsignedShortLE());
            position.setSpeed(buf.readUnsignedShortLE() * 0.0539957);

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

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

            positions.add(position);
        }

        return positions;
    }

    return null;
}

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

License:Apache License

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

    if (channel != null) {
        channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(new byte[] { 0x11 }), remoteAddress));
    }/*from   ww w.java2  s .  c om*/

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedInt(); // length

    int idLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex();
    String id = buf.readBytes(idLength).toString(StandardCharsets.US_ASCII);
    buf.readByte();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }

    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(new Date(buf.readUnsignedInt() * 1000));

    buf.readUnsignedInt(); // bit flags

    while (buf.isReadable()) {

        buf.readUnsignedShort(); // block type
        int blockEnd = buf.readInt() + buf.readerIndex();
        buf.readUnsignedByte(); // security attribute
        int dataType = buf.readUnsignedByte();

        int nameLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex();
        String name = buf.readBytes(nameLength).toString(StandardCharsets.US_ASCII);
        buf.readByte();

        if (name.equals("posinfo")) {
            position.setValid(true);
            position.setLongitude(buf.readDoubleLE());
            position.setLatitude(buf.readDoubleLE());
            position.setAltitude(buf.readDoubleLE());
            position.setSpeed(buf.readShort());
            position.setCourse(buf.readShort());
            position.set(Position.KEY_SATELLITES, buf.readByte());
        } else {
            switch (dataType) {
            case 1:
                int len = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex();
                position.set(name, buf.readBytes(len).toString(StandardCharsets.US_ASCII));
                buf.readByte();
                break;
            case 3:
                position.set(name, buf.readInt());
                break;
            case 4:
                position.set(name, buf.readDoubleLE());
                break;
            case 5:
                position.set(name, buf.readLong());
                break;
            default:
                break;
            }
        }

        buf.readerIndex(blockEnd);

    }

    if (position.getLatitude() == 0 && position.getLongitude() == 0) {
        getLastLocation(position, position.getDeviceTime());
    }

    return position;
}

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

License:Apache License

private long readValue(ByteBuf buf, int length, boolean signed) {
    switch (length) {
    case 1:// w  w  w. ja v  a  2  s  .  c om
        return signed ? buf.readByte() : buf.readUnsignedByte();
    case 2:
        return signed ? buf.readShort() : buf.readUnsignedShort();
    case 4:
        return signed ? buf.readInt() : buf.readUnsignedInt();
    default:
        return buf.readLong();
    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedShort(); // data length

    String imei = String.format("%015d", buf.readLong());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }/*  w  w w . j av  a2s .  c  om*/

    int type = buf.readUnsignedByte();

    if (type == MSG_RECORDS || type == MSG_EXTENDED_RECORDS) {

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

        buf.readUnsignedByte(); // records left
        int count = buf.readUnsignedByte();

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

            position.setTime(new Date(buf.readUnsignedInt() * 1000));
            buf.readUnsignedByte(); // timestamp extension

            if (type == MSG_EXTENDED_RECORDS) {
                buf.readUnsignedByte(); // record extension
            }

            buf.readUnsignedByte(); // priority (reserved)

            position.setValid(true);
            position.setLongitude(buf.readInt() / 10000000.0);
            position.setLatitude(buf.readInt() / 10000000.0);
            position.setAltitude(buf.readUnsignedShort() / 10.0);
            position.setCourse(buf.readUnsignedShort() / 100.0);

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

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

            position.set(Position.KEY_HDOP, buf.readUnsignedByte() / 10.0);

            if (type == MSG_EXTENDED_RECORDS) {
                position.set(Position.KEY_EVENT, buf.readUnsignedShort());
            } else {
                position.set(Position.KEY_EVENT, buf.readUnsignedByte());
            }

            // Read 1 byte data
            int cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 1);
            }

            // Read 2 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 2);
            }

            // Read 4 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 4);
            }

            // Read 8 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 8);
            }

            Long driverIdPart1 = (Long) position.getAttributes().remove(Position.PREFIX_IO + 126);
            Long driverIdPart2 = (Long) position.getAttributes().remove(Position.PREFIX_IO + 127);
            if (driverIdPart1 != null && driverIdPart2 != null) {
                ByteBuf driverId = Unpooled.copyLong(driverIdPart1, driverIdPart2);
                position.set(Position.KEY_DRIVER_UNIQUE_ID, driverId.toString(StandardCharsets.US_ASCII));
                driverId.release();
            }

            positions.add(position);
        }

        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(
                    Unpooled.wrappedBuffer(DataConverter.parseHex("0002640113bc")), remoteAddress));
        }

        return positions;

    } else if (type == MSG_DTCS) {

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

        int count = buf.readUnsignedByte();

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

            buf.readUnsignedByte(); // reserved

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

            position.setValid(true);
            position.setLongitude(buf.readInt() / 10000000.0);
            position.setLatitude(buf.readInt() / 10000000.0);

            if (buf.readUnsignedByte() == 2) {
                position.set(Position.KEY_ARCHIVE, true);
            }

            position.set(Position.KEY_DTCS, buf.readSlice(5).toString(StandardCharsets.US_ASCII));

            positions.add(position);
        }

        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(
                    Unpooled.wrappedBuffer(DataConverter.parseHex("00026d01c4a4")), remoteAddress));
        }

        return positions;

    } else {

        return decodeCommandResponse(deviceSession, type, buf);

    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int apiNumber = buf.readUnsignedShort();
    int commandType = buf.readUnsignedByte();
    int messageType = BitUtil.from(buf.readUnsignedByte(), 4);
    long mask = defaultMask;
    if (buf.readUnsignedByte() == 4) {
        mask = buf.readUnsignedInt();
    }/*  www  . j  a v  a  2s .  c  o m*/

    // Binary position report
    if (apiNumber == 5 && commandType == 2 && messageType == 1 && BitUtil.check(mask, 0)) {

        Position position = new Position(getProtocolName());

        if (BitUtil.check(mask, 1)) {
            position.set(Position.KEY_STATUS, buf.readUnsignedInt());
        }

        String id;
        if (BitUtil.check(mask, 23)) {
            id = buf.toString(buf.readerIndex(), 8, StandardCharsets.US_ASCII).trim();
            buf.skipBytes(8);
        } else if (BitUtil.check(mask, 2)) {
            id = buf.toString(buf.readerIndex(), 22, StandardCharsets.US_ASCII).trim();
            buf.skipBytes(22);
        } else {
            LOGGER.warn("No device id field");
            return null;
        }
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());

        if (BitUtil.check(mask, 3)) {
            position.set(Position.PREFIX_IO + 1, buf.readUnsignedShort());
        }

        if (BitUtil.check(mask, 4)) {
            position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
        }

        if (BitUtil.check(mask, 5)) {
            position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort());
        }

        if (BitUtil.check(mask, 7)) {
            buf.readUnsignedByte(); // function category
        }

        DateBuilder dateBuilder = new DateBuilder();

        if (BitUtil.check(mask, 8)) {
            dateBuilder.setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
        }

        if (BitUtil.check(mask, 9)) {
            position.setValid(buf.readUnsignedByte() == 1); // gps status
        }

        if (BitUtil.check(mask, 10)) {
            position.setLatitude(convertCoordinate(buf.readUnsignedInt()));
        }

        if (BitUtil.check(mask, 11)) {
            position.setLongitude(convertCoordinate(buf.readUnsignedInt()));
        }

        if (BitUtil.check(mask, 12)) {
            position.setSpeed(buf.readUnsignedShort() / 10.0);
        }

        if (BitUtil.check(mask, 13)) {
            position.setCourse(buf.readUnsignedShort() / 10.0);
        }

        if (BitUtil.check(mask, 14)) {
            dateBuilder.setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
        }

        position.setTime(dateBuilder.getDate());

        if (BitUtil.check(mask, 15)) {
            position.setAltitude(buf.readMedium());
        }

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

        if (BitUtil.check(mask, 17)) {
            position.set(Position.KEY_BATTERY, buf.readUnsignedShort());
        }

        if (BitUtil.check(mask, 20)) {
            position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt());
        }

        if (BitUtil.check(mask, 21)) {
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
        }

        if (BitUtil.check(mask, 22)) {
            buf.skipBytes(6); // time of message generation
        }

        if (BitUtil.check(mask, 24)) {
            position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
        }

        if (BitUtil.check(mask, 25)) {
            buf.skipBytes(18); // gps overspeed
        }

        if (BitUtil.check(mask, 26)) {
            buf.skipBytes(54); // cell information
        }

        if (BitUtil.check(mask, 28)) {
            position.set(Position.KEY_INDEX, buf.readUnsignedShort());
        }

        return position;
    }

    return null;
}

From source file:org.traccar.protocol.SmokeyProtocolDecoder.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.readUnsignedByte(); // protocol version

    int type = buf.readUnsignedByte();

    ByteBuf id = buf.readSlice(8);/*from ww w  .  j  a  v a2s.  c  o  m*/
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ByteBufUtil.hexDump(id));
    if (deviceSession == null) {
        return null;
    }

    if (type == MSG_DATE_RECORD) {

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

        position.set(Position.KEY_VERSION_FW, buf.readUnsignedShort());

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

        DateBuilder dateBuilder = new DateBuilder().setDate(2000, 1, 1).addSeconds(buf.readUnsignedInt());

        getLastLocation(position, dateBuilder.getDate());

        int index = buf.readUnsignedByte();
        position.set(Position.KEY_INDEX, index);

        int report = buf.readUnsignedShort();

        buf.readUnsignedShort(); // length

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

        Network network = new Network();

        if (report != 0x0203) {

            int count = 1;
            if (report != 0x0200) {
                count = buf.readUnsignedByte();
            }

            for (int i = 0; i < count; i++) {
                int mcc = buf.readUnsignedShort();
                int mnc = buf.readUnsignedShort();
                int lac = buf.readUnsignedShort();
                int cid = buf.readUnsignedShort();
                if (i == 0) {
                    buf.readByte(); // timing advance
                }
                int rssi = buf.readByte();
                network.addCellTower(CellTower.from(mcc, mnc, lac, cid, rssi));
            }

        }

        if (report == 0x0202 || report == 0x0203) {

            int count = buf.readUnsignedByte();

            for (int i = 0; i < count; i++) {
                buf.readerIndex(buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0) + 1); // ssid

                String mac = String.format("%02x:%02x:%02x:%02x:%02x:%02x", buf.readUnsignedByte(),
                        buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(),
                        buf.readUnsignedByte(), buf.readUnsignedByte());

                network.addWifiAccessPoint(WifiAccessPoint.from(mac, buf.readByte()));
            }

        }

        position.setNetwork(network);

        sendResponse(channel, remoteAddress, id, index, report);

        return position;

    }

    return null;
}

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

License:Apache License

private Position decodePosition(Channel channel, DeviceSession deviceSession, ByteBuf buf, short header,
        int type, int index, ByteBuf imei) {

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

    position.set(Position.KEY_INDEX, index);

    buf.readUnsignedShort(); // acc on interval
    buf.readUnsignedShort(); // acc off interval
    buf.readUnsignedByte(); // angle compensation
    buf.readUnsignedShort(); // distance compensation

    position.set(Position.KEY_RSSI, BitUtil.to(buf.readUnsignedShort(), 7));

    int status = buf.readUnsignedByte();
    position.set(Position.KEY_SATELLITES, BitUtil.to(status, 5));

    buf.readUnsignedByte(); // gsensor manager status
    buf.readUnsignedByte(); // other flags
    buf.readUnsignedByte(); // heartbeat
    buf.readUnsignedByte(); // relay status
    buf.readUnsignedShort(); // drag alarm setting

    int io = buf.readUnsignedShort();
    position.set(Position.KEY_IGNITION, BitUtil.check(io, 14));
    position.set("ac", BitUtil.check(io, 13));
    for (int i = 0; i <= 2; i++) {
        position.set(Position.PREFIX_OUT + (i + 1), BitUtil.check(io, 7 + i));
    }//from w ww.  j a  va2s  .co  m

    position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
    position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort());

    int alarm = buf.readUnsignedByte();
    position.set(Position.KEY_ALARM, decodeAlarm(alarm));

    buf.readUnsignedByte(); // reserved

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

    int battery = BcdUtil.readInteger(buf, 2);
    if (battery == 0) {
        battery = 100;
    }
    position.set(Position.KEY_BATTERY, battery);

    DateBuilder dateBuilder = new DateBuilder().setYear(BcdUtil.readInteger(buf, 2))
            .setMonth(BcdUtil.readInteger(buf, 2)).setDay(BcdUtil.readInteger(buf, 2))
            .setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2))
            .setSecond(BcdUtil.readInteger(buf, 2));

    if (BitUtil.check(status, 6)) {

        position.setValid(!BitUtil.check(status, 7));
        position.setTime(dateBuilder.getDate());
        position.setAltitude(buf.readFloatLE());
        position.setLongitude(buf.readFloatLE());
        position.setLatitude(buf.readFloatLE());
        position.setSpeed(UnitsConverter.knotsFromKph(BcdUtil.readInteger(buf, 4) * 0.1));
        position.setCourse(buf.readUnsignedShort());

    } else {

        getLastLocation(position, dateBuilder.getDate());

        int mcc = buf.readUnsignedShortLE();
        int mnc = buf.readUnsignedShortLE();

        if (mcc != 0xffff && mnc != 0xffff) {
            Network network = new Network();
            for (int i = 0; i < 3; i++) {
                network.addCellTower(
                        CellTower.from(mcc, mnc, buf.readUnsignedShortLE(), buf.readUnsignedShortLE()));
            }
            position.setNetwork(network);
        }

    }

    if (buf.readableBytes() >= 2) {
        position.set(Position.KEY_POWER, BcdUtil.readInteger(buf, 4) * 0.01);
    }

    sendResponse(channel, header, type, index, imei, alarm);

    return position;
}

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   ww w . ja  va  2 s.c o  m

    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)));
        }
    }
}