Example usage for io.netty.buffer ByteBuf readIntLE

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

Introduction

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

Prototype

public abstract int readIntLE();

Source Link

Document

Gets a 32-bit integer at the current readerIndex in the Little Endian Byte Order and increases the readerIndex by 4 in this buffer.

Usage

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

License:Apache License

private Position parseTrackingData(DeviceSession deviceSession, ByteBuf buf, int sequenceNumber,
        long timestamp) {
    Position position = new Position(getProtocolName());

    position.set(Position.KEY_INDEX, sequenceNumber);
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(convertTimestamp(timestamp));

    buf.readUnsignedByte(); // tracking mode

    short flags = buf.readUnsignedByte();
    position.setValid((flags & 0x01) == 0x01);

    buf.readUnsignedShortLE(); // duration

    position.setLatitude(buf.readIntLE() * 0.0000001);
    position.setLongitude(buf.readIntLE() * 0.0000001);

    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
    position.setCourse(buf.readUnsignedByte() * 2.0);

    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
    position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.001);
    position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());

    return position;
}

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;//w w  w  .  j av a  2s  .  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 .  ja  v a 2  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
    }/*  w  ww.j a v  a2  s.  co m*/

    return position;
}

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

    String id = buf.readCharSequence(12, StandardCharsets.US_ASCII).toString();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }/* ww  w  . j  a v a  2  s .co  m*/

    int type = buf.readUnsignedShortLE();

    if (type != MSG_LOGIN && type != MSG_COMPREHENSIVE_LIVE && type != MSG_COMPREHENSIVE_HISTORY
            && type != MSG_ALARM && type != MSG_FIXED) {
        return null;
    }

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

    if (type == MSG_COMPREHENSIVE_LIVE || type == MSG_COMPREHENSIVE_HISTORY) {

        buf.skipBytes(6); // time
        boolean includeLocation = buf.readUnsignedByte() > 0;
        boolean includeObd = buf.readUnsignedByte() > 0;
        buf.readUnsignedByte(); // include sensor

        if (includeLocation) {
            decodeLocation(position, buf);
        } else {
            getLastLocation(position, null);
        }

        if (includeObd) {
            int count = buf.readUnsignedByte();
            for (int i = 0; i < count; i++) {
                int pid = buf.readUnsignedShortLE();
                int length = buf.readUnsignedByte();
                switch (length) {
                case 1:
                    position.add(ObdDecoder.decodeData(pid, buf.readByte(), true));
                    break;
                case 2:
                    position.add(ObdDecoder.decodeData(pid, buf.readShortLE(), true));
                    break;
                case 4:
                    position.add(ObdDecoder.decodeData(pid, buf.readIntLE(), true));
                    break;
                default:
                    buf.skipBytes(length);
                    break;
                }
            }
        }

        position.set(Position.KEY_FUEL_USED, buf.readUnsignedInt() * 0.01);
        position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt());

    } else if (type == MSG_ALARM) {

        buf.readUnsignedShortLE(); // random number
        buf.readUnsignedByte(); // tag
        position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedByte()));
        buf.readUnsignedShortLE(); // threshold
        buf.readUnsignedShortLE(); // value
        buf.skipBytes(6); // time

        decodeLocation(position, buf);

    } else if (type == MSG_FIXED) {

        buf.skipBytes(6); // time

        decodeLocation(position, buf);

    } else {

        decodeLocation(position, buf);

    }

    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  .j a  v a  2s .  c  o 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.ProgressProtocolDecoder.java

License:Apache License

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

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

    if (type == MSG_IDENT || type == MSG_IDENT_FULL) {

        buf.readUnsignedIntLE(); // id
        int length = buf.readUnsignedShortLE();
        buf.skipBytes(length);//  w  w  w. j a va  2  s. co m
        length = buf.readUnsignedShortLE();
        buf.skipBytes(length);
        length = buf.readUnsignedShortLE();
        String imei = buf.readSlice(length).toString(StandardCharsets.US_ASCII);
        getDeviceSession(channel, remoteAddress, imei);

    } else if (type == MSG_POINT || type == MSG_ALARM || type == MSG_LOGMSG) {

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

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

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

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

            if (type == MSG_LOGMSG) {
                position.set(Position.KEY_ARCHIVE, true);
                int subtype = buf.readUnsignedShortLE();
                if (subtype == MSG_ALARM) {
                    position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
                }
                if (buf.readUnsignedShortLE() > buf.readableBytes()) {
                    lastIndex += 1;
                    break; // workaround for device bug
                }
                lastIndex = buf.readUnsignedIntLE();
                position.set(Position.KEY_INDEX, lastIndex);
            } else {
                newIndex = buf.readUnsignedIntLE();
            }

            position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
            position.setLatitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF);
            position.setLongitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF);
            position.setSpeed(buf.readUnsignedIntLE() * 0.01);
            position.setCourse(buf.readUnsignedShortLE() * 0.01);
            position.setAltitude(buf.readUnsignedShortLE() * 0.01);

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

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

            long extraFlags = buf.readLongLE();

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

            if (BitUtil.check(extraFlags, 1)) {
                int size = buf.readUnsignedShortLE();
                position.set("can", buf.toString(buf.readerIndex(), size, StandardCharsets.US_ASCII));
                buf.skipBytes(size);
            }

            if (BitUtil.check(extraFlags, 2)) {
                position.set("passenger", ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedShortLE())));
            }

            if (type == MSG_ALARM) {
                position.set(Position.KEY_ALARM, true);
                byte[] response = { (byte) 0xC9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
                channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(response), remoteAddress));
            }

            buf.readUnsignedIntLE(); // crc

            positions.add(position);
        }

        requestArchive(channel);

        return positions;
    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int type = buf.readIntLE();
    buf.readUnsignedIntLE(); // length

    if (type != MSG_HEARTBEAT) {
        buf.readUnsignedShortLE(); // version
        buf.readUnsignedShortLE(); // index
    }//w  w  w  .  j  ava  2  s . c  om

    if (type == MSG_SIGNAL_LINK_REGISTRATION) {

        getDeviceSession(channel, remoteAddress, buf.readSlice(12).toString(StandardCharsets.US_ASCII));

    } else if (type == MSG_GPS_DATA) {

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

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

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

        int flags = buf.readUnsignedByte();

        if (BitUtil.check(flags, 0)) {

            buf.readUnsignedShortLE(); // declination

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

            position.setLongitude(buf.readUnsignedByte() + buf.readUnsignedByte() / 60.0);
            position.setLatitude(buf.readUnsignedByte() + buf.readUnsignedByte() / 60.0);

            position.setLongitude(position.getLongitude() + buf.readUnsignedIntLE() / 3600.0);
            position.setLatitude(position.getLatitude() + buf.readUnsignedIntLE() / 3600.0);

            int status = buf.readUnsignedByte();

            position.setValid(BitUtil.check(status, 0));
            if (BitUtil.check(status, 1)) {
                position.setLongitude(-position.getLongitude());
            }
            if (!BitUtil.check(status, 2)) {
                position.setLatitude(-position.getLatitude());
            }

        } else {

            getLastLocation(position, position.getDeviceTime());

        }

        return position;

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int type = buf.readUnsignedByte();

    if (type == MSG_ID) {

        buf.skipBytes(16); // name

        String imei = buf.readSlice(15).toString(StandardCharsets.US_ASCII);

        if (getDeviceSession(channel, remoteAddress, imei) != null && channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeByte(MSG_ACK);
            response.writeByte(0x01); // success
            response.writeByte(0x66); // checksum
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }//  w w w .  j a  v  a  2 s .  c o m

    } else if (type == MSG_GPS || type == MSG_GSM) {

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

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

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

        if (type == MSG_GPS) {

            position.setValid(true);
            position.setFixTime(position.getDeviceTime());
            position.setLatitude(buf.readIntLE() * 0.000001);
            position.setLongitude(buf.readIntLE() * 0.000001);
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readByte()));

        } else {

            getLastLocation(position, position.getDeviceTime());

            position.setNetwork(new Network(CellTower.from(buf.readUnsignedShortLE(), buf.readUnsignedShortLE(),
                    buf.readUnsignedShortLE(), buf.readUnsignedShortLE())));

            buf.readUnsignedByte(); // reserved

        }

        int value = buf.readUnsignedByte();

        position.set(Position.KEY_SATELLITES, BitUtil.to(value, 4));
        position.set(Position.KEY_RSSI, BitUtil.between(value, 4, 7));
        position.set(Position.KEY_MOTION, BitUtil.check(value, 7));

        value = buf.readUnsignedByte();

        position.set(Position.KEY_CHARGE, BitUtil.check(value, 0));

        for (int i = 1; i <= 4; i++) {
            position.set(Position.PREFIX_IN + i, BitUtil.check(value, i));
        }

        position.set(Position.KEY_BATTERY_LEVEL, BitUtil.from(value, 5) * 100 / 7);
        position.set(Position.KEY_DEVICE_TEMP, buf.readByte());

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

        return position;

    }

    return null;
}

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

License:Apache License

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

    FullHttpRequest request = (FullHttpRequest) msg;
    JsonObject json = Json//ww  w . j  a  va2 s.  c o m
            .createReader(new StringReader(URLDecoder
                    .decode(request.content().toString(StandardCharsets.UTF_8).split("=")[0], "UTF-8")))
            .readObject();

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, json.getString("device"));
    if (deviceSession == null) {
        sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
        return null;
    }

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

    position.setTime(new Date(json.getInt("time") * 1000L));

    ByteBuf buf = Unpooled.wrappedBuffer(DataConverter.parseHex(json.getString("data")));
    try {
        int type = buf.readUnsignedByte() >> 4;
        if (type == 0) {

            position.setValid(true);
            position.setLatitude(buf.readIntLE() * 0.0000001);
            position.setLongitude(buf.readIntLE() * 0.0000001);
            position.setCourse(buf.readUnsignedByte() * 2);
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));

            position.set(Position.KEY_BATTERY, buf.readUnsignedByte() * 0.025);

        } else {

            getLastLocation(position, position.getDeviceTime());

        }
    } finally {
        buf.release();
    }

    position.set(Position.KEY_RSSI, json.getJsonNumber("rssi").doubleValue());
    position.set(Position.KEY_INDEX, json.getInt("seqNumber"));

    sendResponse(channel, HttpResponseStatus.OK);
    return position;
}