Skip to content
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

Filter out arrivals / departures with negative ETA #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/onebusaway/alexa/AnonSpeechlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ private SpeechletResponse createOrUpdateUser(Session session, String cityName, S
throw new SpeechletException(e);
}

String arrivalInfoText = SpeechUtil.getArrivalText(response.getArrivalInfo(), ARRIVALS_SCAN_MINS, response.getCurrentTime());
String arrivalInfoText = SpeechUtil.prepareArrivalText(response.getArrivalInfo(), ARRIVALS_SCAN_MINS, response.getCurrentTime());

log.info("Full arrival text output: " + arrivalInfoText);
String outText = String.format("Ok, your stop number is %s in the %s region. " +
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/onebusaway/alexa/AuthedSpeechlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private SpeechletResponse tellArrivals() throws SpeechletException {
throw new SpeechletException(e);
}

String output = SpeechUtil.getArrivalText(response.getArrivalInfo(), ARRIVALS_SCAN_MINS, response.getCurrentTime());
String output = SpeechUtil.prepareArrivalText(response.getArrivalInfo(), ARRIVALS_SCAN_MINS, response.getCurrentTime());

log.info("Full text output: " + output);
saveOutputForRepeat(output);
Expand Down
30 changes: 21 additions & 9 deletions src/main/java/org/onebusaway/alexa/util/SpeechUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,46 @@
import org.onebusaway.io.client.elements.ObaArrivalInfo;
import org.onebusaway.io.client.util.ArrivalInfo;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* Utilities for speech-related actions
*/
@Log4j
public class SpeechUtil {

/**
* Format the arrival info for speach
* Filter out arrivals and departures which already happened. Format arrival info for speech.
*
* @param arrivals arrival information
* @param arrivalScanMins number of minutes ahead that the arrival information was requested for
* @param currentTime the time when this arrival information was generated
* @return the arrival info text formatted for speech
*/
public static String getArrivalText(ObaArrivalInfo[] arrivals, int arrivalScanMins, long currentTime) {
public static String prepareArrivalText(ObaArrivalInfo[] arrivals, int arrivalScanMins, long currentTime) {
String output;

if (arrivals.length == 0) {
output = "There are no upcoming arrivals at your stop for the next "
+ arrivalScanMins + " minutes.";
List<ArrivalInfo> arrivalInfos = Arrays.stream(arrivals)
.map(arrival -> {
log.info("Arrival: " + arrival);
return new ArrivalInfo(arrival, currentTime, false); //convert
})
.filter(arrivalInfo -> arrivalInfo.getEta() >= 0L) //leave out arrivals with negative ETA (already arrived/departed), no need to announce them
.collect(Collectors.toList());

if (arrivalInfos.isEmpty()) {
output = String.format("There are no upcoming arrivals at your stop for the next %d minutes.", arrivalScanMins);
} else {
StringBuilder sb = new StringBuilder();
for (ObaArrivalInfo obaArrival: arrivals) {
log.info("Arrival: " + obaArrival);
ArrivalInfo arrival = new ArrivalInfo(obaArrival, currentTime, false);
sb.append(arrival.getLongDescription() + " -- "); //with pause between sentences
for (ArrivalInfo arrivalInfo: arrivalInfos) {
sb.append(arrivalInfo.getLongDescription() + " -- "); //with pause between sentences
}
output = sb.toString();
}

return output;
}

}
29 changes: 29 additions & 0 deletions src/test/java/org/onebusaway/alexa/AuthedSpeechletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,35 @@ public void setStopWithDuplicateIds() throws SpeechletException, IOException {
assertThat(spoken, startsWith("We found 2 stops associated with the stop number."));
}

/**
* Test that arrivals with negative ETA (already arrived) are not announced.
* */
@Test
public void excludeArrivalsAlreadyArrived() throws SpeechletException, IOException {
long now = System.currentTimeMillis();

//initialize arrivals with one backdated arrival
ObaArrivalInfo[] obaArrivalInfoArray = new ObaArrivalInfo[1];
obaArrivalInfoArray[0] = obaArrivalInfo;

new Expectations() {{
obaArrivalInfo.getStopSequence(); result = 1;
obaArrivalInfo.getScheduledArrivalTime(); result = now - 100500; //set scheduled arrival time in the past
obaArrivalInfoResponse.getCurrentTime(); result = now;
obaArrivalInfoResponse.getArrivalInfo(); result = obaArrivalInfoArray;
obaUserClient.getArrivalsAndDeparturesForStop(anyString, anyInt); result = obaArrivalInfoResponse;
}};

SpeechletResponse sr = authedSpeechlet.onLaunch(
launchRequest,
session);

String spoken = ((PlainTextOutputSpeech)sr.getOutputSpeech()).getText();

//verify it's been filtered out
assertThat(spoken, startsWith("There are no upcoming arrivals at your stop for the next"));
}

@Test
public void goodbye() throws SpeechletException, IOException {
TestUtil.assertGoodbye(authedSpeechlet, session);
Expand Down