type == MSG_GPS_LBS_STATUS_2 ||
type == MSG_GPS_PHONE ||
type == MSG_GPS_LBS_EXTEND)) {
// Create new position
Position position = new Position();
position.setDeviceId(deviceId);
ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter(getProtocol());
// Date and time
Calendar time = Calendar.getInstance(timeZone);
time.clear();
time.set(Calendar.YEAR, 2000 + buf.readUnsignedByte());
time.set(Calendar.MONTH, buf.readUnsignedByte() - 1);
time.set(Calendar.DAY_OF_MONTH, buf.readUnsignedByte());
time.set(Calendar.HOUR_OF_DAY, buf.readUnsignedByte());
time.set(Calendar.MINUTE, buf.readUnsignedByte());
time.set(Calendar.SECOND, buf.readUnsignedByte());
position.setTime(time.getTime());
// GPS length and Satellites count
int gpsLength = buf.readUnsignedByte();
extendedInfo.set("satellites", gpsLength & 0xf);
gpsLength >>= 4;
// Latitude
double latitude = buf.readUnsignedInt() / (60.0 * 30000.0);
// Longitude
double longitude = buf.readUnsignedInt() / (60.0 * 30000.0);
// Speed
position.setSpeed(buf.readUnsignedByte() * 0.539957);
// Course and flags
int union = buf.readUnsignedShort();
position.setCourse((double) (union & 0x03FF));
position.setValid((union & 0x1000) != 0);
if ((union & 0x0400) == 0) latitude = -latitude;
if ((union & 0x0800) != 0) longitude = -longitude;
position.setLatitude(latitude);
position.setLongitude(longitude);
position.setAltitude(0.0);
if ((union & 0x4000) != 0) {
extendedInfo.set("acc", (union & 0x8000) != 0);
}
buf.skipBytes(gpsLength - 12); // skip reserved
if (type == MSG_GPS_LBS_1 || type == MSG_GPS_LBS_2 ||
type == MSG_GPS_LBS_STATUS_1 || type == MSG_GPS_LBS_STATUS_2) {
int lbsLength = 0;
if (type == MSG_GPS_LBS_STATUS_1 || type == MSG_GPS_LBS_STATUS_2) {
lbsLength = buf.readUnsignedByte();
}
// Cell information
extendedInfo.set("mcc", buf.readUnsignedShort());
extendedInfo.set("mnc", buf.readUnsignedByte());
extendedInfo.set("lac", buf.readUnsignedShort());
extendedInfo.set("cell", buf.readUnsignedShort() << 8 + buf.readUnsignedByte());
buf.skipBytes(lbsLength - 9);
// Status
if (type == MSG_GPS_LBS_STATUS_1 || type == MSG_GPS_LBS_STATUS_2) {
extendedInfo.set("alarm", true);
int flags = buf.readUnsignedByte();
extendedInfo.set("acc", (flags & 0x2) != 0);
// TODO parse other flags
// Voltage
extendedInfo.set("power", buf.readUnsignedByte());
// GSM signal
extendedInfo.set("gsm", buf.readUnsignedByte());
}
}
// Index
if (buf.readableBytes() > 6) {
buf.skipBytes(buf.readableBytes() - 6);
}
int index = buf.readUnsignedShort();
extendedInfo.set("index", index);
sendResponse(channel, type, index);
position.setExtendedInfo(extendedInfo.toString());
return position;
}
else {
buf.skipBytes(dataLength);