List of usage examples for io.netty.buffer ByteBuf toString
public abstract String toString(int index, int length, Charset charset);
From source file:org.traccar.protocol.ProgressProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; int type = buf.readUnsignedShortLE(); buf.readUnsignedShortLE(); // length if (type == MSG_IDENT || type == MSG_IDENT_FULL) { buf.readUnsignedIntLE(); // id int length = buf.readUnsignedShortLE(); buf.skipBytes(length);/*from w w w . jav a 2 s .c o m*/ length = buf.readUnsignedShortLE(); buf.skipBytes(length); length = buf.readUnsignedShortLE(); String imei = buf.readSlice(length).toString(StandardCharsets.US_ASCII); getDeviceSession(channel, remoteAddress, imei); } else if (type == MSG_POINT || type == MSG_ALARM || type == MSG_LOGMSG) { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; } List<Position> positions = new LinkedList<>(); int recordCount = 1; if (type == MSG_LOGMSG) { recordCount = buf.readUnsignedShortLE(); } for (int j = 0; j < recordCount; j++) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); if (type == MSG_LOGMSG) { position.set(Position.KEY_ARCHIVE, true); int subtype = buf.readUnsignedShortLE(); if (subtype == MSG_ALARM) { position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); } if (buf.readUnsignedShortLE() > buf.readableBytes()) { lastIndex += 1; break; // workaround for device bug } lastIndex = buf.readUnsignedIntLE(); position.set(Position.KEY_INDEX, lastIndex); } else { newIndex = buf.readUnsignedIntLE(); } position.setTime(new Date(buf.readUnsignedIntLE() * 1000)); position.setLatitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF); position.setLongitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF); position.setSpeed(buf.readUnsignedIntLE() * 0.01); position.setCourse(buf.readUnsignedShortLE() * 0.01); position.setAltitude(buf.readUnsignedShortLE() * 0.01); int satellites = buf.readUnsignedByte(); position.setValid(satellites >= 3); position.set(Position.KEY_SATELLITES, satellites); position.set(Position.KEY_RSSI, buf.readUnsignedByte()); position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE()); long extraFlags = buf.readLongLE(); if (BitUtil.check(extraFlags, 0)) { int count = buf.readUnsignedShortLE(); for (int i = 1; i <= count; i++) { position.set(Position.PREFIX_ADC + i, buf.readUnsignedShortLE()); } } if (BitUtil.check(extraFlags, 1)) { int size = buf.readUnsignedShortLE(); position.set("can", buf.toString(buf.readerIndex(), size, StandardCharsets.US_ASCII)); buf.skipBytes(size); } if (BitUtil.check(extraFlags, 2)) { position.set("passenger", ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedShortLE()))); } if (type == MSG_ALARM) { position.set(Position.KEY_ALARM, true); byte[] response = { (byte) 0xC9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(response), remoteAddress)); } buf.readUnsignedIntLE(); // crc positions.add(position); } requestArchive(channel); return positions; } return null; }
From source file:org.traccar.protocol.Pt502FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 10) { return null; }// ww w .j a v a 2 s . c o m if (buf.getUnsignedByte(buf.readerIndex()) == 0xbf && buf.toString(buf.readerIndex() + BINARY_HEADER, 4, StandardCharsets.US_ASCII).equals("$PHD")) { int length = buf.getUnsignedShortLE(buf.readerIndex() + 3); if (buf.readableBytes() >= length) { buf.skipBytes(BINARY_HEADER); ByteBuf result = buf.readRetainedSlice(length - BINARY_HEADER - 2); buf.skipBytes(2); // line break return result; } } else { if (buf.getUnsignedByte(buf.readerIndex()) == 0xbf) { buf.skipBytes(BINARY_HEADER); } int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\r'); if (index < 0) { index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\n'); } if (index > 0) { ByteBuf result = buf.readRetainedSlice(index - buf.readerIndex()); while (buf.isReadable() && (buf.getByte(buf.readerIndex()) == '\r' || buf.getByte(buf.readerIndex()) == '\n')) { buf.skipBytes(1); } return result; } } return null; }
From source file:org.traccar.protocol.Pt502ProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; int typeEndIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); String type = buf.toString(buf.readerIndex(), typeEndIndex - buf.readerIndex(), StandardCharsets.US_ASCII); if (type.startsWith("$PHD")) { int dataIndex = buf.indexOf(typeEndIndex + 1, buf.writerIndex(), (byte) ',') + 1; buf.readerIndex(dataIndex);/*from www.j a v a 2 s .c o m*/ if (photo != null) { photo.writeBytes(buf.readSlice(buf.readableBytes())); if (photo.writableBytes() > 0) { requestPhotoFragment(channel); } else { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); String uniqueId = Context.getIdentityManager().getById(deviceSession.getDeviceId()) .getUniqueId(); Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(uniqueId, photo, "jpg")); photo.release(); photo = null; return position; } } } else { if (type.startsWith("$PHO")) { int size = Integer.parseInt(type.split("-")[0].substring(4)); if (size > 0) { photo = Unpooled.buffer(size); requestPhotoFragment(channel); } } return decodePosition(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII)); } return null; }
From source file:org.traccar.protocol.RitiProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(2); // header Position position = new Position(getProtocolName()); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(buf.readUnsignedShort())); if (deviceSession == null) { return null; }/*w w w.j a va2 s.c o m*/ position.setDeviceId(deviceSession.getDeviceId()); position.set("mode", buf.readUnsignedByte()); position.set(Position.KEY_COMMAND, buf.readUnsignedByte()); position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.001); buf.skipBytes(5); // status buf.readUnsignedShortLE(); // idleCount buf.readUnsignedShortLE(); // idleTime in seconds position.set(Position.KEY_DISTANCE, buf.readUnsignedIntLE()); position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedIntLE()); // Parse GPRMC int end = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); String gprmc = buf.toString(buf.readerIndex(), end - buf.readerIndex(), StandardCharsets.US_ASCII); Parser parser = new Parser(PATTERN, gprmc); if (!parser.matches()) { return null; } DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); position.setValid(parser.next().equals("A")); position.setLatitude(parser.nextCoordinate()); position.setLongitude(parser.nextCoordinate()); position.setSpeed(parser.nextDouble(0)); position.setCourse(parser.nextDouble(0)); dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); position.setTime(dateBuilder.getDate()); return position; }
From source file:org.traccar.protocol.RuptelaProtocolDecoder.java
License:Apache License
private Position decodeCommandResponse(DeviceSession deviceSession, int type, ByteBuf buf) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null);/*from w w w.j av a 2s . com*/ position.set(Position.KEY_TYPE, type); switch (type) { case MSG_DEVICE_CONFIGURATION: case MSG_DEVICE_VERSION: case MSG_FIRMWARE_UPDATE: case MSG_SMS_VIA_GPRS_RESPONSE: position.set(Position.KEY_RESULT, buf.toString(buf.readerIndex(), buf.readableBytes() - 2, StandardCharsets.US_ASCII).trim()); return position; case MSG_SET_IO: position.set(Position.KEY_RESULT, String.valueOf(buf.readUnsignedByte())); return position; default: return null; } }
From source file:org.traccar.protocol.SkypatrolProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; int apiNumber = buf.readUnsignedShort(); int commandType = buf.readUnsignedByte(); int messageType = BitUtil.from(buf.readUnsignedByte(), 4); long mask = defaultMask; if (buf.readUnsignedByte() == 4) { mask = buf.readUnsignedInt();/* w w w.jav a 2s . c o m*/ } // Binary position report if (apiNumber == 5 && commandType == 2 && messageType == 1 && BitUtil.check(mask, 0)) { Position position = new Position(getProtocolName()); if (BitUtil.check(mask, 1)) { position.set(Position.KEY_STATUS, buf.readUnsignedInt()); } String id; if (BitUtil.check(mask, 23)) { id = buf.toString(buf.readerIndex(), 8, StandardCharsets.US_ASCII).trim(); buf.skipBytes(8); } else if (BitUtil.check(mask, 2)) { id = buf.toString(buf.readerIndex(), 22, StandardCharsets.US_ASCII).trim(); buf.skipBytes(22); } else { LOGGER.warn("No device id field"); return null; } DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); if (deviceSession == null) { return null; } position.setDeviceId(deviceSession.getDeviceId()); if (BitUtil.check(mask, 3)) { position.set(Position.PREFIX_IO + 1, buf.readUnsignedShort()); } if (BitUtil.check(mask, 4)) { position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort()); } if (BitUtil.check(mask, 5)) { position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort()); } if (BitUtil.check(mask, 7)) { buf.readUnsignedByte(); // function category } DateBuilder dateBuilder = new DateBuilder(); if (BitUtil.check(mask, 8)) { dateBuilder.setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); } if (BitUtil.check(mask, 9)) { position.setValid(buf.readUnsignedByte() == 1); // gps status } if (BitUtil.check(mask, 10)) { position.setLatitude(convertCoordinate(buf.readUnsignedInt())); } if (BitUtil.check(mask, 11)) { position.setLongitude(convertCoordinate(buf.readUnsignedInt())); } if (BitUtil.check(mask, 12)) { position.setSpeed(buf.readUnsignedShort() / 10.0); } if (BitUtil.check(mask, 13)) { position.setCourse(buf.readUnsignedShort() / 10.0); } if (BitUtil.check(mask, 14)) { dateBuilder.setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); } position.setTime(dateBuilder.getDate()); if (BitUtil.check(mask, 15)) { position.setAltitude(buf.readMedium()); } if (BitUtil.check(mask, 16)) { position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); } if (BitUtil.check(mask, 17)) { position.set(Position.KEY_BATTERY, buf.readUnsignedShort()); } if (BitUtil.check(mask, 20)) { position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt()); } if (BitUtil.check(mask, 21)) { position.set(Position.KEY_ODOMETER, buf.readUnsignedInt()); } if (BitUtil.check(mask, 22)) { buf.skipBytes(6); // time of message generation } if (BitUtil.check(mask, 24)) { position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001); } if (BitUtil.check(mask, 25)) { buf.skipBytes(18); // gps overspeed } if (BitUtil.check(mask, 26)) { buf.skipBytes(54); // cell information } if (BitUtil.check(mask, 28)) { position.set(Position.KEY_INDEX, buf.readUnsignedShort()); } return position; } return null; }
From source file:org.traccar.protocol.T57FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 10) { return null; }//from w w w. j a v a 2 s .c o m String type = buf.toString(buf.readerIndex() + 5, 2, StandardCharsets.US_ASCII); int count = type.equals("F3") ? 12 : 14; int index = 0; while (index >= 0 && count > 0) { index = buf.indexOf(index + 1, buf.writerIndex(), (byte) '#'); if (index > 0) { count -= 1; } } return index > 0 ? buf.readRetainedSlice(index + 1 - buf.readerIndex()) : null; }
From source file:org.traccar.protocol.TeltonikaProtocolDecoder.java
License:Apache License
private void parseIdentification(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { int length = buf.readUnsignedShort(); String imei = buf.toString(buf.readerIndex(), length, StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); if (channel != null) { ByteBuf response = Unpooled.buffer(1); if (deviceSession != null) { response.writeByte(1);/*from w w w . j av a 2 s. c o m*/ } else { response.writeByte(0); } channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); } }
From source file:org.traccar.protocol.ThinkRaceProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(2); // header ByteBuf id = buf.readSlice(12);/* w w w. ja va 2 s . c o m*/ buf.readUnsignedByte(); // separator int type = buf.readUnsignedByte(); buf.readUnsignedShort(); // length if (type == MSG_LOGIN) { int command = buf.readUnsignedByte(); // 0x00 - heartbeat if (command == 0x01) { String imei = buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); if (deviceSession != null && channel != null) { ByteBuf response = Unpooled.buffer(); response.writeByte(0x48); response.writeByte(0x52); // header response.writeBytes(id); response.writeByte(0x2c); // separator response.writeByte(type); response.writeShort(0x0002); // length response.writeShort(0x8000); response.writeShort(0x0000); // checksum channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); } } } else if (type == MSG_GPS) { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date(buf.readUnsignedInt() * 1000)); int flags = buf.readUnsignedByte(); position.setValid(true); position.setLatitude(convertCoordinate(buf.readUnsignedInt(), !BitUtil.check(flags, 0))); position.setLongitude(convertCoordinate(buf.readUnsignedInt(), !BitUtil.check(flags, 1))); position.setSpeed(buf.readUnsignedByte()); position.setCourse(buf.readUnsignedByte()); position.setNetwork( new Network(CellTower.fromLacCid(buf.readUnsignedShort(), buf.readUnsignedShort()))); return position; } return null; }
From source file:org.traccar.protocol.TotemFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 10) { return null; }//ww w.j a v a 2s . co m int beginIndex = BufferUtil.indexOf("$$", buf); if (beginIndex == -1) { return null; } else if (beginIndex > buf.readerIndex()) { buf.readerIndex(beginIndex); } int length; if (buf.getByte(buf.readerIndex() + 2) == (byte) '0') { length = Integer.parseInt(buf.toString(buf.readerIndex() + 2, 4, StandardCharsets.US_ASCII)); } else { length = Integer.parseInt(buf.toString(buf.readerIndex() + 2, 2, StandardCharsets.US_ASCII), 16); } if (length <= buf.readableBytes()) { return buf.readRetainedSlice(length); } return null; }