Search in sources :

Example 66 with Parser

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

the class XexunProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    Pattern pattern = PATTERN_BASIC;
    if (full) {
        pattern = PATTERN_FULL;
    }
    Parser parser = new Parser(pattern, (String) msg);
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    if (full) {
        position.set("serial", parser.next());
        position.set("number", parser.next());
    }
    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(convertSpeed(parser.nextDouble(0), "kn"));
    position.setCourse(parser.nextDouble(0));
    dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    position.setTime(dateBuilder.getDate());
    position.set("signal", parser.next());
    decodeStatus(position, parser.next());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    if (full) {
        position.set(Position.KEY_SATELLITES, parser.nextInt());
        position.setAltitude(parser.nextDouble(0));
        position.set(Position.KEY_POWER, parser.nextDouble(0));
    }
    return position;
}
Also used : Pattern(java.util.regex.Pattern) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Parser(org.traccar.helper.Parser)

Example 67 with Parser

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

the class WatchProtocolDecoder method decodePosition.

private Position decodePosition(DeviceSession deviceSession, String data) {
    Parser parser = new Parser(PATTERN_POSITION, data);
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
    position.setValid(parser.next().equals("A"));
    position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM));
    position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM));
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
    position.setCourse(parser.nextDouble(0));
    position.setAltitude(parser.nextDouble(0));
    position.set(Position.KEY_SATELLITES, parser.nextInt(0));
    position.set(Position.KEY_RSSI, parser.nextInt(0));
    position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt(0));
    position.set(Position.KEY_STEPS, parser.nextInt(0));
    int status = parser.nextHexInt(0);
    position.set(Position.KEY_ALARM, decodeAlarm(status));
    if (BitUtil.check(status, 4)) {
        position.set(Position.KEY_MOTION, true);
    }
    String[] values = parser.next().split(",");
    int index = 0;
    Network network = new Network();
    int cellCount = Integer.parseInt(values[index++]);
    // timing advance
    index += 1;
    int mcc = Integer.parseInt(values[index++]);
    int mnc = Integer.parseInt(values[index++]);
    for (int i = 0; i < cellCount; i++) {
        network.addCellTower(CellTower.from(mcc, mnc, Integer.parseInt(values[index++]), Integer.parseInt(values[index++]), Integer.parseInt(values[index++])));
    }
    if (index < values.length && !values[index].isEmpty()) {
        int wifiCount = Integer.parseInt(values[index++]);
        for (int i = 0; i < wifiCount; i++) {
            // wifi name
            index += 1;
            network.addWifiAccessPoint(WifiAccessPoint.from(values[index++], Integer.parseInt(values[index++])));
        }
    }
    if (network.getCellTowers() != null || network.getWifiAccessPoints() != null) {
        position.setNetwork(network);
    }
    return position;
}
Also used : Position(org.traccar.model.Position) Network(org.traccar.model.Network) WifiAccessPoint(org.traccar.model.WifiAccessPoint) Parser(org.traccar.helper.Parser)

Example 68 with Parser

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

the class Xt013ProtocolDecoder 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.setTime(parser.nextDateTime());
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
    position.setCourse(parser.nextDouble(0));
    position.setAltitude(parser.nextDouble(0));
    position.setValid(parser.next().equals("F"));
    position.set(Position.KEY_SATELLITES, parser.nextInt());
    position.set(Position.KEY_RSSI, parser.nextDouble());
    position.set(Position.KEY_BATTERY, parser.nextDouble(0));
    position.set(Position.KEY_CHARGE, parser.next().equals("1"));
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 69 with Parser

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

the class YwtProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    // Synchronization
    if (sentence.startsWith("%SN") && channel != null) {
        int start = sentence.indexOf(':');
        int end = start;
        for (int i = 0; i < 4; i++) {
            end = sentence.indexOf(',', end + 1);
        }
        if (end == -1) {
            end = sentence.length();
        }
        channel.write("%AT+SN=" + sentence.substring(start, end));
        return null;
    }
    Parser parser = new Parser(PATTERN, sentence);
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    String type = parser.next();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime());
    position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
    position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
    position.setAltitude(parser.nextDouble(0));
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    int satellites = parser.nextInt(0);
    position.setValid(satellites >= 3);
    position.set(Position.KEY_SATELLITES, satellites);
    String reportId = parser.next();
    position.set(Position.KEY_STATUS, parser.next());
    // Send response
    if ((type.equals("KP") || type.equals("EP")) && channel != null) {
        channel.write("%AT+" + type + "=" + reportId + "\r\n");
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 70 with Parser

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

the class XirgoProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    Parser parser;
    if (newFormat == null) {
        parser = new Parser(PATTERN_NEW, sentence);
        if (parser.matches()) {
            newFormat = true;
        } else {
            parser = new Parser(PATTERN_OLD, sentence);
            if (parser.matches()) {
                newFormat = false;
            } else {
                return null;
            }
        }
    } else {
        if (newFormat) {
            parser = new Parser(PATTERN_NEW, sentence);
        } else {
            parser = new Parser(PATTERN_OLD, sentence);
        }
        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());
    decodeEvent(position, parser.nextInt());
    position.setTime(parser.nextDateTime());
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setAltitude(parser.nextDouble(0));
    position.setSpeed(UnitsConverter.knotsFromMph(parser.nextDouble(0)));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_SATELLITES, parser.nextInt());
    position.set(Position.KEY_HDOP, parser.nextDouble());
    if (newFormat) {
        position.set(Position.KEY_ODOMETER, UnitsConverter.metersFromMiles(parser.nextDouble(0)));
        position.set(Position.KEY_FUEL_CONSUMPTION, parser.next());
    }
    position.set(Position.KEY_BATTERY, parser.nextDouble(0));
    position.set(Position.KEY_RSSI, parser.nextDouble());
    if (!newFormat) {
        position.set(Position.KEY_ODOMETER, UnitsConverter.metersFromMiles(parser.nextDouble(0)));
    }
    position.setValid(parser.nextInt(0) == 1);
    if (newFormat && parser.hasNext(13)) {
        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_OUT + 1, parser.nextInt());
        position.set(Position.PREFIX_ADC + 1, parser.nextDouble());
        position.set(Position.KEY_FUEL_LEVEL, parser.nextDouble());
        position.set(Position.KEY_HOURS, parser.nextInt());
        position.set("oilPressure", parser.nextInt());
        position.set("oilLevel", parser.nextInt());
        position.set("oilTemp", parser.nextInt());
        position.set("coolantPressure", parser.nextInt());
        position.set("coolantLevel", parser.nextInt());
        position.set("coolantTemp", 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