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;
}
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());
}
}
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());
}
}
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();
}
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");
}
}
Aggregations