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.GatorProtocolDecoder.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(); // length

    String id = decodeId(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(),
            buf.readUnsignedByte());//from   w  w  w.j  a  va2s .  c  o m

    sendResponse(channel, remoteAddress, buf.getByte(buf.writerIndex() - 2));

    if (type == MSG_POSITION_DATA || type == MSG_ROLLCALL_RESPONSE || type == MSG_ALARM_DATA
            || type == MSG_BLIND_AREA) {

        Position position = new Position(getProtocolName());

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, "1" + id, id);
        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));

        int flags = buf.readUnsignedByte();
        position.setValid((flags & 0x80) != 0);
        position.set(Position.KEY_SATELLITES, flags & 0x0f);

        position.set(Position.KEY_STATUS, buf.readUnsignedByte());
        position.set("key", buf.readUnsignedByte());
        position.set("oil", buf.readUnsignedShort() / 10.0);
        position.set(Position.KEY_POWER, buf.readUnsignedByte() + buf.readUnsignedByte() * 0.01);
        position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());

        return position;
    }

    return null;
}

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   www  . j  a  v a 2  s  .  com

    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

private Position decodeEvent(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

    Position position = new Position(getProtocolName());

    int type = buf.readUnsignedByte();

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

    position.set(Position.KEY_VERSION_FW, String.valueOf(buf.readUnsignedShort()));

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
            String.format("%015d", buf.readLong()));
    if (deviceSession == null) {
        return null;
    }//  w  w w  .j  a  v a  2  s .  com
    position.setDeviceId(deviceSession.getDeviceId());

    position.set(Position.KEY_BATTERY_LEVEL, buf.readUnsignedByte());
    position.set(Position.KEY_POWER, buf.readUnsignedShort());

    buf.readUnsignedByte(); // motion status

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

    switch (type) {
    case MSG_EVT_BPL:
        buf.readUnsignedShort(); // backup battery voltage
        break;
    case MSG_EVT_VGN:
    case MSG_EVT_VGF:
        buf.readUnsignedShort(); // reserved
        buf.readUnsignedByte(); // report type
        buf.readUnsignedInt(); // ignition duration
        break;
    case MSG_EVT_UPD:
        buf.readUnsignedShort(); // code
        buf.readUnsignedByte(); // retry
        break;
    case MSG_EVT_IDF:
        buf.readUnsignedInt(); // idling duration
        break;
    case MSG_EVT_GSS:
        buf.readUnsignedByte(); // gps signal status
        buf.readUnsignedInt(); // reserved
        break;
    case MSG_EVT_GES:
        buf.readUnsignedShort(); // trigger geo id
        buf.readUnsignedByte(); // trigger geo enable
        buf.readUnsignedByte(); // trigger mode
        buf.readUnsignedInt(); // radius
        buf.readUnsignedInt(); // check interval
        break;
    case MSG_EVT_GPJ:
        buf.readUnsignedByte(); // cw jamming value
        buf.readUnsignedByte(); // gps jamming state
        break;
    case MSG_EVT_RMD:
        buf.readUnsignedByte(); // roaming state
        break;
    case MSG_EVT_JDS:
        buf.readUnsignedByte(); // jamming state
        break;
    case MSG_EVT_CRA:
        buf.readUnsignedByte(); // crash counter
        break;
    case MSG_EVT_UPC:
        buf.readUnsignedByte(); // command id
        buf.readUnsignedShort(); // result
        break;
    default:
        break;
    }

    buf.readUnsignedByte(); // count

    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

    return position;
}

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

License:Apache License

private Position decodeInformation(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

    Position position = new Position(getProtocolName());

    int type = buf.readUnsignedByte();

    buf.readUnsignedInt(); // mask
    buf.readUnsignedShort(); // length

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
            String.format("%015d", buf.readLong()));
    if (deviceSession == null) {
        return null;
    }//from   w  w  w.  ja  va2  s  .c  om
    position.setDeviceId(deviceSession.getDeviceId());

    buf.readUnsignedByte(); // device type
    buf.readUnsignedShort(); // protocol version

    position.set(Position.KEY_VERSION_FW, String.valueOf(buf.readUnsignedShort()));

    if (type == MSG_INF_VER) {
        buf.readUnsignedShort(); // hardware version
        buf.readUnsignedShort(); // mcu version
        buf.readUnsignedShort(); // reserved
    }

    buf.readUnsignedByte(); // motion status
    buf.readUnsignedByte(); // reserved

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

    buf.readUnsignedByte(); // mode
    buf.skipBytes(7); // last fix time
    buf.readUnsignedByte(); // reserved
    buf.readUnsignedByte();
    buf.readUnsignedShort(); // response report mask
    buf.readUnsignedShort(); // ign interval
    buf.readUnsignedShort(); // igf interval
    buf.readUnsignedInt(); // reserved
    buf.readUnsignedByte(); // reserved

    if (type == MSG_INF_BAT) {
        position.set(Position.KEY_CHARGE, buf.readUnsignedByte() != 0);
        position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
        position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
        position.set(Position.KEY_BATTERY_LEVEL, buf.readUnsignedByte());
    }

    buf.skipBytes(10); // iccid

    if (type == MSG_INF_CSQ) {
        position.set(Position.KEY_RSSI, buf.readUnsignedByte());
        buf.readUnsignedByte();
    }

    buf.readUnsignedByte(); // time zone flags
    buf.readUnsignedShort(); // time zone offset

    if (type == MSG_INF_GIR) {
        buf.readUnsignedByte(); // gir trigger
        buf.readUnsignedByte(); // cell number
        position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(),
                buf.readUnsignedShort(), buf.readUnsignedShort())));
        buf.readUnsignedByte(); // ta
        buf.readUnsignedByte(); // rx level
    }

    getLastLocation(position, decodeTime(buf));

    return position;
}

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

    Position position = new Position(getProtocolName());

    // Zero for location messages
    int power = buf.readUnsignedByte();
    int gsm = buf.readUnsignedByte();

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

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

    int type = buf.readUnsignedByte();

    if (type == MSG_HEARTBEAT) {

        getLastLocation(position, null);

        position.set(Position.KEY_POWER, power);
        position.set(Position.KEY_RSSI, gsm);

        if (channel != null) {
            byte[] response = { 0x54, 0x68, 0x1A, 0x0D, 0x0A };
            channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(response), remoteAddress));
        }

    } else if (type == MSG_DATA) {

        DateBuilder dateBuilder = new DateBuilder()
                .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
                .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
        position.setTime(dateBuilder.getDate());

        double latitude = buf.readUnsignedInt() / (60.0 * 30000.0);
        double longitude = buf.readUnsignedInt() / (60.0 * 30000.0);

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

        buf.skipBytes(3); // reserved

        long flags = buf.readUnsignedInt();
        position.setValid(BitUtil.check(flags, 0));
        if (!BitUtil.check(flags, 1)) {
            latitude = -latitude;
        }
        if (!BitUtil.check(flags, 2)) {
            longitude = -longitude;
        }

        position.setLatitude(latitude);
        position.setLongitude(longitude);

    } else if (type == MSG_RESPONSE) {

        getLastLocation(position, null);

        position.set(Position.KEY_RESULT,
                buf.readSlice(buf.readUnsignedByte()).toString(StandardCharsets.US_ASCII));

    } else {

        return null;

    }

    return position;
}

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

License:Apache License

private boolean decodeGps(Position position, ByteBuf buf, boolean hasLength, TimeZone timezone) {

    DateBuilder dateBuilder = new DateBuilder(timezone)
            .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
            .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
    position.setTime(dateBuilder.getDate());

    if (hasLength && buf.readUnsignedByte() == 0) {
        return false;
    }/*from w w  w  . j  a v  a2  s .  c  o m*/

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

    double latitude = buf.readUnsignedInt() / 60.0 / 30000.0;
    double longitude = buf.readUnsignedInt() / 60.0 / 30000.0;
    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));

    int flags = buf.readUnsignedShort();
    position.setCourse(BitUtil.to(flags, 10));
    position.setValid(BitUtil.check(flags, 12));

    if (!BitUtil.check(flags, 10)) {
        latitude = -latitude;
    }
    if (BitUtil.check(flags, 11)) {
        longitude = -longitude;
    }

    position.setLatitude(latitude);
    position.setLongitude(longitude);

    if (BitUtil.check(flags, 14)) {
        position.set(Position.KEY_IGNITION, BitUtil.check(flags, 15));
    }

    return true;
}

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

License:Apache License

private Object decodeX1(Channel channel, ByteBuf buf, DeviceSession deviceSession, int type) {

    if (type == MSG_X1_GPS) {

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

        buf.readUnsignedInt(); // data and alarm

        decodeGps(position, buf, false, deviceSession.getTimeZone());

        buf.readUnsignedShort(); // terminal info

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

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

        long driverId = buf.readUnsignedInt();
        if (driverId > 0) {
            position.set(Position.KEY_DRIVER_UNIQUE_ID, String.valueOf(driverId));
        }//from  ww w . j av  a 2s  .  c  o  m

        position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.01);
        position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01);

        return position;

    } else if (type == MSG_X1_PHOTO_INFO) {

        buf.skipBytes(6); // time
        buf.readUnsignedByte(); // fix status
        buf.readUnsignedInt(); // latitude
        buf.readUnsignedInt(); // longitude
        buf.readUnsignedByte(); // camera id
        buf.readUnsignedByte(); // photo source
        buf.readUnsignedByte(); // picture format

        ByteBuf photo = Unpooled.buffer(buf.readInt());
        int pictureId = buf.readInt();
        photos.put(pictureId, photo);
        sendPhotoRequest(channel, pictureId);

    }

    return null;
}

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

License:Apache License

private Object decodeBasicOther(Channel channel, ByteBuf buf, DeviceSession deviceSession, int type,
        int dataLength) {

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

    if (type == MSG_LBS_MULTIPLE || type == MSG_LBS_EXTEND || type == MSG_LBS_WIFI || type == MSG_LBS_2
            || type == MSG_WIFI_3) {/*from ww  w  . j av  a 2 s .c  o m*/

        boolean longFormat = type == MSG_LBS_2 || type == MSG_WIFI_3;

        DateBuilder dateBuilder = new DateBuilder(deviceSession.getTimeZone())
                .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
                .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());

        getLastLocation(position, dateBuilder.getDate());

        int mcc = buf.readUnsignedShort();
        int mnc = BitUtil.check(mcc, 15) ? buf.readUnsignedShort() : buf.readUnsignedByte();
        Network network = new Network();
        for (int i = 0; i < 7; i++) {
            int lac = longFormat ? buf.readInt() : buf.readUnsignedShort();
            int cid = longFormat ? (int) buf.readLong() : buf.readUnsignedMedium();
            int rssi = -buf.readUnsignedByte();
            if (lac > 0) {
                network.addCellTower(CellTower.from(BitUtil.to(mcc, 15), mnc, lac, cid, rssi));
            }
        }

        buf.readUnsignedByte(); // time leads

        if (type != MSG_LBS_MULTIPLE && type != MSG_LBS_2) {
            int wifiCount = buf.readUnsignedByte();
            for (int i = 0; i < wifiCount; i++) {
                String mac = ByteBufUtil.hexDump(buf.readSlice(6)).replaceAll("(..)", "$1:");
                network.addWifiAccessPoint(
                        WifiAccessPoint.from(mac.substring(0, mac.length() - 1), buf.readUnsignedByte()));
            }
        }

        position.setNetwork(network);

    } else if (type == MSG_STRING) {

        getLastLocation(position, null);

        int commandLength = buf.readUnsignedByte();

        if (commandLength > 0) {
            buf.readUnsignedByte(); // server flag (reserved)
            position.set(Position.KEY_RESULT,
                    buf.readSlice(commandLength - 1).toString(StandardCharsets.US_ASCII));
        }

    } else if (isSupported(type)) {

        if (hasGps(type)) {
            decodeGps(position, buf, false, deviceSession.getTimeZone());
        } else {
            getLastLocation(position, null);
        }

        if (hasLbs(type)) {
            decodeLbs(position, buf, hasStatus(type));
        }

        if (hasStatus(type)) {
            decodeStatus(position, buf);
        }

        if (type == MSG_GPS_LBS_1 && buf.readableBytes() == 2 + 6) {
            int mask = buf.readUnsignedShort();
            position.set(Position.KEY_IGNITION, BitUtil.check(mask, 8 + 7));
            position.set(Position.PREFIX_IN + 2, BitUtil.check(mask, 8 + 6));
            if (BitUtil.check(mask, 8 + 4)) {
                int value = BitUtil.to(mask, 8 + 1);
                if (BitUtil.check(mask, 8 + 1)) {
                    value = -value;
                }
                position.set(Position.PREFIX_TEMP + 1, value);
            } else {
                int value = BitUtil.to(mask, 8 + 2);
                if (BitUtil.check(mask, 8 + 5)) {
                    position.set(Position.PREFIX_ADC + 1, value);
                } else {
                    position.set(Position.PREFIX_ADC + 1, value * 0.1);
                }
            }
        }

        if (type == MSG_GPS_LBS_1 && buf.readableBytes() == 4 + 6) {
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
        }

        if (type == MSG_GPS_LBS_2 && buf.readableBytes() == 3 + 6) {
            position.set(Position.KEY_IGNITION, buf.readUnsignedByte() > 0);
            position.set(Position.KEY_EVENT, buf.readUnsignedByte()); // reason
            position.set(Position.KEY_ARCHIVE, buf.readUnsignedByte() > 0);
        }

    } else {

        if (dataLength > 0) {
            buf.skipBytes(dataLength);
        }
        if (type != MSG_COMMAND_0 && type != MSG_COMMAND_1 && type != MSG_COMMAND_2) {
            sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null);
        }
        return null;

    }

    if (hasLanguage(type)) {
        buf.readUnsignedShort();
    }

    if (type == MSG_GPS_LBS_STATUS_3 || type == MSG_FENCE_MULTI) {
        position.set(Position.KEY_GEOFENCE, buf.readUnsignedByte());
    }

    sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null);

    return position;
}

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

License:Apache License

private Object decodeExtended(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

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

    if (deviceSession.getTimeZone() == null) {
        deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId()));
    }

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

    buf.readUnsignedShort(); // length
    int type = buf.readUnsignedByte();

    if (type == MSG_STRING_INFO) {

        buf.readUnsignedInt(); // server flag
        String data;
        if (buf.readUnsignedByte() == 1) {
            data = buf.readSlice(buf.readableBytes() - 6).toString(StandardCharsets.US_ASCII);
        } else {
            data = buf.readSlice(buf.readableBytes() - 6).toString(StandardCharsets.UTF_16BE);
        }

        if (decodeLocationString(position, data) == null) {
            getLastLocation(position, null);
            position.set(Position.KEY_RESULT, data);
        }

        return position;

    } else if (type == MSG_INFO) {

        int subType = buf.readUnsignedByte();

        getLastLocation(position, null);

        if (subType == 0x00) {
            position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01);
            return position;
        } else if (subType == 0x05) {
            int flags = buf.readUnsignedByte();
            position.set(Position.KEY_DOOR, BitUtil.check(flags, 0));
            position.set(Position.PREFIX_IO + 1, BitUtil.check(flags, 2));
            return position;
        } else if (subType == 0x0a) {
            buf.skipBytes(8); // imei
            buf.skipBytes(8); // imsi
            position.set("iccid", ByteBufUtil.hexDump(buf.readSlice(8)));
            return position;
        } else if (subType == 0x0d) {
            if (buf.getByte(buf.readerIndex()) != '!') {
                buf.skipBytes(6);
            }
            return decodeFuelData(position,
                    buf.toString(buf.readerIndex(), buf.readableBytes() - 4 - 2, StandardCharsets.US_ASCII));
        }

    } else if (type == MSG_X1_PHOTO_DATA) {

        int pictureId = buf.readInt();

        ByteBuf photo = photos.get(pictureId);

        buf.readUnsignedInt(); // offset
        buf.readBytes(photo, buf.readUnsignedShort());

        if (photo.writableBytes() > 0) {
            sendPhotoRequest(channel, pictureId);
        } else {
            Device device = Context.getDeviceManager().getById(deviceSession.getDeviceId());
            position.set(Position.KEY_IMAGE,
                    Context.getMediaManager().writeFile(device.getUniqueId(), photo, "jpg"));
            photos.remove(pictureId).release();
        }

    } else if (type == MSG_AZ735_GPS || type == MSG_AZ735_ALARM) {

        if (!decodeGps(position, buf, true, deviceSession.getTimeZone())) {
            getLastLocation(position, position.getDeviceTime());
        }

        if (decodeLbs(position, buf, true)) {
            position.set(Position.KEY_RSSI, buf.readUnsignedByte());
        }

        buf.skipBytes(buf.readUnsignedByte()); // additional cell towers
        buf.skipBytes(buf.readUnsignedByte()); // wifi access point

        int status = buf.readUnsignedByte();
        position.set(Position.KEY_STATUS, status);

        if (type == MSG_AZ735_ALARM) {
            switch (status) {
            case 0xA0:
                position.set(Position.KEY_ARMED, true);
                break;
            case 0xA1:
                position.set(Position.KEY_ARMED, false);
                break;
            case 0xA2:
            case 0xA3:
                position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY);
                break;
            case 0xA4:
                position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
                break;
            case 0xA5:
                position.set(Position.KEY_ALARM, Position.ALARM_DOOR);
                break;
            default:
                break;
            }
        }

        buf.skipBytes(buf.readUnsignedByte()); // reserved extension

        sendResponse(channel, true, type, buf.getShort(buf.writerIndex() - 6), null);

        return position;

    } else if (type == MSG_OBD) {

        DateBuilder dateBuilder = new DateBuilder(deviceSession.getTimeZone())
                .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
                .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());

        getLastLocation(position, dateBuilder.getDate());

        position.set(Position.KEY_IGNITION, buf.readUnsignedByte() > 0);

        String data = buf.readCharSequence(buf.readableBytes() - 18, StandardCharsets.US_ASCII).toString();
        for (String pair : data.split(",")) {
            String[] values = pair.split("=");
            switch (Integer.parseInt(values[0].substring(0, 2), 16)) {
            case 40:
                position.set(Position.KEY_ODOMETER, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 43:
                position.set(Position.KEY_FUEL_LEVEL, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 45:
                position.set(Position.KEY_COOLANT_TEMP, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 53:
                position.set(Position.KEY_OBD_SPEED, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 54:
                position.set(Position.KEY_RPM, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 71:
                position.set(Position.KEY_FUEL_USED, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 73:
                position.set(Position.KEY_HOURS, Integer.parseInt(values[1], 16) * 0.01);
                break;
            case 74:
                position.set(Position.KEY_VIN, values[1]);
                break;
            default:
                break;
            }
        }

        return position;

    }

    return null;
}

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

License:Apache License

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

    Position position = new Position(getProtocolName());

    boolean longId = buf.readableBytes() == 42;

    buf.readByte(); // marker

    String id;//from w w  w .ja  v  a 2 s .co m
    if (longId) {
        id = ByteBufUtil.hexDump(buf.readSlice(8)).substring(0, 15);
    } else {
        id = ByteBufUtil.hexDump(buf.readSlice(5));
    }

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

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

    double latitude = readCoordinate(buf, false);
    position.set(Position.KEY_BATTERY_LEVEL, decodeBattery(buf.readUnsignedByte()));
    double longitude = readCoordinate(buf, true);

    int flags = buf.readUnsignedByte() & 0x0f;
    position.setValid((flags & 0x02) != 0);
    if ((flags & 0x04) == 0) {
        latitude = -latitude;
    }
    if ((flags & 0x08) == 0) {
        longitude = -longitude;
    }

    position.setLatitude(latitude);
    position.setLongitude(longitude);

    position.setSpeed(BcdUtil.readInteger(buf, 3));
    position.setCourse((buf.readUnsignedByte() & 0x0f) * 100.0 + BcdUtil.readInteger(buf, 2));

    processStatus(position, buf.readUnsignedInt());

    return position;
}