Search in sources :

Example 81 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project traccar by traccar.

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)

Example 82 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project traccar by traccar.

the class OsmAndProtocolDecoder 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();
    if (params.isEmpty()) {
        decoder = new QueryStringDecoder(request.getContent().toString(StandardCharsets.US_ASCII), false);
        params = decoder.getParameters();
    }
    Position position = new Position(getProtocolName());
    position.setValid(true);
    Network network = new Network();
    for (Map.Entry<String, List<String>> entry : params.entrySet()) {
        for (String value : entry.getValue()) {
            switch(entry.getKey()) {
                case "id":
                case "deviceid":
                    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value);
                    if (deviceSession == null) {
                        sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
                        return null;
                    }
                    position.setDeviceId(deviceSession.getDeviceId());
                    break;
                case "valid":
                    position.setValid(Boolean.parseBoolean(value));
                    break;
                case "timestamp":
                    try {
                        long timestamp = Long.parseLong(value);
                        if (timestamp < Integer.MAX_VALUE) {
                            timestamp *= 1000;
                        }
                        position.setTime(new Date(timestamp));
                    } catch (NumberFormatException error) {
                        if (value.contains("T")) {
                            position.setTime(new Date(ISODateTimeFormat.dateTimeParser().parseMillis(value)));
                        } else {
                            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            position.setTime(dateFormat.parse(value));
                        }
                    }
                    break;
                case "lat":
                    position.setLatitude(Double.parseDouble(value));
                    break;
                case "lon":
                    position.setLongitude(Double.parseDouble(value));
                    break;
                case "location":
                    String[] location = value.split(",");
                    position.setLatitude(Double.parseDouble(location[0]));
                    position.setLongitude(Double.parseDouble(location[1]));
                    break;
                case "cell":
                    String[] cell = value.split(",");
                    if (cell.length > 4) {
                        network.addCellTower(CellTower.from(Integer.parseInt(cell[0]), Integer.parseInt(cell[1]), Integer.parseInt(cell[2]), Integer.parseInt(cell[3]), Integer.parseInt(cell[4])));
                    } else {
                        network.addCellTower(CellTower.from(Integer.parseInt(cell[0]), Integer.parseInt(cell[1]), Integer.parseInt(cell[2]), Integer.parseInt(cell[3])));
                    }
                    break;
                case "wifi":
                    String[] wifi = value.split(",");
                    network.addWifiAccessPoint(WifiAccessPoint.from(wifi[0].replace('-', ':'), Integer.parseInt(wifi[1])));
                    break;
                case "speed":
                    position.setSpeed(convertSpeed(Double.parseDouble(value), "kn"));
                    break;
                case "bearing":
                case "heading":
                    position.setCourse(Double.parseDouble(value));
                    break;
                case "altitude":
                    position.setAltitude(Double.parseDouble(value));
                    break;
                case "accuracy":
                    position.setAccuracy(Double.parseDouble(value));
                    break;
                case "hdop":
                    position.set(Position.KEY_HDOP, Double.parseDouble(value));
                    break;
                case "batt":
                    position.set(Position.KEY_BATTERY_LEVEL, Double.parseDouble(value));
                    break;
                case "driverUniqueId":
                    position.set(Position.KEY_DRIVER_UNIQUE_ID, value);
                    break;
                default:
                    try {
                        position.set(entry.getKey(), Double.parseDouble(value));
                    } catch (NumberFormatException e) {
                        switch(value) {
                            case "true":
                                position.set(entry.getKey(), true);
                                break;
                            case "false":
                                position.set(entry.getKey(), false);
                                break;
                            default:
                                position.set(entry.getKey(), value);
                                break;
                        }
                    }
                    break;
            }
        }
    }
    if (position.getFixTime() == null) {
        position.setTime(new Date());
    }
    if (network.getCellTowers() != null || network.getWifiAccessPoints() != null) {
        position.setNetwork(network);
    }
    if (position.getLatitude() == 0 && position.getLongitude() == 0) {
        getLastLocation(position, position.getDeviceTime());
    }
    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) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Date(java.util.Date) QueryStringDecoder(org.jboss.netty.handler.codec.http.QueryStringDecoder) Network(org.traccar.model.Network) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) List(java.util.List) Map(java.util.Map) SimpleDateFormat(java.text.SimpleDateFormat)

Example 83 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project traccar by traccar.

the class OwnTracksProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    HttpRequest request = (HttpRequest) msg;
    JsonObject root = Json.createReader(new StringReader(request.getContent().toString(StandardCharsets.US_ASCII))).readObject();
    if (!root.containsKey("_type") || !root.getString("_type").equals("location")) {
        sendResponse(channel, HttpResponseStatus.OK);
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setValid(true);
    position.setLatitude(root.getJsonNumber("lat").doubleValue());
    position.setLongitude(root.getJsonNumber("lon").doubleValue());
    if (root.containsKey("vel")) {
        position.setSpeed(UnitsConverter.knotsFromKph(root.getInt("vel")));
    }
    if (root.containsKey("alt")) {
        position.setAltitude(root.getInt("alt"));
    }
    if (root.containsKey("cog")) {
        position.setCourse(root.getInt("cog"));
    }
    if (root.containsKey("acc")) {
        position.setAccuracy(root.getInt("acc"));
    }
    if (root.containsKey("t")) {
        position.set("t", root.getString("t"));
    }
    if (root.containsKey("batt")) {
        position.set(Position.KEY_BATTERY, root.getInt("batt"));
    }
    position.setTime(new Date(root.getJsonNumber("tst").longValue() * 1000));
    String uniqueId;
    if (root.containsKey("topic")) {
        uniqueId = root.getString("topic");
        if (root.containsKey("tid")) {
            position.set("tid", root.getString("tid"));
        }
    } else {
        uniqueId = root.getString("tid");
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, uniqueId);
    if (deviceSession == null) {
        sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    sendResponse(channel, HttpResponseStatus.OK);
    return position;
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Date(java.util.Date)

Example 84 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project traccar by traccar.

the class PiligrimProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    HttpRequest request = (HttpRequest) msg;
    String uri = request.getUri();
    if (uri.startsWith("/config")) {
        sendResponse(channel, "CONFIG: OK");
    } else if (uri.startsWith("/addlog")) {
        sendResponse(channel, "ADDLOG: OK");
    } else if (uri.startsWith("/inform")) {
        sendResponse(channel, "INFORM: OK");
    } else if (uri.startsWith("/bingps")) {
        sendResponse(channel, "BINGPS: OK");
        QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, decoder.getParameters().get("imei").get(0));
        if (deviceSession == null) {
            return null;
        }
        List<Position> positions = new LinkedList<>();
        ChannelBuffer buf = request.getContent();
        while (buf.readableBytes() > 2) {
            // header
            buf.readUnsignedByte();
            int type = buf.readUnsignedByte();
            // length
            buf.readUnsignedByte();
            if (type == MSG_GPS || type == MSG_GPS_SENSORS) {
                Position position = new Position(getProtocolName());
                position.setDeviceId(deviceSession.getDeviceId());
                DateBuilder dateBuilder = new DateBuilder().setDay(buf.readUnsignedByte()).setMonth(buf.getByte(buf.readerIndex()) & 0x0f).setYear(2010 + (buf.readUnsignedByte() >> 4)).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
                position.setTime(dateBuilder.getDate());
                double latitude = buf.readUnsignedByte();
                latitude += buf.readUnsignedByte() / 60.0;
                latitude += buf.readUnsignedByte() / 6000.0;
                latitude += buf.readUnsignedByte() / 600000.0;
                double longitude = buf.readUnsignedByte();
                longitude += buf.readUnsignedByte() / 60.0;
                longitude += buf.readUnsignedByte() / 6000.0;
                longitude += buf.readUnsignedByte() / 600000.0;
                int flags = buf.readUnsignedByte();
                if (BitUtil.check(flags, 0)) {
                    latitude = -latitude;
                }
                if (BitUtil.check(flags, 1)) {
                    longitude = -longitude;
                }
                position.setLatitude(latitude);
                position.setLongitude(longitude);
                int satellites = buf.readUnsignedByte();
                position.set(Position.KEY_SATELLITES, satellites);
                position.setValid(satellites >= 3);
                position.setSpeed(buf.readUnsignedByte());
                double course = buf.readUnsignedByte() << 1;
                course += (flags >> 2) & 1;
                course += buf.readUnsignedByte() / 100.0;
                position.setCourse(course);
                if (type == MSG_GPS_SENSORS) {
                    double power = buf.readUnsignedByte();
                    power += buf.readUnsignedByte() << 8;
                    position.set(Position.KEY_POWER, power * 0.01);
                    double battery = buf.readUnsignedByte();
                    battery += buf.readUnsignedByte() << 8;
                    position.set(Position.KEY_BATTERY, battery * 0.01);
                    buf.skipBytes(6);
                }
                positions.add(position);
            } else if (type == MSG_EVENTS) {
                buf.skipBytes(13);
            }
        }
        return positions;
    }
    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) LinkedList(java.util.LinkedList) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 85 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project traccar by traccar.

the class SigfoxProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    HttpRequest request = (HttpRequest) msg;
    JsonObject json = Json.createReader(new StringReader(URLDecoder.decode(request.getContent().toString(StandardCharsets.UTF_8).split("=")[0], "UTF-8"))).readObject();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, json.getString("device"));
    if (deviceSession == null) {
        sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(new Date(json.getInt("time") * 1000L));
    ChannelBuffer buf = ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, DataConverter.parseHex(json.getString("data")));
    int type = buf.readUnsignedByte() >> 4;
    if (type == 0) {
        position.setValid(true);
        position.setLatitude(buf.readInt() * 0.0000001);
        position.setLongitude(buf.readInt() * 0.0000001);
        position.setCourse(buf.readUnsignedByte() * 2);
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
        position.set(Position.KEY_BATTERY, buf.readUnsignedByte() * 0.025);
    } else {
        getLastLocation(position, position.getDeviceTime());
    }
    position.set(Position.KEY_RSSI, json.getJsonNumber("rssi").doubleValue());
    position.set(Position.KEY_INDEX, json.getInt("seqNumber"));
    sendResponse(channel, HttpResponseStatus.OK);
    return position;
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Date(java.util.Date) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)136 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)69 Channel (org.jboss.netty.channel.Channel)47 Test (org.junit.Test)47 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)46 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)43 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)31 ChannelFuture (org.jboss.netty.channel.ChannelFuture)26 List (java.util.List)19 ArrayList (java.util.ArrayList)17 Map (java.util.Map)17 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)17 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)17 InetSocketAddress (java.net.InetSocketAddress)14 ChannelHandlerContext (org.jboss.netty.channel.ChannelHandlerContext)13 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)13 SimpleHttpResponseHandler (com.linkedin.databus.core.test.netty.SimpleHttpResponseHandler)12 SimpleTestHttpClient (com.linkedin.databus.core.test.netty.SimpleTestHttpClient)12 Configuration (org.apache.hadoop.conf.Configuration)12 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)12