@Override public PlayerPositionLookMessage decode(ChannelBuffer buffer) throws IOException { double x = buffer.readDouble(); double y = buffer.readDouble(); double stance = buffer.readDouble(); double z = buffer.readDouble(); float yaw = buffer.readFloat(); float pitch = buffer.readFloat(); boolean onGround = buffer.readByte() == 1; return new PlayerPositionLookMessage(x, y, z, stance, yaw, pitch, onGround); }
/** * Read double from this packet buffer. * * @return double */ protected final float readF() { try { return buf.readFloat(); } catch (Exception e) { log.error("Missing F for: " + this); } return 0; }
private ParseResult parsePosition(ChannelBuffer buf) { Position position = new Position(); position.setProtocol(getProtocolName()); position.setDeviceId(getDeviceId()); int format; if (buf.getUnsignedByte(buf.readerIndex()) == 0) { format = buf.readUnsignedShort(); } else { format = buf.readUnsignedByte(); } position.set("format", format); long index = buf.readUnsignedInt(); position.set(Event.KEY_INDEX, index); position.set(Event.KEY_EVENT, buf.readUnsignedShort()); buf.skipBytes(6); // event time position.set(Event.KEY_ALARM, buf.readUnsignedByte()); position.set(Event.KEY_STATUS, buf.readUnsignedByte()); position.set(Event.KEY_GSM, buf.readUnsignedByte()); if (isFormat(format, F10, F20, F30)) { position.set(Event.KEY_OUTPUT, buf.readUnsignedShort()); } else if (isFormat(format, F40, F50, F51, F52)) { position.set(Event.KEY_OUTPUT, buf.readUnsignedByte()); } if (isFormat(format, F10, F20, F30, F40)) { position.set(Event.KEY_INPUT, buf.readUnsignedShort()); } else if (isFormat(format, F50, F51, F52)) { position.set(Event.KEY_INPUT, buf.readUnsignedByte()); } position.set(Event.KEY_POWER, buf.readUnsignedShort() * 0.001); position.set(Event.KEY_BATTERY, buf.readUnsignedShort()); if (isFormat(format, F10, F20, F30)) { position.set(Event.PREFIX_TEMP + 1, buf.readShort()); } if (isFormat(format, F10, F20, F50, F52)) { position.set(Event.PREFIX_ADC + 1, buf.readUnsignedShort()); position.set(Event.PREFIX_ADC + 2, buf.readUnsignedShort()); } // Impulse counters if (isFormat(format, F20, F50, F51, F52)) { buf.readUnsignedInt(); buf.readUnsignedInt(); } if (isFormat(format, F20, F50, F51, F52)) { int locationStatus = buf.readUnsignedByte(); position.setValid(BitUtil.check(locationStatus, 1)); DateBuilder dateBuilder = new DateBuilder() .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) .setDateReverse( buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); position.setTime(dateBuilder.getDate()); position.setLatitude(buf.readFloat() / Math.PI * 180); position.setLongitude(buf.readFloat() / Math.PI * 180); position.setSpeed(buf.readFloat()); position.setCourse(buf.readUnsignedShort()); position.set(Event.KEY_ODOMETER, buf.readFloat()); position.set("segment", buf.readFloat()); // last segment // Segment times buf.readUnsignedShort(); buf.readUnsignedShort(); } // Other if (isFormat(format, F51, F52)) { buf.readUnsignedShort(); buf.readByte(); buf.readUnsignedShort(); buf.readUnsignedShort(); buf.readByte(); buf.readUnsignedShort(); buf.readUnsignedShort(); buf.readByte(); buf.readUnsignedShort(); } // Four temperature sensors if (isFormat(format, F40, F52)) { buf.readByte(); buf.readByte(); buf.readByte(); buf.readByte(); } return new ParseResult(index, position); }
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ChannelBuffer buf = (ChannelBuffer) msg; buf.readUnsignedShort(); // device id buf.readUnsignedByte(); // length int type = buf.readUnsignedByte(); DeviceSession deviceSession; if (type == MSG_IMEI) { deviceSession = getDeviceSession( channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII)); } else { deviceSession = getDeviceSession(channel, remoteAddress); } if (deviceSession == null) { return null; } if (BitUtil.to(type, 2) == 0) { Position position = new Position(); position.setProtocol(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); buf.readUnsignedByte(); // firmware version buf.readUnsignedShort(); // index position.set(Position.KEY_STATUS, buf.readUnsignedShort()); position.setValid(true); position.setLatitude(buf.readFloat()); position.setLongitude(buf.readFloat()); position.setCourse(buf.readUnsignedShort() * 0.1); position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort() * 0.1)); buf.readUnsignedByte(); // acceleration position.setAltitude(buf.readUnsignedShort()); position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1); position.set(Position.KEY_SATELLITES, buf.readUnsignedByte() & 0x0f); position.setTime(new Date(buf.readUnsignedInt() * 1000)); position.set(Position.KEY_POWER, buf.readUnsignedShort()); position.set(Position.KEY_BATTERY, buf.readUnsignedShort()); if (BitUtil.check(type, 2)) { buf.skipBytes(4); } if (BitUtil.check(type, 3)) { buf.skipBytes(12); } if (BitUtil.check(type, 4)) { buf.skipBytes(8); } if (BitUtil.check(type, 5)) { buf.skipBytes(9); } if (BitUtil.check(type, 6)) { buf.skipBytes(buf.getUnsignedByte(buf.readerIndex())); } if (BitUtil.check(type, 7)) { position.set(Position.KEY_ODOMETER, buf.readUnsignedInt()); } return position; } return null; }