Example usage for io.netty.buffer ByteBuf readFloatLE

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

Introduction

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

Prototype

public float readFloatLE() 

Source Link

Document

Gets a 32-bit floating point number at the current readerIndex in Little Endian Byte Order and increases the readerIndex by 4 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;
    }//from w w  w .jav a  2s. c  o m

    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.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
    }/*www  .j av  a 2  s  .  com*/

    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.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  ww.  jav a  2  s .  co 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.NavisProtocolDecoder.java

License:Apache License

private Position parseNtcbPosition(DeviceSession deviceSession, ByteBuf buf) {
    Position position = new Position(getProtocolName());

    position.setDeviceId(deviceSession.getDeviceId());

    int format;/*from   ww w.  j av  a 2 s  . c o  m*/
    if (buf.getUnsignedByte(buf.readerIndex()) == 0) {
        format = buf.readUnsignedShortLE();
    } else {
        format = buf.readUnsignedByte();
    }
    position.set("format", format);

    position.set(Position.KEY_INDEX, buf.readUnsignedIntLE());
    position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());

    buf.skipBytes(6); // event time

    short armedStatus = buf.readUnsignedByte();
    if (isFormat(format, F10, F20, F30, F40, F50, F51, F52)) {
        position.set(Position.KEY_ARMED, BitUtil.to(armedStatus, 7));
        if (BitUtil.check(armedStatus, 7)) {
            position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
        }
    } else if (isFormat(format, F60)) {
        position.set(Position.KEY_ARMED, BitUtil.check(armedStatus, 0));
        if (BitUtil.check(armedStatus, 1)) {
            position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
        }
    }

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

    if (isFormat(format, F10, F20, F30)) {
        int output = buf.readUnsignedShortLE();
        position.set(Position.KEY_OUTPUT, output);
        for (int i = 0; i < 16; i++) {
            position.set(Position.PREFIX_OUT + (i + 1), BitUtil.check(output, i));
        }
    } else if (isFormat(format, F50, F51, F52)) {
        short extField = buf.readUnsignedByte();
        position.set(Position.KEY_OUTPUT, BitUtil.to(extField, 2));
        position.set(Position.PREFIX_OUT + 1, BitUtil.check(extField, 0));
        position.set(Position.PREFIX_OUT + 2, BitUtil.check(extField, 1));
        position.set(Position.KEY_SATELLITES, BitUtil.from(extField, 2));
    } else if (isFormat(format, F40, F60)) {
        short output = buf.readUnsignedByte();
        position.set(Position.KEY_OUTPUT, BitUtil.to(output, 4));
        for (int i = 0; i < 4; i++) {
            position.set(Position.PREFIX_OUT + (i + 1), BitUtil.check(output, i));
        }
    }

    if (isFormat(format, F10, F20, F30, F40)) {
        int input = buf.readUnsignedShortLE();
        position.set(Position.KEY_INPUT, input);
        if (!isFormat(format, F40)) {
            for (int i = 0; i < 16; i++) {
                position.set(Position.PREFIX_IN + (i + 1), BitUtil.check(input, i));
            }
        } else {
            position.set(Position.PREFIX_IN + 1, BitUtil.check(input, 0));
            position.set(Position.PREFIX_IN + 2, BitUtil.check(input, 1));
            position.set(Position.PREFIX_IN + 3, BitUtil.check(input, 2));
            position.set(Position.PREFIX_IN + 4, BitUtil.check(input, 3));
            position.set(Position.PREFIX_IN + 5, BitUtil.between(input, 4, 7));
            position.set(Position.PREFIX_IN + 6, BitUtil.between(input, 7, 10));
            position.set(Position.PREFIX_IN + 7, BitUtil.between(input, 10, 12));
            position.set(Position.PREFIX_IN + 8, BitUtil.between(input, 12, 14));
        }
    } else if (isFormat(format, F50, F51, F52, F60)) {
        short input = buf.readUnsignedByte();
        position.set(Position.KEY_INPUT, input);
        for (int i = 0; i < 8; i++) {
            position.set(Position.PREFIX_IN + (i + 1), BitUtil.check(input, i));
        }
    }

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

    if (isFormat(format, F10, F20, F30)) {
        position.set(Position.PREFIX_TEMP + 1, buf.readShortLE());
    }

    if (isFormat(format, F10, F20, F50, F51, F52, F60)) {
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE());
        position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShortLE());
    }
    if (isFormat(format, F60)) {
        position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShortLE());
    }

    // Impulse counters
    if (isFormat(format, F20, F50, F51, F52, F60)) {
        buf.readUnsignedIntLE();
        buf.readUnsignedIntLE();
    }

    if (isFormat(format, F60)) {
        // Fuel
        buf.readUnsignedShortLE();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();

        position.set(Position.PREFIX_TEMP + 1, buf.readByte());
        position.set(Position.PREFIX_TEMP + 2, buf.readByte());
        position.set(Position.PREFIX_TEMP + 3, buf.readByte());
        position.set(Position.PREFIX_TEMP + 4, buf.readByte());
        position.set(Position.KEY_AXLE_WEIGHT, buf.readIntLE());
        position.set(Position.KEY_RPM, buf.readUnsignedShortLE());
    }

    if (isFormat(format, F20, F50, F51, F52, F60)) {
        int navSensorState = buf.readUnsignedByte();
        position.setValid(BitUtil.check(navSensorState, 1));
        if (isFormat(format, F60)) {
            position.set(Position.KEY_SATELLITES, BitUtil.from(navSensorState, 2));
        }

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

        if (isFormat(format, F60)) {
            position.setLatitude(buf.readIntLE() / 600000.0);
            position.setLongitude(buf.readIntLE() / 600000.0);
            position.setAltitude(buf.readIntLE() * 0.1);
        } else {
            position.setLatitude(buf.readFloatLE() / Math.PI * 180);
            position.setLongitude(buf.readFloatLE() / Math.PI * 180);
        }

        position.setSpeed(UnitsConverter.knotsFromKph(buf.readFloatLE()));
        position.setCourse(buf.readUnsignedShortLE());

        position.set(Position.KEY_ODOMETER, buf.readFloatLE() * 1000);
        position.set(Position.KEY_DISTANCE, buf.readFloatLE() * 1000);

        // Segment times
        buf.readUnsignedShortLE();
        buf.readUnsignedShortLE();
    }

    // Other
    if (isFormat(format, F51, F52)) {
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
    }

    // Four temperature sensors
    if (isFormat(format, F40, F52)) {
        position.set(Position.PREFIX_TEMP + 1, buf.readByte());
        position.set(Position.PREFIX_TEMP + 2, buf.readByte());
        position.set(Position.PREFIX_TEMP + 3, buf.readByte());
        position.set(Position.PREFIX_TEMP + 4, buf.readByte());
    }

    return position;
}

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

License:Apache License

private Position parseFlexPosition(DeviceSession deviceSession, ByteBuf buf) {

    Position position = new Position(getProtocolName());

    position.setDeviceId(deviceSession.getDeviceId());

    int status = 0;
    short input = 0;
    short output = 0;

    for (int i = 0; i < flexBitFieldSize; i++) {
        if (!checkFlexBitfield(i)) {
            continue;
        }//from w w w .  j  av a2 s .  c  o m

        switch (i) {
        case 0:
            position.set(Position.KEY_INDEX, buf.readUnsignedIntLE());
            break;
        case 1:
            position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());
            break;
        case 3:
            short armedStatus = buf.readUnsignedByte();
            position.set(Position.KEY_ARMED, BitUtil.check(armedStatus, 0));
            if (BitUtil.check(armedStatus, 1)) {
                position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
            }
            break;
        case 4:
            status = buf.readUnsignedByte();
            position.set(Position.KEY_STATUS, status);
            break;
        case 5:
            int status2 = buf.readUnsignedByte();
            position.set(Position.KEY_STATUS, (short) (BitUtil.to(status, 8) | (status2 << 8)));
            break;
        case 6:
            position.set(Position.KEY_RSSI, buf.readUnsignedByte());
            break;
        case 7:
            int navSensorState = buf.readUnsignedByte();
            position.setValid(BitUtil.check(navSensorState, 1));
            position.set(Position.KEY_SATELLITES, BitUtil.from(navSensorState, 2));
            break;
        case 8:
            position.setTime(new DateBuilder(new Date(buf.readUnsignedIntLE() * 1000)).getDate());
            break;
        case 9:
            position.setLatitude(buf.readIntLE() / 600000.0);
            break;
        case 10:
            position.setLongitude(buf.readIntLE() / 600000.0);
            break;
        case 11:
            position.setAltitude(buf.readIntLE() * 0.1);
            break;
        case 12:
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readFloatLE()));
            break;
        case 13:
            position.setCourse(buf.readUnsignedShortLE());
            break;
        case 14:
            position.set(Position.KEY_ODOMETER, buf.readFloatLE() * 1000);
            break;
        case 15:
            position.set(Position.KEY_DISTANCE, buf.readFloatLE() * 1000);
            break;
        case 18:
            position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.001);
            break;
        case 19:
            position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.001);
            break;
        case 20:
        case 21:
        case 22:
        case 23:
        case 24:
        case 25:
        case 26:
        case 27:
            position.set(Position.PREFIX_ADC + (i - 19), buf.readUnsignedShortLE());
            break;
        case 28:
            input = buf.readUnsignedByte();
            position.set(Position.KEY_INPUT, input);
            for (int k = 0; k < 8; k++) {
                position.set(Position.PREFIX_IN + (k + 1), BitUtil.check(input, k));
            }
            break;
        case 29:
            short input2 = buf.readUnsignedByte();
            position.set(Position.KEY_INPUT, (short) (BitUtil.to(input, 8) | (input2 << 8)));
            for (int k = 0; k < 8; k++) {
                position.set(Position.PREFIX_IN + (k + 9), BitUtil.check(input2, k));
            }
            break;
        case 30:
            output = buf.readUnsignedByte();
            position.set(Position.KEY_OUTPUT, output);
            for (int k = 0; k < 8; k++) {
                position.set(Position.PREFIX_OUT + (k + 1), BitUtil.check(output, k));
            }
            break;
        case 31:
            short output2 = buf.readUnsignedByte();
            position.set(Position.KEY_OUTPUT, (short) (BitUtil.to(output, 8) | (output2 << 8)));
            for (int k = 0; k < 8; k++) {
                position.set(Position.PREFIX_OUT + (k + 9), BitUtil.check(output2, k));
            }
            break;
        case 36:
            position.set(Position.KEY_HOURS, buf.readUnsignedIntLE() * 1000);
            break;
        case 44:
        case 45:
        case 46:
        case 47:
        case 48:
        case 49:
        case 50:
        case 51:
            position.set(Position.PREFIX_TEMP + (i - 43), buf.readByte());
            break;
        case 68:
            position.set("can-speed", buf.readUnsignedByte());
            break;
        // FLEX 2.0
        case 69:
            int satVisible = 0;
            for (int k = 0; k < 8; k++) {
                satVisible += buf.readUnsignedByte();
            }
            position.set(Position.KEY_SATELLITES_VISIBLE, satVisible);
            break;
        case 70:
            position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
            position.set(Position.KEY_PDOP, buf.readUnsignedByte() * 0.1);
            break;
        default:
            if (i < FLEX_FIELDS_SIZES.length) {
                buf.skipBytes(FLEX_FIELDS_SIZES[i]);
            }
            break;
        }
    }

    return position;
}

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

License:Apache License

private Position parseFlex20Position(DeviceSession deviceSession, ByteBuf buf) {

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

    int length = buf.readUnsignedShort();
    if (length <= buf.readableBytes() && buf.readUnsignedByte() == 0x0A) {

        buf.readUnsignedByte(); // length of static part

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

        position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());
        buf.readUnsignedInt(); // event time

        int navSensorState = buf.readUnsignedByte();
        position.setValid(BitUtil.check(navSensorState, 1));
        position.set(Position.KEY_SATELLITES, BitUtil.from(navSensorState, 2));

        position.setTime(new DateBuilder(new Date(buf.readUnsignedIntLE() * 1000)).getDate());
        position.setLatitude(buf.readIntLE() / 600000.0);
        position.setLongitude(buf.readIntLE() / 600000.0);
        position.setAltitude(buf.readIntLE() * 0.1);
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readFloatLE()));
        position.setCourse(buf.readUnsignedShortLE());
        position.set(Position.KEY_ODOMETER, buf.readFloatLE() * 1000);

        buf.skipBytes(length - buf.readerIndex() - 1); // skip unused part
    }//from w w w.  j  a v  a2  s .co  m

    return position;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

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

    if (type == MSG_SHAKE_HAND && channel != null) {

        ByteBuf response = Unpooled.buffer(13);
        response.writeCharSequence("\r\n*KW", StandardCharsets.US_ASCII);
        response.writeByte(0);//ww w .ja v a 2s . co  m
        response.writeShortLE(response.capacity());
        response.writeShortLE(MSG_SHAKE_HAND_RESPONSE);
        response.writeByte(1); // status
        response.writeCharSequence("\r\n", StandardCharsets.US_ASCII);

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

    } else if (type == MSG_UPLOAD_POSITION || type == MSG_UPLOAD_POSITION_NEW || type == MSG_CONTROL_RESPONSE
            || type == MSG_ALARM) {

        boolean newFormat = false;
        if (type == MSG_UPLOAD_POSITION && buf.readableBytes() == 48
                || type == MSG_ALARM && buf.readableBytes() == 48
                || type == MSG_CONTROL_RESPONSE && buf.readableBytes() == 57) {
            newFormat = true;
        }

        Position position = new Position(getProtocolName());

        if (type == MSG_CONTROL_RESPONSE) {
            buf.readUnsignedIntLE(); // GIS ip
            buf.readUnsignedIntLE(); // GIS port
        }

        position.setValid(BitUtil.check(buf.readUnsignedByte(), 0));

        short alarm = buf.readUnsignedByte();
        switch (alarm) {
        case 1:
            position.set(Position.KEY_ALARM, Position.ALARM_SOS);
            break;
        case 2:
            position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED);
            break;
        case 3:
            position.set(Position.KEY_ALARM, Position.ALARM_GEOFENCE_EXIT);
            break;
        case 9:
            position.set(Position.KEY_ALARM, Position.ALARM_POWER_OFF);
            break;
        default:
            break;
        }

        if (newFormat) {
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedIntLE()));
            position.setCourse(buf.readFloatLE());
        } else {
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
            position.setCourse(buf.readUnsignedShortLE());
        }
        position.setLongitude(buf.readFloatLE());
        position.setLatitude(buf.readFloatLE());

        if (!newFormat) {
            long timeValue = buf.readUnsignedIntLE();
            DateBuilder dateBuilder = new DateBuilder().setYear((int) BitUtil.from(timeValue, 26))
                    .setMonth((int) BitUtil.between(timeValue, 22, 26))
                    .setDay((int) BitUtil.between(timeValue, 17, 22))
                    .setHour((int) BitUtil.between(timeValue, 12, 17))
                    .setMinute((int) BitUtil.between(timeValue, 6, 12))
                    .setSecond((int) BitUtil.to(timeValue, 6));
            position.setTime(dateBuilder.getDate());
        }

        ByteBuf rawId;
        if (newFormat) {
            rawId = buf.readSlice(12);
        } else {
            rawId = buf.readSlice(11);
        }
        String id = rawId.toString(StandardCharsets.US_ASCII).replaceAll("[^\\p{Print}]", "");
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());

        if (newFormat) {
            DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            position.setTime(dateFormat.parse(buf.readSlice(17).toString(StandardCharsets.US_ASCII)));
            buf.readByte();
        }

        if (!newFormat) {
            position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());
            position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte());
        } else if (type == MSG_UPLOAD_POSITION_NEW) {
            position.set(Position.PREFIX_TEMP + 1, buf.readShortLE());
            position.set(Position.KEY_ODOMETER, buf.readFloatLE());
        }

        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  w  w .  j a  va 2 s.c  o 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;
}