private async Task SendCompletionMessageForBookToCustomLocation(string location) { string slat, dlat, slng, dlng; GeoResponse locationResponse; string gettingCabsToDesiredLocation = "Getting Details of " + location; await ShowProgressScreen(gettingCabsToDesiredLocation); CabsAPI api = new CabsAPI(); string token = Windows.Storage.ApplicationData.Current.LocalSettings.Values["Token"].ToString(); string loadingCabsToDesiredLocation = "Loading Details of cabs to " + location; await ShowProgressScreen(loadingCabsToDesiredLocation); Geolocator locator = new Geolocator(); var userMessage1 = new VoiceCommandUserMessage(); locator.DesiredAccuracyInMeters = 50; var position = await locator.GetGeopositionAsync(); slat = position.Coordinate.Point.Position.Latitude.ToString(); slng = position.Coordinate.Point.Position.Longitude.ToString(); string latlng1 = slat + "," + slng; ReverseGeoResposne reverse = await api.GetReverseCodingResultlatlng(token, latlng1); locationResponse = await api.GeoCodingResult(token, location); dlat = locationResponse.Position.Latitude; dlng = locationResponse.Position.Longitude; PriceEstimateResponse Responseprice = await api.GetEstimate(token, slat, slng, dlat, dlng); List <CabEstimate> desiredList = Responseprice.Estimates; var sortedCabs = desiredList.OrderBy(o => o.CurrentEstimate.HighRange); var userMessage = new VoiceCommandUserMessage(); var cabTiles = new List <VoiceCommandContentTile>(); if (desiredList == null) { string foundNoCabs = "Sorry No Cabs Found"; userMessage.DisplayMessage = foundNoCabs; userMessage.SpokenMessage = foundNoCabs; } else { string message = "Ok! I have found the Best Cab for you"; userMessage.DisplayMessage = message; userMessage.SpokenMessage = message; var cabsNeeded = desiredList[0]; var cabShow = new VoiceCommandContentTile(); cabShow.ContentTileType = VoiceCommandContentTileType.TitleWith68x68IconAndText; if (cabsNeeded.Provider.Equals("UBER")) { cabShow.Image = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///ContosoCabs.VoiceCommandService/img/uber.png")); } else { cabShow.Image = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///ContosoCabs.VoiceCommandService/img/ola.png")); } cabShow.AppContext = cabsNeeded; cabShow.Title = cabsNeeded.Provider; cabShow.TextLine1 = cabsNeeded.Type; cabShow.TextLine2 = "ETA : " + cabsNeeded.Eta; cabShow.TextLine3 = "Estimated Fare: " + cabsNeeded.CurrentEstimate.LowRange + "-" + cabsNeeded.CurrentEstimate.HighRange; cabTiles.Add(cabShow); var userPrompt = new VoiceCommandUserMessage(); VoiceCommandResponse response1; string source; try { source = reverse.FormattedAddress.Substring(0, reverse.FormattedAddress.IndexOf(", Hyderabad")); } catch { source = reverse.FormattedAddress; } string BookCabToDestination = "Booking " + cabsNeeded.Provider + " with " + cabsNeeded.Type + " from " + source + " to " + location + " arriving in " + cabsNeeded.Eta + cabsNeeded.CurrentEstimate.LowRange + " to " + cabsNeeded.CurrentEstimate.HighRange + " cost estimation"; userPrompt.DisplayMessage = userPrompt.SpokenMessage = BookCabToDestination; var userReprompt = new VoiceCommandUserMessage(); string confirmBookCabToDestination = "confirm booking"; userReprompt.DisplayMessage = userReprompt.SpokenMessage = confirmBookCabToDestination; response1 = VoiceCommandResponse.CreateResponseForPrompt(userPrompt, userReprompt, cabTiles); var voiceCommandConfirmation = await voiceServiceConnection.RequestConfirmationAsync(response1); if (voiceCommandConfirmation != null) { if (voiceCommandConfirmation.Confirmed == true) { BookingDetailsResponse booking = await api.BookCab(token, slat, slng); string BookCabInformation = "Cab booked. your cab driver " + booking.BookingData.DriverDetails.Name + " will be arriving shortly."; userMessage1 = new VoiceCommandUserMessage(); //userPrompt.DisplayMessage = userPrompt.SpokenMessage = BookCabInformation; userMessage.DisplayMessage = userMessage.SpokenMessage = BookCabInformation; userMessage1.DisplayMessage = userMessage1.SpokenMessage = BookCabInformation; var response = VoiceCommandResponse.CreateResponse(userMessage1, cabTiles); await voiceServiceConnection.ReportSuccessAsync(response); } else { userMessage = new VoiceCommandUserMessage(); string BookingCabToDestination = "Cancelling cab request..."; await ShowProgressScreen(BookingCabToDestination); string keepingTripToDestination = "Cancelled."; userMessage.DisplayMessage = userMessage.SpokenMessage = keepingTripToDestination; response1 = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportSuccessAsync(response1); } } } }
public async void Run(IBackgroundTaskInstance taskInstance) { serviceDeferral = taskInstance.GetDeferral(); var userMessage1 = new VoiceCommandUserMessage(); string book; taskInstance.Canceled += OnTaskCanceled; var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; if (triggerDetails != null) { try { voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted; VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync(); switch (voiceCommand.CommandName) { case "bookFromXToY": try { string source = voiceCommand.SpeechRecognitionResult.SemanticInterpretation.Properties["bookFromX"].FirstOrDefault(); string destination = voiceCommand.SpeechRecognitionResult.SemanticInterpretation.Properties["bookFromY"].FirstOrDefault(); await SendCompletionMessageForBookFromXtoY(source, destination); } catch { userMessage1 = new VoiceCommandUserMessage(); book = "Please give proper saved places"; userMessage1.DisplayMessage = userMessage1.SpokenMessage = book; var response = VoiceCommandResponse.CreateResponse(userMessage1); await voiceServiceConnection.ReportSuccessAsync(response); } break; case "costEstimate": try { string sourceCost = voiceCommand.SpeechRecognitionResult.SemanticInterpretation.Properties["source"].First(); string destinationCost = voiceCommand.SpeechRecognitionResult.SemanticInterpretation.Properties["destination"].First(); await SendCompletionMessageForCostEstimate(sourceCost, destinationCost); } catch { userMessage1 = new VoiceCommandUserMessage(); book = "Please give proper source and destination"; userMessage1.DisplayMessage = userMessage1.SpokenMessage = book; var response = VoiceCommandResponse.CreateResponse(userMessage1); await voiceServiceConnection.ReportSuccessAsync(response); } break; case "costEstimateCustom": //try //{ string destinationCost1 = voiceCommand.SpeechRecognitionResult.SemanticInterpretation.Properties["destination"].First(); string slat, slng, token; Geolocator locator = new Geolocator(); locator.DesiredAccuracyInMeters = 50; var mPosition = await locator.GetGeopositionAsync(); slat = mPosition.Coordinate.Point.Position.Latitude.ToString(); slng = mPosition.Coordinate.Point.Position.Longitude.ToString(); token = Windows.Storage.ApplicationData.Current.LocalSettings.Values["Token"].ToString(); CabsAPI api = new CabsAPI(); ReverseGeoResposne current = await api.GetReverseCodingResultlatlng(token, slat + "," + slng); string source1; try { source1 = current.FormattedAddress.Substring(0, current.FormattedAddress.IndexOf(", Hyderabad")); } catch { source1 = current.FormattedAddress; } await SendCompletionMessageForCostEstimate(source1, destinationCost1); //catch (Exception e) //{ // userMessage1 = new VoiceCommandUserMessage(); // book = "please give proper Destination"; // userMessage1.DisplayMessage = userMessage1.SpokenMessage = book; // var response = VoiceCommandResponse.CreateResponse(userMessage1); // await voiceServiceConnection.ReportSuccessAsync(response); //} break; case "toCustomLocation": try { string location = voiceCommand.SpeechRecognitionResult.SemanticInterpretation.Properties["location"].FirstOrDefault(); await SendCompletionMessageForBookToCustomLocation(location); } catch (Exception e) { userMessage1 = new VoiceCommandUserMessage(); book = "Please give proper Destination"; userMessage1.DisplayMessage = userMessage1.SpokenMessage = book; var response = VoiceCommandResponse.CreateResponse(userMessage1); await voiceServiceConnection.ReportSuccessAsync(response); } break; case "bookcheapest": string mSource; IReadOnlyList <string> voicecommandphrase; if (voiceCommand.SpeechRecognitionResult.SemanticInterpretation.Properties.TryGetValue("source", out voicecommandphrase)) { mSource = voicecommandphrase.First(); } else { mSource = ""; } string mDest = voiceCommand.SpeechRecognitionResult.SemanticInterpretation.Properties["destination"].FirstOrDefault(); await SendCompletionMessageForBookCheapestFromXtoY(mSource, mDest); break; default: LaunchAppInForeground(); break; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Handling Voice Command failed " + ex.ToString()); } } }
protected override async void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); switch (requestCode) { case 100: if (resultCode == Result.Ok && data != null) { Intent mIntent = new Intent(this, typeof(ConfirmedCookingActivityViaSpeech)); IList <string> mResult = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults); System.Diagnostics.Debug.WriteLine(mResult[0]); string command = mResult[0]; Classifier mClassifier = new Classifier(command); ClassifiedResult mResp = mClassifier.Classify(); switch (mResp.Type) { case VoiceCommandType.BOOK_ME_CHEAPEST_CAB_FROM_X_TO_Y: string recs = mResp.GetData("source"); if (!recs.Equals("")) { mSourceAddress = recs; } mDestinationAddress = mResp.GetData("destination"); error = "Booking Cheapest cab from " + mSourceAddress + "to" + mDestinationAddress; speakUp(); mIntent.PutExtra("mSource", mSourceAddress); mIntent.PutExtra("mDestination", mDestinationAddress); StartActivity(mIntent); break; case VoiceCommandType.BOOK_TO_CUSTOM_LOCATION: mDestinationAddress = mResp.GetData("location"); mIntent.PutExtra("mSource", mSourceAddress); mIntent.PutExtra("mDestinationAddress", mDestinationAddress); StartActivity(mIntent); break; case VoiceCommandType.BOOK_ME_A_CAB_FROM_X_TO_Y: { CabsAPI api = new CabsAPI(); string source = mResp.GetData("source"); string destination = mResp.GetData("destination"); mLoadingDialog.Show(); PlacesResponse mResposneSource = await api.GetPlaceLocation(source, token); PlacesResponse mResponseDest = await api.GetPlaceLocation(destination, token); if (mResposneSource.Code == ResponseCode.SUCCESS && mResponseDest.Code == ResponseCode.SUCCESS) { string msource = mResposneSource.Location.Latitude + "," + mResposneSource.Location.Longitude; string mDest = mResponseDest.Location.Latitude + "," + mResponseDest.Location.Longitude; ReverseGeoResposne mResSrc = await api.GetReverseCodingResultlatlng(token, msource); ReverseGeoResposne mResDest = await api.GetReverseCodingResultlatlng(token, mDest); if (mResSrc.Code == ResponseCode.SUCCESS && mResDest.Code == ResponseCode.SUCCESS) { error = "Booking cab from " + source + "to" + destination; speakUp(); mLoadingDialog.Dismiss(); mSourceAddress = mResSrc.FormattedAddress; mDestinationAddress = mResDest.FormattedAddress; mIntent.PutExtra("mSource", mSourceAddress); mIntent.PutExtra("mDestination", mDestinationAddress); StartActivity(mIntent); } else { error = "Google maps error"; speakUp(); return; } } else { error = "These places are not there in your notebook"; speakUp(); return; } break; } case VoiceCommandType.INVALID_COMMAND: error = "Invalid Commmand Please try again"; speakUp(); break; } } break; } }
private async Task <Arguments> ToCustomLocation(VoiceCommandActivatedEventArgs voiceCommandArgs) { string slat, dlat, slng, dlng, desiredLocation, token; GeoResponse locationResponse; CabsAPI api = new CabsAPI(); var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; token = localSettings.Values["Token"].ToString(); IReadOnlyList <string> voiceCommandPhrases; if (voiceCommandArgs.Result.SemanticInterpretation.Properties.TryGetValue("location", out voiceCommandPhrases)) { desiredLocation = voiceCommandPhrases.First(); } else { desiredLocation = ""; } Geolocator locator = new Geolocator(); locator.DesiredAccuracyInMeters = 50; var position = await locator.GetGeopositionAsync(); slat = position.Coordinate.Point.Position.Latitude.ToString(); slng = position.Coordinate.Point.Position.Longitude.ToString(); string source; ReverseGeoResposne current = await api.GetReverseCodingResultlatlng(token, position.Coordinate.Point.Position.Latitude.ToString() + "," + position.Coordinate.Point.Position.Longitude.ToString()); try { source = current.FormattedAddress.Substring(0, current.FormattedAddress.IndexOf(", Hyderabad")); } catch { source = current.FormattedAddress; } ReadSpeech(new MediaElement(), source); ReadSpeech(new MediaElement(), desiredLocation); locationResponse = await api.GeoCodingResult(token, desiredLocation); if (locationResponse.Code == ResponseCode.SUCCESS) { dlat = locationResponse.Position.Latitude; dlng = locationResponse.Position.Longitude; PriceEstimateResponse mResponseprice = await api.GetEstimate(token, slat, slng, dlat, dlng); if (mResponseprice.Code == ResponseCode.SUCCESS) { List <CabEstimate> desiredList = mResponseprice.Estimates; var sortedCabs = desiredList.OrderBy(o => o.CurrentEstimate.HighRange); Dictionary <string, object> Parameters = new Dictionary <string, object>(); Parameters.Add("location", desiredLocation); Parameters.Add("source", source); Parameters.Add("list", desiredList); Arguments data = new Arguments(null); data.AddType(VoiceCommandType.IF_FROM_APP, true); data.AddType(VoiceCommandType.BOOK_TO_CUSTOM_LOCATION, true); data.Values = Parameters; return(data); } else { await new MessageDialog("Errors fetching estimates").ShowAsync(); return(null); } } else { await new MessageDialog("Errors fetching cordinates").ShowAsync(); return(null); } }
private async Task <Arguments> ProcessBookCheapest(VoiceCommandActivatedEventArgs voiceCommandArgs) { string slat, dlat, slng, dlng, mSource, mDestination, token; GeoResponse mResponse; CabsAPI api = new CabsAPI(); var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; token = localSettings.Values["Token"].ToString(); IReadOnlyList <string> voiceCommandPhrases; if (voiceCommandArgs.Result.SemanticInterpretation.Properties.TryGetValue("source", out voiceCommandPhrases)) { mSource = voiceCommandPhrases.First(); } else { mSource = ""; } if (voiceCommandArgs.Result.SemanticInterpretation.Properties.TryGetValue("destination", out voiceCommandPhrases)) { mDestination = voiceCommandPhrases.First(); } else { mDestination = ""; } if (mSource == "") { Geolocator locator = new Geolocator(); locator.DesiredAccuracyInMeters = 50; var position = await locator.GetGeopositionAsync(); slat = position.Coordinate.Point.Position.Latitude.ToString(); slng = position.Coordinate.Point.Position.Longitude.ToString(); ReverseGeoResposne mRevResponse = await api.GetReverseCodingResultlatlng(token, position.Coordinate.Point.Position.Latitude.ToString() + "," + position.Coordinate.Point.Position.Longitude.ToString()); //mSource = mRevResponse.FormattedAddress; try { mSource = mRevResponse.FormattedAddress.Substring(0, mRevResponse.FormattedAddress.IndexOf(", Hyderabad")); } catch { mSource = mRevResponse.FormattedAddress; } System.Diagnostics.Debug.WriteLine("source is" + mSource); } else { mResponse = await api.GeoCodingResult(token, mSource); if (mResponse.Code == ResponseCode.SUCCESS) { slat = mResponse.Position.Latitude; slng = mResponse.Position.Longitude; } else { await new MessageDialog("Errors fetching source cordinates").ShowAsync(); return(null); } } if (mDestination == "") { await new MessageDialog("No Destination provided").ShowAsync(); return(null); } else { mResponse = await api.GeoCodingResult(token, mDestination); if (mResponse.Code == ResponseCode.SUCCESS) { dlat = mResponse.Position.Latitude; dlng = mResponse.Position.Longitude; PriceEstimateResponse mResponseprice = await api.GetEstimate(token, slat, slng, dlat, dlng); if (mResponseprice.Code == ResponseCode.SUCCESS) { List <CabEstimate> mList = mResponseprice.Estimates; //var sortedCabs = mList.OrderBy(o => o.AverageEstimate); Dictionary <string, object> Parameters = new Dictionary <string, object>(); Parameters.Add("source", mSource); Parameters.Add("destination", mDestination); Parameters.Add("list", mList); Arguments data = new Arguments(null); data.AddType(VoiceCommandType.IF_FROM_APP, true); data.AddType(VoiceCommandType.BOOK_CHEAPEST_TO_DEST, true); data.AddType(VoiceCommandType.BOOK_ME_CHEAPEST_CAB_FROM_X_TO_Y, true); data.Values = Parameters; return(data); } else { await new MessageDialog("Error fetching estimaates").ShowAsync(); return(null); } } else { await new MessageDialog("Error retrieving Destination cordinates").ShowAsync(); return(null); } } }
protected async override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); if (!IsInternet()) { await new MessageDialog("Seems you are not connected to the Internet").ShowAsync(); return; } else { progress.IsActive = true; speechRecognition = e.Parameter as SpeechRecognitionResult; if (speechRecognition != null) { mSource = this.SemanticInterpretation("source", speechRecognition); mDestination = this.SemanticInterpretation("destination", speechRecognition); if (mSource != null) { SearchSourceBox.Text = mSource; } if (mDestination != null) { SearchDestinationBox.Text = mDestination; } } try { var parameter = e.Parameter as WwwFormUrlDecoder; _destination = parameter.GetFirstValueByName("nmlocation"); if (_destination == null || _destination.Equals("")) { _destination = "Gachibowli"; } System.Diagnostics.Debug.WriteLine(_destination); _isFromInsight = true; } catch (Exception) { } finally { MyMap.MapServiceToken = "YP9WTSzOHUo0dbahsH8J~J-K6u01dT98SF4uCCKpiwA~AnGaYM6yJxoLF1tGHEIXHFskGfwSRJTr1S5aO1dB-TCXjQ1ZX0xEWgeYslbC3Fov"; Geolocator locator = new Geolocator(); locator.DesiredAccuracyInMeters = 50; position = await locator.GetGeopositionAsync(); await MyMap.TrySetViewAsync(position.Coordinate.Point, 17D); progress.IsActive = false; mySlider.Value = MyMap.ZoomLevel; AddMapIcon(position.Coordinate.Point.Position.Latitude, position.Coordinate.Point.Position.Longitude); CabsAPI api = new CabsAPI(); latlng = position.Coordinate.Point.Position.Latitude.ToString() + "," + position.Coordinate.Point.Position.Longitude.ToString(); ReverseGeoResposne res = await api.GetReverseCodingResultlatlng(Token, latlng); if (res.Code == ResponseCode.SUCCESS) { CurrentSource = res.FormattedAddress; SearchSourceBox.Text = CurrentSource; _source = CurrentSource; string lat = position.Coordinate.Point.Position.Latitude.ToString(); string lng = position.Coordinate.Point.Position.Longitude.ToString(); _cabsView = new CabsListViewModel(lng, lat, Token); latlng = position.Coordinate.Point.Position.Latitude.ToString() + "," + position.Coordinate.Point.Position.Longitude.ToString(); if (_isFromInsight) { SearchDestinationBox.Text = _destination; GeoResponse location; var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; token = localSettings.Values["Token"].ToString(); location = await api.GeoCodingResult(token, _destination); if (location.Code == ResponseCode.SUCCESS) { dlat = location.Position.Latitude; dlng = location.Position.Longitude; AddMapIcon(double.Parse(dlat), double.Parse(dlng)); _cabsView.SetLatLng(dlat, dlng); ShowLoader(true); await _cabsView.RefreshView(CabsListViewModel.REFRESH_ESTIMATE); CabsListView.ItemsSource = _cabsView.Cabs; ShowLoader(false); } else { await new MessageDialog("Error fetching coordinates").ShowAsync(); } } else { ShowLoader(true); await _cabsView.RefreshView(CabsListViewModel.REFRESH_SURGE); CabsListView.ItemsSource = _cabsView.Cabs; ShowLoader(false); } } else { await new MessageDialog("No such location exists").ShowAsync(); } } } }