}
}
private ParseResult parsePosition(ChannelBuffer buf) {
Position position = new Position();
ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter(getProtocol());
position.setDeviceId(databaseDeviceId);
position.setAltitude(0.0);
// Format type
int format;
if (buf.getUnsignedByte(buf.readerIndex()) == 0) {
format = buf.readUnsignedShort();
} else {
format = buf.readUnsignedByte();
}
extendedInfo.set("format", format);
long index = buf.readUnsignedInt();
extendedInfo.set("index", index);
// Event type
extendedInfo.set("event", buf.readUnsignedShort());
// Event time
Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
time.clear();
time.set(Calendar.HOUR_OF_DAY, buf.readUnsignedByte());
time.set(Calendar.MINUTE, buf.readUnsignedByte());
time.set(Calendar.SECOND, buf.readUnsignedByte());
time.set(Calendar.DAY_OF_MONTH, buf.readUnsignedByte());
time.set(Calendar.MONTH, buf.readUnsignedByte());
time.set(Calendar.YEAR, 2000 + buf.readUnsignedByte());
extendedInfo.set("time", time.getTimeInMillis());
// Alarm status
extendedInfo.set("alarm", buf.readUnsignedByte());
// Modules status
extendedInfo.set("status", buf.readUnsignedByte());
// GSM signal
extendedInfo.set("gsm", buf.readUnsignedByte());
// Output
if (isFormat(format, F10, F20, F30)) {
extendedInfo.set("output", buf.readUnsignedShort());
} else if (isFormat(format, F40, F50, F51, F52)) {
extendedInfo.set("output", buf.readUnsignedByte());
}
// Input
if (isFormat(format, F10, F20, F30, F40)) {
extendedInfo.set("input", buf.readUnsignedShort());
} else if (isFormat(format, F50, F51, F52)) {
extendedInfo.set("input", buf.readUnsignedByte());
}
extendedInfo.set("power", buf.readUnsignedShort() / 1000.0);
// Battery power
extendedInfo.set("battery", buf.readUnsignedShort());
// Temperature
if (isFormat(format, F10, F20, F30)) {
extendedInfo.set("temperature", buf.readShort());
}
if (isFormat(format, F10, F20, F50, F52)) {
extendedInfo.set("adc1", buf.readUnsignedShort());
extendedInfo.set("adc2", buf.readUnsignedShort());
}
if (isFormat(format, F20, F50, F51, F52)) {
// Impulse counters
buf.readUnsignedInt();
buf.readUnsignedInt();
}
if (isFormat(format, F20, F50, F51, F52)) {
// Validity
int locationStatus = buf.readUnsignedByte();
position.setValid((locationStatus & 0x02) == 0x02);
// Location time
time.clear();
time.set(Calendar.HOUR_OF_DAY, buf.readUnsignedByte());
time.set(Calendar.MINUTE, buf.readUnsignedByte());
time.set(Calendar.SECOND, buf.readUnsignedByte());
time.set(Calendar.DAY_OF_MONTH, buf.readUnsignedByte());
time.set(Calendar.MONTH, buf.readUnsignedByte());
time.set(Calendar.YEAR, 2000 + buf.readUnsignedByte());
position.setTime(time.getTime());
// Location data
position.setLatitude(buf.readFloat() / Math.PI * 180);
position.setLongitude(buf.readFloat() / Math.PI * 180);
position.setSpeed((double) buf.readFloat());
position.setCourse((double) buf.readUnsignedShort());
// Milage
extendedInfo.set("milage", buf.readFloat());
// Last segment
extendedInfo.set("segment", buf.readFloat());
// Segment times
buf.readUnsignedShort();
buf.readUnsignedShort();
}
if (isFormat(format, F51, F52)) {
// Other stuff
buf.readUnsignedShort();
buf.readByte();
buf.readUnsignedShort();
buf.readUnsignedShort();
buf.readByte();
buf.readUnsignedShort();
buf.readUnsignedShort();
buf.readByte();
buf.readUnsignedShort();
}
if (isFormat(format, F40, F52)) {
// Four temperature sensors
buf.readByte();
buf.readByte();
buf.readByte();
buf.readByte();
}
// Extended info
position.setExtendedInfo(extendedInfo.toString());
return new ParseResult(index, position);
}