-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a MQTT GTFS-RT trip update updater #2516
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
src/main/java/org/opentripplanner/updater/stoptime/MqttGtfsRealtimeUpdater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package org.opentripplanner.updater.stoptime; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import com.google.transit.realtime.GtfsRealtime; | ||
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; | ||
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended; | ||
import org.eclipse.paho.client.mqttv3.MqttClient; | ||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; | ||
import org.eclipse.paho.client.mqttv3.MqttException; | ||
import org.eclipse.paho.client.mqttv3.MqttMessage; | ||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; | ||
import org.opentripplanner.routing.graph.Graph; | ||
import org.opentripplanner.updater.GraphUpdater; | ||
import org.opentripplanner.updater.GraphUpdaterManager; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* This class starts an Paho MQTT client which opens a connection to a GTFS-RT data source. | ||
* A callback is registered which handles incoming GTFS-RT messages as they stream in by placing a | ||
* GTFS-RT decoder Runnable task in the single-threaded executor for handling. | ||
* | ||
* Usage example ('bessersmith' name is an example) in the file 'Graph.properties': | ||
* | ||
* <pre> | ||
* bessersmith.type = mqtt-gtfs-rt-updater | ||
* bessersmith.feedId = hsl | ||
* bessersmith.url = ssl://mqtt.hsl.fi:443 | ||
* bessersmith.topic = "gtfs/trip-updates/#" | ||
* </pre> | ||
* | ||
*/ | ||
public class MqttGtfsRealtimeUpdater implements GraphUpdater { | ||
private static Logger LOG = LoggerFactory.getLogger(MqttGtfsRealtimeUpdater.class); | ||
|
||
private GraphUpdaterManager updaterManager; | ||
|
||
private String url; | ||
|
||
private String topic; | ||
|
||
private String feedId; | ||
|
||
private int qos; | ||
|
||
private String clientId = "OpenTripPlanner-" + MqttClient.generateClientId(); | ||
|
||
MemoryPersistence persistence = new MemoryPersistence(); | ||
|
||
private MqttClient client; | ||
|
||
@Override public void configure(Graph graph, JsonNode config) throws Exception { | ||
url = config.path("url").asText(); | ||
topic = config.path("topic").asText(); | ||
feedId = config.path("feedId").asText(""); | ||
qos = config.path("qos").asInt(0); | ||
} | ||
|
||
@Override public void setGraphUpdaterManager(GraphUpdaterManager updaterManager) { | ||
this.updaterManager = updaterManager; | ||
} | ||
|
||
@Override public void setup(Graph graph) throws Exception { | ||
// Only create a realtime data snapshot source if none exists already | ||
if (graph.timetableSnapshotSource == null) { | ||
TimetableSnapshotSource snapshotSource = new TimetableSnapshotSource(graph); | ||
// Add snapshot source to graph | ||
graph.timetableSnapshotSource = (snapshotSource); | ||
} | ||
} | ||
|
||
@Override public void run() throws Exception { | ||
client = new MqttClient(url, clientId, persistence); | ||
MqttConnectOptions connOpts = new MqttConnectOptions(); | ||
connOpts.setCleanSession(true); | ||
connOpts.setAutomaticReconnect(true); | ||
client.setCallback(new MqttCallbackExtended() { | ||
@Override public void connectComplete(boolean reconnect, String serverURI) { | ||
try { | ||
LOG.debug("Connected"); | ||
client.subscribe(topic, qos); | ||
} catch (MqttException e) { | ||
LOG.warn("Could not subscribe to: " + topic); | ||
} | ||
} | ||
|
||
@Override public void connectionLost(Throwable cause) { | ||
LOG.debug("Disconnected"); | ||
} | ||
|
||
@Override public void messageArrived(String topic, MqttMessage message) throws Exception { | ||
GtfsRealtime.FeedMessage feedMessage; | ||
List<GtfsRealtime.FeedEntity> feedEntityList; | ||
List<GtfsRealtime.TripUpdate> updates = null; | ||
boolean fullDataset = true; | ||
try { | ||
// Decode message | ||
feedMessage = GtfsRealtime.FeedMessage.PARSER.parseFrom(message.getPayload()); | ||
feedEntityList = feedMessage.getEntityList(); | ||
|
||
// Change fullDataset value if this is an incremental update | ||
if (feedMessage.hasHeader() | ||
&& feedMessage.getHeader().hasIncrementality() | ||
&& feedMessage.getHeader().getIncrementality() | ||
.equals(GtfsRealtime.FeedHeader.Incrementality.DIFFERENTIAL)) { | ||
fullDataset = false; | ||
} | ||
|
||
// Create List of TripUpdates | ||
updates = new ArrayList<>(feedEntityList.size()); | ||
for (GtfsRealtime.FeedEntity feedEntity : feedEntityList) { | ||
if (feedEntity.hasTripUpdate()) { | ||
updates.add(feedEntity.getTripUpdate()); | ||
} | ||
} | ||
} catch (InvalidProtocolBufferException e) { | ||
LOG.error("Could not decode gtfs-rt message:", e); | ||
} | ||
|
||
if (updates != null) { | ||
// Handle trip updates via graph writer runnable | ||
TripUpdateGraphWriterRunnable runnable = new TripUpdateGraphWriterRunnable( | ||
fullDataset, updates, feedId); | ||
updaterManager.execute(runnable); | ||
} | ||
} | ||
|
||
@Override public void deliveryComplete(IMqttDeliveryToken token) { | ||
|
||
} | ||
}); | ||
|
||
LOG.debug("Connecting to broker: " + url); | ||
client.connect(connOpts); | ||
} | ||
|
||
@Override public void teardown() { | ||
try { | ||
client.disconnect(); | ||
} catch (MqttException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hannesj pointed out that the URL is currently different. However, this old URL given here should be production ready soon again. If I'm not mistaken, the new port for MQTT will be 8883 (as recently updated in the documentation here)