use of org.jboss.netty.buffer.ChannelBufferIndexFinder in project traccar by tananaev.
the class TmgFrameDecoder method decode.
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
int beginIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new ChannelBufferIndexFinder() {
@Override
public boolean find(ChannelBuffer buffer, int guessedIndex) {
if (buffer.getByte(guessedIndex) != (byte) '$' || buffer.writerIndex() - guessedIndex < 5) {
return false;
}
return buffer.getByte(guessedIndex + 4) == ',' && isLetter(buffer.getByte(guessedIndex + 1)) && isLetter(buffer.getByte(guessedIndex + 2)) && isLetter(buffer.getByte(guessedIndex + 3));
}
});
if (beginIndex >= 0) {
buf.readerIndex(beginIndex);
int endIndex = buf.indexOf(beginIndex, buf.writerIndex(), (byte) '\n');
if (endIndex >= 0) {
ChannelBuffer frame = buf.readBytes(endIndex - beginIndex);
// delimiter
buf.readByte();
return frame;
}
}
return null;
}
use of org.jboss.netty.buffer.ChannelBufferIndexFinder in project traccar by tananaev.
the class EnforaProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
// Find IMEI number
int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new ChannelBufferIndexFinder() {
@Override
public boolean find(ChannelBuffer buffer, int guessedIndex) {
if (buffer.writerIndex() - guessedIndex >= IMEI_LENGTH) {
for (int i = 0; i < IMEI_LENGTH; i++) {
if (!Character.isDigit((char) buffer.getByte(guessedIndex + i))) {
return false;
}
}
return true;
}
return false;
}
});
if (index == -1) {
return null;
}
String imei = buf.toString(index, IMEI_LENGTH, StandardCharsets.US_ASCII);
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
if (deviceSession == null) {
return null;
}
// Find NMEA sentence
int start = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("GPRMC"));
if (start == -1) {
return null;
}
String sentence = buf.toString(start, buf.readableBytes() - start, StandardCharsets.US_ASCII);
Parser parser = new Parser(PATTERN, sentence);
if (!parser.matches()) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
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;
}
Aggregations