Example usage for io.netty.buffer ByteBuf readUnsignedIntLE

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

Introduction

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

Prototype

public abstract long readUnsignedIntLE();

Source Link

Document

Gets an unsigned 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.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;
    }/*w  ww.  j a v a 2s .  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 w  w  .  j  a va  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;/*from   www. j a v  a 2  s.co m*/
    }
    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;
}

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

License:Apache License

private void decodeStat(Position position, ByteBuf buf) {

    buf.readUnsignedIntLE(); // ACC ON time
    buf.readUnsignedIntLE(); // UTC time
    position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
    position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedIntLE());
    position.set(Position.KEY_FUEL_CONSUMPTION, buf.readUnsignedIntLE());
    buf.readUnsignedShortLE(); // current fuel consumption
    position.set(Position.KEY_STATUS, buf.readUnsignedIntLE());
    buf.skipBytes(8);//from w w  w.jav a2s.  c om
}

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

License:Apache License

private Object decodeSc(Channel channel, SocketAddress remoteAddress, ByteBuf buf, int version, ByteBuf id,
        short type, DeviceSession deviceSession) {

    if (type == MSG_SC_HEARTBEAT) {

        sendResponse(channel, remoteAddress, version, id, MSG_SC_HEARTBEAT_RESPONSE, null);

    } else if (type == MSG_SC_LOGIN || type == MSG_SC_LOGOUT || type == MSG_SC_GPS || type == MSG_SC_ALARM
            || type == MSG_SC_CURRENT_LOCATION || type == MSG_SC_FUEL) {

        if (type == MSG_SC_LOGIN) {
            ByteBuf response = Unpooled.buffer(10);
            response.writeIntLE(0xFFFFFFFF);
            response.writeShortLE(0);// ww w.  j  a  v a 2s .c o m
            response.writeIntLE((int) (System.currentTimeMillis() / 1000));
            sendResponse(channel, remoteAddress, version, id, MSG_SC_LOGIN_RESPONSE, response);
        }

        if (type == MSG_SC_GPS) {
            buf.readUnsignedByte(); // historical
        } else if (type == MSG_SC_ALARM) {
            buf.readUnsignedIntLE(); // alarm
        } else if (type == MSG_SC_CURRENT_LOCATION) {
            buf.readUnsignedShortLE();
        }

        buf.readUnsignedIntLE(); // ACC ON time
        buf.readUnsignedIntLE(); // UTC time
        long odometer = buf.readUnsignedIntLE();
        long tripOdometer = buf.readUnsignedIntLE();
        long fuelConsumption = buf.readUnsignedIntLE();
        buf.readUnsignedShortLE(); // current fuel consumption
        long status = buf.readUnsignedIntLE();
        buf.skipBytes(8);

        int count = buf.readUnsignedByte();

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

        for (int i = 0; i < count; i++) {
            Position position = readPosition(deviceSession, buf);
            position.set(Position.KEY_ODOMETER, odometer);
            position.set(Position.KEY_ODOMETER_TRIP, tripOdometer);
            position.set(Position.KEY_FUEL_CONSUMPTION, fuelConsumption);
            position.set(Position.KEY_STATUS, status);
            positions.add(position);
        }

        if (type == MSG_SC_ALARM) {
            int alarmCount = buf.readUnsignedByte();
            for (int i = 0; i < alarmCount; i++) {
                if (buf.readUnsignedByte() != 0) {
                    int alarm = buf.readUnsignedByte();
                    for (Position position : positions) {
                        decodeAlarm(position, alarm);
                    }
                    buf.readUnsignedShortLE(); // description
                    buf.readUnsignedShortLE(); // threshold
                }
            }
        } else if (type == MSG_SC_FUEL) {
            for (Position position : positions) {
                position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE());
            }
        }

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

    } else if (type == MSG_SC_GPS_SLEEP) {

        buf.readUnsignedIntLE(); // device time

        return readPosition(deviceSession, buf);

    } else if (type == MSG_SC_AGPS_REQUEST) {

        return readPosition(deviceSession, buf);

    } else if (type == MSG_SC_PID_DATA) {

        Position position = createPosition(deviceSession);

        decodeStat(position, buf);

        buf.readUnsignedShortLE(); // sample rate
        decodeObd(position, buf, true);

        return position;

    } else if (type == MSG_SC_DTCS_PASSENGER) {

        Position position = createPosition(deviceSession);

        decodeStat(position, buf);

        buf.readUnsignedByte(); // flag
        position.add(ObdDecoder.decodeCodes(ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte()))));

        return position;

    } else if (type == MSG_SC_OBD_DATA) {

        Position position = createPosition(deviceSession);

        decodeStat(position, buf);

        buf.readUnsignedByte(); // flag
        decodeObd(position, buf, false);

        return position;

    } else if (type == MSG_SC_CELL) {

        Position position = createPosition(deviceSession);

        decodeStat(position, buf);

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

        return position;

    } else if (type == MSG_SC_QUERY_RESPONSE) {

        Position position = createPosition(deviceSession);

        buf.readUnsignedShortLE(); // index
        buf.readUnsignedByte(); // response count
        buf.readUnsignedByte(); // response index

        int failureCount = buf.readUnsignedByte();
        for (int i = 0; i < failureCount; i++) {
            buf.readUnsignedShortLE(); // tag
        }

        int successCount = buf.readUnsignedByte();
        for (int i = 0; i < successCount; i++) {
            buf.readUnsignedShortLE(); // tag
            position.set(Position.KEY_RESULT,
                    buf.readSlice(buf.readUnsignedShortLE()).toString(StandardCharsets.US_ASCII));
        }

        return position;

    }

    return null;
}

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

License:Apache License

private Object decodeCc(Channel channel, SocketAddress remoteAddress, ByteBuf buf, int version, ByteBuf id,
        short type, DeviceSession deviceSession) {

    if (type == MSG_CC_HEARTBEAT) {

        sendResponse(channel, remoteAddress, version, id, MSG_CC_HEARTBEAT_RESPONSE, null);

        buf.readUnsignedByte(); // 0x01 for history
        int count = buf.readUnsignedByte();

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

        for (int i = 0; i < count; i++) {
            Position position = readPosition(deviceSession, buf);

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

            buf.readUnsignedByte(); // geo-fencing id
            buf.readUnsignedByte(); // geo-fencing flags
            buf.readUnsignedByte(); // additional flags

            position.setNetwork(/*from w w  w .jav  a  2  s  .c o  m*/
                    new Network(CellTower.fromLacCid(buf.readUnsignedShortLE(), buf.readUnsignedShortLE())));

            positions.add(position);
        }

        return positions;

    } else if (type == MSG_CC_LOGIN) {

        sendResponse(channel, remoteAddress, version, id, MSG_CC_LOGIN_RESPONSE, null);

        Position position = readPosition(deviceSession, buf);

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

        buf.readUnsignedByte(); // geo-fencing id
        buf.readUnsignedByte(); // geo-fencing flags
        buf.readUnsignedByte(); // additional flags

        // GSM_CELL_CODE
        // STR_Z - firmware version
        // STR_Z - hardware version

        return position;

    }

    return null;
}

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

License:Apache License

private Object decodeMpip(Channel channel, SocketAddress remoteAddress, ByteBuf buf, int version, ByteBuf id,
        short type, DeviceSession deviceSession) {

    if (type == 0x4001) {

        sendResponse(channel, remoteAddress, version, id, (short) type, null);

        return readPosition(deviceSession, buf);

    } else if (type == 0x2001) {

        sendResponse(channel, remoteAddress, id, (short) 0x1001);

        buf.readUnsignedIntLE(); // index
        buf.readUnsignedIntLE(); // unix time
        buf.readUnsignedByte();/*  w w  w .  j a  v a2  s  .  co m*/

        return readPosition(deviceSession, buf);

    } else if (type == 0x4201 || type == 0x4202 || type == 0x4206) {

        return readPosition(deviceSession, buf);

    } else if (type == 0x4204) {

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

        for (int i = 0; i < 8; i++) {
            Position position = readPosition(deviceSession, buf);
            buf.skipBytes(31);
            positions.add(position);
        }

        return positions;

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    boolean alternative = buf.getByte(buf.readerIndex() + 3) != 'P';

    buf.skipBytes(4); // system code
    int type = buf.readUnsignedByte();
    long deviceUniqueId = buf.readUnsignedIntLE();

    if (type != MSG_CLIENT_SERIAL) {
        buf.readUnsignedShortLE(); // communication control
    }/*from  www  . j ava 2 s . c o m*/
    byte packetNumber = buf.readByte();

    sendReply(channel, remoteAddress, deviceUniqueId, packetNumber);

    if (type == MSG_CLIENT_STATUS) {

        Position position = new Position(getProtocolName());

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceUniqueId));
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());

        position.set(Position.KEY_VERSION_HW, buf.readUnsignedByte());
        position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
        buf.readUnsignedByte(); // protocol version

        position.set(Position.KEY_STATUS, buf.readUnsignedByte() & 0x0f);

        buf.readUnsignedByte(); // operator / configuration flags
        buf.readUnsignedByte(); // reason data
        position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedByte()));

        position.set("mode", buf.readUnsignedByte());
        position.set(Position.KEY_INPUT, buf.readUnsignedIntLE());

        if (alternative) {
            buf.readUnsignedByte(); // input
            position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE());
            position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShortLE());
        } else {
            buf.readUnsignedByte(); // operator
            position.set(Position.PREFIX_ADC + 1, buf.readUnsignedIntLE());
        }

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

        buf.skipBytes(6); // multi-purpose data
        buf.readUnsignedShortLE(); // fix time
        buf.readUnsignedByte(); // location status
        buf.readUnsignedByte(); // mode 1
        buf.readUnsignedByte(); // mode 2

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

        position.setValid(true);

        if (alternative) {
            position.setLongitude(buf.readIntLE() / 10000000.0);
            position.setLatitude(buf.readIntLE() / 10000000.0);
        } else {
            position.setLongitude(buf.readIntLE() / Math.PI * 180 / 100000000);
            position.setLatitude(buf.readIntLE() / Math.PI * 180 / 100000000);
        }

        position.setAltitude(buf.readIntLE() * 0.01);

        if (alternative) {
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedIntLE()));
            position.setCourse(buf.readUnsignedShortLE() / 1000.0);
        } else {
            position.setSpeed(UnitsConverter.knotsFromMps(buf.readUnsignedIntLE() * 0.01));
            position.setCourse(buf.readUnsignedShortLE() / Math.PI * 180.0 / 1000.0);
        }

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

        return position;
    }

    return null;
}

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

License:Apache License

private List<Position> decodeFixed64(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

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

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

    while (buf.readableBytes() >= 64) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        buf.readByte(); // type

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

        long time = buf.readUnsignedIntLE();
        position.setTime(
                new DateBuilder().setYear((int) (2000 + (time & 0x3F))).setMonth((int) (time >> 6) & 0xF)
                        .setDay((int) (time >> 10) & 0x1F).setHour((int) (time >> 15) & 0x1F)
                        .setMinute((int) (time >> 20) & 0x3F).setSecond((int) (time >> 26) & 0x3F).getDate());

        position.setLongitude(buf.readIntLE() * 0.0000001);
        position.setLatitude(buf.readIntLE() * 0.0000001);
        position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedShortLE()));
        position.setCourse(buf.readUnsignedByte() * 2);
        position.setAltitude(buf.readShortLE());

        buf.readUnsignedShortLE(); // position accuracy
        buf.readUnsignedByte(); // speed accuracy

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

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

        position.set(Position.KEY_INPUT, buf.readUnsignedIntLE());
        position.set(Position.KEY_OUTPUT, buf.readUnsignedShortLE());

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

        position.set(Position.KEY_DEVICE_TEMP, buf.readByte());

        buf.readShortLE(); // accelerometer x
        buf.readShortLE(); // accelerometer y
        buf.readShortLE(); // accelerometer z

        buf.skipBytes(8); // device id

        position.set(Position.KEY_PDOP, buf.readUnsignedShortLE() * 0.01);

        buf.skipBytes(2); // reserved

        buf.readUnsignedShortLE(); // checksum

        positions.add(position);
    }

    return positions;
}

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

License:Apache License

private List<Position> decodeStandard(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }//  www  .j  av  a  2 s .com

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

    while (buf.isReadable()) {
        int recordEnd = buf.readerIndex() + buf.readUnsignedShortLE();

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

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

        position.setDeviceTime(new Date(1356998400000L + buf.readUnsignedIntLE() * 1000)); // since 1 Jan 2013

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

        while (buf.readerIndex() < recordEnd) {

            int fieldId = buf.readUnsignedByte();
            int fieldLength = buf.readUnsignedByte();
            int fieldEnd = buf.readerIndex() + (fieldLength == 255 ? buf.readUnsignedShortLE() : fieldLength);

            if (fieldId == 0) {

                position.setFixTime(new Date(1356998400000L + buf.readUnsignedIntLE() * 1000));
                position.setLatitude(buf.readIntLE() * 0.0000001);
                position.setLongitude(buf.readIntLE() * 0.0000001);
                position.setAltitude(buf.readShortLE());
                position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedShortLE()));

                buf.readUnsignedByte(); // speed accuracy

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

                position.set(Position.KEY_PDOP, buf.readUnsignedByte() * 0.1);

                position.setAccuracy(buf.readUnsignedByte());
                position.setValid(buf.readUnsignedByte() != 0);

            } else if (fieldId == 2) {

                int input = buf.readIntLE();
                int output = buf.readUnsignedShortLE();
                int status = buf.readUnsignedShortLE();

                position.set(Position.KEY_IGNITION, BitUtil.check(input, 0));

                position.set(Position.KEY_INPUT, input);
                position.set(Position.KEY_OUTPUT, output);
                position.set(Position.KEY_STATUS, status);

            } else if (fieldId == 6) {

                while (buf.readerIndex() < fieldEnd) {
                    switch (buf.readUnsignedByte()) {
                    case 1:
                        position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.001);
                        break;
                    case 2:
                        position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.01);
                        break;
                    case 3:
                        position.set(Position.KEY_DEVICE_TEMP, buf.readShortLE() * 0.01);
                        break;
                    case 4:
                        position.set(Position.KEY_RSSI, buf.readUnsignedShortLE());
                        break;
                    case 5:
                        position.set("solarPower", buf.readUnsignedShortLE() * 0.001);
                        break;
                    default:
                        break;
                    }
                }

            }

            buf.readerIndex(fieldEnd);

        }

        if (position.getFixTime() == null) {
            getLastLocation(position, position.getDeviceTime());
        }

        positions.add(position);
    }

    return positions;
}