Search in sources :

Example 76 with Position

use of org.traccar.model.Position in project traccar by traccar.

the class FilterHandler method filter.

private boolean filter(Position position) {
    StringBuilder filterType = new StringBuilder();
    Position last = null;
    if (Context.getIdentityManager() != null) {
        last = Context.getIdentityManager().getLastPosition(position.getDeviceId());
    }
    if (skipLimit(position, last) || skipAttributes(position)) {
        return false;
    }
    if (filterInvalid(position)) {
        filterType.append("Invalid ");
    }
    if (filterZero(position)) {
        filterType.append("Zero ");
    }
    if (filterDuplicate(position, last)) {
        filterType.append("Duplicate ");
    }
    if (filterFuture(position)) {
        filterType.append("Future ");
    }
    if (filterAccuracy(position)) {
        filterType.append("Accuracy ");
    }
    if (filterApproximate(position)) {
        filterType.append("Approximate ");
    }
    if (filterStatic(position)) {
        filterType.append("Static ");
    }
    if (filterDistance(position, last)) {
        filterType.append("Distance ");
    }
    if (filterMaxSpeed(position, last)) {
        filterType.append("MaxSpeed ");
    }
    if (filterType.length() > 0) {
        StringBuilder message = new StringBuilder();
        message.append("Position filtered by ");
        message.append(filterType.toString());
        message.append("filters from device: ");
        message.append(Context.getIdentityManager().getById(position.getDeviceId()).getUniqueId());
        message.append(" with id: ");
        message.append(position.getDeviceId());
        Log.info(message.toString());
        return true;
    }
    return false;
}
Also used : Position(org.traccar.model.Position)

Example 77 with Position

use of org.traccar.model.Position in project traccar by traccar.

the class GeolocationHandler method handleUpstream.

@Override
public void handleUpstream(final ChannelHandlerContext ctx, ChannelEvent evt) throws Exception {
    if (!(evt instanceof MessageEvent)) {
        ctx.sendUpstream(evt);
        return;
    }
    final MessageEvent event = (MessageEvent) evt;
    Object message = event.getMessage();
    if (message instanceof Position) {
        final Position position = (Position) message;
        if ((position.getOutdated() || processInvalidPositions && !position.getValid()) && position.getNetwork() != null) {
            Context.getStatisticsManager().registerGeolocationRequest();
            geolocationProvider.getLocation(position.getNetwork(), new GeolocationProvider.LocationProviderCallback() {

                @Override
                public void onSuccess(double latitude, double longitude, double accuracy) {
                    position.set(Position.KEY_APPROXIMATE, true);
                    position.setValid(true);
                    position.setFixTime(position.getDeviceTime());
                    position.setLatitude(latitude);
                    position.setLongitude(longitude);
                    position.setAccuracy(accuracy);
                    position.setAltitude(0);
                    position.setSpeed(0);
                    position.setCourse(0);
                    position.set(Position.KEY_RSSI, 0);
                    Channels.fireMessageReceived(ctx, position, event.getRemoteAddress());
                }

                @Override
                public void onFailure(Throwable e) {
                    Log.warning(e);
                    Channels.fireMessageReceived(ctx, position, event.getRemoteAddress());
                }
            });
        } else {
            Channels.fireMessageReceived(ctx, position, event.getRemoteAddress());
        }
    } else {
        Channels.fireMessageReceived(ctx, message, event.getRemoteAddress());
    }
}
Also used : Position(org.traccar.model.Position) MessageEvent(org.jboss.netty.channel.MessageEvent) GeolocationProvider(org.traccar.geolocation.GeolocationProvider)

Example 78 with Position

use of org.traccar.model.Position in project traccar by traccar.

the class GeocoderHandler method handleUpstream.

@Override
public void handleUpstream(final ChannelHandlerContext ctx, ChannelEvent evt) throws Exception {
    if (!(evt instanceof MessageEvent)) {
        ctx.sendUpstream(evt);
        return;
    }
    final MessageEvent event = (MessageEvent) evt;
    Object message = event.getMessage();
    if (message instanceof Position) {
        final Position position = (Position) message;
        if (processInvalidPositions || position.getValid()) {
            if (geocoderReuseDistance != 0) {
                Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId());
                if (lastPosition != null && lastPosition.getAddress() != null && position.getDouble(Position.KEY_DISTANCE) <= geocoderReuseDistance) {
                    position.setAddress(lastPosition.getAddress());
                    Channels.fireMessageReceived(ctx, position, event.getRemoteAddress());
                    return;
                }
            }
            Context.getStatisticsManager().registerGeocoderRequest();
            geocoder.getAddress(position.getLatitude(), position.getLongitude(), new Geocoder.ReverseGeocoderCallback() {

                @Override
                public void onSuccess(String address) {
                    position.setAddress(address);
                    Channels.fireMessageReceived(ctx, position, event.getRemoteAddress());
                }

                @Override
                public void onFailure(Throwable e) {
                    Log.warning("Geocoding failed", e);
                    Channels.fireMessageReceived(ctx, position, event.getRemoteAddress());
                }
            });
        } else {
            Channels.fireMessageReceived(ctx, position, event.getRemoteAddress());
        }
    } else {
        Channels.fireMessageReceived(ctx, message, event.getRemoteAddress());
    }
}
Also used : Position(org.traccar.model.Position) MessageEvent(org.jboss.netty.channel.MessageEvent) Geocoder(org.traccar.geocoder.Geocoder)

Example 79 with Position

use of org.traccar.model.Position in project traccar by traccar.

the class PositionResource method getCsv.

@GET
@Produces(TEXT_CSV)
public Response getCsv(@QueryParam("deviceId") long deviceId, @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException {
    Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
    CsvBuilder csv = new CsvBuilder();
    csv.addHeaderLine(new Position());
    csv.addArray(Context.getDataManager().getPositions(deviceId, DateUtil.parseDate(from), DateUtil.parseDate(to)));
    return Response.ok(csv.build()).header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_CSV).build();
}
Also used : Position(org.traccar.model.Position) CsvBuilder(org.traccar.web.CsvBuilder) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 80 with Position

use of org.traccar.model.Position in project traccar by traccar.

the class AttributeResource method test.

@POST
@Path("test")
public Response test(@QueryParam("deviceId") long deviceId, Attribute entity) throws SQLException {
    Context.getPermissionsManager().checkReadonly(getUserId());
    Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
    Position last = Context.getIdentityManager().getLastPosition(deviceId);
    if (last != null) {
        Object result = new ComputedAttributesHandler().computeAttribute(entity, last);
        if (result != null) {
            switch(entity.getType()) {
                case "number":
                    Number numberValue = (Number) result;
                    return Response.ok(numberValue).build();
                case "boolean":
                    Boolean booleanValue = (Boolean) result;
                    return Response.ok(booleanValue).build();
                default:
                    return Response.ok(result.toString()).build();
            }
        } else {
            return Response.noContent().build();
        }
    } else {
        throw new IllegalArgumentException("Device has no last position");
    }
}
Also used : Position(org.traccar.model.Position) ComputedAttributesHandler(org.traccar.processing.ComputedAttributesHandler) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

Position (org.traccar.model.Position)831 DeviceSession (org.traccar.DeviceSession)505 Parser (org.traccar.helper.Parser)319 DateBuilder (org.traccar.helper.DateBuilder)179 Date (java.util.Date)154 Network (org.traccar.model.Network)143 LinkedList (java.util.LinkedList)112 ByteBuf (io.netty.buffer.ByteBuf)105 Event (org.traccar.model.Event)75 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)69 NetworkMessage (org.traccar.NetworkMessage)67 Test (org.junit.Test)56 WifiAccessPoint (org.traccar.model.WifiAccessPoint)54 BaseTest (org.traccar.BaseTest)40 SimpleDateFormat (java.text.SimpleDateFormat)38 Device (org.traccar.model.Device)29 DateFormat (java.text.DateFormat)27 DeviceState (org.traccar.model.DeviceState)27 List (java.util.List)25 TripsConfig (org.traccar.reports.model.TripsConfig)25