Example usage for io.netty.buffer ByteBuf readUnsignedInt

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

Introduction

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

Prototype

public abstract long readUnsignedInt();

Source Link

Document

Gets an unsigned 32-bit integer at the current readerIndex and increases the readerIndex by 4 in this buffer.

Usage

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

License:Apache License

private Position decodeLocation(DeviceSession deviceSession, ByteBuf buf) {

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

    position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedInt()));

    int flags = buf.readInt();

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

    position.setValid(BitUtil.check(flags, 1));

    double lat = buf.readUnsignedInt() * 0.000001;
    double lon = buf.readUnsignedInt() * 0.000001;

    if (BitUtil.check(flags, 2)) {
        position.setLatitude(-lat);/*from w  ww . java 2  s .  c om*/
    } else {
        position.setLatitude(lat);
    }

    if (BitUtil.check(flags, 3)) {
        position.setLongitude(-lon);
    } else {
        position.setLongitude(lon);
    }

    position.setAltitude(buf.readShort());
    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort() * 0.1));
    position.setCourse(buf.readUnsignedShort());

    DateBuilder dateBuilder = new DateBuilder(deviceSession.getTimeZone()).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));
    position.setTime(dateBuilder.getDate());

    while (buf.readableBytes() > 2) {
        int subtype = buf.readUnsignedByte();
        int length = buf.readUnsignedByte();
        switch (subtype) {
        case 0x01:
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 100);
            break;
        case 0x30:
            position.set(Position.KEY_RSSI, buf.readUnsignedByte());
            break;
        case 0x31:
            position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
            break;
        default:
            buf.skipBytes(length);
            break;
        }
    }

    return position;
}

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

License:Apache License

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

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

    buf.readByte(); // header

    boolean longFormat = buf.getUnsignedByte(buf.readerIndex()) == 0x75;

    String id = String.valueOf(Long.parseLong(ByteBufUtil.hexDump(buf.readSlice(5))));
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }/*from   w w w .  j a  va2 s.c  o  m*/

    int protocolVersion = 0;
    if (longFormat) {
        protocolVersion = buf.readUnsignedByte();
    }

    int version = BitUtil.from(buf.readUnsignedByte(), 4);
    buf.readUnsignedShort(); // length

    while (buf.readableBytes() > 1) {

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

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

        double latitude = convertCoordinate(BcdUtil.readInteger(buf, 8));
        double longitude = convertCoordinate(BcdUtil.readInteger(buf, 9));

        byte flags = buf.readByte();
        position.setValid((flags & 0x1) == 0x1);
        if ((flags & 0x2) == 0) {
            latitude = -latitude;
        }
        position.setLatitude(latitude);
        if ((flags & 0x4) == 0) {
            longitude = -longitude;
        }
        position.setLongitude(longitude);

        position.setSpeed(BcdUtil.readInteger(buf, 2));
        position.setCourse(buf.readUnsignedByte() * 2.0);

        if (longFormat) {

            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
            position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());

            buf.readUnsignedInt(); // vehicle id combined

            int status = buf.readUnsignedShort();
            position.set(Position.KEY_ALARM, BitUtil.check(status, 1) ? Position.ALARM_GEOFENCE_ENTER : null);
            position.set(Position.KEY_ALARM, BitUtil.check(status, 2) ? Position.ALARM_GEOFENCE_EXIT : null);
            position.set(Position.KEY_ALARM, BitUtil.check(status, 3) ? Position.ALARM_POWER_CUT : null);
            position.set(Position.KEY_ALARM, BitUtil.check(status, 4) ? Position.ALARM_VIBRATION : null);
            position.set(Position.KEY_BLOCKED, BitUtil.check(status, 7));
            position.set(Position.KEY_ALARM, BitUtil.check(status, 8 + 3) ? Position.ALARM_LOW_BATTERY : null);
            position.set(Position.KEY_ALARM, BitUtil.check(status, 8 + 6) ? Position.ALARM_FAULT : null);
            position.set(Position.KEY_STATUS, status);

            int battery = buf.readUnsignedByte();
            if (battery == 0xff) {
                position.set(Position.KEY_CHARGE, true);
            } else {
                position.set(Position.KEY_BATTERY_LEVEL, battery);
            }

            CellTower cellTower = CellTower.fromCidLac(buf.readUnsignedShort(), buf.readUnsignedShort());
            cellTower.setSignalStrength((int) buf.readUnsignedByte());
            position.setNetwork(new Network(cellTower));

            if (protocolVersion == 0x17) {
                buf.readUnsignedByte(); // geofence id
                buf.skipBytes(3); // reserved
            }

        } else if (version == 1) {

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

            buf.readByte(); // other flags and sensors

            position.setAltitude(buf.readUnsignedShort());

            int cid = buf.readUnsignedShort();
            int lac = buf.readUnsignedShort();
            int rssi = buf.readUnsignedByte();

            if (cid != 0 && lac != 0) {
                CellTower cellTower = CellTower.fromCidLac(cid, lac);
                cellTower.setSignalStrength(rssi);
                position.setNetwork(new Network(cellTower));
            } else {
                position.set(Position.KEY_RSSI, rssi);
            }

        } else if (version == 2) {

            int fuel = buf.readUnsignedByte() << 8;

            decodeStatus(position, buf);
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);

            fuel += buf.readUnsignedByte();
            position.set(Position.KEY_FUEL_LEVEL, fuel);

        } else if (version == 3) {

            BitBuffer bitBuffer = new BitBuffer(buf);

            position.set("fuel1", bitBuffer.readUnsigned(12));
            position.set("fuel2", bitBuffer.readUnsigned(12));
            position.set("fuel3", bitBuffer.readUnsigned(12));
            position.set(Position.KEY_ODOMETER, bitBuffer.readUnsigned(20) * 1000);

            int status = bitBuffer.readUnsigned(24);
            position.set(Position.KEY_IGNITION, BitUtil.check(status, 0));
            position.set(Position.KEY_STATUS, status);

        }

        positions.add(position);

    }

    buf.readUnsignedByte(); // index

    return positions;
}

From source file:org.traccar.protocol.KhdProtocolDecoder.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();
    buf.readUnsignedShort(); // size

    if (type == MSG_LOGIN || type == MSG_ADMIN_NUMBER || type == MSG_SEND_TEXT || type == MSG_SMS_ALARM_SWITCH
            || type == MSG_POSITION_REUPLOAD) {

        ByteBuf response = Unpooled.buffer();
        response.writeByte(0x29);//from w w  w  .ja va2 s.co m
        response.writeByte(0x29); // header
        response.writeByte(MSG_CONFIRMATION);
        response.writeShort(5); // size
        response.writeByte(buf.getByte(buf.writerIndex() - 2));
        response.writeByte(type);
        response.writeByte(buf.writerIndex() > 9 ? buf.getByte(9) : 0); // 10th byte
        response.writeByte(Checksum.xor(response.nioBuffer()));
        response.writeByte(0x0D); // ending

        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }

    }

    if (type == MSG_ON_DEMAND || type == MSG_POSITION_UPLOAD || type == MSG_POSITION_REUPLOAD
            || type == MSG_ALARM || type == MSG_REPLY || type == MSG_PERIPHERAL) {

        Position position = new Position(getProtocolName());

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, readSerialNumber(buf));
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());

        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));
        position.setTime(dateBuilder.getDate());

        position.setLatitude(BcdUtil.readCoordinate(buf));
        position.setLongitude(BcdUtil.readCoordinate(buf));
        position.setSpeed(UnitsConverter.knotsFromKph(BcdUtil.readInteger(buf, 4)));
        position.setCourse(BcdUtil.readInteger(buf, 4));
        position.setValid((buf.readUnsignedByte() & 0x80) != 0);

        if (type != MSG_ALARM) {

            position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium());
            position.set(Position.KEY_STATUS, buf.readUnsignedInt());
            position.set(Position.KEY_HDOP, buf.readUnsignedByte());
            position.set(Position.KEY_VDOP, buf.readUnsignedByte());
            position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());

            buf.skipBytes(5); // other location data

            if (type == MSG_PERIPHERAL) {

                buf.readUnsignedShort(); // data length

                int dataType = buf.readUnsignedByte();

                buf.readUnsignedByte(); // content length

                switch (dataType) {
                case 0x01:
                    position.set(Position.KEY_FUEL_LEVEL,
                            buf.readUnsignedByte() * 100 + buf.readUnsignedByte());
                    break;
                case 0x02:
                    position.set(Position.PREFIX_TEMP + 1,
                            buf.readUnsignedByte() * 100 + buf.readUnsignedByte());
                    break;
                default:
                    break;
                }

            }

        }

        return position;

    }

    return null;
}

From source file:org.traccar.protocol.MeiligaoProtocolDecoder.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.readShort(); // length
    ByteBuf id = buf.readSlice(7);/*from   www .j  av  a2s  . co m*/
    int command = buf.readUnsignedShort();

    if (command == MSG_LOGIN) {
        ByteBuf response = Unpooled.wrappedBuffer(new byte[] { 0x01 });
        sendResponse(channel, remoteAddress, id, MSG_LOGIN_RESPONSE, response);
        return null;
    } else if (command == MSG_HEARTBEAT) {
        ByteBuf response = Unpooled.wrappedBuffer(new byte[] { 0x01 });
        sendResponse(channel, remoteAddress, id, MSG_HEARTBEAT, response);
        return null;
    } else if (command == MSG_SERVER) {
        ByteBuf response = Unpooled.copiedBuffer(getServer(channel, ':'), StandardCharsets.US_ASCII);
        sendResponse(channel, remoteAddress, id, MSG_SERVER, response);
        return null;
    } else if (command == MSG_UPLOAD_PHOTO) {
        byte imageIndex = buf.readByte();
        photos.put(imageIndex, Unpooled.buffer());
        ByteBuf response = Unpooled.copiedBuffer(new byte[] { imageIndex });
        sendResponse(channel, remoteAddress, id, MSG_UPLOAD_PHOTO_RESPONSE, response);
        return null;
    } else if (command == MSG_UPLOAD_COMPLETE) {
        byte imageIndex = buf.readByte();
        ByteBuf response = Unpooled.copiedBuffer(new byte[] { imageIndex, 0, 0 });
        sendResponse(channel, remoteAddress, id, MSG_RETRANSMISSION, response);
        return null;
    }

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

    if (command == MSG_DATA_PHOTO) {

        byte imageIndex = buf.readByte();
        buf.readUnsignedShort(); // image footage
        buf.readUnsignedByte(); // total packets
        buf.readUnsignedByte(); // packet index

        photos.get(imageIndex).writeBytes(buf, buf.readableBytes() - 2 - 2);

        return null;

    } else if (command == MSG_RETRANSMISSION) {

        return decodeRetransmission(buf, deviceSession);

    } else {

        Position position = new Position(getProtocolName());

        position.setDeviceId(deviceSession.getDeviceId());

        if (command == MSG_ALARM) {
            short alarmCode = buf.readUnsignedByte();
            position.set(Position.KEY_ALARM, decodeAlarm(alarmCode));
            if (alarmCode >= 0x02 && alarmCode <= 0x05) {
                position.set(Position.PREFIX_IN + alarmCode, 1);
            } else if (alarmCode >= 0x32 && alarmCode <= 0x35) {
                position.set(Position.PREFIX_IN + (alarmCode - 0x30), 0);
            }
        } else if (command == MSG_POSITION_LOGGED) {
            buf.skipBytes(6);
        } else if (command == MSG_RFID) {
            for (int i = 0; i < 15; i++) {
                long rfid = buf.readUnsignedInt();
                if (rfid != 0) {
                    String card = String.format("%010d", rfid);
                    position.set("card" + (i + 1), card);
                    position.set(Position.KEY_DRIVER_UNIQUE_ID, card);
                }
            }
        } else if (command == MSG_POSITION_IMAGE) {
            byte imageIndex = buf.readByte();
            buf.readUnsignedByte(); // image upload type
            String uniqueId = Context.getIdentityManager().getById(deviceSession.getDeviceId()).getUniqueId();
            ByteBuf photo = photos.remove(imageIndex);
            try {
                position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(uniqueId, photo, "jpg"));
            } finally {
                photo.release();
            }
        }

        String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 4, StandardCharsets.US_ASCII);

        switch (command) {
        case MSG_POSITION:
        case MSG_POSITION_LOGGED:
        case MSG_ALARM:
        case MSG_POSITION_IMAGE:
            return decodeRegular(position, sentence);
        case MSG_RFID:
            return decodeRfid(position, sentence);
        case MSG_OBD_RT:
            return decodeObd(position, sentence);
        case MSG_OBD_RTA:
            return decodeObdA(position, sentence);
        default:
            return null;
        }

    }
}

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

License:Apache License

private List<Position> parseFormatA(DeviceSession deviceSession, ByteBuf buf) {
    List<Position> positions = new LinkedList<>();

    FloatReader latitudeReader = new FloatReader();
    FloatReader longitudeReader = new FloatReader();
    TimeReader timeReader = new TimeReader();

    try {//from w  ww.  java 2 s .c  o  m
        while (buf.isReadable()) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());

            short flags = buf.readUnsignedByte();

            short event = buf.readUnsignedByte();
            if (BitUtil.check(event, 7)) {
                if (BitUtil.check(event, 6)) {
                    buf.skipBytes(8);
                } else {
                    while (BitUtil.check(event, 7)) {
                        event = buf.readUnsignedByte();
                    }
                }
            }

            position.setLatitude(latitudeReader.readFloat(buf) / Math.PI * 180);
            position.setLongitude(longitudeReader.readFloat(buf) / Math.PI * 180);
            position.setTime(timeReader.readTime(buf));

            if (BitUtil.check(flags, 0)) {
                buf.readUnsignedByte(); // status
            }

            if (BitUtil.check(flags, 1)) {
                position.setAltitude(buf.readUnsignedShort());
            }

            if (BitUtil.check(flags, 2)) {
                position.setSpeed(buf.readUnsignedShort() & 0x03ff);
                position.setCourse(buf.readUnsignedByte());
            }

            if (BitUtil.check(flags, 3)) {
                position.set(Position.KEY_ODOMETER, buf.readUnsignedShort() * 1000);
            }

            if (BitUtil.check(flags, 4)) {
                position.set(Position.KEY_FUEL_CONSUMPTION + "Accumulator1", buf.readUnsignedInt());
                position.set(Position.KEY_FUEL_CONSUMPTION + "Accumulator2", buf.readUnsignedInt());
                position.set("hours1", buf.readUnsignedShort());
                position.set("hours2", buf.readUnsignedShort());
            }

            if (BitUtil.check(flags, 5)) {
                position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort() & 0x03ff);
                position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort() & 0x03ff);
                position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShort() & 0x03ff);
                position.set(Position.PREFIX_ADC + 4, buf.readUnsignedShort() & 0x03ff);
            }

            if (BitUtil.check(flags, 6)) {
                position.set(Position.PREFIX_TEMP + 1, buf.readByte());
                buf.getUnsignedByte(buf.readerIndex()); // control (>> 4)
                position.set(Position.KEY_INPUT, buf.readUnsignedShort() & 0x0fff);
                buf.readUnsignedShort(); // old sensor state (& 0x0fff)
            }

            if (BitUtil.check(flags, 7)) {
                position.set(Position.KEY_BATTERY, buf.getUnsignedByte(buf.readerIndex()) >> 2);
                position.set(Position.KEY_POWER, buf.readUnsignedShort() & 0x03ff);
                position.set(Position.KEY_DEVICE_TEMP, buf.readByte());

                position.set(Position.KEY_RSSI, (buf.getUnsignedByte(buf.readerIndex()) >> 4) & 0x07);

                int satellites = buf.readUnsignedByte() & 0x0f;
                position.setValid(satellites >= 3);
                position.set(Position.KEY_SATELLITES, satellites);
            }
            positions.add(position);
        }
    } catch (IndexOutOfBoundsException error) {
        LOGGER.warn("MTA6 parsing error", error);
    }

    return positions;
}

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  .  ja v a 2 s.  c  o m

    return position;
}

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

License:Apache License

private Object processFlexSingle(FlexPositionParser parser, String flexHeader, DeviceSession deviceSession,
        Channel channel, ByteBuf buf) {

    if (!flexHeader.equals("~C")) {
        buf.readUnsignedInt(); // event index
    }//from  www . ja v a  2  s . co m

    Position position = parser.parsePosition(deviceSession, buf);

    ByteBuf response = Unpooled.buffer();
    response.writeCharSequence(flexHeader, StandardCharsets.US_ASCII);
    response.writeIntLE((int) position.getLong(Position.KEY_INDEX));
    sendFlexReply(channel, response);

    return position.getFixTime() != null ? position : null;
}

From source file:org.traccar.protocol.NvsProtocolDecoder.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()) == 0) {

        buf.readUnsignedShort(); // length

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

        if (getDeviceSession(channel, remoteAddress, imei) != null) {
            sendResponse(channel, remoteAddress, "OK");
        } else {//from  w w w .  ja v  a2s .com
            sendResponse(channel, remoteAddress, "NO01");
        }

    } else {

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

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

        buf.skipBytes(4); // marker
        buf.readUnsignedShort(); // length
        buf.readLong(); // imei
        buf.readUnsignedByte(); // codec
        int count = buf.readUnsignedByte();

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

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

            position.set("reason", buf.readUnsignedByte());

            position.setLongitude(buf.readInt() / 10000000.0);
            position.setLatitude(buf.readInt() / 10000000.0);
            position.setAltitude(buf.readShort());
            position.setCourse(buf.readUnsignedShort());

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

            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
            position.setValid(buf.readUnsignedByte() != 0);

            buf.readUnsignedByte(); // used systems

            buf.readUnsignedByte(); // cause element id

            // Read 1 byte data
            int cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedByte());
            }

            // Read 2 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedShort());
            }

            // Read 4 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedInt());
            }

            // Read 8 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readLong());
            }

            positions.add(position);
        }

        return positions;

    }

    return null;
}

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;
    }/*from w  ww .jav  a  2s. c  om*/

    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.ObdDongleProtocolDecoder.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 index = buf.readUnsignedShort();

    String imei = buf.readSlice(15).toString(StandardCharsets.US_ASCII);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }//w ww  .j  a v  a2  s.  c o  m

    int type = buf.readUnsignedByte();

    buf.readUnsignedShort(); // data length

    if (type == MSG_TYPE_CONNECT) {

        ByteBuf response = Unpooled.buffer();
        response.writeByte(1);
        response.writeShort(0);
        response.writeInt(0);
        sendResponse(channel, MSG_TYPE_CONNACK, index, imei, response);

    } else if (type == MSG_TYPE_PUBLISH) {

        int typeMajor = buf.readUnsignedByte();
        int typeMinor = buf.readUnsignedByte();

        buf.readUnsignedByte(); // event id

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

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

        int flags = buf.readUnsignedByte();

        position.setValid(!BitUtil.check(flags, 6));

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

        double longitude = ((BitUtil.to(buf.readUnsignedShort(), 1) << 24) + buf.readUnsignedMedium())
                * 0.00001;
        position.setLongitude(BitUtil.check(flags, 5) ? longitude : -longitude);

        double latitude = buf.readUnsignedMedium() * 0.00001;
        position.setLatitude(BitUtil.check(flags, 4) ? latitude : -latitude);

        int speedCourse = buf.readUnsignedMedium();
        position.setSpeed(UnitsConverter.knotsFromMph(BitUtil.from(speedCourse, 10) * 0.1));
        position.setCourse(BitUtil.to(speedCourse, 10));

        ByteBuf response = Unpooled.buffer();
        response.writeByte(typeMajor);
        response.writeByte(typeMinor);
        sendResponse(channel, MSG_TYPE_PUBACK, index, imei, response);

        return position;

    }

    return null;
}