Search in sources :

Example 1 with StringFinder

use of org.traccar.helper.StringFinder in project traccar by tananaev.

the class GranitFrameDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
    int indexEnd = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("\r\n"));
    if (indexEnd != -1) {
        int indexTilde = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("~"));
        if (indexTilde != -1 && indexTilde < indexEnd) {
            int length = buf.getUnsignedShort(indexTilde + 1);
            indexEnd = buf.indexOf(indexTilde + 2 + length, buf.writerIndex(), new StringFinder("\r\n"));
            if (indexEnd == -1) {
                return null;
            }
        }
        ChannelBuffer frame = buf.readBytes(indexEnd - buf.readerIndex());
        buf.skipBytes(2);
        return frame;
    }
    return null;
}
Also used : StringFinder(org.traccar.helper.StringFinder) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 2 with StringFinder

use of org.traccar.helper.StringFinder in project traccar by tananaev.

the class GranitProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    int indexTilde = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("~"));
    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);
        position.set(Position.KEY_RESULT, bufString);
        return position;
    }
    if (buf.readableBytes() < HEADER_LENGTH) {
        return null;
    }
    String header = buf.readBytes(HEADER_LENGTH).toString(StandardCharsets.US_ASCII);
    if (header.equals("+RRCB~")) {
        // binary length 26
        buf.skipBytes(2);
        int deviceId = buf.readUnsignedShort();
        deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId));
        if (deviceSession == null) {
            return null;
        }
        long unixTime = buf.readUnsignedInt();
        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~")) {
        // binary length
        buf.skipBytes(2);
        int deviceId = buf.readUnsignedShort();
        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.readUnsignedShort();
        if (channel != null) {
            sendResponseArchive(channel, deviceId, packNum);
        }
        List<Position> positions = new ArrayList<>();
        while (nblocks > 0) {
            nblocks--;
            long unixTime = buf.readUnsignedInt();
            int timeIncrement = buf.getUnsignedShort(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 {
                    // skip filled 0xFE structure
                    buf.skipBytes(20);
                }
            }
            // increment
            buf.skipBytes(2);
        }
        return positions;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) StringFinder(org.traccar.helper.StringFinder) ArrayList(java.util.ArrayList) List(java.util.List) Date(java.util.Date) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 3 with StringFinder

use of org.traccar.helper.StringFinder in project traccar by tananaev.

the class WondexFrameDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
    if (buf.readableBytes() < KEEP_ALIVE_LENGTH) {
        return null;
    }
    if (buf.getUnsignedByte(buf.readerIndex()) == 0xD0) {
        // Send response
        ChannelBuffer frame = buf.readBytes(KEEP_ALIVE_LENGTH);
        if (channel != null) {
            channel.write(frame);
        }
        return frame;
    } else {
        int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("\r\n"));
        if (index != -1) {
            ChannelBuffer frame = buf.readBytes(index - buf.readerIndex());
            buf.skipBytes(2);
            return frame;
        }
    }
    return null;
}
Also used : StringFinder(org.traccar.helper.StringFinder) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 4 with StringFinder

use of org.traccar.helper.StringFinder in project traccar by tananaev.

the class XexunFrameDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
    if (buf.readableBytes() < 80) {
        return null;
    }
    int beginIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("GPRMC"));
    if (beginIndex == -1) {
        beginIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("GNRMC"));
        if (beginIndex == -1) {
            return null;
        }
    }
    int identifierIndex = buf.indexOf(beginIndex, buf.writerIndex(), new StringFinder("imei:"));
    if (identifierIndex == -1) {
        return null;
    }
    int endIndex = buf.indexOf(identifierIndex, buf.writerIndex(), (byte) ',');
    if (endIndex == -1) {
        return null;
    }
    buf.skipBytes(beginIndex - buf.readerIndex());
    return buf.readBytes(endIndex - beginIndex + 1);
}
Also used : StringFinder(org.traccar.helper.StringFinder)

Example 5 with StringFinder

use of org.traccar.helper.StringFinder 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

StringFinder (org.traccar.helper.StringFinder)7 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)5 DeviceSession (org.traccar.DeviceSession)2 Position (org.traccar.model.Position)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 ChannelBufferIndexFinder (org.jboss.netty.buffer.ChannelBufferIndexFinder)1 DateBuilder (org.traccar.helper.DateBuilder)1 Parser (org.traccar.helper.Parser)1