Skip to content

Commit

Permalink
Merge 3250c2a into 26f075b
Browse files Browse the repository at this point in the history
  • Loading branch information
barbeau committed May 12, 2016
2 parents 26f075b + 3250c2a commit 0881c07
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 23 deletions.
30 changes: 25 additions & 5 deletions src/main/java/org/onebusaway/alexa/AnonSpeechlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import org.onebusaway.alexa.lib.ObaUserClient;
import org.onebusaway.alexa.storage.ObaDao;
import org.onebusaway.alexa.storage.ObaUserDataItem;
import org.onebusaway.alexa.util.SpeechUtil;
import org.onebusaway.io.client.elements.ObaRegion;
import org.onebusaway.io.client.elements.ObaStop;
import org.onebusaway.io.client.request.ObaArrivalInfoResponse;
import org.onebusaway.io.client.util.RegionUtils;
import org.onebusaway.location.Location;

Expand All @@ -40,6 +42,7 @@

import static org.onebusaway.alexa.ObaIntent.*;
import static org.onebusaway.alexa.SessionAttribute.*;
import static org.onebusaway.alexa.lib.ObaUserClient.ARRIVALS_SCAN_MINS;

@Log4j
public class AnonSpeechlet implements Speechlet {
Expand Down Expand Up @@ -182,7 +185,7 @@ private SpeechletResponse reaskForStopNumber() {
return SpeechletResponse.newAskResponse(out, stopNumReprompt);
}

private SpeechletResponse setStopNumber(String spokenStopNumber, Session session) {
private SpeechletResponse setStopNumber(String spokenStopNumber, Session session) throws SpeechletException {
String cityName = (String) session.getAttribute(CITY_NAME);
String regionName = (String) session.getAttribute(REGION_NAME);
log.debug(String.format(
Expand Down Expand Up @@ -238,20 +241,37 @@ private SpeechletResponse setStopNumber(String spokenStopNumber, Session session
return SpeechletResponse.newTellResponse(out);
} else {
// Perfect!
return createOrUpdateUser(session, cityName, searchResults[0], region.get());
return createOrUpdateUser(session, cityName, searchResults[0], region.get(), obaUserClient);
}
}

private SpeechletResponse createOrUpdateUser(Session session,
String cityName,
ObaStop stop,
ObaRegion region) {
ObaRegion region,
ObaUserClient obaUserClient) throws SpeechletException {
log.debug(String.format(
"Crupdating user with city %s and stop ID %s, code %s, regionId %d, regionName %s, obaBaseUrl %s.",
cityName, stop.getId(), stop.getStopCode(), region.getId(), region.getName(), region.getObaBaseUrl()));

ObaArrivalInfoResponse response;
try {
response = obaUserClient.getArrivalsAndDeparturesForStop(
stop.getId(),
ARRIVALS_SCAN_MINS
);
} catch (IOException e) {
throw new SpeechletException(e);
}

String arrivalInfoText = SpeechUtil.getArrivalText(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. " +
"Great. I am ready to tell you about the next bus.",
stop.getStopCode(), region.getName());
"Great. I am ready to tell you about the next bus. You can always ask me for arrival times " +
"by saying 'Alexa, open One Bus Away'. Right now, %s",
stop.getStopCode(), region.getName(), arrivalInfoText);

Optional<ObaUserDataItem> optUserData = obaDao.getUserData(session);
if (optUserData.isPresent()) {
ObaUserDataItem userData = optUserData.get();
Expand Down
20 changes: 3 additions & 17 deletions src/main/java/org/onebusaway/alexa/AuthedSpeechlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,21 @@
import org.onebusaway.alexa.lib.ObaUserClient;
import org.onebusaway.alexa.storage.ObaDao;
import org.onebusaway.alexa.storage.ObaUserDataItem;
import org.onebusaway.io.client.elements.ObaArrivalInfo;
import org.onebusaway.alexa.util.SpeechUtil;
import org.onebusaway.io.client.request.ObaArrivalInfoResponse;
import org.onebusaway.io.client.request.ObaStopResponse;
import org.onebusaway.io.client.util.ArrivalInfo;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URISyntaxException;

import static org.onebusaway.alexa.ObaIntent.*;
import static org.onebusaway.alexa.SessionAttribute.*;
import static org.onebusaway.alexa.lib.ObaUserClient.ARRIVALS_SCAN_MINS;

@NoArgsConstructor
@Log4j
public class AuthedSpeechlet implements Speechlet {
public static final int ARRIVALS_SCAN_MINS = 35;

@Resource
private ObaDao obaDao;
Expand Down Expand Up @@ -170,22 +169,9 @@ private SpeechletResponse tellArrivals() throws SpeechletException {
} catch (IOException e) {
throw new SpeechletException(e);
}
ObaArrivalInfo[] arrivals = response.getArrivalInfo();

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

if (arrivals.length == 0) {
output = "There are no upcoming arrivals at your stop for the next "
+ ARRIVALS_SCAN_MINS + " minutes.";
} else {
StringBuilder sb = new StringBuilder();
for (ObaArrivalInfo obaArrival: arrivals) {
log.info("Arrival: " + obaArrival);
ArrivalInfo arrival = new ArrivalInfo(obaArrival, response.getCurrentTime());
sb.append(arrival.getLongDescription() + " -- "); //with pause between sentences
}
output = sb.toString();
}
log.info("Full text output: " + output);
saveOutputForRepeat(output);
PlainTextOutputSpeech out = new PlainTextOutputSpeech();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/onebusaway/alexa/lib/ObaUserClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*/
public class ObaUserClient extends ObaClientSharedCode {
private static final int DEFAULT_SEARCH_RADIUS_METERS = 40000;
public static final int ARRIVALS_SCAN_MINS = 35;

public ObaUserClient(@NonNull String obaBaseUrl) throws URISyntaxException {
log.debug("Instantiating ObaUserClient with obaBaseUrl " + obaBaseUrl);
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/org/onebusaway/alexa/util/SpeechUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2016 Sean J. Barbeau (sjbarbeau@gmail.com),
* Philip M. White (philip@mailworks.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onebusaway.alexa.util;

import lombok.extern.log4j.Log4j;
import org.onebusaway.io.client.elements.ObaArrivalInfo;
import org.onebusaway.io.client.util.ArrivalInfo;

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

/**
* Format the arrival info for speach
* @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) {
String output;

if (arrivals.length == 0) {
output = "There are no upcoming arrivals at your stop for the next "
+ arrivalScanMins + " minutes.";
} else {
StringBuilder sb = new StringBuilder();
for (ObaArrivalInfo obaArrival: arrivals) {
log.info("Arrival: " + obaArrival);
ArrivalInfo arrival = new ArrivalInfo(obaArrival, currentTime);
sb.append(arrival.getLongDescription() + " -- "); //with pause between sentences
}
output = sb.toString();
}
return output;
}
}
3 changes: 2 additions & 1 deletion src/test/java/org/onebusaway/alexa/AuthedSpeechletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import static org.onebusaway.alexa.ObaIntent.*;
import static org.onebusaway.alexa.SessionAttribute.CITY_NAME;
import static org.onebusaway.alexa.SessionAttribute.STOP_NUMBER;
import static org.onebusaway.alexa.lib.ObaUserClient.ARRIVALS_SCAN_MINS;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class,
Expand Down Expand Up @@ -348,7 +349,7 @@ public void noUpcomingArrivals() throws SpeechletException, IOException {
session);
String spoken = ((PlainTextOutputSpeech)sr.getOutputSpeech()).getText();
assertThat(spoken, equalTo("There are no upcoming arrivals at your stop for the next "
+ AuthedSpeechlet.ARRIVALS_SCAN_MINS + " minutes."));
+ ARRIVALS_SCAN_MINS + " minutes."));
}

@Test
Expand Down

0 comments on commit 0881c07

Please sign in to comment.