Search in sources :

Example 1 with ChannelBufferIndexFinder

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;
}
Also used : ChannelBufferIndexFinder(org.jboss.netty.buffer.ChannelBufferIndexFinder) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 2 with ChannelBufferIndexFinder

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;
}
Also used : ChannelBufferIndexFinder(org.jboss.netty.buffer.ChannelBufferIndexFinder) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) StringFinder(org.traccar.helper.StringFinder) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Parser(org.traccar.helper.Parser)

Aggregations

ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2 ChannelBufferIndexFinder (org.jboss.netty.buffer.ChannelBufferIndexFinder)2 DeviceSession (org.traccar.DeviceSession)1 DateBuilder (org.traccar.helper.DateBuilder)1 Parser (org.traccar.helper.Parser)1 StringFinder (org.traccar.helper.StringFinder)1 Position (org.traccar.model.Position)1