Search in sources :

Example 76 with Parser

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

the class MeitrackProtocolDecoder method decodeRegular.

private Position decodeRegular(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
    Parser parser = new Parser(PATTERN, buf.toString(StandardCharsets.US_ASCII));
    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());
    int event = parser.nextInt(0);
    position.set(Position.KEY_EVENT, event);
    position.set(Position.KEY_ALARM, decodeAlarm(event));
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setTime(parser.nextDateTime());
    position.setValid(parser.next().equals("A"));
    position.set(Position.KEY_SATELLITES, parser.nextInt());
    int rssi = parser.nextInt(0);
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_HDOP, parser.nextDouble());
    position.setAltitude(parser.nextDouble(0));
    position.set(Position.KEY_ODOMETER, parser.nextInt(0));
    position.set("runtime", parser.next());
    position.setNetwork(new Network(CellTower.from(parser.nextInt(0), parser.nextInt(0), parser.nextHexInt(0), parser.nextHexInt(0), rssi)));
    position.set(Position.KEY_STATUS, parser.next());
    for (int i = 1; i <= 3; i++) {
        if (parser.hasNext()) {
            position.set(Position.PREFIX_ADC + i, parser.nextHexInt(0));
        }
    }
    String deviceModel = Context.getIdentityManager().getById(deviceSession.getDeviceId()).getModel();
    if (deviceModel == null) {
        deviceModel = "";
    }
    switch(deviceModel.toUpperCase()) {
        case "MVT340":
        case "MVT380":
            position.set(Position.KEY_BATTERY, parser.nextHexInt(0) * 3.0 * 2.0 / 1024.0);
            position.set(Position.KEY_POWER, parser.nextHexInt(0) * 3.0 * 16.0 / 1024.0);
            break;
        case "MT90":
            position.set(Position.KEY_BATTERY, parser.nextHexInt(0) * 3.3 * 2.0 / 4096.0);
            position.set(Position.KEY_POWER, parser.nextHexInt(0));
            break;
        case "T1":
        case "T3":
        case "MVT100":
        case "MVT600":
        case "MVT800":
        case "TC68":
        case "TC68S":
            position.set(Position.KEY_BATTERY, parser.nextHexInt(0) * 3.3 * 2.0 / 4096.0);
            position.set(Position.KEY_POWER, parser.nextHexInt(0) * 3.3 * 16.0 / 4096.0);
            break;
        case "T311":
        case "T322X":
        case "T333":
        case "T355":
            position.set(Position.KEY_BATTERY, parser.nextHexInt(0) / 100.0);
            position.set(Position.KEY_POWER, parser.nextHexInt(0) / 100.0);
            break;
        default:
            position.set(Position.KEY_BATTERY, parser.nextHexInt(0));
            position.set(Position.KEY_POWER, parser.nextHexInt(0));
            break;
    }
    String eventData = parser.next();
    if (eventData != null && !eventData.isEmpty()) {
        switch(event) {
            case 37:
                position.set(Position.KEY_DRIVER_UNIQUE_ID, eventData);
                break;
            default:
                position.set("eventData", eventData);
                break;
        }
    }
    int protocol = parser.nextInt(0);
    if (parser.hasNext()) {
        String fuel = parser.next();
        position.set(Position.KEY_FUEL_LEVEL, Integer.parseInt(fuel.substring(0, 2), 16) + Integer.parseInt(fuel.substring(2), 16) * 0.01);
    }
    if (parser.hasNext()) {
        for (String temp : parser.next().split("\\|")) {
            int index = Integer.parseInt(temp.substring(0, 2), 16);
            if (protocol >= 3) {
                double value = (short) Integer.parseInt(temp.substring(2), 16);
                position.set(Position.PREFIX_TEMP + index, value * 0.01);
            } else {
                double value = Byte.parseByte(temp.substring(2, 4), 16);
                value += (value < 0 ? -0.01 : 0.01) * Integer.parseInt(temp.substring(4), 16);
                position.set(Position.PREFIX_TEMP + index, value);
            }
        }
    }
    if (parser.hasNext(2)) {
        // count
        parser.nextInt();
        decodeDataFields(position, parser.next().split(","));
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) Parser(org.traccar.helper.Parser)

Example 77 with Parser

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

the class MtxProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    if (channel != null) {
        channel.write("#ACK");
    }
    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.setValid(true);
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000);
    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());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 78 with Parser

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

the class PathAwayProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    HttpRequest request = (HttpRequest) msg;
    QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, decoder.getParameters().get("UserName").get(0));
    if (deviceSession == null) {
        return null;
    }
    Parser parser = new Parser(PATTERN, decoder.getParameters().get("LOC").get(0));
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
    position.setValid(true);
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setAltitude(parser.nextDouble(0));
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    if (channel != null) {
        HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        channel.write(response).addListener(ChannelFutureListener.CLOSE);
    }
    return position;
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) QueryStringDecoder(org.jboss.netty.handler.codec.http.QueryStringDecoder) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Parser(org.traccar.helper.Parser)

Example 79 with Parser

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

the class OkoProtocolDecoder 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;
    if (parser.hasNext()) {
        deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    } else {
        deviceSession = getDeviceSession(channel, remoteAddress);
    }
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
    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(), parser.nextInt(), parser.nextInt());
    position.setTime(dateBuilder.getDate());
    position.set(Position.KEY_SATELLITES, parser.nextInt());
    position.set(Position.PREFIX_ADC + 1, parser.nextDouble());
    position.set(Position.KEY_EVENT, parser.next());
    position.set(Position.KEY_POWER, parser.nextDouble());
    position.set(Position.KEY_INPUT, parser.nextHexInt());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Parser(org.traccar.helper.Parser)

Example 80 with Parser

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

the class OpenGtsProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    HttpRequest request = (HttpRequest) msg;
    QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
    Map<String, List<String>> params = decoder.getParameters();
    Position position = new Position();
    position.setProtocol(getProtocolName());
    for (Map.Entry<String, List<String>> entry : params.entrySet()) {
        String value = entry.getValue().get(0);
        switch(entry.getKey()) {
            case "id":
                DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value);
                if (deviceSession == null) {
                    sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
                    return null;
                }
                position.setDeviceId(deviceSession.getDeviceId());
                break;
            case "gprmc":
                Parser parser = new Parser(PATTERN, value);
                if (!parser.matches()) {
                    sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
                    return null;
                }
                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());
                break;
            case "alt":
                position.setAltitude(Double.parseDouble(value));
                break;
            case "batt":
                position.set(Position.KEY_BATTERY_LEVEL, Double.parseDouble(value));
                break;
            default:
                break;
        }
    }
    if (position.getDeviceId() != 0) {
        sendResponse(channel, HttpResponseStatus.OK);
        return position;
    } else {
        sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
        return null;
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) QueryStringDecoder(org.jboss.netty.handler.codec.http.QueryStringDecoder) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) List(java.util.List) Map(java.util.Map) 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