List of usage examples for io.netty.buffer ByteBuf toString
public abstract String toString(Charset charset);
From source file:org.rzo.yajsw.controller.jvm.MessageDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext paramChannelHandlerContext, ByteBuf b, List<Object> out) throws Exception { if (!b.isReadable()) return;/* w w w . j a v a2 s . c o m*/ byte code = b.readByte(); b.writerIndex(b.writerIndex()); String msg = b.toString(Charset.defaultCharset()); Message result = new Message(code, msg); out.add(result); }
From source file:org.thingsboard.server.transport.mqtt.adaptors.JsonMqttAdaptor.java
License:Apache License
private static String validatePayload(UUID sessionId, ByteBuf payloadData) throws AdaptorException { try {//from ww w . ja va2 s. c o m String payload = payloadData.toString(UTF8); if (payload == null) { log.warn("[{}] Payload is empty!", sessionId); throw new AdaptorException(new IllegalArgumentException("Payload is empty!")); } return payload; } finally { payloadData.release(); } }
From source file:org.tiger.netty.http.xml.codec.AbstractHttpXmlDecoder.java
License:Apache License
protected Object decode0(ChannelHandlerContext arg0, ByteBuf body) throws Exception { String content = body.toString(UTF_8); XStream xs = new XStream(); xs.processAnnotations(Order.class);//Hotel.class Order result = (Order) xs.fromXML(content); /*factory = BindingDirectory.getFactory(clazz); if (isPrint)//from ww w . jav a 2s .c o m System.out.println("The body is : " + content); reader = new StringReader(content); IUnmarshallingContext uctx = factory.createUnmarshallingContext(); Object result = uctx.unmarshalDocument(reader); reader.close(); reader = null;*/ return result; }
From source file:org.traccar.protocol.AtrackProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; if (buf.getUnsignedShort(buf.readerIndex()) == 0xfe02) { if (channel != null) { channel.writeAndFlush(new NetworkMessage(buf.retain(), remoteAddress)); // keep-alive message }/* w w w .j a v a 2 s . co m*/ return null; } else if (buf.getByte(buf.readerIndex()) == '$') { return decodeInfo(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII).trim()); } else if (buf.getByte(buf.readerIndex() + 2) == ',') { return decodeText(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII).trim()); } else { return decodeBinary(channel, remoteAddress, buf); } }
From source file:org.traccar.protocol.CastelProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg;/*from w w w .ja va 2 s. com*/ int header = buf.readUnsignedShortLE(); buf.readUnsignedShortLE(); // length int version = -1; if (header == 0x4040) { version = buf.readUnsignedByte(); } ByteBuf id = buf.readSlice(20); short type = buf.readShort(); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id.toString(StandardCharsets.US_ASCII).trim()); if (deviceSession == null) { return null; } switch (version) { case -1: return decodeMpip(channel, remoteAddress, buf, version, id, type, deviceSession); case 3: case 4: return decodeSc(channel, remoteAddress, buf, version, id, type, deviceSession); default: return decodeCc(channel, remoteAddress, buf, version, id, type, deviceSession); } }
From source file:org.traccar.protocol.EelinkProtocolDecoder.java
License:Apache License
private Position decodeResult(DeviceSession deviceSession, ByteBuf buf, int index) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_INDEX, index); buf.readUnsignedByte(); // type buf.readUnsignedInt(); // uid String sentence = buf.toString(StandardCharsets.UTF_8); Parser parser = new Parser(PATTERN, sentence); if (parser.matches()) { position.setValid(true);/*w ww. ja v a2 s . c o m*/ position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG)); position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG)); position.setCourse(parser.nextDouble()); position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble())); position.setTime(parser.nextDateTime()); } else { getLastLocation(position, null); position.set(Position.KEY_RESULT, sentence); } return position; }
From source file:org.traccar.protocol.GranitProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; int indexTilde = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '~'); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession != null && indexTilde == -1) { String bufString = buf.toString(StandardCharsets.US_ASCII); Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date()); getLastLocation(position, new Date()); position.setValid(false);//from w w w .j ava 2 s . c om position.set(Position.KEY_RESULT, bufString); return position; } if (buf.readableBytes() < HEADER_LENGTH) { return null; } String header = buf.readSlice(HEADER_LENGTH).toString(StandardCharsets.US_ASCII); if (header.equals("+RRCB~")) { buf.skipBytes(2); // binary length 26 int deviceId = buf.readUnsignedShortLE(); deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId)); if (deviceSession == null) { return null; } long unixTime = buf.readUnsignedIntLE(); if (channel != null) { sendResponseCurrent(channel, deviceId, unixTime); } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date(unixTime * 1000)); decodeStructure(buf, position); return position; } else if (header.equals("+DDAT~")) { buf.skipBytes(2); // binary length int deviceId = buf.readUnsignedShortLE(); deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId)); if (deviceSession == null) { return null; } byte format = buf.readByte(); if (format != 4) { return null; } byte nblocks = buf.readByte(); int packNum = buf.readUnsignedShortLE(); if (channel != null) { sendResponseArchive(channel, deviceId, packNum); } List<Position> positions = new ArrayList<>(); while (nblocks > 0) { nblocks--; long unixTime = buf.readUnsignedIntLE(); int timeIncrement = buf.getUnsignedShortLE(buf.readerIndex() + 120); for (int i = 0; i < 6; i++) { if (buf.getUnsignedByte(buf.readerIndex()) != 0xFE) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date((unixTime + i * timeIncrement) * 1000)); decodeStructure(buf, position); position.set(Position.KEY_ARCHIVE, true); positions.add(position); } else { buf.skipBytes(20); // skip filled 0xFE structure } } buf.skipBytes(2); // increment } return positions; } return null; }
From source file:org.traccar.protocol.Jt600ProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; char first = (char) buf.getByte(0); if (first == '$') { return decodeBinary(buf, channel, remoteAddress); } else if (first == '(') { String sentence = buf.toString(StandardCharsets.US_ASCII); if (sentence.contains("W01")) { return decodeW01(sentence, channel, remoteAddress); } else {//from ww w . java2s.c om return decodeU01(sentence, channel, remoteAddress); } } return null; }
From source file:org.traccar.protocol.MeitrackProtocolDecoder.java
License:Apache License
private Position decodeRegular(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { Parser parser = new Parser(PATTERN, buf.toString(StandardCharsets.US_ASCII)); if (!parser.matches()) { return null; }//from w w w . j a v a 2s . c om Position position = new Position(getProtocolName()); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); if (deviceSession == null) { return null; } position.setDeviceId(deviceSession.getDeviceId()); int event = parser.nextInt(0); position.set(Position.KEY_EVENT, event); position.set(Position.KEY_ALARM, decodeAlarm(event)); position.setLatitude(parser.nextDouble(0)); position.setLongitude(parser.nextDouble(0)); position.setTime(parser.nextDateTime()); position.setValid(parser.next().equals("A")); position.set(Position.KEY_SATELLITES, parser.nextInt()); int rssi = parser.nextInt(0); position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0))); position.setCourse(parser.nextDouble(0)); position.set(Position.KEY_HDOP, parser.nextDouble()); position.setAltitude(parser.nextDouble(0)); position.set(Position.KEY_ODOMETER, parser.nextInt(0)); position.set("runtime", parser.next()); position.setNetwork(new Network(CellTower.from(parser.nextInt(0), parser.nextInt(0), parser.nextHexInt(0), parser.nextHexInt(0), rssi))); position.set(Position.KEY_STATUS, parser.next()); for (int i = 1; i <= 3; i++) { if (parser.hasNext()) { position.set(Position.PREFIX_ADC + i, parser.nextHexInt(0)); } } String deviceModel = Context.getIdentityManager().getById(deviceSession.getDeviceId()).getModel(); if (deviceModel == null) { deviceModel = ""; } switch (deviceModel.toUpperCase()) { case "MVT340": case "MVT380": position.set(Position.KEY_BATTERY, parser.nextHexInt(0) * 3.0 * 2.0 / 1024.0); position.set(Position.KEY_POWER, parser.nextHexInt(0) * 3.0 * 16.0 / 1024.0); break; case "MT90": position.set(Position.KEY_BATTERY, parser.nextHexInt(0) * 3.3 * 2.0 / 4096.0); position.set(Position.KEY_POWER, parser.nextHexInt(0)); break; case "T1": case "T3": case "MVT100": case "MVT600": case "MVT800": case "TC68": case "TC68S": position.set(Position.KEY_BATTERY, parser.nextHexInt(0) * 3.3 * 2.0 / 4096.0); position.set(Position.KEY_POWER, parser.nextHexInt(0) * 3.3 * 16.0 / 4096.0); break; case "T311": case "T322X": case "T333": case "T355": position.set(Position.KEY_BATTERY, parser.nextHexInt(0) / 100.0); position.set(Position.KEY_POWER, parser.nextHexInt(0) / 100.0); break; default: position.set(Position.KEY_BATTERY, parser.nextHexInt(0)); position.set(Position.KEY_POWER, parser.nextHexInt(0)); break; } String eventData = parser.next(); if (eventData != null && !eventData.isEmpty()) { switch (event) { case 37: position.set(Position.KEY_DRIVER_UNIQUE_ID, eventData); break; default: position.set("eventData", eventData); break; } } int protocol = parser.nextInt(0); if (parser.hasNext()) { String fuel = parser.next(); position.set(Position.KEY_FUEL_LEVEL, Integer.parseInt(fuel.substring(0, 2), 16) + Integer.parseInt(fuel.substring(2), 16) * 0.01); } if (parser.hasNext()) { for (String temp : parser.next().split("\\|")) { int index = Integer.parseInt(temp.substring(0, 2), 16); if (protocol >= 3) { double value = (short) Integer.parseInt(temp.substring(2), 16); position.set(Position.PREFIX_TEMP + index, value * 0.01); } else { double value = Byte.parseByte(temp.substring(2, 4), 16); value += (value < 0 ? -0.01 : 0.01) * Integer.parseInt(temp.substring(4), 16); position.set(Position.PREFIX_TEMP + index, value); } } } if (parser.hasNext(2)) { parser.nextInt(); // count decodeDataFields(position, parser.next().split(",")); } return position; }
From source file:org.traccar.protocol.NavisProtocolDecoder.java
License:Apache License
private Object processHandshake(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { buf.readByte(); // colon if (getDeviceSession(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII)) != null) { sendNtcbReply(channel, Unpooled.copiedBuffer("*<S", StandardCharsets.US_ASCII)); }//from www . j a v a2 s . c o m return null; }