Example usage for io.netty.buffer ByteBuf readUnsignedShortLE

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

Introduction

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

Prototype

public abstract int readUnsignedShortLE();

Source Link

Document

Gets an unsigned 16-bit short integer at the current readerIndex in the Little Endian Byte Order and increases the readerIndex by 2 in this buffer.

Usage

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

License:Apache License

private Position decodeData(Channel channel, SocketAddress remoteAddress, ByteBuf buf, int type) {

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

    if (BitUtil.to(type, 2) == 0) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
        position.set(Position.KEY_INDEX, buf.readUnsignedShortLE());

        int status = buf.readUnsignedShortLE();
        position.set(Position.KEY_STATUS, status);
        position.setValid(!BitUtil.check(status, 5));
        position.setLatitude(buf.readFloatLE());
        position.setLongitude(buf.readFloatLE());
        position.setCourse(buf.readUnsignedShortLE() * 0.1);
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE() * 0.1));

        position.set(Position.KEY_ACCELERATION, buf.readUnsignedByte() * 0.1);
        position.setAltitude(buf.readShortLE());
        position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
        position.set(Position.KEY_SATELLITES, buf.readUnsignedByte() & 0x0f);

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

        position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.001);
        position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.001);

        if (BitUtil.check(type, 2)) {
            buf.readUnsignedByte(); // vib
            buf.readUnsignedByte(); // vib_count

            int out = buf.readUnsignedByte();
            for (int i = 0; i <= 3; i++) {
                position.set(Position.PREFIX_OUT + (i + 1), BitUtil.check(out, i) ? 1 : 0);
            }

            buf.readUnsignedByte(); // in_alarm
        }

        if (BitUtil.check(type, 3)) {
            for (int i = 1; i <= 6; i++) {
                position.set(Position.PREFIX_ADC + i, buf.readUnsignedShortLE() * 0.001);
            }
        }

        if (BitUtil.check(type, 4)) {
            for (int i = 1; i <= 2; i++) {
                position.set(Position.PREFIX_COUNT + i, buf.readUnsignedIntLE());
            }
        }

        if (BitUtil.check(type, 5)) {
            for (int i = 1; i <= 3; i++) {
                buf.readUnsignedShortLE(); // fuel level
            }
            for (int i = 1; i <= 3; i++) {
                position.set(Position.PREFIX_TEMP + i, buf.readUnsignedByte());
            }
        }

        if (BitUtil.check(type, 6)) {
            int endIndex = buf.readerIndex() + buf.readUnsignedByte();
            while (buf.readerIndex() < endIndex) {
                int mask = buf.readUnsignedByte();
                long value;
                switch (BitUtil.from(mask, 6)) {
                case 3:
                    value = buf.readLongLE();
                    break;
                case 2:
                    value = buf.readUnsignedIntLE();
                    break;
                case 1:
                    value = buf.readUnsignedShortLE();
                    break;
                default:
                    value = buf.readUnsignedByte();
                    break;
                }
                int index = BitUtil.to(mask, 6);
                switch (index) {
                case 1:
                    position.set(Position.PREFIX_TEMP + 1, value);
                    break;
                case 2:
                    position.set("humidity", value);
                    break;
                case 3:
                    position.set("illumination", value);
                    break;
                case 4:
                    position.set(Position.KEY_BATTERY, value);
                    break;
                default:
                    position.set("can" + index, value);
                    break;
                }
            }
        }

        if (BitUtil.check(type, 7)) {
            position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
        }

        return position;
    }

    return null;
}

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

License:Apache License

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

    buf.readUnsignedShortLE(); // device id

    int size = buf.readUnsignedByte();
    if (size != CMD_RESPONSE_SIZE) {
        int type = buf.readUnsignedByte();
        if (type == MSG_IMEI) {
            getDeviceSession(channel, remoteAddress, buf.readSlice(15).toString(StandardCharsets.UTF_8));
        } else {/*w w w  .j ava  2 s  .  c o m*/
            return decodeData(channel, remoteAddress, buf, type);
        }
    } else {
        return parseCommandResponse(channel, remoteAddress, buf);
    }

    return null;
}

From source file:org.traccar.protocol.AnytrekProtocolDecoder.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.readUnsignedShortLE(); // size
    int type = buf.readUnsignedByte();

    String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(2);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }//  w  w w.j  a va 2  s  .  co  m

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

    position.set(Position.KEY_VERSION_FW, buf.readUnsignedShortLE());
    position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.01);
    position.set(Position.KEY_RSSI, buf.readUnsignedByte());

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

    position.set(Position.KEY_SATELLITES, BitUtil.to(buf.readUnsignedByte(), 4));

    double latitude = buf.readUnsignedIntLE() / 1800000.0;
    double longitude = buf.readUnsignedIntLE() / 1800000.0;
    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));

    int flags = buf.readUnsignedShortLE();
    position.setCourse(BitUtil.to(flags, 10));
    position.setValid(BitUtil.check(flags, 12));

    if (!BitUtil.check(flags, 10)) {
        latitude = -latitude;
    }
    if (BitUtil.check(flags, 11)) {
        longitude = -longitude;
    }

    position.setLatitude(latitude);
    position.setLongitude(longitude);

    buf.readUnsignedIntLE(); // info index
    buf.readUnsignedIntLE(); // setting index

    flags = buf.readUnsignedByte();
    position.set(Position.KEY_CHARGE, BitUtil.check(flags, 0));
    position.set(Position.KEY_IGNITION, BitUtil.check(flags, 1));
    position.set(Position.KEY_ALARM, BitUtil.check(flags, 4) ? Position.ALARM_GENERAL : null);

    buf.readUnsignedShortLE(); // charge current

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

    sendResponse(channel, remoteAddress, type);

    return position;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;
    int type = buf.readUnsignedShortLE();
    boolean alarm = (type & 0x8000) != 0;
    type = type & 0x7FFF;/*from w w w  .  j a  v a2  s.  c  om*/
    buf.readUnsignedShortLE(); // length

    if (alarm) {
        sendSimpleMessage(channel, MSG_ACK_ALARM);
    }

    if (type == MSG_TRACKER_ID) {
        return null; // unsupported authentication type
    }

    if (type == MSG_TRACKER_ID_EXT) {

        buf.readUnsignedIntLE(); // id
        int length = buf.readUnsignedShortLE();
        buf.skipBytes(length);
        length = buf.readUnsignedShortLE();
        getDeviceSession(channel, remoteAddress, buf.readSlice(length).toString(StandardCharsets.US_ASCII));

    } else if (type == MSG_LAST_LOG_INDEX) {

        long index = buf.readUnsignedIntLE();
        if (index > 0) {
            newIndex = index;
            requestArchive(channel);
        }

    } else if (type == MSG_CURRENT_GPS_DATA || type == MSG_STATE_FULL_INFO_T104 || type == MSG_LOG_RECORDS) {

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

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

        int recordCount = 1;
        if (type == MSG_LOG_RECORDS) {
            recordCount = buf.readUnsignedShortLE();
        }

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

            int subtype = type;
            if (type == MSG_LOG_RECORDS) {
                position.set(Position.KEY_ARCHIVE, true);
                lastIndex = buf.readUnsignedIntLE() + 1;
                position.set(Position.KEY_INDEX, lastIndex);

                subtype = buf.readUnsignedShortLE();
                if (subtype != MSG_CURRENT_GPS_DATA && subtype != MSG_STATE_FULL_INFO_T104) {
                    buf.skipBytes(buf.readUnsignedShortLE());
                    continue;
                }
                buf.readUnsignedShortLE(); // length
            }

            position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
            position.setLatitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF);
            position.setLongitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF);

            if (subtype == MSG_STATE_FULL_INFO_T104) {
                int speed = buf.readUnsignedByte();
                position.setValid(speed != 255);
                position.setSpeed(UnitsConverter.knotsFromKph(speed));
                position.set(Position.KEY_HDOP, buf.readByte());
            } else {
                int speed = buf.readShortLE();
                position.setValid(speed != -1);
                position.setSpeed(UnitsConverter.knotsFromKph(speed * 0.01));
            }

            position.setCourse(buf.readShortLE() * 0.01);
            position.setAltitude(buf.readShortLE());

            if (subtype == MSG_STATE_FULL_INFO_T104) {

                position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
                position.set(Position.KEY_RSSI, buf.readUnsignedByte());
                position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());
                position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
                position.set(Position.KEY_INPUT, buf.readUnsignedByte());
                position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());

                for (int i = 1; i <= 8; i++) {
                    position.set(Position.PREFIX_ADC + i, buf.readUnsignedShortLE());
                }

                position.set(Position.PREFIX_COUNT + 1, buf.readUnsignedIntLE());
                position.set(Position.PREFIX_COUNT + 2, buf.readUnsignedIntLE());
                position.set(Position.PREFIX_COUNT + 3, buf.readUnsignedIntLE());
            }

            positions.add(position);
        }

        buf.readUnsignedIntLE(); // crc

        if (type == MSG_LOG_RECORDS) {
            requestArchive(channel);
        } else {
            sendSimpleMessage(channel, MSG_REQUEST_LAST_LOG_INDEX);
        }

        return positions;
    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    if (buf.getUnsignedByte(buf.readerIndex()) == 0x01) {
        buf.readUnsignedByte(); // codec id
    }/* w ww  . j  ava2 s.  co m*/

    int type = buf.readUnsignedByte();
    buf.readUnsignedMediumLE(); // length
    buf.skipBytes(BLOCK_LENGTH - 1 - 3);

    if (type == MSG_DEVICE_ID) {

        String imei = buf.readSlice(15).toString(StandardCharsets.US_ASCII);
        if (getDeviceSession(channel, remoteAddress, imei) != null) {

            byte[] iv = new byte[BLOCK_LENGTH];
            buf.readBytes(iv);
            IvParameterSpec ivSpec = new IvParameterSpec(iv);

            SecretKeySpec keySpec = new SecretKeySpec(
                    DataConverter.parseHex("000102030405060708090a0b0c0d0e0f"), "AES");

            cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

            byte[] data = new byte[BLOCK_LENGTH];
            buf.readBytes(data);
            cipher.update(data);

        }

    } else if (type == MSG_TRACK_RESPONSE) {

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

        if (buf.capacity() <= BLOCK_LENGTH) {
            return null; // empty message
        }

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

        byte[] data = new byte[buf.capacity() - BLOCK_LENGTH];
        buf.readBytes(data);
        buf = Unpooled.wrappedBuffer(cipher.update(data));
        try {
            while (buf.readableBytes() >= 63) {

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

                buf.readUnsignedShortLE(); // index
                buf.readUnsignedShortLE(); // reserved

                position.setValid(true);

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

                position.setLatitude(buf.readFloatLE());
                position.setLongitude(buf.readFloatLE());
                position.setAltitude(buf.readFloatLE());
                position.setSpeed(UnitsConverter.knotsFromKph(buf.readFloatLE()));
                position.setCourse(buf.readFloatLE());

                buf.readUnsignedIntLE(); // geozone event
                buf.readUnsignedIntLE(); // io events
                buf.readUnsignedIntLE(); // geozone value
                buf.readUnsignedIntLE(); // io values
                buf.readUnsignedShortLE(); // operator

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

                position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.001);

                buf.readUnsignedShortLE(); // cid
                position.set(Position.KEY_RSSI, buf.readUnsignedByte());
                buf.readUnsignedByte(); // current profile

                position.set(Position.KEY_BATTERY, buf.readUnsignedByte());
                position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedByte());
                position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());

                positions.add(position);

            }
        } finally {
            buf.release();
        }

        return positions;

    }

    if (type == MSG_DEVICE_ID) {
        sendRequest(channel);
    }

    return null;
}

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

License:Apache License

private Position decodeTelemetry(Channel channel, SocketAddress remoteAddress, DeviceSession deviceSession,
        ByteBuf buf) {

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

    position.setTime(new Date(1009843200000L + buf.readUnsignedIntLE() * 1000)); // seconds since 2002
    position.setLatitude(buf.readIntLE() * 0.0000001);
    position.setLongitude(buf.readIntLE() * 0.0000001);

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

    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE()));
    buf.readUnsignedShortLE(); // max speed

    position.set(Position.KEY_INPUT, buf.readUnsignedShortLE());
    buf.readUnsignedIntLE(); // di 3 count
    buf.readUnsignedIntLE(); // di 4 count

    for (int i = 0; i < 5; i++) {
        position.set(Position.PREFIX_ADC + (i + 1), buf.readUnsignedShortLE());
    }/*from  w ww . j  a  v a  2  s  .  c  o m*/

    position.setCourse(buf.readUnsignedShortLE());

    position.set(Position.KEY_STATUS, buf.readUnsignedShortLE());
    position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());
    position.set(Position.KEY_DRIVER_UNIQUE_ID, buf.readLongLE());

    int index = buf.readUnsignedShortLE();

    buf.readUnsignedShortLE(); // checksum

    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeInt(0xF1F1F1F1); // sync
        response.writeByte(MSG_TELEMETRY_CONFIRM);
        response.writeShortLE(2); // length
        response.writeShortLE(index);
        response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer()));
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }

    return position;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.skipBytes(4); // sync
    int type = buf.readUnsignedByte();
    buf.readUnsignedShortLE(); // length

    switch (type) {
    case MSG_LOGIN_REQUEST:
        String imei = ByteBufUtil.hexDump(buf.readBytes(8));
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
        if (deviceSession == null) {
            return null;
        }//from w w w .  j  a  va 2  s.  c  o  m
        int fuelConst = buf.readUnsignedShortLE();
        int tripConst = buf.readUnsignedShortLE();
        if (channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeInt(0xF1F1F1F1); // sync
            response.writeByte(MSG_LOGIN_CONFIRM);
            response.writeShortLE(12); // length
            response.writeBytes(ByteBufUtil.decodeHexDump(imei));
            response.writeShortLE(fuelConst);
            response.writeShortLE(tripConst);
            response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer()));
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
        return null;
    case MSG_TELEMETRY_1:
    case MSG_TELEMETRY_2:
    case MSG_TELEMETRY_3:
        deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        return decodeTelemetry(channel, remoteAddress, deviceSession, buf);
    default:
        return null;
    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    String imei = String.format("%015d", buf.readLongLE());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }//from  w  w w  . ja va 2 s .  c o  m

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

    while (buf.readableBytes() > 1) {

        int dataEnd = buf.readUnsignedShortLE() + buf.readerIndex();
        int type = buf.readUnsignedByte();

        if (type != MSG_ASYNC_STACK && type != MSG_TIME_TRIGGERED) {
            return null;
        }

        int confirmKey = buf.readUnsignedByte() & 0x7F;

        while (buf.readerIndex() < dataEnd) {

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

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

            long time = buf.readUnsignedIntLE();
            if ((time & 0x0f) == DATA_TYPE) {

                time = time >> 4 << 1;
                time += 0x47798280; // 01/01/2008
                position.setTime(new Date(time * 1000));

                // Read masks
                int mask;
                List<Integer> masks = new LinkedList<>();
                do {
                    mask = buf.readUnsignedShortLE();
                    masks.add(mask);
                } while (BitUtil.check(mask, 15));

                mask = masks.get(0);

                if (BitUtil.check(mask, 0)) {
                    position.setValid(true);
                    position.setLongitude(buf.readFloatLE());
                    position.setLatitude(buf.readFloatLE());
                    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));

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

                    position.setCourse(buf.readUnsignedByte() * 2);
                    position.setAltitude(buf.readUnsignedShortLE());

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

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

                for (int i = 1; i <= 8; i++) {
                    if (BitUtil.check(mask, i + 1)) {
                        position.set(Position.PREFIX_ADC + i, buf.readUnsignedShortLE());
                    }
                }

                if (BitUtil.check(mask, 10)) {
                    buf.skipBytes(4);
                }
                if (BitUtil.check(mask, 11)) {
                    buf.skipBytes(4);
                }
                if (BitUtil.check(mask, 12)) {
                    buf.skipBytes(2);
                }
                if (BitUtil.check(mask, 13)) {
                    buf.skipBytes(2);
                }

                if (BitUtil.check(mask, 14)) {
                    position.setNetwork(new Network(CellTower.from(buf.readUnsignedShortLE(),
                            buf.readUnsignedByte(), buf.readUnsignedShortLE(), buf.readUnsignedShortLE(),
                            buf.readUnsignedByte())));
                    buf.readUnsignedByte();
                }

                if (BitUtil.check(mask, 0)) {
                    positions.add(position);
                }
            }

            buf.readerIndex(structEnd);
        }

        // Send response
        if (type == MSG_ASYNC_STACK && channel != null) {
            ByteBuf response = Unpooled.buffer(8 + 2 + 2 + 1);
            response.writeLongLE(Long.parseLong(imei));
            response.writeShortLE(2);
            response.writeByte(MSG_STACK_COFIRM);
            response.writeByte(confirmKey);

            int checksum = 0;
            for (int i = 0; i < response.writerIndex(); i++) {
                checksum += response.getUnsignedByte(i);
            }
            response.writeByte(checksum);

            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
    }

    return positions;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedByte(); // header
    int length = (buf.readUnsignedShortLE() & 0x7fff) + 3;

    List<Position> positions = new LinkedList<>();
    Set<Integer> tags = new HashSet<>();
    boolean hasLocation = false;
    Position position = new Position(getProtocolName());

    while (buf.readerIndex() < length) {

        // Check if new message started
        int tag = buf.readUnsignedByte();
        if (tags.contains(tag)) {
            if (hasLocation && position.getFixTime() != null) {
                positions.add(position);
            }/*from w ww .  jav  a 2 s .c o  m*/
            tags.clear();
            hasLocation = false;
            position = new Position(getProtocolName());
        }
        tags.add(tag);

        switch (tag) {

        case TAG_IMEI:
            getDeviceSession(channel, remoteAddress, buf.readSlice(15).toString(StandardCharsets.US_ASCII));
            break;

        case TAG_DATE:
            position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
            break;

        case TAG_COORDINATES:
            hasLocation = true;
            position.setValid((buf.readUnsignedByte() & 0xf0) == 0x00);
            position.setLatitude(buf.readIntLE() / 1000000.0);
            position.setLongitude(buf.readIntLE() / 1000000.0);
            break;

        case TAG_SPEED_COURSE:
            position.setSpeed(buf.readUnsignedShortLE() * 0.0539957);
            position.setCourse(buf.readUnsignedShortLE() * 0.1);
            break;

        case TAG_ALTITUDE:
            position.setAltitude(buf.readShortLE());
            break;

        case TAG_STATUS:
            int status = buf.readUnsignedShortLE();
            position.set(Position.KEY_IGNITION, BitUtil.check(status, 9));
            if (BitUtil.check(status, 15)) {
                position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
            }
            position.set(Position.KEY_CHARGE, BitUtil.check(status, 2));
            break;

        case TAG_DIGITAL_INPUTS:
            int input = buf.readUnsignedShortLE();
            for (int i = 0; i < 16; i++) {
                position.set(Position.PREFIX_IO + (i + 1), BitUtil.check(input, i));
            }
            break;

        case TAG_DIGITAL_OUTPUTS:
            int output = buf.readUnsignedShortLE();
            for (int i = 0; i < 16; i++) {
                position.set(Position.PREFIX_IO + (i + 17), BitUtil.check(output, i));
            }
            break;

        case TAG_INPUT_VOLTAGE1:
            position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_INPUT_VOLTAGE2:
            position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_INPUT_VOLTAGE3:
            position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_INPUT_VOLTAGE4:
            position.set(Position.PREFIX_ADC + 4, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_XT1:
        case TAG_XT2:
        case TAG_XT3:
            buf.skipBytes(16);
            break;

        default:
            break;

        }
    }

    if (hasLocation && position.getFixTime() != null) {
        positions.add(position);
    }

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

    sendReply(channel, buf.readUnsignedShortLE());

    for (Position p : positions) {
        p.setDeviceId(deviceSession.getDeviceId());
    }

    if (positions.isEmpty()) {
        return null;
    }

    return positions;
}

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

License:Apache License

private Position readPosition(DeviceSession deviceSession, ByteBuf buf) {

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

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

    double lat = buf.readUnsignedIntLE() / 3600000.0;
    double lon = buf.readUnsignedIntLE() / 3600000.0;
    position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedShortLE()));
    position.setCourse(buf.readUnsignedShortLE() * 0.1);

    int flags = buf.readUnsignedByte();
    if ((flags & 0x02) == 0) {
        lat = -lat;/*w w w. ja va 2s  .com*/
    }
    if ((flags & 0x01) == 0) {
        lon = -lon;
    }
    position.setLatitude(lat);
    position.setLongitude(lon);
    position.setValid((flags & 0x0C) > 0);
    position.set(Position.KEY_SATELLITES, flags >> 4);

    return position;
}