Search in sources :

Example 51 with Parser

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

the class IdplProtocolDecoder 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());
    position.set(Position.KEY_TYPE, parser.nextInt(0));
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
    position.setValid(parser.next().equals("A"));
    position.setLatitude(parser.nextCoordinate(CoordinateFormat.DEG_MIN_MIN_HEM));
    position.setLongitude(parser.nextCoordinate(CoordinateFormat.DEG_MIN_MIN_HEM));
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_SATELLITES, parser.nextInt(0));
    position.set(Position.KEY_RSSI, parser.nextInt(0));
    position.set("vehicleStatus", parser.next());
    position.set(Position.KEY_POWER, parser.nextInt(0));
    position.set(Position.KEY_BATTERY, parser.nextDouble(0));
    if (parser.nextInt(0) == 1) {
        position.set(Position.KEY_ALARM, Position.ALARM_SOS);
    }
    // body tamper
    parser.nextInt(0);
    position.set("acStatus", parser.nextInt(0));
    position.set(Position.KEY_IGNITION, parser.nextInt(0) == 1);
    position.set(Position.KEY_OUTPUT, parser.nextInt(0));
    position.set(Position.PREFIX_ADC + 1, parser.nextInt(0));
    position.set(Position.PREFIX_ADC + 2, parser.nextInt(0));
    position.set(Position.KEY_VERSION_FW, parser.next());
    position.set(Position.KEY_ARCHIVE, parser.next().equals("R"));
    // checksum
    parser.next();
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 52 with Parser

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

the class KenjiProtocolDecoder 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.set(Position.KEY_ALARM, decodeAlarm(parser.nextHexInt(0)));
    position.set(Position.KEY_OUTPUT, parser.nextHexInt(0));
    position.set(Position.KEY_INPUT, parser.nextHexInt(0));
    DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    position.setValid(parser.next().equals("A"));
    position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN));
    position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN));
    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());
    position.set(Position.KEY_SATELLITES, parser.nextInt(0));
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Parser(org.traccar.helper.Parser)

Example 53 with Parser

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

the class L100ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // start marker
    buf.readUnsignedByte();
    // type
    buf.readUnsignedByte();
    String sentence = buf.readBytes(buf.readableBytes() - 2).toString(StandardCharsets.US_ASCII);
    Parser parser = new Parser(PATTERN, 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());
    DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), 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());
    position.set(Position.KEY_STATUS, parser.next());
    position.set(Position.PREFIX_ADC + 1, parser.next());
    position.set(Position.KEY_ODOMETER, parser.nextDouble(0));
    position.set(Position.PREFIX_TEMP + 1, parser.nextDouble(0));
    position.set(Position.KEY_BATTERY, parser.nextDouble(0));
    int rssi = parser.nextInt(0);
    position.setNetwork(new Network(CellTower.from(parser.nextInt(0), parser.nextInt(0), parser.nextHexInt(0), parser.nextHexInt(0), rssi)));
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Network(org.traccar.model.Network) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Parser(org.traccar.helper.Parser)

Example 54 with Parser

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

the class LaipacProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    if (sentence.startsWith("$ECHK") && channel != null) {
        // heartbeat
        channel.write(sentence + "\r\n");
        return null;
    }
    Parser parser = new Parser(PATTERN, sentence);
    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());
    DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    String status = parser.next();
    position.setValid(status.toUpperCase().equals("A"));
    position.set(Position.KEY_STATUS, status);
    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());
    String event = parser.next();
    position.set(Position.KEY_ALARM, decodeAlarm(event));
    position.set(Position.KEY_EVENT, event);
    position.set(Position.KEY_BATTERY, Double.parseDouble(parser.next().replaceAll("\\.", "")) * 0.001);
    position.set(Position.KEY_ODOMETER, parser.nextDouble());
    position.set(Position.KEY_GPS, parser.nextInt());
    position.set(Position.PREFIX_ADC + 1, parser.nextDouble() * 0.001);
    position.set(Position.PREFIX_ADC + 2, parser.nextDouble() * 0.001);
    Integer lac = parser.nextHexInt();
    Integer cid = parser.nextHexInt();
    Integer mcc = parser.nextInt();
    Integer mnc = parser.nextInt();
    if (lac != null && cid != null && mcc != null && mnc != null) {
        position.setNetwork(new Network(CellTower.from(mcc, mnc, lac, cid)));
    }
    String checksum = parser.next();
    if (channel != null) {
        if (event.equals("3")) {
            channel.write("$AVCFG,00000000,d*31\r\n");
        } else if (event.equals("X") || event.equals("4")) {
            channel.write("$AVCFG,00000000,x*2D\r\n");
        } else if (event.equals("Z")) {
            channel.write("$AVCFG,00000000,z*2F\r\n");
        } else if (Character.isLowerCase(status.charAt(0))) {
            String response = "$EAVACK," + event + "," + checksum;
            response += Checksum.nmea(response) + "\r\n";
            channel.write(response);
        }
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Network(org.traccar.model.Network) Parser(org.traccar.helper.Parser)

Example 55 with Parser

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

the class MegastekProtocolDecoder method decodeOld.

private Position decodeOld(Channel channel, SocketAddress remoteAddress, String sentence) {
    // Detect type
    boolean simple = sentence.charAt(3) == ',' || sentence.charAt(6) == ',';
    // Split message
    String id;
    String location;
    String status;
    if (simple) {
        int beginIndex = sentence.indexOf(',') + 1;
        int endIndex = sentence.indexOf(',', beginIndex);
        id = sentence.substring(beginIndex, endIndex);
        beginIndex = endIndex + 1;
        endIndex = sentence.indexOf('*', beginIndex);
        if (endIndex != -1) {
            endIndex += 3;
        } else {
            endIndex = sentence.length();
        }
        location = sentence.substring(beginIndex, endIndex);
        beginIndex = endIndex + 1;
        if (beginIndex > sentence.length()) {
            beginIndex = endIndex;
        }
        status = sentence.substring(beginIndex);
    } else {
        int beginIndex = 3;
        int endIndex = beginIndex + 16;
        id = sentence.substring(beginIndex, endIndex).trim();
        beginIndex = endIndex + 2;
        endIndex = sentence.indexOf('*', beginIndex) + 3;
        location = sentence.substring(beginIndex, endIndex);
        beginIndex = endIndex + 1;
        status = sentence.substring(beginIndex);
    }
    Position position = new Position(getProtocolName());
    if (!parseLocation(location, position)) {
        return null;
    }
    if (simple) {
        Parser parser = new Parser(PATTERN_SIMPLE, status);
        if (parser.matches()) {
            position.set(Position.KEY_ALARM, decodeAlarm(parser.next()));
            DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next(), id);
            if (deviceSession == null) {
                return null;
            }
            position.setDeviceId(deviceSession.getDeviceId());
            String sat = parser.next();
            if (sat.contains("/")) {
                position.set(Position.KEY_SATELLITES, Integer.parseInt(sat.split("/")[0]));
                position.set(Position.KEY_SATELLITES_VISIBLE, Integer.parseInt(sat.split("/")[1]));
            } else {
                position.set(Position.KEY_SATELLITES, Integer.parseInt(sat));
            }
            position.setAltitude(parser.nextDouble(0));
            position.set(Position.KEY_BATTERY_LEVEL, parser.nextDouble(0));
            String charger = parser.next();
            if (charger != null) {
                position.set(Position.KEY_CHARGE, Integer.parseInt(charger) == 1);
            }
            if (parser.hasNext(4)) {
                position.setNetwork(new Network(CellTower.from(parser.nextInt(0), parser.nextInt(0), parser.nextHexInt(0), parser.nextHexInt(0))));
            }
        } else {
            DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
            if (deviceSession == null) {
                return null;
            }
            position.setDeviceId(deviceSession.getDeviceId());
        }
    } else {
        Parser parser = new Parser(PATTERN_ALTERNATIVE, status);
        if (parser.matches()) {
            DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
            if (deviceSession == null) {
                return null;
            }
            position.setDeviceId(deviceSession.getDeviceId());
            position.setNetwork(new Network(CellTower.from(parser.nextInt(0), parser.nextInt(0), parser.nextHexInt(0), parser.nextHexInt(0), parser.nextInt(0))));
            position.set(Position.KEY_BATTERY_LEVEL, parser.nextDouble());
            position.set(Position.KEY_FLAGS, parser.next());
            position.set(Position.KEY_INPUT, parser.next());
            position.set(Position.KEY_OUTPUT, parser.next());
            position.set(Position.PREFIX_ADC + 1, parser.next());
            position.set(Position.PREFIX_ADC + 2, parser.next());
            position.set(Position.PREFIX_ADC + 3, parser.next());
            position.set(Position.KEY_ALARM, decodeAlarm(parser.next()));
        }
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) 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