Example usage for io.netty.buffer ByteBuf readSlice

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

Introduction

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

Prototype

public abstract ByteBuf readSlice(int length);

Source Link

Document

Returns a new slice of this buffer's sub-region starting at the current readerIndex and increases the readerIndex by the size of the new slice (= length ).

Usage

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

    String imei = ByteBufUtil.hexDump(buf.readSlice(7));
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei,
            imei + Checksum.luhn(Long.parseLong(imei)));
    if (deviceSession == null) {
        return null;
    }/*from   www.j a  v a2 s .  co m*/

    int type = buf.readUnsignedShort();

    if (type == MSG_LOCATION_REPORT || type == MSG_LOCATION_REQUEST) {

        String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 8, StandardCharsets.US_ASCII);
        Parser parser = new Parser(PATTERN, sentence);
        if (!parser.matches()) {
            return null;
        }

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

        if (parser.hasNext(15)) {

            position.setTime(parser.nextDateTime());

            position.setValid(parser.next().equals("A"));
            position.set(Position.KEY_SATELLITES, parser.nextInt());

            position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
            position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));

            position.setSpeed(parser.nextDouble(0));
            position.set(Position.KEY_HDOP, parser.nextDouble(0));
            position.setAltitude(parser.nextDouble(0));

        } else {

            getLastLocation(position, null);

        }

        position.setNetwork(new Network(
                CellTower.from(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0), parser.nextInt(0))));

        return position;
    }

    return null;
}

From source file:org.traccar.protocol.DmtProtocolDecoder.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();
    int length = buf.readUnsignedShortLE();

    if (type == MSG_HELLO) {

        buf.readUnsignedIntLE(); // device serial number

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
                buf.readSlice(15).toString(StandardCharsets.US_ASCII));

        ByteBuf response = Unpooled.buffer();
        if (length == 51) {
            response.writeByte(0); // reserved
            response.writeIntLE(0); // reserved
        } else {//from w  w  w.j  a v  a  2 s.  c  o  m
            response.writeIntLE((int) ((System.currentTimeMillis() - 1356998400000L) / 1000));
            response.writeIntLE(deviceSession != null ? 0 : 1); // flags
        }

        sendResponse(channel, MSG_HELLO_RESPONSE, response);

    } else if (type == MSG_COMMIT) {

        ByteBuf response = Unpooled.buffer(0);
        response.writeByte(1); // flags (success)
        sendResponse(channel, MSG_COMMIT_RESPONSE, response);

    } else if (type == MSG_CANNED_REQUEST_1) {

        ByteBuf response = Unpooled.buffer(0);
        response.writeBytes(new byte[12]);
        sendResponse(channel, MSG_CANNED_RESPONSE_1, response);

    } else if (type == MSG_CANNED_REQUEST_2) {

        sendResponse(channel, MSG_CANNED_RESPONSE_2, null);

    } else if (type == MSG_DATA_RECORD_64) {

        return decodeFixed64(channel, remoteAddress, buf);

    } else if (type == MSG_DATA_RECORD) {

        return decodeStandard(channel, remoteAddress, buf);

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    String uniqueId = null;//w ww  . j a  v  a  2  s  . co m
    DeviceSession deviceSession;

    if (buf.getByte(0) == 'E' && buf.getByte(1) == 'L') {
        buf.skipBytes(2 + 2 + 2); // udp header
        uniqueId = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
        deviceSession = getDeviceSession(channel, remoteAddress, uniqueId);
    } else {
        deviceSession = getDeviceSession(channel, remoteAddress);
    }

    buf.skipBytes(2); // header
    int type = buf.readUnsignedByte();
    buf.readShort(); // length
    int index = buf.readUnsignedShort();

    if (type != MSG_GPS && type != MSG_DATA) {
        ByteBuf content = Unpooled.buffer();
        if (type == MSG_LOGIN) {
            content.writeInt((int) (System.currentTimeMillis() / 1000));
            content.writeByte(1); // protocol version
            content.writeByte(0); // action mask
        }
        ByteBuf response = EelinkProtocolEncoder.encodeContent(channel instanceof DatagramChannel, uniqueId,
                type, index, content);
        content.release();
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
    }

    if (type == MSG_LOGIN) {

        if (deviceSession == null) {
            getDeviceSession(channel, remoteAddress, ByteBufUtil.hexDump(buf.readSlice(8)).substring(1));
        }

    } else {

        if (deviceSession == null) {
            return null;
        }

        if (type == MSG_GPS || type == MSG_ALARM || type == MSG_STATE || type == MSG_SMS) {

            return decodeOld(deviceSession, buf, type, index);

        } else if (type >= MSG_NORMAL && type <= MSG_OBD_CODE) {

            return decodeNew(deviceSession, buf, type, index);

        } else if (type == MSG_HEARTBEAT && buf.readableBytes() >= 2) {

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

            getLastLocation(position, null);

            decodeStatus(position, buf.readUnsignedShort());

            return position;

        } else if (type == MSG_OBD) {

            return decodeObd(deviceSession, buf, index);

        } else if (type == MSG_DOWNLINK) {

            return decodeResult(deviceSession, buf, index);

        }

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int index = buf.getUnsignedShort(buf.readerIndex() + 5 + 2);
    buf.skipBytes(buf.getUnsignedByte(buf.readerIndex() + 3));

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

    while (buf.readableBytes() > 2) {

        int length = buf.readUnsignedShortLE();
        int recordIndex = buf.readUnsignedShortLE();
        int recordFlags = buf.readUnsignedByte();

        if (BitUtil.check(recordFlags, 0)) {
            buf.readUnsignedIntLE(); // object id
        }/*  w w  w. j  ava  2 s  . c  o  m*/

        if (BitUtil.check(recordFlags, 1)) {
            buf.readUnsignedIntLE(); // event id
        }
        if (BitUtil.check(recordFlags, 2)) {
            buf.readUnsignedIntLE(); // time
        }

        int serviceType = buf.readUnsignedByte();
        buf.readUnsignedByte(); // recipient service type

        int recordEnd = buf.readerIndex() + length;

        Position position = new Position(getProtocolName());
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession != null) {
            position.setDeviceId(deviceSession.getDeviceId());
        }

        ByteBuf response = Unpooled.buffer();
        response.writeShortLE(recordIndex);
        response.writeByte(0); // success
        sendResponse(channel, PT_RESPONSE, index, serviceType, MSG_RECORD_RESPONSE, response);

        while (buf.readerIndex() < recordEnd) {
            int type = buf.readUnsignedByte();
            int end = buf.readUnsignedShortLE() + buf.readerIndex();

            if (type == MSG_TERM_IDENTITY) {

                buf.readUnsignedIntLE(); // object id
                int flags = buf.readUnsignedByte();

                if (BitUtil.check(flags, 0)) {
                    buf.readUnsignedShortLE(); // home dispatcher identifier
                }
                if (BitUtil.check(flags, 1)) {
                    getDeviceSession(channel, remoteAddress,
                            buf.readSlice(15).toString(StandardCharsets.US_ASCII).trim());
                }
                if (BitUtil.check(flags, 2)) {
                    getDeviceSession(channel, remoteAddress,
                            buf.readSlice(16).toString(StandardCharsets.US_ASCII).trim());
                }
                if (BitUtil.check(flags, 3)) {
                    buf.skipBytes(3); // language identifier
                }
                if (BitUtil.check(flags, 5)) {
                    buf.skipBytes(3); // network identifier
                }
                if (BitUtil.check(flags, 6)) {
                    buf.readUnsignedShortLE(); // buffer size
                }
                if (BitUtil.check(flags, 7)) {
                    getDeviceSession(channel, remoteAddress,
                            buf.readSlice(15).toString(StandardCharsets.US_ASCII).trim());
                }

                response = Unpooled.buffer();
                response.writeByte(0); // success
                sendResponse(channel, PT_APPDATA, 0, serviceType, MSG_RESULT_CODE, response);

            } else if (type == MSG_POS_DATA) {

                position.setTime(new Date((buf.readUnsignedIntLE() + 1262304000) * 1000)); // since 2010-01-01
                position.setLatitude(buf.readUnsignedIntLE() * 90.0 / 0xFFFFFFFFL);
                position.setLongitude(buf.readUnsignedIntLE() * 180.0 / 0xFFFFFFFFL);

                int flags = buf.readUnsignedByte();
                position.setValid(BitUtil.check(flags, 0));
                if (BitUtil.check(flags, 5)) {
                    position.setLatitude(-position.getLatitude());
                }
                if (BitUtil.check(flags, 6)) {
                    position.setLongitude(-position.getLongitude());
                }

                int speed = buf.readUnsignedShortLE();
                position.setSpeed(UnitsConverter.knotsFromKph(BitUtil.to(speed, 14) * 0.1));
                position.setCourse(buf.readUnsignedByte() + (BitUtil.check(speed, 15) ? 0x100 : 0));

                position.set(Position.KEY_ODOMETER, buf.readUnsignedMediumLE() * 100);
                position.set(Position.KEY_INPUT, buf.readUnsignedByte());
                position.set(Position.KEY_EVENT, buf.readUnsignedByte());

                if (BitUtil.check(flags, 7)) {
                    position.setAltitude(buf.readMediumLE());
                }

            } else if (type == MSG_EXT_POS_DATA) {

                int flags = buf.readUnsignedByte();

                if (BitUtil.check(flags, 0)) {
                    position.set(Position.KEY_VDOP, buf.readUnsignedShortLE());
                }
                if (BitUtil.check(flags, 1)) {
                    position.set(Position.KEY_HDOP, buf.readUnsignedShortLE());
                }
                if (BitUtil.check(flags, 2)) {
                    position.set(Position.KEY_PDOP, buf.readUnsignedShortLE());
                }
                if (BitUtil.check(flags, 3)) {
                    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
                }

            } else if (type == MSG_AD_SENSORS_DATA) {

                buf.readUnsignedByte(); // inputs flags

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

                buf.readUnsignedByte(); // adc flags

            }

            buf.readerIndex(end);
        }

        if (serviceType == SERVICE_TELEDATA && deviceSession != null) {
            positions.add(position);
        }
    }

    return positions.isEmpty() ? null : positions;
}

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

License:Apache License

private void decodeTagOther(Position position, ByteBuf buf, int tag) {
    switch (tag) {
    case 0x01:/* ww w .j a v a 2s.c o  m*/
        position.set(Position.KEY_VERSION_HW, buf.readUnsignedByte());
        break;
    case 0x02:
        position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
        break;
    case 0x04:
        position.set("deviceId", buf.readUnsignedShortLE());
        break;
    case 0x10:
        position.set(Position.KEY_INDEX, buf.readUnsignedShortLE());
        break;
    case 0x20:
        position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
        break;
    case 0x33:
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE() * 0.1));
        position.setCourse(buf.readUnsignedShortLE() * 0.1);
        break;
    case 0x34:
        position.setAltitude(buf.readShortLE());
        break;
    case 0x35:
        position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
        break;
    case 0x40:
        position.set(Position.KEY_STATUS, buf.readUnsignedShortLE());
        break;
    case 0x41:
        position.set(Position.KEY_POWER, buf.readUnsignedShortLE() / 1000.0);
        break;
    case 0x42:
        position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() / 1000.0);
        break;
    case 0x43:
        position.set(Position.KEY_DEVICE_TEMP, buf.readByte());
        break;
    case 0x44:
        position.set(Position.KEY_ACCELERATION, buf.readUnsignedIntLE());
        break;
    case 0x45:
        position.set(Position.KEY_OUTPUT, buf.readUnsignedShortLE());
        break;
    case 0x46:
        position.set(Position.KEY_INPUT, buf.readUnsignedShortLE());
        break;
    case 0x48:
        position.set("statusExtended", buf.readUnsignedShortLE());
        break;
    case 0x58:
        position.set("rs2320", buf.readUnsignedShortLE());
        break;
    case 0x59:
        position.set("rs2321", buf.readUnsignedShortLE());
        break;
    case 0x90:
        position.set(Position.KEY_DRIVER_UNIQUE_ID, String.valueOf(buf.readUnsignedIntLE()));
        break;
    case 0xc0:
        position.set("fuelTotal", buf.readUnsignedIntLE() * 0.5);
        break;
    case 0xc1:
        position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte() * 0.4);
        position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedByte() - 40);
        position.set(Position.KEY_RPM, buf.readUnsignedShortLE() * 0.125);
        break;
    case 0xc2:
        position.set("canB0", buf.readUnsignedIntLE());
        break;
    case 0xc3:
        position.set("canB1", buf.readUnsignedIntLE());
        break;
    case 0xd4:
        position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
        break;
    case 0xe0:
        position.set(Position.KEY_INDEX, buf.readUnsignedIntLE());
        break;
    case 0xe1:
        position.set(Position.KEY_RESULT,
                buf.readSlice(buf.readUnsignedByte()).toString(StandardCharsets.US_ASCII));
        break;
    case 0xea:
        position.set("userDataArray", ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte())));
        position.set("userDataArray", ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte())));
        break;
    default:
        buf.skipBytes(getTagLength(tag));
        break;
    }
}

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

License:Apache License

private Object decodePositions(Channel channel, SocketAddress remoteAddress, ByteBuf buf) throws Exception {

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

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

    DeviceSession deviceSession = null;/*from  w  w  w . j a  va  2  s. com*/
    Position position = new Position(getProtocolName());

    while (buf.readerIndex() < length) {

        int tag = buf.readUnsignedByte();
        if (tags.contains(tag)) {
            if (hasLocation && position.getFixTime() != null) {
                positions.add(position);
            }
            tags.clear();
            hasLocation = false;
            position = new Position(getProtocolName()); // new position starts
        }
        tags.add(tag);

        if (tag == 0x03) {
            deviceSession = getDeviceSession(channel, remoteAddress,
                    buf.readSlice(15).toString(StandardCharsets.US_ASCII));
        } else if (tag == 0x30) {
            hasLocation = true;
            position.setValid((buf.readUnsignedByte() & 0xf0) == 0x00);
            position.setLatitude(buf.readIntLE() / 1000000.0);
            position.setLongitude(buf.readIntLE() / 1000000.0);
        } else {
            decodeTag(position, buf, tag);
        }

    }

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

    if (hasLocation && position.getFixTime() != null) {
        positions.add(position);
    } else if (position.getAttributes().containsKey(Position.KEY_RESULT)) {
        position.setDeviceId(deviceSession.getDeviceId());
        getLastLocation(position, null);
        positions.add(position);
    }

    sendReply(channel, 0x02, buf.readUnsignedShortLE());

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

    return positions.isEmpty() ? null : positions;
}

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

License:Apache License

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

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

    int type = buf.readUnsignedByte();

    buf.readUnsignedInt(); // mask
    buf.readUnsignedShort(); // length
    buf.readUnsignedByte(); // device type
    buf.readUnsignedShort(); // protocol version
    buf.readUnsignedShort(); // firmware version

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

    int battery = buf.readUnsignedByte();
    int power = buf.readUnsignedShort();

    if (type == MSG_RSP_GEO) {
        buf.readUnsignedByte(); // reserved
        buf.readUnsignedByte(); // reserved
    }

    buf.readUnsignedByte(); // motion status
    int satellites = buf.readUnsignedByte();

    if (type != MSG_RSP_COMPRESSED) {
        buf.readUnsignedByte(); // index
    }

    if (type == MSG_RSP_LCB) {
        buf.readUnsignedByte(); // phone length
        for (int b = buf.readUnsignedByte();; b = buf.readUnsignedByte()) {
            if ((b & 0xf) == 0xf || (b & 0xf0) == 0xf0) {
                break;
            }
        }
    }

    if (type == MSG_RSP_COMPRESSED) {

        int count = buf.readUnsignedShort();

        BitBuffer bits;
        int speed = 0;
        int heading = 0;
        int latitude = 0;
        int longitude = 0;
        long time = 0;

        for (int i = 0; i < count; i++) {

            if (time > 0) {
                time += 1;
            }

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

            switch (BitUtil.from(buf.getUnsignedByte(buf.readerIndex()), 8 - 2)) {
            case 1:
                bits = new BitBuffer(buf.readSlice(3));
                bits.readUnsigned(2); // point attribute
                bits.readUnsigned(1); // fix type
                speed = bits.readUnsigned(12);
                heading = bits.readUnsigned(9);
                longitude = buf.readInt();
                latitude = buf.readInt();
                if (time == 0) {
                    time = buf.readUnsignedInt();
                }
                break;
            case 2:
                bits = new BitBuffer(buf.readSlice(5));
                bits.readUnsigned(2); // point attribute
                bits.readUnsigned(1); // fix type
                speed += bits.readSigned(7);
                heading += bits.readSigned(7);
                longitude += bits.readSigned(12);
                latitude += bits.readSigned(11);
                break;
            default:
                buf.readUnsignedByte(); // invalid or same
                continue;
            }

            position.setValid(true);
            position.setTime(new Date(time * 1000));
            position.setSpeed(UnitsConverter.knotsFromKph(speed * 0.1));
            position.setCourse(heading);
            position.setLongitude(longitude * 0.000001);
            position.setLatitude(latitude * 0.000001);

            positions.add(position);

        }

    } else {

        int count = buf.readUnsignedByte();

        for (int i = 0; i < count; i++) {

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

            position.set(Position.KEY_BATTERY_LEVEL, battery);
            position.set(Position.KEY_POWER, power);
            position.set(Position.KEY_SATELLITES, satellites);

            int hdop = buf.readUnsignedByte();
            position.setValid(hdop > 0);
            position.set(Position.KEY_HDOP, hdop);

            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedMedium() * 0.1));
            position.setCourse(buf.readUnsignedShort());
            position.setAltitude(buf.readShort());
            position.setLongitude(buf.readInt() * 0.000001);
            position.setLatitude(buf.readInt() * 0.000001);

            position.setTime(decodeTime(buf));

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

            buf.readUnsignedByte(); // reserved

            positions.add(position);

        }

    }

    return positions;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    switch (buf.readSlice(4).toString(StandardCharsets.US_ASCII)) {
    case "+RSP":
        return decodeLocation(channel, remoteAddress, buf);
    case "+INF":
        return decodeInformation(channel, remoteAddress, buf);
    case "+EVT":
        return decodeEvent(channel, remoteAddress, buf);
    default://from   www. j  a  v a  2s .  co m
        return null;
    }
}

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

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

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }//from ww  w.  ja  v a2  s .  c  o m

    if (type.startsWith("LOGN")) {

        ByteBuf content = Unpooled.copiedBuffer("1", StandardCharsets.US_ASCII);
        try {
            sendResponse(channel, "LGSA" + type.substring(4), imei, content);
        } finally {
            content.release();
        }

    } else if (type.startsWith("GPSL")) {

        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.setValid(true);
        position.setTime(dateBuilder.getDate());
        position.setLatitude(decodeCoordinate(buf));
        position.setLongitude(decodeCoordinate(buf));
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
        position.setCourse(buf.readUnsignedShort());

        decodeStatus(buf, position);

        sendResponse(channel, "GPSA" + type.substring(4), imei, buf.readSlice(2));

        return position;

    } else if (type.startsWith("SYNC")) {

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

        getLastLocation(position, null);

        decodeStatus(buf, position);

        sendResponse(channel, "SYSA" + type.substring(4), imei, null);

        return position;

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int indexTilde = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '~');

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);

    if (deviceSession != null && indexTilde == -1) {
        String bufString = buf.toString(StandardCharsets.US_ASCII);
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        position.setTime(new Date());
        getLastLocation(position, new Date());
        position.setValid(false);//from   w  w w .  j  av  a 2 s.c om
        position.set(Position.KEY_RESULT, bufString);
        return position;
    }

    if (buf.readableBytes() < HEADER_LENGTH) {
        return null;
    }
    String header = buf.readSlice(HEADER_LENGTH).toString(StandardCharsets.US_ASCII);

    if (header.equals("+RRCB~")) {

        buf.skipBytes(2); // binary length 26
        int deviceId = buf.readUnsignedShortLE();
        deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId));
        if (deviceSession == null) {
            return null;
        }
        long unixTime = buf.readUnsignedIntLE();
        if (channel != null) {
            sendResponseCurrent(channel, deviceId, unixTime);
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        position.setTime(new Date(unixTime * 1000));

        decodeStructure(buf, position);
        return position;

    } else if (header.equals("+DDAT~")) {

        buf.skipBytes(2); // binary length
        int deviceId = buf.readUnsignedShortLE();
        deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId));
        if (deviceSession == null) {
            return null;
        }
        byte format = buf.readByte();
        if (format != 4) {
            return null;
        }
        byte nblocks = buf.readByte();
        int packNum = buf.readUnsignedShortLE();
        if (channel != null) {
            sendResponseArchive(channel, deviceId, packNum);
        }
        List<Position> positions = new ArrayList<>();
        while (nblocks > 0) {
            nblocks--;
            long unixTime = buf.readUnsignedIntLE();
            int timeIncrement = buf.getUnsignedShortLE(buf.readerIndex() + 120);
            for (int i = 0; i < 6; i++) {
                if (buf.getUnsignedByte(buf.readerIndex()) != 0xFE) {
                    Position position = new Position(getProtocolName());
                    position.setDeviceId(deviceSession.getDeviceId());
                    position.setTime(new Date((unixTime + i * timeIncrement) * 1000));
                    decodeStructure(buf, position);
                    position.set(Position.KEY_ARCHIVE, true);
                    positions.add(position);
                } else {
                    buf.skipBytes(20); // skip filled 0xFE structure
                }
            }
            buf.skipBytes(2); // increment
        }
        return positions;

    }

    return null;
}