Example usage for io.netty.buffer ByteBuf readShort

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

Introduction

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

Prototype

public abstract short readShort();

Source Link

Document

Gets a 16-bit short integer at the current readerIndex and increases the readerIndex by 2 in this buffer.

Usage

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

License:Apache License

private void decodeCanData(ByteBuf buf, Position position) {

    buf.readUnsignedMedium(); // packet identifier
    position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
    int count = buf.readUnsignedByte();
    buf.readUnsignedByte(); // batch count
    buf.readUnsignedShort(); // selector bit
    buf.readUnsignedInt(); // timestamp

    buf.skipBytes(8);/*from w  w  w  .  j  a  v a  2 s .  co m*/

    ArrayList<ByteBuf> values = new ArrayList<>(count);

    for (int i = 0; i < count; i++) {
        values.add(buf.readSlice(8));
    }

    for (int i = 0; i < count; i++) {
        ByteBuf value = values.get(i);
        switch (buf.readInt()) {
        case 0x20D:
            position.set(Position.KEY_RPM, value.readShortLE());
            position.set("dieselTemperature", value.readShortLE() * 0.1);
            position.set("batteryVoltage", value.readShortLE() * 0.01);
            position.set("supplyAirTempDep1", value.readShortLE() * 0.1);
            break;
        case 0x30D:
            position.set("activeAlarm", ByteBufUtil.hexDump(value));
            break;
        case 0x40C:
            position.set("airTempDep1", value.readShortLE() * 0.1);
            position.set("airTempDep2", value.readShortLE() * 0.1);
            break;
        case 0x40D:
            position.set("coldUnitState", ByteBufUtil.hexDump(value));
            break;
        case 0x50C:
            position.set("defrostTempDep1", value.readShortLE() * 0.1);
            position.set("defrostTempDep2", value.readShortLE() * 0.1);
            break;
        case 0x50D:
            position.set("condenserPressure", value.readShortLE() * 0.1);
            position.set("suctionPressure", value.readShortLE() * 0.1);
            break;
        case 0x58C:
            value.readByte();
            value.readShort(); // index
            switch (value.readByte()) {
            case 0x01:
                position.set("setpointZone1", value.readIntLE() * 0.1);
                break;
            case 0x02:
                position.set("setpointZone2", value.readIntLE() * 0.1);
                break;
            case 0x05:
                position.set("unitType", value.readIntLE());
                break;
            case 0x13:
                position.set("dieselHours", value.readIntLE() / 60 / 60);
                break;
            case 0x14:
                position.set("electricHours", value.readIntLE() / 60 / 60);
                break;
            case 0x17:
                position.set("serviceIndicator", value.readIntLE());
                break;
            case 0x18:
                position.set("softwareVersion", value.readIntLE() * 0.01);
                break;
            default:
                break;
            }
            break;
        default:
            LOGGER.warn("Aplicom CAN decoding error", new UnsupportedOperationException());
            break;
        }
    }
}

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

License:Apache License

private void decodeD(Position position, ByteBuf buf, int selector, int event) {

    if ((selector & 0x0008) != 0) {
        position.setValid((buf.readUnsignedByte() & 0x40) != 0);
    } else {/*from w  ww.j a v  a  2 s . c  o  m*/
        getLastLocation(position, null);
    }

    if ((selector & 0x0004) != 0) {
        position.setDeviceTime(new Date(buf.readUnsignedInt() * 1000));
    }

    if ((selector & 0x0008) != 0) {
        position.setFixTime(new Date(buf.readUnsignedInt() * 1000));
        if (position.getDeviceTime() == null) {
            position.setDeviceTime(position.getFixTime());
        }
        position.setLatitude(buf.readInt() / 1000000.0);
        position.setLongitude(buf.readInt() / 1000000.0);
        position.set(Position.KEY_SATELLITES_VISIBLE, buf.readUnsignedByte());
    }

    if ((selector & 0x0010) != 0) {
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
        position.set("maximumSpeed", buf.readUnsignedByte());
        position.setCourse(buf.readUnsignedByte() * 2.0);
    }

    if ((selector & 0x0040) != 0) {
        position.set(Position.KEY_INPUT, buf.readUnsignedByte());
    }

    if ((selector & 0x0020) != 0) {
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 4, buf.readUnsignedShort());
    }

    if ((selector & 0x8000) != 0) {
        position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
        position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
    }

    // Pulse rate 1
    if ((selector & 0x10000) != 0) {
        buf.readUnsignedShort();
        buf.readUnsignedInt();
    }

    // Pulse rate 2
    if ((selector & 0x20000) != 0) {
        buf.readUnsignedShort();
        buf.readUnsignedInt();
    }

    if ((selector & 0x0080) != 0) {
        position.set("trip1", buf.readUnsignedInt());
    }

    if ((selector & 0x0100) != 0) {
        position.set("trip2", buf.readUnsignedInt());
    }

    if ((selector & 0x0040) != 0) {
        position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());
    }

    if ((selector & 0x0200) != 0) {
        position.set(Position.KEY_DRIVER_UNIQUE_ID,
                String.valueOf(((long) buf.readUnsignedShort()) << 32) + buf.readUnsignedInt());
    }

    if ((selector & 0x0400) != 0) {
        buf.readUnsignedByte(); // Keypad
    }

    if ((selector & 0x0800) != 0) {
        position.setAltitude(buf.readShort());
    }

    if ((selector & 0x2000) != 0) {
        buf.readUnsignedShort(); // snapshot counter
    }

    if ((selector & 0x4000) != 0) {
        buf.skipBytes(8); // state flags
    }

    if ((selector & 0x80000) != 0) {
        buf.skipBytes(11); // cell info
    }

    if ((selector & 0x1000) != 0) {
        decodeEventData(position, buf, event);
    }

    if (Context.getConfig().getBoolean(getProtocolName() + ".can") && buf.isReadable()
            && (selector & 0x1000) != 0 && event == EVENT_DATA) {
        decodeCanData(buf, position);
    }
}

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

License:Apache License

private void decodeH(Position position, ByteBuf buf, int selector) {

    if ((selector & 0x0004) != 0) {
        getLastLocation(position, new Date(buf.readUnsignedInt() * 1000));
    } else {/*from   w ww  . j  a v  a 2  s .co m*/
        getLastLocation(position, null);
    }

    if ((selector & 0x0040) != 0) {
        buf.readUnsignedInt(); // reset time
    }

    if ((selector & 0x2000) != 0) {
        buf.readUnsignedShort(); // snapshot counter
    }

    int index = 1;
    while (buf.readableBytes() > 0) {

        position.set("h" + index + "Index", buf.readUnsignedByte());

        buf.readUnsignedShort(); // length

        int n = buf.readUnsignedByte();
        int m = buf.readUnsignedByte();

        position.set("h" + index + "XLength", n);
        position.set("h" + index + "YLength", m);

        if ((selector & 0x0008) != 0) {
            position.set("h" + index + "XType", buf.readUnsignedByte());
            position.set("h" + index + "YType", buf.readUnsignedByte());
            position.set("h" + index + "Parameters", buf.readUnsignedByte());
        }

        boolean percentageFormat = (selector & 0x0020) != 0;

        StringBuilder data = new StringBuilder();
        for (int i = 0; i < n * m; i++) {
            if (percentageFormat) {
                data.append(buf.readUnsignedByte() * 0.5).append("%").append(" ");
            } else {
                data.append(buf.readUnsignedShort()).append(" ");
            }
        }

        position.set("h" + index + "Data", data.toString().trim());

        position.set("h" + index + "Total", buf.readUnsignedInt());

        if ((selector & 0x0010) != 0) {
            int k = buf.readUnsignedByte();

            data = new StringBuilder();
            for (int i = 1; i < n; i++) {
                if (k == 1) {
                    data.append(buf.readByte()).append(" ");
                } else if (k == 2) {
                    data.append(buf.readShort()).append(" ");
                }
            }
            position.set("h" + index + "XLimits", data.toString().trim());

            data = new StringBuilder();
            for (int i = 1; i < m; i++) {
                if (k == 1) {
                    data.append(buf.readByte()).append(" ");
                } else if (k == 2) {
                    data.append(buf.readShort()).append(" ");
                }
            }
            position.set("h" + index + "YLimits", data.toString().trim());
        }

        index += 1;
    }
}

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

License:Apache License

private void decodeF(Position position, ByteBuf buf, int selector) {

    Date deviceTime = null;/*from   ww  w  .  j  a  v a2s  . c  om*/

    if ((selector & 0x0004) != 0) {
        deviceTime = new Date(buf.readUnsignedInt() * 1000);
    }

    getLastLocation(position, deviceTime);

    buf.readUnsignedByte(); // data validity

    if ((selector & 0x0008) != 0) {
        position.set(Position.KEY_RPM, buf.readUnsignedShort());
        position.set("rpmMax", buf.readUnsignedShort());
        position.set("rpmMin", buf.readUnsignedShort());
    }

    if ((selector & 0x0010) != 0) {
        position.set("engineTemp", buf.readShort());
        position.set("engineTempMax", buf.readShort());
        position.set("engineTempMin", buf.readShort());
    }

    if ((selector & 0x0020) != 0) {
        position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(buf.readUnsignedInt()));
        position.set("serviceDistance", buf.readInt());
        position.set("driverActivity", buf.readUnsignedByte());
        position.set(Position.KEY_THROTTLE, buf.readUnsignedByte());
        position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte());
    }

    if ((selector & 0x0040) != 0) {
        position.set(Position.KEY_FUEL_USED, buf.readUnsignedInt());
    }

    if ((selector & 0x0080) != 0) {
        position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
    }

    if ((selector & 0x0100) != 0) {
        position.set(Position.KEY_OBD_SPEED, buf.readUnsignedByte());
        position.set("speedMax", buf.readUnsignedByte());
        position.set("speedMin", buf.readUnsignedByte());
        position.set("hardBraking", buf.readUnsignedByte());
    }

    if ((selector & 0x0200) != 0) {
        position.set("tachographSpeed", buf.readUnsignedByte());
        position.set("driver1State", buf.readUnsignedByte());
        position.set("driver2State", buf.readUnsignedByte());
        position.set("tachographStatus", buf.readUnsignedByte());
        position.set("overspeedCount", buf.readUnsignedByte());
    }

    if ((selector & 0x0800) != 0) {
        position.set(Position.KEY_HOURS, buf.readUnsignedInt() * 0.05);
        position.set(Position.KEY_RPM, buf.readUnsignedShort() * 0.125);
        position.set(Position.KEY_OBD_SPEED, buf.readUnsignedShort() / 256.0);
        position.set(Position.KEY_FUEL_USED, buf.readUnsignedInt() * 0.5);
        position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte() * 0.4);
    }

    if ((selector & 0x1000) != 0) {
        position.set("ambientTemperature", buf.readUnsignedShort() * 0.03125 - 273);
        buf.readUnsignedShort(); // fuel rate
        position.set("fuelEconomy", buf.readUnsignedShort() / 512.0);
        position.set(Position.KEY_FUEL_CONSUMPTION, buf.readUnsignedInt() * 0.001);
        buf.readUnsignedByte(); // pto drive engagement
    }

    if ((selector & 0x2000) != 0) {
        buf.skipBytes(buf.readUnsignedByte()); // driver identification
    }

    if ((selector & 0x4000) != 0) {
        position.set("torque", buf.readUnsignedByte());
        position.set("brakePressure1", buf.readUnsignedByte() * 8);
        position.set("brakePressure2", buf.readUnsignedByte() * 8);
        position.set("grossWeight", buf.readUnsignedShort() * 10);
        position.set("exhaustFluid", buf.readUnsignedByte() * 0.4);
        buf.readUnsignedByte(); // retarder torque mode
        position.set("retarderTorque", buf.readUnsignedByte());
        position.set("retarderSelection", buf.readUnsignedByte() * 0.4);
        buf.skipBytes(8); // tell tale status block 1
        buf.skipBytes(8); // tell tale status block 2
        buf.skipBytes(8); // tell tale status block 3
        buf.skipBytes(8); // tell tale status block 4
    }

    if ((selector & 0x8000) != 0) {
        position.set("parkingBrakeStatus", buf.readUnsignedByte());
        position.set("doorStatus", buf.readUnsignedByte());
        buf.skipBytes(8); // status per door
        position.set("alternatorStatus", buf.readUnsignedByte());
        position.set("selectedGear", buf.readUnsignedByte());
        position.set("currentGear", buf.readUnsignedByte());
        buf.skipBytes(4 * 2); // air suspension pressure
    }

    if ((selector & 0x0400) != 0) {
        int count = buf.readUnsignedByte();
        for (int i = 0; i < count; i++) {
            position.set("axle" + i, buf.readUnsignedShort());
        }
    }

}

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

License:Apache License

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

    buf.skipBytes(2); // prefix
    buf.readUnsignedShort(); // checksum
    buf.readUnsignedShort(); // length
    int index = buf.readUnsignedShort();

    long id = buf.readLong();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(id));
    if (deviceSession == null) {
        return null;
    }//w ww .  j a  v  a 2 s .  c  o  m

    sendResponse(channel, remoteAddress, id, index);

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

    while (buf.readableBytes() >= MIN_DATA_LENGTH) {

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

        if (longDate) {

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

            buf.skipBytes(7 + 7);

        } else {
            position.setFixTime(new Date(buf.readUnsignedInt() * 1000));
            position.setDeviceTime(new Date(buf.readUnsignedInt() * 1000));
            buf.readUnsignedInt(); // send time
        }

        position.setValid(true);
        position.setLongitude(buf.readInt() * 0.000001);
        position.setLatitude(buf.readInt() * 0.000001);
        position.setCourse(buf.readUnsignedShort());

        int type = buf.readUnsignedByte();
        position.set(Position.KEY_TYPE, type);
        position.set(Position.KEY_ALARM, alarmMap.get(type));

        position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 100);
        position.set(Position.KEY_HDOP, buf.readUnsignedShort() * 0.1);
        position.set(Position.KEY_INPUT, buf.readUnsignedByte());

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

        position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort() * 0.001);

        position.set(Position.KEY_DRIVER_UNIQUE_ID, readString(buf));

        position.set(Position.PREFIX_TEMP + 1, buf.readShort() * 0.1);
        position.set(Position.PREFIX_TEMP + 2, buf.readShort() * 0.1);

        String message = readString(buf);
        if (message != null && !message.isEmpty()) {
            Pattern pattern = Pattern.compile("FULS:F=(\\p{XDigit}+) t=(\\p{XDigit}+) N=(\\p{XDigit}+)");
            Matcher matcher = pattern.matcher(message);
            if (matcher.find()) {
                int value = Integer.parseInt(matcher.group(3), decimalFuel ? 10 : 16);
                position.set(Position.KEY_FUEL_LEVEL, value * 0.1);
            } else {
                position.set("message", message);
            }
        }

        if (custom) {
            String form = this.form;
            if (form == null) {
                form = readString(buf).trim().substring("%CI".length());
            }
            readBinaryCustomData(position, buf, form);
        }

        positions.add(position);

    }

    return positions;
}

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

License:Apache License

private Position decodePosition(DeviceSession deviceSession, ByteBuf buf, boolean history) {

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

    if (!history) {
        buf.readUnsignedByte(); // interval
        buf.skipBytes(8); // settings
    }/*from  w w  w  . j  a  v  a 2  s.  c o m*/
    position.set(Position.KEY_STATUS, buf.readUnsignedByte());
    if (!history) {
        buf.readUnsignedShort();
    }
    position.set(Position.KEY_BATTERY, buf.readUnsignedByte());
    buf.skipBytes(6); // time

    if (!history) {
        for (int i = 0; i < 2; i++) {
            buf.skipBytes(5); // time
            buf.readUnsignedShort(); // interval
            buf.skipBytes(5); // mode
        }
    }

    position.set(Position.PREFIX_TEMP + 1, buf.readByte());

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

    int valid = buf.readUnsignedByte();
    position.setValid((valid & 0xc0) != 0);
    position.set(Position.KEY_SATELLITES, valid & 0x3f);

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

    position.setLatitude(convertCoordinate(buf.readInt()));
    position.setLongitude(convertCoordinate(buf.readInt()));
    position.setAltitude(buf.readShort());
    position.setSpeed(buf.readUnsignedByte());
    position.setCourse(buf.readUnsignedByte() * 2.0);

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

    buf.readUnsignedShort(); // reserved
    buf.readUnsignedByte(); // checksum
    return position;
}

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

License:Apache License

private Position decodePosition(DeviceSession deviceSession, int type, ByteBuf buf) {

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

    position.setTime(new Date(buf.readUnsignedInt() * 1000));
    if (type != MSG_MINI_EVENT_REPORT) {
        buf.readUnsignedInt(); // fix time
    }//from ww w . j  a  v  a2s  .  co  m
    position.setLatitude(buf.readInt() * 0.0000001);
    position.setLongitude(buf.readInt() * 0.0000001);
    if (type != MSG_MINI_EVENT_REPORT) {
        position.setAltitude(buf.readInt() * 0.01);
        position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedInt()));
    }
    position.setCourse(buf.readShort());
    if (type == MSG_MINI_EVENT_REPORT) {
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
    }

    if (type == MSG_MINI_EVENT_REPORT) {
        position.set(Position.KEY_SATELLITES, buf.getUnsignedByte(buf.readerIndex()) & 0xf);
        position.setValid((buf.readUnsignedByte() & 0x20) == 0);
    } else {
        position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
        position.setValid((buf.readUnsignedByte() & 0x08) == 0);
    }

    if (type != MSG_MINI_EVENT_REPORT) {
        position.set("carrier", buf.readUnsignedShort());
        position.set(Position.KEY_RSSI, buf.readShort());
    }

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

    if (type != MSG_MINI_EVENT_REPORT) {
        position.set(Position.KEY_HDOP, buf.readUnsignedByte());
    }

    int input = buf.readUnsignedByte();
    position.set(Position.KEY_INPUT, input);
    position.set(Position.KEY_IGNITION, BitUtil.check(input, 0));

    if (type != MSG_MINI_EVENT_REPORT) {
        position.set(Position.KEY_STATUS, buf.readUnsignedByte());
    }

    if (type == MSG_EVENT_REPORT || type == MSG_MINI_EVENT_REPORT) {
        if (type != MSG_MINI_EVENT_REPORT) {
            buf.readUnsignedByte(); // event index
        }
        position.set(Position.KEY_EVENT, buf.readUnsignedByte());
    }

    int accType = BitUtil.from(buf.getUnsignedByte(buf.readerIndex()), 6);
    int accCount = BitUtil.to(buf.readUnsignedByte(), 6);

    if (type != MSG_MINI_EVENT_REPORT) {
        position.set("append", buf.readUnsignedByte());
    }

    if (accType == 1) {
        buf.readUnsignedInt(); // threshold
        buf.readUnsignedInt(); // mask
    }

    for (int i = 0; i < accCount; i++) {
        if (buf.readableBytes() >= 4) {
            position.set("acc" + i, buf.readUnsignedInt());
        }
    }

    return position;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

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

    int version = -1;
    if (header == 0x4040) {
        version = buf.readUnsignedByte();
    }// ww  w . j  a va  2 s  .  co  m

    ByteBuf id = buf.readSlice(20);
    short type = buf.readShort();

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
            id.toString(StandardCharsets.US_ASCII).trim());
    if (deviceSession == null) {
        return null;
    }

    switch (version) {
    case -1:
        return decodeMpip(channel, remoteAddress, buf, version, id, type, deviceSession);
    case 3:
    case 4:
        return decodeSc(channel, remoteAddress, buf, version, id, type, deviceSession);
    default:
        return decodeCc(channel, remoteAddress, buf, version, id, type, deviceSession);
    }
}

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

License:Apache License

private Position decodeNew(DeviceSession deviceSession, ByteBuf buf, int type, int index) {

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

    position.set(Position.KEY_INDEX, index);

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

    int flags = buf.readUnsignedByte();

    if (BitUtil.check(flags, 0)) {
        position.setLatitude(buf.readInt() / 1800000.0);
        position.setLongitude(buf.readInt() / 1800000.0);
        position.setAltitude(buf.readShort());
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
        position.setCourse(buf.readUnsignedShort());
        position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
    } else {/*from   ww  w .  j a v a 2s. c  o m*/
        getLastLocation(position, position.getDeviceTime());
    }

    if (BitUtil.check(flags, 1)) {
        position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(),
                buf.readUnsignedShort(), buf.readUnsignedInt(), buf.readUnsignedByte())));
    }

    if (BitUtil.check(flags, 2)) {
        buf.skipBytes(7); // bsid1
    }

    if (BitUtil.check(flags, 3)) {
        buf.skipBytes(7); // bsid2
    }

    if (BitUtil.check(flags, 4)) {
        buf.skipBytes(7); // bss0
    }

    if (BitUtil.check(flags, 5)) {
        buf.skipBytes(7); // bss1
    }

    if (BitUtil.check(flags, 6)) {
        buf.skipBytes(7); // bss2
    }

    if (type == MSG_WARNING) {

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

    } else if (type == MSG_REPORT) {

        buf.readUnsignedByte(); // report type

    }

    if (type == MSG_NORMAL || type == MSG_WARNING || type == MSG_REPORT) {

        int status = buf.readUnsignedShort();
        position.setValid(BitUtil.check(status, 0));
        if (BitUtil.check(status, 1)) {
            position.set(Position.KEY_IGNITION, BitUtil.check(status, 2));
        }
        position.set(Position.KEY_STATUS, status);

    }

    if (type == MSG_NORMAL) {

        if (buf.readableBytes() >= 2) {
            position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
        }

        if (buf.readableBytes() >= 4) {
            position.set(Position.PREFIX_ADC + 0, buf.readUnsignedShort());
            position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
        }

        if (buf.readableBytes() >= 4) {
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
        }

        if (buf.readableBytes() >= 4) {
            buf.readUnsignedShort(); // gsm counter
            buf.readUnsignedShort(); // gps counter
        }

        if (buf.readableBytes() >= 4) {
            position.set(Position.KEY_STEPS, buf.readUnsignedShort());
            buf.readUnsignedShort(); // walking time
        }

        if (buf.readableBytes() >= 12) {
            position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedShort() / 256.0);
            position.set("humidity", buf.readUnsignedShort() * 0.1);
            position.set("illuminance", buf.readUnsignedInt() / 256.0);
            position.set("co2", buf.readUnsignedInt());
        }

        if (buf.readableBytes() >= 2) {
            position.set(Position.PREFIX_TEMP + 2, buf.readShort() / 16.0);
        }

    }

    return position;
}

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;/*from ww w .  java  2s.com*/
    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;
}