Search in sources :

Example 31 with Parser

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

the class TrakMateProtocolDecoder method decodePer.

private Object decodePer(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    Parser parser = new Parser(PATTERN_PER, (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());
    // seq
    parser.next();
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.HMS_DMY));
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_IGNITION, parser.nextInt(0) == 1);
    position.set("dop1", parser.next());
    position.set("dop2", parser.next());
    position.set(Position.KEY_INPUT, parser.next());
    position.set(Position.KEY_BATTERY, parser.nextDouble(0));
    position.set(Position.KEY_POWER, parser.nextDouble());
    position.set(Position.KEY_ODOMETER, parser.nextDouble(0));
    position.set("pulseOdometer", parser.next());
    position.set(Position.KEY_STATUS, parser.nextInt(0));
    position.setValid(parser.nextInt(0) != 0);
    position.set(Position.KEY_ARCHIVE, parser.nextInt(0) == 1);
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 32 with Parser

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

the class UproProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    if (buf.getByte(buf.readerIndex()) != '*') {
        throw new ParseException(null, 0);
    }
    int headerIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&');
    if (headerIndex < 0) {
        headerIndex = buf.writerIndex();
    }
    String header = buf.readBytes(headerIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII);
    Parser parser = new Parser(PATTERN_HEADER, header);
    if (!parser.matches()) {
        return null;
    }
    boolean reply = parser.next().equals("1");
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    String type = parser.next();
    String subtype = parser.next();
    if (reply && channel != null) {
        channel.write("*MG20Y" + type + subtype + "#");
    }
    while (buf.readable()) {
        // skip delimiter
        buf.readByte();
        byte dataType = buf.readByte();
        int delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&');
        if (delimiterIndex < 0) {
            delimiterIndex = buf.writerIndex();
        }
        ChannelBuffer data = buf.readBytes(delimiterIndex - buf.readerIndex());
        switch(dataType) {
            case 'A':
                decodeLocation(position, data.toString(StandardCharsets.US_ASCII));
                break;
            case 'B':
                position.set(Position.KEY_STATUS, data.toString(StandardCharsets.US_ASCII));
                break;
            case 'C':
                long odometer = 0;
                while (data.readable()) {
                    odometer <<= 4;
                    odometer += data.readByte() - (byte) '0';
                }
                position.set(Position.KEY_ODOMETER, odometer * 2 * 1852 / 3600);
                break;
            case 'P':
                position.setNetwork(new Network(CellTower.from(Integer.parseInt(data.readBytes(4).toString(StandardCharsets.US_ASCII)), Integer.parseInt(data.readBytes(4).toString(StandardCharsets.US_ASCII)), Integer.parseInt(data.readBytes(4).toString(StandardCharsets.US_ASCII), 16), Integer.parseInt(data.readBytes(4).toString(StandardCharsets.US_ASCII), 16))));
                break;
            case 'Q':
                position.set("obd-pid", ChannelBuffers.hexDump(data));
                break;
            case 'R':
                position.set("odb-travel", ChannelBuffers.hexDump(data));
                break;
            case 'S':
                position.set("obd-traffic", ChannelBuffers.hexDump(data));
                break;
            default:
                break;
        }
    }
    if (position.getLatitude() != 0 && position.getLongitude() != 0) {
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) ParseException(java.text.ParseException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Parser(org.traccar.helper.Parser)

Example 33 with Parser

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

the class UproProtocolDecoder method decodeLocation.

private void decodeLocation(Position position, String data) {
    Parser parser = new Parser(PATTERN_LOCATION, data);
    if (parser.matches()) {
        DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
        position.setValid(true);
        position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_MIN_MIN));
        position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_MIN_MIN));
        int flags = parser.nextInt(0);
        position.setValid(BitUtil.check(flags, 0));
        if (!BitUtil.check(flags, 1)) {
            position.setLatitude(-position.getLatitude());
        }
        if (!BitUtil.check(flags, 2)) {
            position.setLongitude(-position.getLongitude());
        }
        position.setSpeed(parser.nextInt(0) * 2);
        position.setCourse(parser.nextInt(0) * 10);
        dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
        position.setTime(dateBuilder.getDate());
    }
}
Also used : DateBuilder(org.traccar.helper.DateBuilder) Parser(org.traccar.helper.Parser)

Example 34 with Parser

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

the class V680ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    sentence = sentence.trim();
    if (sentence.length() == 16) {
        getDeviceSession(channel, remoteAddress, sentence.substring(1, sentence.length()));
    } else {
        Parser parser = new Parser(PATTERN, sentence);
        if (!parser.matches()) {
            return null;
        }
        Position position = new Position(getProtocolName());
        DeviceSession deviceSession;
        if (parser.hasNext()) {
            deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
        } else {
            deviceSession = getDeviceSession(channel, remoteAddress);
        }
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());
        position.set("user", parser.next());
        position.setValid(parser.nextInt(0) > 0);
        position.set("password", parser.next());
        position.set(Position.KEY_EVENT, parser.next());
        position.set("packet", parser.next());
        position.set("lbsData", parser.next());
        double lon = parser.nextDouble(0);
        boolean west = parser.next().equals("W");
        double lat = parser.nextDouble(0);
        boolean south = parser.next().equals("S");
        if (lat > 90 || lon > 180) {
            int lonDegrees = (int) (lon * 0.01);
            lon = (lon - lonDegrees * 100) / 60.0;
            lon += lonDegrees;
            int latDegrees = (int) (lat * 0.01);
            lat = (lat - latDegrees * 100) / 60.0;
            lat += latDegrees;
        }
        position.setLongitude(west ? -lon : lon);
        position.setLatitude(south ? -lat : lat);
        position.setSpeed(parser.nextDouble(0));
        position.setCourse(parser.nextDouble(0));
        int day = parser.nextInt(0);
        int month = parser.nextInt(0);
        if (day == 0 && month == 0) {
            // invalid date
            return null;
        }
        DateBuilder dateBuilder = new DateBuilder().setDate(parser.nextInt(0), month, day).setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
        position.setTime(dateBuilder.getDate());
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Parser(org.traccar.helper.Parser)

Example 35 with Parser

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

the class VtfmsProtocolDecoder 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_ALARM, decodeAlarm(parser.nextInt()));
    position.set(Position.KEY_RSSI, parser.nextInt());
    position.set(Position.KEY_SATELLITES, parser.nextInt());
    position.setValid(parser.next().equals("A"));
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.HMS_DMY));
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    if (parser.hasNext()) {
        position.setCourse(parser.nextDouble(0));
    }
    if (parser.hasNext()) {
        String direction = parser.next();
        for (int i = 0; i < DIRECTIONS.length; i++) {
            if (direction.equals(DIRECTIONS[i])) {
                position.setCourse(i * 45.0);
                break;
            }
        }
    }
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
    position.set(Position.KEY_HOURS, parser.nextInt());
    position.set("idleHours", parser.nextInt());
    position.set(Position.KEY_ODOMETER, parser.nextInt() * 100);
    position.set(Position.KEY_CHARGE, parser.next().equals("1"));
    position.set(Position.KEY_POWER, parser.nextDouble());
    position.set(Position.KEY_FUEL_LEVEL, parser.nextInt());
    position.set(Position.PREFIX_ADC + 1, parser.nextDouble());
    position.set(Position.PREFIX_ADC + 2, parser.nextDouble());
    position.set(Position.PREFIX_IN + 1, parser.nextInt());
    position.set(Position.PREFIX_IN + 2, parser.nextInt());
    position.set(Position.PREFIX_IN + 3, parser.nextInt());
    position.set(Position.PREFIX_IN + 4, parser.nextInt());
    position.set(Position.PREFIX_OUT + 1, parser.nextInt());
    position.set(Position.PREFIX_OUT + 2, parser.nextInt());
    position.set(Position.PREFIX_OUT + 3, parser.nextInt());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Aggregations

Parser (org.traccar.helper.Parser)137 Position (org.traccar.model.Position)129 DeviceSession (org.traccar.DeviceSession)110 DateBuilder (org.traccar.helper.DateBuilder)41 Network (org.traccar.model.Network)25 WifiAccessPoint (org.traccar.model.WifiAccessPoint)8 Date (java.util.Date)7 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)7 LinkedList (java.util.LinkedList)4 Pattern (java.util.regex.Pattern)4 Matcher (java.util.regex.Matcher)2 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)2 QueryStringDecoder (org.jboss.netty.handler.codec.http.QueryStringDecoder)2 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1