-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Fixed merge conflicts and moved leg trans notify test into o…
…wn class):
- Loading branch information
Showing
23 changed files
with
832 additions
and
136 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
- id: 1001:SWSE | ||
start: | ||
lat: 33.95684 | ||
lon: -83.97971 | ||
end: | ||
lat: 33.95653 | ||
lon: -83.97973 | ||
trigger: org.opentripplanner.middleware.triptracker.interactions.UsGdotGwinnettTrafficSignalNotifier |
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
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
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
7 changes: 7 additions & 0 deletions
7
src/main/java/org/opentripplanner/middleware/triptracker/interactions/Interaction.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,7 @@ | ||
package org.opentripplanner.middleware.triptracker.interactions; | ||
|
||
import org.opentripplanner.middleware.models.OtpUser; | ||
|
||
public interface Interaction { | ||
void triggerAction(SegmentAction segmentAction, OtpUser otpUser) throws Exception; | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/org/opentripplanner/middleware/triptracker/interactions/SegmentAction.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,30 @@ | ||
package org.opentripplanner.middleware.triptracker.interactions; | ||
|
||
import org.opentripplanner.middleware.triptracker.Segment; | ||
import org.opentripplanner.middleware.utils.Coordinates; | ||
|
||
/** Associates a segment (a pair of coordinates, optionally oriented) to an action or handler. */ | ||
public class SegmentAction { | ||
/** Identifier string for this object. */ | ||
public String id; | ||
|
||
/** The starting coordinated of the segment to which the trigger should be applied. */ | ||
public Coordinates start; | ||
|
||
/** The starting coordinated of the segment to which the trigger should be applied. */ | ||
public Coordinates end; | ||
|
||
/** The fully-qualified Java class to execute. */ | ||
public String trigger; | ||
|
||
public SegmentAction() { | ||
// For persistence. | ||
} | ||
|
||
public SegmentAction(String id, Segment segment, String trigger) { | ||
this.id = id; | ||
this.start = segment.start; | ||
this.end = segment.end; | ||
this.trigger = trigger; | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
src/main/java/org/opentripplanner/middleware/triptracker/interactions/TripActions.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,99 @@ | ||
package org.opentripplanner.middleware.triptracker.interactions; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import org.opentripplanner.middleware.models.OtpUser; | ||
import org.opentripplanner.middleware.otp.response.Step; | ||
import org.opentripplanner.middleware.triptracker.Segment; | ||
import org.opentripplanner.middleware.utils.Coordinates; | ||
import org.opentripplanner.middleware.utils.GeometryUtils; | ||
import org.opentripplanner.middleware.utils.JsonUtils; | ||
import org.opentripplanner.middleware.utils.YamlUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
/** Holds configured trip actions. */ | ||
public class TripActions { | ||
private static final Logger LOG = LoggerFactory.getLogger(TripActions.class); | ||
|
||
public static final String TRIP_ACTIONS_YML = "configurations/default/trip-actions.yml"; | ||
|
||
private static TripActions defaultInstance; | ||
|
||
private final List<SegmentAction> segmentActions; | ||
|
||
public static TripActions getDefault() { | ||
if (defaultInstance == null) { | ||
try (InputStream stream = new FileInputStream(TRIP_ACTIONS_YML)) { | ||
JsonNode tripActionsYml = YamlUtils.yamlMapper.readTree(stream); | ||
defaultInstance = new TripActions(JsonUtils.getPOJOFromJSONAsList(tripActionsYml, SegmentAction.class)); | ||
} catch (IOException e) { | ||
LOG.error("Error parsing trip-actions.yml", e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
return defaultInstance; | ||
} | ||
|
||
public TripActions(List<SegmentAction> segmentActions) { | ||
this.segmentActions = segmentActions; | ||
} | ||
|
||
/** | ||
* @param segment The {@link Segment} to test | ||
* @return The first {@link SegmentAction} found for the given segment | ||
*/ | ||
public SegmentAction getSegmentAction(Segment segment) { | ||
for (SegmentAction a : segmentActions) { | ||
if (segmentMatchesAction(segment, a)) { | ||
return a; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public static boolean segmentMatchesAction(Segment segment, SegmentAction action) { | ||
final int MAX_RADIUS = 10; // meters // TODO: get this from config. | ||
return (GeometryUtils.getDistance(segment.start, action.start) <= MAX_RADIUS && GeometryUtils.getDistance(segment.end, action.end) <= MAX_RADIUS) | ||
|| | ||
(GeometryUtils.getDistance(segment.start, action.end) <= MAX_RADIUS && GeometryUtils.getDistance(segment.end, action.start) <= MAX_RADIUS); | ||
} | ||
|
||
public void handleSegmentAction(Segment segment, OtpUser otpUser) { | ||
LOG.info("Looking for segment action: start: {}, end: {}", segment.start, segment.end); | ||
SegmentAction action = getSegmentAction(segment); | ||
if (action != null) { | ||
LOG.info("Found segment action: {}", action.id); | ||
Interaction interaction = null; | ||
try { | ||
Class<?> interactionClass = Class.forName(action.trigger); | ||
interaction = (Interaction) interactionClass.getDeclaredConstructor().newInstance(); | ||
interaction.triggerAction(action, otpUser); | ||
} catch (Exception e) { | ||
if (interaction == null) { | ||
LOG.error("Error instantiating class {}", action.trigger, e); | ||
} else { | ||
LOG.error("Could not trigger class {} on action {}", action.trigger, action.id, e); | ||
} | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
|
||
public void handleSegmentAction(Step step, List<Step> steps, OtpUser user) { | ||
int stepIndex = steps.indexOf(step); | ||
LOG.info("Looking for segment actions: step {}/{}", stepIndex, steps.size()); | ||
if (stepIndex >= 0 && stepIndex < steps.size() - 1) { | ||
Step stepAfter = steps.get(stepIndex + 1); | ||
Segment segment = new Segment( | ||
new Coordinates(step), | ||
new Coordinates(stepAfter) | ||
); | ||
handleSegmentAction(segment, user); | ||
} | ||
} | ||
} |
Oops, something went wrong.