Example usage for io.netty.buffer ByteBuf toString

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

Introduction

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

Prototype

public abstract String toString(Charset charset);

Source Link

Document

Decodes this buffer's readable bytes into a string with the specified character set name.

Usage

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;//  w w  w .java2  s. c  o m

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

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

        ByteBuf response = Unpooled.buffer(13);
        response.writeCharSequence("\r\n*KW", StandardCharsets.US_ASCII);
        response.writeByte(0);
        response.writeShortLE(response.capacity());
        response.writeShortLE(MSG_SHAKE_HAND_RESPONSE);
        response.writeByte(1); // status
        response.writeCharSequence("\r\n", StandardCharsets.US_ASCII);

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

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

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

        Position position = new Position(getProtocolName());

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

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

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

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

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

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

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

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

        return position;
    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;/*w  w  w .j  a  va2 s .  c om*/

    buf.readUnsignedShort(); // data length

    String imei = String.format("%015d", buf.readLong());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }

    int type = buf.readUnsignedByte();

    if (type == MSG_RECORDS || type == MSG_EXTENDED_RECORDS) {

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

        buf.readUnsignedByte(); // records left
        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));
            buf.readUnsignedByte(); // timestamp extension

            if (type == MSG_EXTENDED_RECORDS) {
                buf.readUnsignedByte(); // record extension
            }

            buf.readUnsignedByte(); // priority (reserved)

            position.setValid(true);
            position.setLongitude(buf.readInt() / 10000000.0);
            position.setLatitude(buf.readInt() / 10000000.0);
            position.setAltitude(buf.readUnsignedShort() / 10.0);
            position.setCourse(buf.readUnsignedShort() / 100.0);

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

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

            position.set(Position.KEY_HDOP, buf.readUnsignedByte() / 10.0);

            if (type == MSG_EXTENDED_RECORDS) {
                position.set(Position.KEY_EVENT, buf.readUnsignedShort());
            } else {
                position.set(Position.KEY_EVENT, buf.readUnsignedByte());
            }

            // Read 1 byte data
            int cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 1);
            }

            // Read 2 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 2);
            }

            // Read 4 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 4);
            }

            // Read 8 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 8);
            }

            Long driverIdPart1 = (Long) position.getAttributes().remove(Position.PREFIX_IO + 126);
            Long driverIdPart2 = (Long) position.getAttributes().remove(Position.PREFIX_IO + 127);
            if (driverIdPart1 != null && driverIdPart2 != null) {
                ByteBuf driverId = Unpooled.copyLong(driverIdPart1, driverIdPart2);
                position.set(Position.KEY_DRIVER_UNIQUE_ID, driverId.toString(StandardCharsets.US_ASCII));
                driverId.release();
            }

            positions.add(position);
        }

        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(
                    Unpooled.wrappedBuffer(DataConverter.parseHex("0002640113bc")), remoteAddress));
        }

        return positions;

    } else if (type == MSG_DTCS) {

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

        int count = buf.readUnsignedByte();

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

            buf.readUnsignedByte(); // reserved

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

            position.setValid(true);
            position.setLongitude(buf.readInt() / 10000000.0);
            position.setLatitude(buf.readInt() / 10000000.0);

            if (buf.readUnsignedByte() == 2) {
                position.set(Position.KEY_ARCHIVE, true);
            }

            position.set(Position.KEY_DTCS, buf.readSlice(5).toString(StandardCharsets.US_ASCII));

            positions.add(position);
        }

        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(
                    Unpooled.wrappedBuffer(DataConverter.parseHex("00026d01c4a4")), remoteAddress));
        }

        return positions;

    } else {

        return decodeCommandResponse(deviceSession, type, buf);

    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedByte(); // header
    buf.readUnsignedShortLE(); // reserved
    buf.readUnsignedShortLE(); // length
    buf.readUnsignedByte(); // edition

    int type = buf.readUnsignedShortLE();

    buf.readUnsignedIntLE(); // command id

    sendResponse(channel, type);/*from  w  ww .  j a v  a  2s  .com*/

    if (type == MSG_LOGIN) {

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

    } else if (type == MSG_LOCATION) {

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

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

        getLastLocation(position, null);

        return position;

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    short header = buf.readShort();
    int type = buf.readUnsignedByte();
    buf.readUnsignedShort(); // length
    int index = buf.readUnsignedShort();
    ByteBuf imei = buf.readSlice(8);//from ww w. j  a va  2  s .co m

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
            ByteBufUtil.hexDump(imei).substring(1));
    if (deviceSession == null) {
        return null;
    }

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

        return decodePosition(channel, deviceSession, buf, header, type, index, imei);

    } else if (type == MSG_COMMAND) {

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

        getLastLocation(position, null);

        buf.readUnsignedByte(); // protocol number

        position.set(Position.KEY_RESULT, buf.toString(StandardCharsets.UTF_16LE));

        sendResponse(channel, header, type, index, imei, 0);

        return position;

    }

    sendResponse(channel, header, type, index, imei, 0);

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedByte(); // product type
    buf.readUnsignedByte(); // hardware version
    buf.readUnsignedByte(); // firmware version
    buf.readUnsignedByte(); // contact reason
    buf.readUnsignedByte(); // alarm / status
    buf.readUnsignedByte(); // rssi
    buf.readUnsignedByte(); // battery / status

    String imei = ByteBufUtil.hexDump(buf.readBytes(8)).substring(1);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }//from  w  ww  .  ja  v  a  2  s .  c o  m

    int type = BitUtil.to(buf.readUnsignedByte(), 6);
    buf.readUnsignedByte(); // length

    if (type == 4 || type == 8) {

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

        int count = buf.readUnsignedShort();
        buf.readUnsignedByte(); // hours / tickets
        buf.readUnsignedByte(); // error code
        buf.readUnsignedByte(); // reserved
        buf.readUnsignedByte(); // logger speed
        buf.readUnsignedByte(); // login time
        buf.readUnsignedByte(); // minutes

        for (int i = 0; i < count; i++) {
            position.set("rssi" + (i + 1), buf.readUnsignedByte());
            position.set("temp" + (i + 1), buf.readUnsignedByte() - 30);
            int data = buf.readUnsignedShort();
            position.set("src" + (i + 1), BitUtil.from(data, 10));
            position.set("ullage" + (i + 1), BitUtil.to(data, 10));
        }

        return position;

    } else if (type == 17) {

        String sentence = buf.toString(StandardCharsets.US_ASCII);

        Parser parser = new Parser(PATTERN, sentence);
        if (!parser.matches()) {
            return null;
        }

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

        DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(), parser.nextInt(),
                parser.nextInt());

        position.setLatitude(parser.nextCoordinate());
        position.setLongitude(parser.nextCoordinate());

        position.set(Position.KEY_HDOP, parser.nextDouble());

        position.setAltitude(parser.nextDouble());
        position.setValid(parser.nextInt() > 0);
        position.setCourse(parser.nextDouble());
        position.setSpeed(parser.nextDouble());

        dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt());
        position.setTime(dateBuilder.getDate());

        return position;

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;/*from   w  w w. ja v a 2  s.  c o  m*/

    buf.skipBytes(1); // header
    int type = buf.readUnsignedByte();
    ByteBuf dataSequence = buf.readSlice(10);
    int length = buf.readUnsignedByte();

    if (type == MSG_LOGIN_REQUEST || type == MSG_LOGIN_REQUEST_2) {

        ByteBuf data = buf.readSlice(length);

        String id;
        if (type == MSG_LOGIN_REQUEST) {
            id = data.toString(StandardCharsets.US_ASCII);
        } else {
            id = data.copy(1, 15).toString(StandardCharsets.US_ASCII);
        }

        if (getDeviceSession(channel, remoteAddress, id) != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeByte(MODE_GPRS);
            response.writeBytes(data);
            sendResponse(channel, MSG_LOGIN_RESPONSE, dataSequence, response);
        }

    } else if (type == MSG_HEARTBEAT_REQUEST) {

        sendResponse(channel, MSG_HEARTBEAT_RESPONSE, dataSequence, buf.readRetainedSlice(length));

    } else {

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

        Parser parser = new Parser(PATTERN, buf.readSlice(length).toString(StandardCharsets.US_ASCII));
        if (!parser.matches()) {
            return null;
        }

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

        DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0),
                parser.nextInt(0));

        position.setValid(parser.next().equals("A"));
        position.setLatitude(parser.nextCoordinate());
        position.setLongitude(parser.nextCoordinate());
        position.setSpeed(parser.nextDouble(0));

        dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
        position.setTime(dateBuilder.getDate());

        return position;

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int protocol = buf.readUnsignedByte();
    boolean legacy = protocol == 0x80;

    buf.readUnsignedByte(); // version id
    int index = legacy ? buf.readUnsignedShort() : buf.readUnsignedShortLE();
    int type = legacy ? buf.readUnsignedShort() : buf.readUnsignedShortLE();
    buf.readUnsignedShort(); // length
    buf.readUnsignedShort(); // mask
    buf.readUnsignedShort(); // checksum
    long id = legacy ? buf.readUnsignedInt() : buf.readUnsignedIntLE();
    buf.readUnsignedInt(); // time

    Position position = new Position(getProtocolName());
    position.set(Position.KEY_INDEX, index);
    position.setValid(true);//from   w w w. j  a  v a 2 s. c o m

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

    if (protocol == 0x01 && (type == MSG_COMPACT || type == MSG_FULL)) {

        // need to send ack?

        buf.readUnsignedShortLE(); // report trigger
        buf.readUnsignedShortLE(); // state flag

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

        position.set(Position.KEY_RSSI, buf.readUnsignedShortLE());
        position.set(Position.KEY_SATELLITES, buf.readUnsignedShortLE());
        position.set(Position.KEY_SATELLITES_VISIBLE, buf.readUnsignedShortLE());
        position.set("gpsAntennaStatus", buf.readUnsignedShortLE());

        position.setSpeed(buf.readUnsignedShortLE() * 0.194384);
        position.setCourse(buf.readUnsignedShortLE());

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

        position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE());

        position.set(Position.KEY_CHARGE, buf.readUnsignedShortLE());

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

        // parse other data

        return position;

    } else if (legacy) {

        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(
                    Unpooled.copiedBuffer("gprs,ack," + index, StandardCharsets.US_ASCII), remoteAddress));
        }

        String sentence = buf.toString(StandardCharsets.US_ASCII);

        Pattern pattern = Pattern.compile("(-?\\d+\\.\\d+), (-?\\d+\\.\\d+)");
        Matcher matcher = pattern.matcher(sentence);
        if (!matcher.find()) {
            return null;
        }
        position.setLatitude(Double.parseDouble(matcher.group(1)));
        position.setLongitude(Double.parseDouble(matcher.group(2)));

        pattern = Pattern.compile("([NSWE]{1,2}) with speed (\\d+) km/h");
        matcher = pattern.matcher(sentence);
        if (matcher.find()) {
            for (int i = 0; i < DIRECTIONS.length; i++) {
                if (matcher.group(1).equals(DIRECTIONS[i])) {
                    position.setCourse(i * 45.0);
                    break;
                }
            }
            position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(matcher.group(2))));
        }

        pattern = Pattern.compile("(\\d{1,2}:\\d{2}(:\\d{2})? \\w{3} \\d{1,2})");
        matcher = pattern.matcher(sentence);
        if (!matcher.find()) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat(
                matcher.group(2) != null ? "HH:mm:ss MMM d yyyy" : "HH:mm MMM d yyyy", Locale.ENGLISH);
        position.setTime(DateUtil.correctYear(
                dateFormat.parse(matcher.group(1) + " " + Calendar.getInstance().get(Calendar.YEAR))));

        if (sentence.contains("Ignition on detected")) {
            position.set(Position.KEY_IGNITION, true);
        } else if (sentence.contains("Ignition off detected")) {
            position.set(Position.KEY_IGNITION, false);
        }

        return position;

    }

    return null;
}

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

        if (channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeByte(0xF8);/*from w ww .j  ava  2  s .  c o  m*/
            response.writeByte(DATA_GPS);
            response.writeByte(0xFE);
            response.writeShort(buf.getShort(response.writerIndex() - 1 - 2));
            response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer(1, 4)));
            response.writeByte(0xF8);
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }

        return decodeBinary(channel, remoteAddress, buf);
    } else {

        if (channel != null) {
            channel.writeAndFlush(
                    new NetworkMessage(Unpooled.copiedBuffer(
                            String.format("*TS01,ACK:%04X#",
                                    Checksum.crc16(Checksum.CRC16_XMODEM,
                                            buf.nioBuffer(1, buf.writerIndex() - 2))),
                            StandardCharsets.US_ASCII), remoteAddress));
        }

        return decodeText(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII));
    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;//from   www. jav  a2  s. co  m

    if (buf.getByte(buf.readerIndex()) != '*') {
        return null;
    }

    int headerIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&');
    if (headerIndex < 0) {
        headerIndex = buf.writerIndex();
    }
    String header = buf.readSlice(headerIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII);

    Parser parser = new Parser(PATTERN_HEADER, header);
    if (!parser.matches()) {
        return null;
    }

    String head = parser.next();
    boolean reply = parser.next().equals("1");

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

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

    String type = parser.next();
    String subtype = parser.next();

    if (reply && channel != null) {
        channel.writeAndFlush(new NetworkMessage("*" + head + "Y" + type + subtype + "#", remoteAddress));
    }

    while (buf.isReadable()) {

        buf.readByte(); // skip delimiter

        byte dataType = buf.readByte();

        int delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&');
        if (delimiterIndex < 0) {
            delimiterIndex = buf.writerIndex();
        }

        ByteBuf data = buf.readSlice(delimiterIndex - buf.readerIndex());

        switch (dataType) {
        case 'A':
            decodeLocation(position, data.toString(StandardCharsets.US_ASCII));
            break;
        case 'B':
            position.set(Position.KEY_STATUS, data.toString(StandardCharsets.US_ASCII));
            break;
        case 'C':
            long odometer = 0;
            while (data.isReadable()) {
                odometer <<= 4;
                odometer += data.readByte() - (byte) '0';
            }
            position.set(Position.KEY_ODOMETER, odometer * 2 * 1852 / 3600);
            break;
        case 'F':
            position.setSpeed(Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)) * 0.1);
            break;
        case 'K':
            position.set("statusExtended", data.toString(StandardCharsets.US_ASCII));
            break;
        case 'P':
            if (data.readableBytes() >= 16) {
                position.setNetwork(new Network(
                        CellTower.from(Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)),
                                Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)),
                                Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII), 16),
                                Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII), 16))));
            }
            break;
        case 'Q':
            position.set("obdPid", ByteBufUtil.hexDump(data));
            break;
        case 'R':
            if (head.startsWith("HQ")) {
                position.set(Position.KEY_RSSI,
                        Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII)));
                position.set(Position.KEY_SATELLITES,
                        Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII)));
            } else {
                position.set("odbTravel", ByteBufUtil.hexDump(data));
            }
            break;
        case 'S':
            position.set("obdTraffic", ByteBufUtil.hexDump(data));
            break;
        case 'T':
            position.set(Position.KEY_BATTERY_LEVEL,
                    Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII)));
            break;
        case 'V':
            position.set(Position.KEY_POWER,
                    Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)) * 0.1);
            break;
        default:
            break;
        }

    }

    if (position.getLatitude() != 0 && position.getLongitude() != 0) {
        return position;
    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    if (buf.getUnsignedByte(0) == 0xD0) {

        long deviceId = ((Long.reverseBytes(buf.getLong(0))) >> 32) & 0xFFFFFFFFL;
        getDeviceSession(channel, remoteAddress, String.valueOf(deviceId));

        return null;
    } else if (buf.toString(StandardCharsets.US_ASCII).startsWith("$OK:")
            || buf.toString(StandardCharsets.US_ASCII).startsWith("$ERR:")
            || buf.toString(StandardCharsets.US_ASCII).startsWith("$MSG:")) {

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        getLastLocation(position, new Date());
        position.set(Position.KEY_RESULT, buf.toString(StandardCharsets.US_ASCII));

        return position;

    } else {/*from   w w w .j av a 2  s.  c  om*/

        Parser parser = new Parser(PATTERN, buf.toString(StandardCharsets.US_ASCII));
        if (!parser.matches()) {
            return null;
        }

        Position position = new Position(getProtocolName());

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

        position.setTime(parser.nextDateTime());

        position.setLongitude(parser.nextDouble(0));
        position.setLatitude(parser.nextDouble(0));
        position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
        position.setCourse(parser.nextDouble(0));
        position.setAltitude(parser.nextDouble(0));

        int satellites = parser.nextInt(0);
        position.setValid(satellites != 0);
        position.set(Position.KEY_SATELLITES, satellites);

        position.set(Position.KEY_EVENT, parser.next());
        position.set(Position.KEY_BATTERY, parser.nextDouble());
        if (parser.hasNext()) {
            position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000);
        }
        position.set(Position.KEY_INPUT, parser.next());
        position.set(Position.PREFIX_ADC + 1, parser.next());
        position.set(Position.PREFIX_ADC + 2, parser.next());
        position.set(Position.KEY_OUTPUT, parser.next());

        return position;

    }

}