Search in sources :

Example 1 with Parser

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

the class AisProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String[] sentences = ((String) msg).split("\\r\\n");
    List<Position> positions = new ArrayList<>();
    Map<Integer, BitBuffer> buffers = new HashMap<>();
    for (String sentence : sentences) {
        if (!sentence.isEmpty()) {
            Parser parser = new Parser(PATTERN, sentence);
            if (parser.matches()) {
                int count = parser.nextInt(0);
                int index = parser.nextInt(0);
                int id = parser.nextInt(0);
                Position position = null;
                if (count == 1) {
                    BitBuffer bits = new BitBuffer();
                    bits.writeEncoded(parser.next().getBytes(StandardCharsets.US_ASCII));
                    position = decodePayload(channel, remoteAddress, bits);
                } else {
                    BitBuffer bits = buffers.get(id);
                    if (bits == null) {
                        bits = new BitBuffer();
                        buffers.put(id, bits);
                    }
                    bits.writeEncoded(parser.next().getBytes(StandardCharsets.US_ASCII));
                    if (count == index) {
                        position = decodePayload(channel, remoteAddress, bits);
                        buffers.remove(id);
                    }
                }
                if (position != null) {
                    positions.add(position);
                }
            }
        }
    }
    return positions;
}
Also used : Position(org.traccar.model.Position) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BitBuffer(org.traccar.helper.BitBuffer) Parser(org.traccar.helper.Parser)

Example 2 with Parser

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

the class AppelloProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    Parser parser = new Parser(PATTERN, (String) msg);
    if (!parser.matches()) {
        return null;
    }
    String imei = parser.next();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    if (parser.hasNext(6)) {
        position.setTime(parser.nextDateTime());
    } else {
        getLastLocation(position, null);
    }
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_SATELLITES, parser.nextInt(0));
    position.setAltitude(parser.nextDouble(0));
    position.setValid(parser.next().equals("F"));
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 3 with Parser

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

the class AquilaProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    Parser parser = new Parser(PATTERN, (String) msg);
    if (!parser.matches()) {
        return null;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.set(Position.KEY_EVENT, parser.nextInt(0));
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setTime(parser.nextDateTime());
    position.setValid(parser.next().equals("A"));
    if (parser.hasNext(3)) {
        position.set(Position.KEY_RSSI, parser.nextInt(0));
        position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
        position.set(Position.KEY_ODOMETER, parser.nextInt(0));
    }
    if (parser.hasNext(9)) {
        position.set(Position.KEY_FUEL_LEVEL, parser.nextInt());
        position.set(Position.PREFIX_IN + 1, parser.next());
        position.set(Position.KEY_CHARGE, parser.next().equals("1"));
        position.set(Position.PREFIX_IN + 2, parser.next());
        position.set(Position.KEY_IGNITION, parser.nextInt(0) == 1);
        int course = (parser.nextInt(0) << 3) + (parser.nextInt(0) << 2) + (parser.nextInt(0) << 1) + parser.nextInt(0);
        if (course > 0 && course <= 8) {
            position.setCourse((course - 1) * 45);
        }
    } else if (parser.hasNext(7)) {
        position.setCourse(parser.nextInt(0));
        position.set(Position.KEY_CHARGE, parser.next().equals("1"));
        position.set(Position.KEY_IGNITION, parser.nextInt(0) == 1);
        position.set(Position.KEY_POWER, parser.nextInt(0));
        position.set(Position.KEY_BATTERY, parser.nextInt(0));
        String obd = parser.next();
        position.set("obd", obd.substring(1, obd.length() - 1));
        String dtcs = parser.next();
        position.set(Position.KEY_DTCS, dtcs.substring(1, dtcs.length() - 1).replace('|', ' '));
    } else if (parser.hasNext(10)) {
        position.setCourse(parser.nextInt(0));
        position.set(Position.KEY_SATELLITES, parser.nextInt(0));
        position.set(Position.KEY_HDOP, parser.nextDouble(0));
        position.set(Position.PREFIX_ADC + 1, parser.nextInt(0));
        position.set(Position.PREFIX_IN + 1, parser.nextInt(0));
        position.set(Position.KEY_CHARGE, parser.next().equals("1"));
        position.set(Position.PREFIX_IN + 2, parser.nextInt(0));
        position.set(Position.KEY_IGNITION, parser.nextInt(0) == 1);
        position.set(Position.KEY_POWER, parser.nextInt(0));
        position.set(Position.KEY_BATTERY, parser.nextInt(0));
    } else if (parser.hasNext(2)) {
        position.set("sensorId", parser.nextInt());
        position.set("sensorData", parser.next());
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 4 with Parser

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

the class ArknavProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    Parser parser = new Parser(PATTERN, (String) msg);
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.setValid(parser.next().equals("A"));
    position.setLatitude(parser.nextCoordinate());
    position.setLongitude(parser.nextCoordinate());
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_HDOP, parser.nextDouble(0));
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.HMS_DMY));
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 5 with Parser

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

the class ArknavX8ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    if (sentence.charAt(2) != ',') {
        getDeviceSession(channel, remoteAddress, sentence.substring(0, 15));
        return null;
    }
    Parser parser = new Parser(PATTERN, sentence);
    if (!parser.matches()) {
        return null;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.set(Position.KEY_TYPE, parser.next());
    position.setTime(parser.nextDateTime());
    position.setValid(parser.next().equals("A"));
    position.setLatitude(parser.nextCoordinate());
    position.setLongitude(parser.nextCoordinate());
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_HDOP, parser.nextDouble(0));
    position.set(Position.KEY_STATUS, parser.next());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Aggregations

Parser (org.traccar.helper.Parser)276 Position (org.traccar.model.Position)260 DeviceSession (org.traccar.DeviceSession)222 DateBuilder (org.traccar.helper.DateBuilder)82 Network (org.traccar.model.Network)51 WifiAccessPoint (org.traccar.model.WifiAccessPoint)16 Date (java.util.Date)14 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)14 LinkedList (java.util.LinkedList)8 Pattern (java.util.regex.Pattern)8 Matcher (java.util.regex.Matcher)4 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)4 QueryStringDecoder (org.jboss.netty.handler.codec.http.QueryStringDecoder)4 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2