List of usage examples for io.netty.buffer ByteBuf writeCharSequence
public abstract int writeCharSequence(CharSequence sequence, Charset charset);
From source file:org.traccar.protocol.NavisProtocolDecoder.java
License:Apache License
private Object processNtcbSingle(DeviceSession deviceSession, Channel channel, ByteBuf buf) { Position position = parseNtcbPosition(deviceSession, buf); ByteBuf response = Unpooled.buffer(7); response.writeCharSequence("*<T", StandardCharsets.US_ASCII); response.writeIntLE((int) position.getLong(Position.KEY_INDEX)); sendNtcbReply(channel, response);/* w w w .j ava 2 s.com*/ return position.getFixTime() != null ? position : null; }
From source file:org.traccar.protocol.NavisProtocolDecoder.java
License:Apache License
private Object processNtcbArray(DeviceSession deviceSession, Channel channel, ByteBuf buf) { List<Position> positions = new LinkedList<>(); int count = buf.readUnsignedByte(); for (int i = 0; i < count; i++) { Position position = parseNtcbPosition(deviceSession, buf); if (position.getFixTime() != null) { positions.add(position);/* www. ja va 2s . co m*/ } } ByteBuf response = Unpooled.buffer(7); response.writeCharSequence("*<A", StandardCharsets.US_ASCII); response.writeByte(count); sendNtcbReply(channel, response); if (positions.isEmpty()) { return null; } return positions; }
From source file:org.traccar.protocol.NavisProtocolDecoder.java
License:Apache License
private Object processFlexSingle(FlexPositionParser parser, String flexHeader, DeviceSession deviceSession, Channel channel, ByteBuf buf) { if (!flexHeader.equals("~C")) { buf.readUnsignedInt(); // event index }/*from ww w. ja va2 s.com*/ Position position = parser.parsePosition(deviceSession, buf); ByteBuf response = Unpooled.buffer(); response.writeCharSequence(flexHeader, StandardCharsets.US_ASCII); response.writeIntLE((int) position.getLong(Position.KEY_INDEX)); sendFlexReply(channel, response); return position.getFixTime() != null ? position : null; }
From source file:org.traccar.protocol.NavisProtocolDecoder.java
License:Apache License
private Object processFlexArray(FlexPositionParser parser, String flexHeader, DeviceSession deviceSession, Channel channel, ByteBuf buf) { List<Position> positions = new LinkedList<>(); int count = buf.readUnsignedByte(); for (int i = 0; i < count; i++) { Position position = parser.parsePosition(deviceSession, buf); if (position.getFixTime() != null) { positions.add(position);/* w w w .j a v a2 s . co m*/ } } ByteBuf response = Unpooled.buffer(); response.writeCharSequence(flexHeader, StandardCharsets.US_ASCII); response.writeByte(count); sendFlexReply(channel, response); return !positions.isEmpty() ? positions : null; }
From source file:org.traccar.protocol.NavisProtocolDecoder.java
License:Apache License
private Object processFlexNegotiation(Channel channel, ByteBuf buf) { if ((byte) buf.readUnsignedByte() != (byte) 0xB0) { return null; }// w ww . j a v a 2 s . c o m short flexProtocolVersion = buf.readUnsignedByte(); short flexStructVersion = buf.readUnsignedByte(); if ((flexProtocolVersion == 0x0A || flexProtocolVersion == 0x14) && (flexStructVersion == 0x0A || flexStructVersion == 0x14)) { flexBitFieldSize = buf.readUnsignedByte(); if (flexBitFieldSize > 122) { return null; } buf.readBytes(flexBitField, 0, (int) Math.ceil((double) flexBitFieldSize / 8)); flexDataSize = 0; for (int i = 0; i < flexBitFieldSize; i++) { if (checkFlexBitfield(i)) { flexDataSize += FLEX_FIELDS_SIZES[i]; } } } else { flexProtocolVersion = 0x14; flexStructVersion = 0x14; } ByteBuf response = Unpooled.buffer(9); response.writeCharSequence("*<FLEX", StandardCharsets.US_ASCII); response.writeByte(0xB0); response.writeByte(flexProtocolVersion); response.writeByte(flexStructVersion); sendNtcbReply(channel, response); return null; }
From source file:org.traccar.protocol.NavisProtocolDecoder.java
License:Apache License
private void sendNtcbReply(Channel channel, ByteBuf data) { if (channel != null) { ByteBuf header = Unpooled.buffer(16); header.writeCharSequence(prefix, StandardCharsets.US_ASCII); header.writeIntLE((int) deviceUniqueId); header.writeIntLE((int) serverId); header.writeShortLE(data.readableBytes()); header.writeByte(Checksum.xor(data.nioBuffer())); header.writeByte(Checksum.xor(header.nioBuffer())); channel.writeAndFlush(// w ww . ja v a2 s . c o m new NetworkMessage(Unpooled.wrappedBuffer(header, data), channel.remoteAddress())); } }
From source file:org.traccar.protocol.NoranProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg;//w ww.j a v a2 s. co m buf.readUnsignedShortLE(); // length int type = buf.readUnsignedShortLE(); if (type == MSG_SHAKE_HAND && channel != null) { ByteBuf response = Unpooled.buffer(13); response.writeCharSequence("\r\n*KW", StandardCharsets.US_ASCII); response.writeByte(0); response.writeShortLE(response.capacity()); response.writeShortLE(MSG_SHAKE_HAND_RESPONSE); response.writeByte(1); // status response.writeCharSequence("\r\n", StandardCharsets.US_ASCII); channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); } else if (type == MSG_UPLOAD_POSITION || type == MSG_UPLOAD_POSITION_NEW || type == MSG_CONTROL_RESPONSE || type == MSG_ALARM) { boolean newFormat = false; if (type == MSG_UPLOAD_POSITION && buf.readableBytes() == 48 || type == MSG_ALARM && buf.readableBytes() == 48 || type == MSG_CONTROL_RESPONSE && buf.readableBytes() == 57) { newFormat = true; } Position position = new Position(getProtocolName()); if (type == MSG_CONTROL_RESPONSE) { buf.readUnsignedIntLE(); // GIS ip buf.readUnsignedIntLE(); // GIS port } position.setValid(BitUtil.check(buf.readUnsignedByte(), 0)); short alarm = buf.readUnsignedByte(); switch (alarm) { case 1: position.set(Position.KEY_ALARM, Position.ALARM_SOS); break; case 2: position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED); break; case 3: position.set(Position.KEY_ALARM, Position.ALARM_GEOFENCE_EXIT); break; case 9: position.set(Position.KEY_ALARM, Position.ALARM_POWER_OFF); break; default: break; } if (newFormat) { position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedIntLE())); position.setCourse(buf.readFloatLE()); } else { position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); position.setCourse(buf.readUnsignedShortLE()); } position.setLongitude(buf.readFloatLE()); position.setLatitude(buf.readFloatLE()); if (!newFormat) { long timeValue = buf.readUnsignedIntLE(); DateBuilder dateBuilder = new DateBuilder().setYear((int) BitUtil.from(timeValue, 26)) .setMonth((int) BitUtil.between(timeValue, 22, 26)) .setDay((int) BitUtil.between(timeValue, 17, 22)) .setHour((int) BitUtil.between(timeValue, 12, 17)) .setMinute((int) BitUtil.between(timeValue, 6, 12)) .setSecond((int) BitUtil.to(timeValue, 6)); position.setTime(dateBuilder.getDate()); } ByteBuf rawId; if (newFormat) { rawId = buf.readSlice(12); } else { rawId = buf.readSlice(11); } String id = rawId.toString(StandardCharsets.US_ASCII).replaceAll("[^\\p{Print}]", ""); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); if (deviceSession == null) { return null; } position.setDeviceId(deviceSession.getDeviceId()); if (newFormat) { DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); position.setTime(dateFormat.parse(buf.readSlice(17).toString(StandardCharsets.US_ASCII))); buf.readByte(); } if (!newFormat) { position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte()); position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte()); } else if (type == MSG_UPLOAD_POSITION_NEW) { position.set(Position.PREFIX_TEMP + 1, buf.readShortLE()); position.set(Position.KEY_ODOMETER, buf.readFloatLE()); } return position; } return null; }
From source file:org.traccar.protocol.NoranProtocolEncoder.java
License:Apache License
private ByteBuf encodeContent(String content) { ByteBuf buf = Unpooled.buffer(12 + 56); buf.writeCharSequence("\r\n*KW", StandardCharsets.US_ASCII); buf.writeByte(0);//from ww w .java 2 s .c om buf.writeShortLE(buf.capacity()); buf.writeShortLE(NoranProtocolDecoder.MSG_CONTROL); buf.writeInt(0); // gis ip buf.writeShortLE(0); // gis port buf.writeBytes(content.getBytes(StandardCharsets.US_ASCII)); buf.writerIndex(buf.writerIndex() + 50 - content.length()); buf.writeCharSequence("\r\n", StandardCharsets.US_ASCII); return buf; }
From source file:org.traccar.protocol.TlvProtocolDecoder.java
License:Apache License
private void sendResponse(Channel channel, SocketAddress remoteAddress, String type, String... arguments) { if (channel != null) { ByteBuf response = Unpooled.buffer(); response.writeCharSequence(type, StandardCharsets.US_ASCII); for (String argument : arguments) { response.writeByte(argument.length()); response.writeCharSequence(argument, StandardCharsets.US_ASCII); }/*from w w w . j a v a 2 s . c o m*/ response.writeByte(0); channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); } }
From source file:org.traccar.protocol.WatchProtocolEncoder.java
License:Apache License
protected ByteBuf formatBinaryCommand(Channel channel, Command command, String textPrefix, ByteBuf data) { boolean hasIndex = false; String manufacturer = "CS"; if (channel != null) { WatchProtocolDecoder decoder = channel.pipeline().get(WatchProtocolDecoder.class); if (decoder != null) { hasIndex = decoder.getHasIndex(); manufacturer = decoder.getManufacturer(); }//ww w .j av a 2s . co m } ByteBuf buf = Unpooled.buffer(); buf.writeByte('['); buf.writeCharSequence(manufacturer, StandardCharsets.US_ASCII); buf.writeByte('*'); buf.writeCharSequence(getUniqueId(command.getDeviceId()), StandardCharsets.US_ASCII); buf.writeByte('*'); if (hasIndex) { buf.writeCharSequence("0001", StandardCharsets.US_ASCII); buf.writeByte('*'); } buf.writeCharSequence(String.format("%04x", data.readableBytes() + textPrefix.length()), StandardCharsets.US_ASCII); buf.writeByte('*'); buf.writeCharSequence(textPrefix, StandardCharsets.US_ASCII); buf.writeBytes(data); buf.writeByte(']'); return buf; }