if ((version & 0x40) != 0) {
selector = buf.readUnsignedMedium();
}
// Create new position
Position position = new Position();
ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter(getProtocol());
try {
position.setDeviceId(getDataManager().getDeviceByImei(imei).getId());
} catch(Exception error) {
Log.warning("Unknown device - " + imei);
return null;
}
// Event
extendedInfo.set("event", buf.readUnsignedByte());
buf.readUnsignedByte();
// Validity
if ((selector & 0x0008) != 0) {
position.setValid((buf.readUnsignedByte() & 0x40) != 0);
} else {
return null; // no location data
}
// Time
if ((selector & 0x0004) != 0) {
buf.skipBytes(4); // snapshot time
}
// Location
if ((selector & 0x0008) != 0) {
position.setTime(new Date(buf.readUnsignedInt() * 1000));
position.setLatitude(buf.readInt() / 1000000.0);
position.setLongitude(buf.readInt() / 1000000.0);
extendedInfo.set("satellites", buf.readUnsignedByte());
}
// Speed and heading
if ((selector & 0x0010) != 0) {
position.setSpeed(buf.readUnsignedByte() * 0.539957);
buf.readUnsignedByte(); // maximum speed
position.setCourse(buf.readUnsignedByte() * 2.0);
} else {
position.setSpeed(0.0);
position.setCourse(0.0);
}
// Input
if ((selector & 0x0040) != 0) {
extendedInfo.set("input", buf.readUnsignedByte());
}
// ADC
if ((selector & 0x0020) != 0) {
extendedInfo.set("adc1", buf.readUnsignedShort());
extendedInfo.set("adc2", buf.readUnsignedShort());
extendedInfo.set("adc3", buf.readUnsignedShort());
extendedInfo.set("adc4", buf.readUnsignedShort());
}
// Power
if ((selector & 0x8000) != 0) {
extendedInfo.set("power", buf.readUnsignedShort() / 1000.0);
extendedInfo.set("battery", buf.readUnsignedShort());
}
// Pulse rate 1
if ((selector & 0x10000) != 0) {
buf.readUnsignedShort();
buf.readUnsignedInt();
}
// Pulse rate 2
if ((selector & 0x20000) != 0) {
buf.readUnsignedShort();
buf.readUnsignedInt();
}
// Trip 1
if ((selector & 0x0080) != 0) {
extendedInfo.set("trip1", buf.readUnsignedInt());
}
// Trip 2
if ((selector & 0x0100) != 0) {
extendedInfo.set("trip2", buf.readUnsignedInt());
}
// Output
if ((selector & 0x0040) != 0) {
extendedInfo.set("output", buf.readUnsignedByte());
}
// Button
if ((selector & 0x0200) != 0) {
buf.skipBytes(6);
}
// Keypad
if ((selector & 0x0400) != 0) {
buf.readUnsignedByte();
}
// Altitude
if ((selector & 0x0800) != 0) {
position.setAltitude((double) buf.readShort());
} else {
position.setAltitude(0.0);
}
position.setExtendedInfo(extendedInfo.toString());
return position;
}