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.WatchProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(1); // '[' header manufacturer = buf.readSlice(2).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // '*' delimiter int idIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); String id = buf.readSlice(idIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); if (deviceSession == null) { return null; }/*www.ja v a 2s .c o m*/ buf.skipBytes(1); // '*' delimiter String index = null; int contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); if (contentIndex + 5 < buf.writerIndex() && buf.getByte(contentIndex + 5) == '*' && buf.toString(contentIndex + 1, 4, StandardCharsets.US_ASCII).matches("\\p{XDigit}+")) { int indexLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') - buf.readerIndex(); hasIndex = true; index = buf.readSlice(indexLength).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // '*' delimiter } buf.skipBytes(4); // length buf.skipBytes(1); // '*' delimiter buf.writerIndex(buf.writerIndex() - 1); // ']' ignore ending contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); if (contentIndex < 0) { contentIndex = buf.writerIndex(); } String type = buf.readSlice(contentIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII); if (contentIndex < buf.writerIndex()) { buf.readerIndex(contentIndex + 1); } if (type.equals("INIT")) { sendResponse(channel, id, index, "INIT,1"); } else if (type.equals("LK")) { sendResponse(channel, id, index, "LK"); if (buf.isReadable()) { String[] values = buf.toString(StandardCharsets.US_ASCII).split(","); if (values.length >= 3) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); position.set(Position.KEY_BATTERY_LEVEL, Integer.parseInt(values[2])); return position; } } } else if (type.equals("UD") || type.equals("UD2") || type.equals("UD3") || type.equals("AL") || type.equals("WT")) { Position position = decodePosition(deviceSession, buf.toString(StandardCharsets.US_ASCII)); if (type.equals("AL")) { if (position != null) { position.set(Position.KEY_ALARM, Position.ALARM_SOS); } sendResponse(channel, id, index, "AL"); } return position; } else if (type.equals("TKQ")) { sendResponse(channel, id, index, "TKQ"); } else if (type.equals("PULSE") || type.equals("heart") || type.equals("bphrt")) { if (buf.isReadable()) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, new Date()); String[] values = buf.toString(StandardCharsets.US_ASCII).split(","); int valueIndex = 0; if (type.equals("bphrt")) { position.set("pressureHigh", values[valueIndex++]); position.set("pressureLow", values[valueIndex++]); } position.set(Position.KEY_HEART_RATE, Integer.parseInt(values[valueIndex])); return position; } } else if (type.equals("img")) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); int timeIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); buf.readerIndex(timeIndex + 12 + 2); position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(id, buf, "jpg")); return position; } else if (type.equals("TK")) { if (buf.readableBytes() == 1) { byte result = buf.readByte(); if (result != '1') { LOGGER.warn(type + "," + result); } return null; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); position.set(Position.KEY_AUDIO, Context.getMediaManager().writeFile(id, buf, "amr")); return position; } return null; }
From source file:org.traccar.protocol.WristbandProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(3); // header buf.readUnsignedShort(); // length String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 3, StandardCharsets.US_ASCII); buf.skipBytes(3); // footer return decodeMessage(channel, remoteAddress, sentence); }
From source file:org.waarp.openr66.protocol.localhandler.packet.EndRequestPacket.java
License:Open Source License
/** * @param headerLength//from w ww .j av a 2s . co m * @param middleLength * @param endLength * @param buf * @return the new EndTransferPacket from buffer * @throws OpenR66ProtocolPacketException */ public static EndRequestPacket createFromBuffer(int headerLength, int middleLength, int endLength, ByteBuf buf) throws OpenR66ProtocolPacketException { if (headerLength - 1 != 4) { throw new OpenR66ProtocolPacketException("Not enough data"); } if (middleLength != 1) { throw new OpenR66ProtocolPacketException("Not enough data"); } final int bheader = buf.readInt(); byte valid = buf.readByte(); String optional; if (endLength > 0) { optional = buf.toString(buf.readerIndex(), endLength, Charset.defaultCharset()); buf.skipBytes(endLength); return new EndRequestPacket(bheader, valid, optional); } return new EndRequestPacket(bheader, valid); }