diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-06 11:17:18 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-06 11:17:18 +1300 |
commit | c8ad5cc5c45ab47abe35e3ad2e4f607d520e627d (patch) | |
tree | a86dd0c94803608b6f72f6a23ecb084d3ec68728 /src/org/traccar/ReverseGeocoderHandler.java | |
parent | 923a49d6bc33f94bf39b8be13c77b1dad4d1dda0 (diff) | |
download | trackermap-server-c8ad5cc5c45ab47abe35e3ad2e4f607d520e627d.tar.gz trackermap-server-c8ad5cc5c45ab47abe35e3ad2e4f607d520e627d.tar.bz2 trackermap-server-c8ad5cc5c45ab47abe35e3ad2e4f607d520e627d.zip |
Implement async reverse geocoding
Diffstat (limited to 'src/org/traccar/ReverseGeocoderHandler.java')
-rw-r--r-- | src/org/traccar/ReverseGeocoderHandler.java | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/src/org/traccar/ReverseGeocoderHandler.java b/src/org/traccar/ReverseGeocoderHandler.java index bb4f5cedd..298f59736 100644 --- a/src/org/traccar/ReverseGeocoderHandler.java +++ b/src/org/traccar/ReverseGeocoderHandler.java @@ -15,29 +15,51 @@ */ package org.traccar; +import org.jboss.netty.channel.ChannelEvent; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.ChannelUpstreamHandler; +import org.jboss.netty.channel.Channels; +import org.jboss.netty.channel.MessageEvent; import org.traccar.geocode.AddressFormat; import org.traccar.geocode.ReverseGeocoder; import org.traccar.model.Position; -public class ReverseGeocoderHandler extends BaseDataHandler { +public class ReverseGeocoderHandler implements ChannelUpstreamHandler { private final ReverseGeocoder geocoder; private final boolean processInvalidPositions; private final AddressFormat addressFormat; - public ReverseGeocoderHandler(ReverseGeocoder geocoder, boolean processInvalidPositions ) { + public ReverseGeocoderHandler(ReverseGeocoder geocoder, boolean processInvalidPositions) { this.geocoder = geocoder; this.processInvalidPositions = processInvalidPositions; addressFormat = new AddressFormat(); } @Override - protected Position handlePosition(Position position) { - if (geocoder != null && (processInvalidPositions || position.getValid())) { - position.setAddress(geocoder.getAddress( - addressFormat, position.getLatitude(), position.getLongitude())); + public void handleUpstream(final ChannelHandlerContext ctx, ChannelEvent evt) throws Exception { + if (!(evt instanceof MessageEvent)) { + ctx.sendUpstream(evt); + return; + } + + final MessageEvent e = (MessageEvent) evt; + Object message = e.getMessage(); + if (message instanceof Position) { + final Position position = (Position) message; + if (geocoder != null && (processInvalidPositions || position.getValid())) { + geocoder.getAddress(addressFormat, position.getLatitude(), position.getLongitude(), + new ReverseGeocoder.ReverseGeocoderCallback() { + @Override + public void onResult(String address) { + position.setAddress(address); + Channels.fireMessageReceived(ctx, position, e.getRemoteAddress()); + } + }); + } + } else { + Channels.fireMessageReceived(ctx, message, e.getRemoteAddress()); } - return position; } } |