/// <summary> /// Helper method for initalizing the voice service, bridge, and lights. Returns if successful. /// </summary> private async Task <bool> InitializeAsync(AppServiceTriggerDetails triggerDetails) { _voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); _voiceServiceConnection.VoiceCommandCompleted += (s, e) => _deferral.Complete(); _voiceCommand = await _voiceServiceConnection.GetVoiceCommandAsync(); _colors = HsbColor.CreateAll().ToDictionary(x => x.Name); var localStorage = ApplicationData.Current.LocalSettings.Values; _bridge = new Bridge( localStorage["bridgeIp"].ToString(), localStorage["userId"].ToString()); try { _lights = await _bridge.GetLightsAsync(); } catch (Exception) { var response = CreateCortanaResponse("Sorry, I couldn't connect to your bridge."); await _voiceServiceConnection.ReportFailureAsync(response); return(false); } if (!_lights.Any()) { var response = CreateCortanaResponse("Sorry, I couldn't find any lights."); await _voiceServiceConnection.ReportFailureAsync(response); return(false); } return(true); }
private async Task HandleLightsOnOrOff(string lightState) { await ShowProgressScreen("Hold on"); VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage(); string defaultMessage = $"Turning your lights {lightState}"; JObject on = new JObject(); try { SetOnOrOff(lightState, on); httpClient.PutAsync($"{baseUrl}/groups/0/action", new StringContent(on.ToString())); userMessage.DisplayMessage = defaultMessage; userMessage.SpokenMessage = defaultMessage; } catch (Exception ex) { SetError(userMessage); VoiceCommandResponse errResponse = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportFailureAsync(errResponse); } VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportSuccessAsync(response); }
private async Task ReportFailureToGetCurrentLocation() { var userMessage = new VoiceCommandUserMessage(); userMessage.DisplayMessage = userMessage.SpokenMessage = "Sorry, I can't access your location at the moment."; var response = VoiceCommandResponse.CreateResponse(userMessage); response.AppLaunchArgument = "LaunchApp"; await _voiceServiceConnection.ReportFailureAsync(response); }
private async Task ReportNoSensorsFoundAsync(VoiceCommandServiceConnection connection) { VoiceCommandResponse failureNoSensors = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = "No sensors found", SpokenMessage = "Sorry I cannot see any sensors connected" }); await connection.ReportFailureAsync(failureNoSensors); }
private async Task CompleteMessageError(string message) { // Provide a completion message to the user. var userMessage = new VoiceCommandUserMessage(); userMessage.DisplayMessage = userMessage.SpokenMessage = $"Error message: {message}"; var response = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportFailureAsync(response); }
internal static async Task ReportFailureAsync(VoiceCommandServiceConnection voiceServiceConnection, string message) { var userMessage = new VoiceCommandUserMessage(); string noSuchTripToDestination = message; userMessage.DisplayMessage = userMessage.SpokenMessage = noSuchTripToDestination; var response = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportFailureAsync(response); return; }
private async Task ToggleSwitch(string pref, string switchname, string state) { HttpClient client = new HttpClient(); var newvalue = state == "ein" || state == "on" ? "true" : "false"; var uri = new Uri(String.Format("http://homematic-ccu2/config/xmlapi/statechange.cgi?ise_id=1643&new_value={0}", newvalue)); bool success = true; try { var result = await client.GetAsync(uri); if (result.IsSuccessStatusCode) { // First, create the VoiceCommandUserMessage with the strings // that Cortana will show and speak. var userMessage = new VoiceCommandUserMessage(); userMessage.DisplayMessage = String.Format("Ich habe {0} {1} {2} geschaltet", pref, switchname, state); userMessage.SpokenMessage = String.Format("Ich habe {0} {1} {2} geschaltet", pref, switchname, state); var response = VoiceCommandResponse.CreateResponse( userMessage); await voiceServiceConnection.ReportSuccessAsync(response); } else { success = false; } } catch (Exception ex) { Debug.WriteLine(ex.Message); success = false; } if (!success) { var userMessage = new VoiceCommandUserMessage(); userMessage.DisplayMessage = String.Format("Ich konnte {0} {1} nicht {2} schalten", pref, switchname, state); userMessage.SpokenMessage = String.Format("Ich konnte {0} {1} nicht {2} schalten", pref, switchname, state); var response = VoiceCommandResponse.CreateResponse( userMessage); await voiceServiceConnection.ReportFailureAsync(response); } }
private async void ShowHaveOtherBusConfirm() { List <VoiceCommandContentTile> vcTiles = new List <VoiceCommandContentTile>(); vcTiles.Add(new VoiceCommandContentTile { Title = "目前已经没有公车了,是否继续查询?", ContentTileType = VoiceCommandContentTileType.TitleWithText, Image = await StorageFile.GetFileFromApplicationUriAsync( new Uri("ms-appx:///BusSearchVCService/Images/GreyTile.png")) }); // Create a VoiceCommandUserMessage for the initial question. VoiceCommandUserMessage userMsg = new VoiceCommandUserMessage(); userMsg.DisplayMessage = userMsg.SpokenMessage = "请选择"; // Create a VoiceCommandUserMessage for the second question, // in case Cortana needs to reprompt. VoiceCommandUserMessage repeatMsg = new VoiceCommandUserMessage(); repeatMsg.DisplayMessage = repeatMsg.SpokenMessage = "请重新选择"; VoiceCommandResponse response = VoiceCommandResponse.CreateResponseForPrompt(userMsg, repeatMsg, vcTiles); var vcConfirm = await vcConnection.RequestConfirmationAsync(response); if (vcConfirm != null) { if (vcConfirm.Confirmed) { await HandleSearchWhereBusList(); } else { VoiceCommandUserMessage cancelMsg = new VoiceCommandUserMessage(); cancelMsg.DisplayMessage = repeatMsg.SpokenMessage = "非常抱歉"; VoiceCommandResponse response1 = VoiceCommandResponse.CreateResponse(cancelMsg); vcConnection.ReportFailureAsync(response1); } } }
private async Task ReportTemperatureAsync(TemperatureSensorConsumer consumer, VoiceCommandServiceConnection connection) { var getTempResult = await consumer.GetCurrentTemperatureAsync(); if (getTempResult.Status == AllJoynStatus.Ok) { string text = String.Format("The temperature is {0:N1} degrees", getTempResult.Temp); VoiceCommandResponse tempResponse = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = text, SpokenMessage = text }); await connection.ReportSuccessAsync(tempResponse); } else { VoiceCommandResponse failure = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = "Failed to get temperature", SpokenMessage = "Sorry failed to get the temperature" }); await connection.ReportFailureAsync(failure); } }
private async Task SendCompletionMessageForBookCheapestFromXtoY(string mSource, string mDest) { string slat, slng, dlat, dlng, token, mDisplay; slat = slng = dlng = dlat = null; var mUserMessage = new VoiceCommandUserMessage(); var mUserPrompt = new VoiceCommandUserMessage(); VoiceCommandResponse mResponseError, mResponseSuccess, mResponseUserPrompt; VoiceCommandContentTile mCabTile = new VoiceCommandContentTile(); CabsAPI mCabsApi = new CabsAPI(); GeoResponse mGeoResp; Geolocator mLocator; ReverseGeoResposne mRevGeoResp; PriceEstimateResponse mPriceEstResp; BookingDetailsResponse mBookingDetailsResp; CabEstimate mCabToDisplay; List <CabEstimate> mCabEstimate; List <VoiceCommandContentTile> mCabTiles = new List <VoiceCommandContentTile>(); token = Windows.Storage.ApplicationData.Current.LocalSettings.Values["Token"].ToString(); if (mDest.Equals("")) { await ShowProgressScreen("Insufficient Info"); mDisplay = "Sorry no destination provided Please enter a valid destination"; mUserMessage.DisplayMessage = mUserMessage.SpokenMessage = mDisplay; mResponseError = VoiceCommandResponse.CreateResponse(mUserMessage); } else { mGeoResp = await mCabsApi.GeoCodingResult(token, mDest); if (mGeoResp.Code == ResponseCode.SUCCESS) { dlat = mGeoResp.Position.Latitude; dlng = mGeoResp.Position.Longitude; } else { mDisplay = "Plese enter proper Destination"; mUserMessage.DisplayMessage = mUserMessage.SpokenMessage = mDisplay; mResponseError = VoiceCommandResponse.CreateResponse(mUserMessage); await voiceServiceConnection.ReportFailureAsync(mResponseError); } } if (mSource.Equals("")) { mLocator = new Geolocator(); mLocator.DesiredAccuracyInMeters = 50; var mPosition = await mLocator.GetGeopositionAsync(); slat = mPosition.Coordinate.Point.Position.Latitude.ToString(); slng = mPosition.Coordinate.Point.Position.Longitude.ToString(); mRevGeoResp = await mCabsApi.GetReverseCodingResultlatlng(token, slat + "," + slng); if (mRevGeoResp.Code == ResponseCode.SUCCESS) { try { mSource = mRevGeoResp.FormattedAddress.Substring(0, mRevGeoResp.FormattedAddress.IndexOf(", Hyderabad")); } catch { mSource = mRevGeoResp.FormattedAddress; } } else { mDisplay = "Source not found with current location"; mUserMessage.DisplayMessage = mUserMessage.SpokenMessage = mDisplay; mResponseError = VoiceCommandResponse.CreateResponse(mUserMessage); await voiceServiceConnection.ReportFailureAsync(mResponseError); } } else { mGeoResp = await mCabsApi.GeoCodingResult(token, mSource); if (mGeoResp.Code == ResponseCode.SUCCESS) { slat = mGeoResp.Position.Latitude; slng = mGeoResp.Position.Longitude; } else { mDisplay = "Source not found"; mUserMessage.DisplayMessage = mUserMessage.SpokenMessage = mDisplay; mResponseError = VoiceCommandResponse.CreateResponse(mUserMessage); await voiceServiceConnection.ReportFailureAsync(mResponseError); } } if (slat != "" && slng != "" && dlat != "" && dlng != "") { mDisplay = "Looking for best cab providers from " + mSource.ToUpperInvariant() + " to " + mDest.ToUpperInvariant(); await ShowProgressScreen(mDisplay); mDisplay = "Booking cheapest cab from " + mSource.ToUpperInvariant() + " to " + mDest.ToUpperInvariant(); await ShowProgressScreen(mDisplay); mPriceEstResp = await mCabsApi.GetEstimate(token, slat, slng, dlat, dlng); if (mPriceEstResp.Code == ResponseCode.SUCCESS) { mCabEstimate = mPriceEstResp.Estimates; if (mCabEstimate.Count != 0) { string mDisplay1 = "Ok I have found the cheapest cab for you. Shall i book it?"; mUserPrompt.DisplayMessage = mUserPrompt.SpokenMessage = mDisplay1; mDisplay = "Shall i book it"; mUserMessage.DisplayMessage = mUserMessage.SpokenMessage = mDisplay; mCabTile.ContentTileType = VoiceCommandContentTileType.TitleWith68x68IconAndText; mCabToDisplay = mCabEstimate[0]; if (mCabToDisplay.Provider.Equals("UBER")) { mCabTile.Image = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///ContosoCabs.VoiceCommandService/img/uber.png")); } else { mCabTile.Image = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///ContosoCabs.VoiceCommandService/img/ola.png")); } mCabTile.Title = mCabToDisplay.Provider; mCabTile.TextLine1 = "Type : " + mCabToDisplay.Type; mCabTile.TextLine2 = "ETA : " + mCabToDisplay.Eta; mCabTile.TextLine3 = "Estimated Fare : " + mCabToDisplay.CurrentEstimate.LowRange + "-" + mCabToDisplay.CurrentEstimate.HighRange; mCabTiles.Add(mCabTile); mResponseUserPrompt = VoiceCommandResponse.CreateResponseForPrompt(mUserPrompt, mUserMessage, mCabTiles); var voiceCommandConfirmation = await voiceServiceConnection.RequestConfirmationAsync(mResponseUserPrompt); if (voiceCommandConfirmation.Confirmed) { mBookingDetailsResp = await mCabsApi.BookCab(token, slat, slng); mDisplay = "Successfully booked." + mCabToDisplay.Type + " Your cab driver " + mBookingDetailsResp.BookingData.DriverDetails.Name + " will be arriving in " + mCabToDisplay.Eta; mUserMessage.DisplayMessage = mUserMessage.SpokenMessage = mDisplay; mResponseSuccess = VoiceCommandResponse.CreateResponse(mUserMessage, mCabTiles); await voiceServiceConnection.ReportSuccessAsync(mResponseSuccess); } else { var userMessage = new VoiceCommandUserMessage(); string BookingCabToDestination = "Cancelling cab request..."; await ShowProgressScreen(BookingCabToDestination); string keepingTripToDestination = "Cancelled."; userMessage.DisplayMessage = userMessage.SpokenMessage = keepingTripToDestination; var response1 = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportSuccessAsync(response1); } } else { mDisplay = "Sorry there are no cabs available right now"; mUserMessage.DisplayMessage = mUserMessage.SpokenMessage = mDisplay; mResponseError = VoiceCommandResponse.CreateResponse(mUserMessage); await voiceServiceConnection.ReportFailureAsync(mResponseError); } } } }
private async Task SendCompletionMessage(CustomCommand custom) { // If this operation is expected to take longer than 0.5 seconds, the task must // supply a progress response to Cortana before starting the operation, and // updates must be provided at least every 5 seconds. await ShowProgressScreen("Executing the command \"" + custom.Name + "\""); int type = TYPE_CMD;// TODO add command type - CMD, SERIAL, HTTP/CURL try { switch (type) { case 0: // TODO figure out how to use the TYPE_* variable here { Debug.WriteLine("ProcessStartInfo"); ProcessStartInfo cmdStartInfo = new ProcessStartInfo { FileName = @"C:\Windows\System32\cmd.exe", RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true, UseShellExecute = false, CreateNoWindow = true }; Debug.WriteLine("Process"); Process cmdProcess = new Process { StartInfo = cmdStartInfo, EnableRaisingEvents = true }; cmdProcess.ErrorDataReceived += Cmd_Error; cmdProcess.OutputDataReceived += Cmd_DataReceived; cmdProcess.Start(); cmdProcess.BeginOutputReadLine(); cmdProcess.BeginErrorReadLine(); Debug.WriteLine("Command"); command = custom.BatchCommand.Replace("{searchTerm}", searchTerm); Debug.WriteLine(command); cmdProcess.StandardInput.WriteLine(custom.BatchCommand.Replace("{searchTerm}", searchTerm)); //Execute batch command cmdProcess.StandardInput.WriteLine("exit"); //Execute exit. Debug.WriteLine("WaitForExit"); cmdProcess.WaitForExit(); Debug.WriteLine("Done waiting"); break; } default: { Debug.WriteLine("TYPE \"" + type + "\" is not implemented yet"); throw new NotImplementedException(); } } } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace); } // Done. Return feedback Result = Result.Trim(); Debug.WriteLine("Result:"); Debug.WriteLine(Result); if (Result.Length > 0) { var userMessage = new VoiceCommandUserMessage { DisplayMessage = "The command \"" + custom.Name + "\" has been executed. Result:\n" + Result, //SpokenMessage = custom.Feedback.Replace("{result}", Result) // TODO re-enable when ParamList in MainPage works SpokenMessage = custom.Feedback + Result }; Debug.WriteLine("Feedback"); VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportSuccessAsync(response); } else { var userMessage = new VoiceCommandUserMessage { DisplayMessage = "The command \"" + custom.Name + "\" returned no data", //SpokenMessage = custom.Feedback.Replace("{result}", Result) // TODO re-enable when ParamList in MainPage works SpokenMessage = "An error occurred" }; Debug.WriteLine("Feedback"); VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportFailureAsync(response); } }
protected override async void OnRun(IBackgroundTaskInstance taskInstance) { serviceDeferral = taskInstance.GetDeferral(); taskInstance.Canceled += OnTaskCanceled; var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; VoiceCommandUserMessage userMessage; VoiceCommandResponse response; try { voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); voiceServiceConnection.VoiceCommandCompleted += VoiceCommandCompleted; VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync(); switch (voiceCommand.CommandName) { case "graphParams": await ShowProgressScreen("Working on it..."); var modelnumber = voiceCommand.Properties["modelnumber"][0]; double lambda = 0; double mu = 0; int model = Models.Point.GetNumberByModel(Models.Point.GetModelByNumber(modelnumber)); if (GetAllParameters(model, voiceCommand, ref lambda, ref mu)) { bool allowed = false; bool unsupported = false; if (model.Equals(1) || model.Equals(2)) { var responseMessage = new VoiceCommandUserMessage() { DisplayMessage = String.Format("Get likelihood results for the model {0} with λ={1} and μ={2}?", modelnumber, lambda, mu), SpokenMessage = String.Format("Do you want me to get likelihood results for the model {0} with these input data?", modelnumber) }; var repeatMessage = new VoiceCommandUserMessage() { DisplayMessage = String.Format("Do you still want me to get likelihood results for the model {0} with λ={1} and μ={2}?", modelnumber, lambda, mu), SpokenMessage = String.Format("Do you still want me to get likelihood results for the model {0} with these input data?", modelnumber) }; response = VoiceCommandResponse.CreateResponseForPrompt(responseMessage, repeatMessage); try { var confirmation = await voiceServiceConnection.RequestConfirmationAsync(response); allowed = confirmation.Confirmed; } catch { } } else { unsupported = true; } if (allowed) { await ShowProgressScreen("Calculating..."); List<VoiceCommandContentTile> resultContentTiles = GetLikelihoodForSelectedModel(lambda, mu, model); userMessage = new VoiceCommandUserMessage() { DisplayMessage = String.Format("Here is your likelihood results for the model {0}", modelnumber), SpokenMessage = "Done and Done! Here is your results" }; response = VoiceCommandResponse.CreateResponse(userMessage, resultContentTiles); response.AppLaunchArgument = modelnumber; await voiceServiceConnection.ReportSuccessAsync(response); } else if (unsupported) { userMessage = new VoiceCommandUserMessage() { DisplayMessage = String.Format("Model {0} is not supported now", modelnumber), SpokenMessage = "Sorry, this model is not supported now" }; response = VoiceCommandResponse.CreateResponse(userMessage); response.AppLaunchArgument = modelnumber; await voiceServiceConnection.ReportFailureAsync(response); } else { userMessage = new VoiceCommandUserMessage() { DisplayMessage = "Okay then", SpokenMessage = "Okay, then" }; response = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportSuccessAsync(response); } } else { userMessage = new VoiceCommandUserMessage() { DisplayMessage = "The arguments is incorrect", SpokenMessage = "Sorry, it seems the arguments is incorrect" }; response = VoiceCommandResponse.CreateResponse(userMessage); response.AppLaunchArgument = ""; await voiceServiceConnection.ReportFailureAsync(response); } break; default: LaunchAppInForeground(); break; } } catch (Exception ex) { Debug.WriteLine(ex.Message); } finally { if (serviceDeferral != null) { //Complete the service deferral serviceDeferral.Complete(); } } }
/// <summary> /// /// </summary> /// <param name="connection"></param> /// <returns></returns> private static async Task HandleReadImageLabelsCommandAsync(VoiceCommandServiceConnection connection) { //Initialize some stuff var CaptureClient = new CameraCaptureService.CameraCaptureService(); await CaptureClient.Init(); ///var GoogleVisionClient = new GoogleVisionAPI.VisionClient(); var MicrosoftVisionClient = new MicrosoftCognitiveVisionRepository.MicrosoftCognitiveVisionClient(); //Tell the user that we are doing something var userMessage = new VoiceCommandUserMessage(); userMessage.DisplayMessage = "Analyzing your surroundings"; userMessage.SpokenMessage = "Analyzing"; var response = VoiceCommandResponse.CreateResponse(userMessage); await connection.ReportProgressAsync(response); //TODO: Get the image string imageString; try { imageString = await CaptureClient.Capture(); } catch (Exception) { userMessage.DisplayMessage = "I can't access the camera, try opening up the Identify app first"; userMessage.SpokenMessage = "No Camera. Try opening Identify first, then ask me again."; response = VoiceCommandResponse.CreateResponse(userMessage); await connection.ReportFailureAsync(response); return; } //TODO: Send the Image through the Vision Client ///List<string> googleAnnotationResponse; string microsoftAnnotationResponse; try { //Use Google ///googleAnnotationResponse = await GoogleVisionClient.Run(imageString); //Use Microsoft microsoftAnnotationResponse = await MicrosoftVisionClient.Run(imageString); } catch (Exception) { userMessage.DisplayMessage = "Try checking your connection"; userMessage.SpokenMessage = "Hmm... I can't get a response from the cloud."; response = VoiceCommandResponse.CreateResponse(userMessage); await connection.ReportFailureAsync(response); return; } //var finalResponse = string.Join(", ", googleAnnotationResponse); var finalResponse = microsoftAnnotationResponse; //TODO: Set the User Message, Display & Spoken userMessage.DisplayMessage = finalResponse; userMessage.SpokenMessage = finalResponse; response = VoiceCommandResponse.CreateResponse(userMessage); await connection.ReportSuccessAsync(response); }
async Task ProcessSwitchLightCommandAsync( List<ServiceInfoWithLocation> serviceInfoList, VoiceCommandServiceConnection voiceConnection) { var message = new VoiceCommandUserMessage(); var tiles = new List<VoiceCommandContentTile>(); bool worked = false; if ((serviceInfoList == null) || (serviceInfoList.Count == 0)) { message.SpokenMessage = "I couldn't find any lights at all, sorry"; message.DisplayMessage = "No lights could be found at any location"; } else { var voiceCommand = await voiceConnection.GetVoiceCommandAsync(); var location = ExtractPropertyFromVoiceCommand(voiceCommand, VOICE_COMMAND_LOCATION_KEY); var onOff = ExtractPropertyFromVoiceCommand(voiceCommand, VOICE_COMMAND_ON_OFF_KEY); if (string.IsNullOrEmpty(location)) { message.SpokenMessage = "I couldn't find a location in what you said, sorry"; message.DisplayMessage = "Interpreted text did not contain an audible location"; } else if (string.IsNullOrEmpty(onOff)) { message.SpokenMessage = "I couldn't figure out whether you said on or off, sorry"; message.DisplayMessage = "Not clear around on/off status"; } else { var serviceInfo = serviceInfoList.SingleOrDefault( sinfo => string.Compare(sinfo.Location.Trim(), location.Trim(), true) == 0); if (serviceInfo == null) { message.SpokenMessage = $"I couldn't find any lights in the location {location}, sorry"; message.DisplayMessage = $"No lights in the {location}"; } else { // It may just work... await serviceInfo.Consumer.SwitchAsync(string.Compare(onOff, "on", true) == 0); message.SpokenMessage = $"I think I did it! The light should now be {onOff}"; message.DisplayMessage = $"the light is now {onOff}"; } } } var response = VoiceCommandResponse.CreateResponse(message); if (worked) { await voiceConnection.ReportSuccessAsync(response); } else { await voiceConnection.ReportFailureAsync(response); } }
public async void Run(IBackgroundTaskInstance taskInstance) { this.serviceDeferral = taskInstance.GetDeferral(); try { var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; if (triggerDetails != null && triggerDetails.Name == "CortanaCommandService") { voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); var voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync(); Debug.WriteLine(voiceCommand.CommandName); MainViewModel viewModel = new MainViewModel(); var vm = await DataLoadAsync(); if (vm != null) { viewModel = vm; } var cols = voiceCommand.CommandName.Split('_'); var commandName = cols[0]; var stateName = cols[1]; var commandViewModel = viewModel.CommandList.First(q => q.Name == commandName); commandViewModel.CurrentStateNum++; var stateViewModel = commandViewModel.StateList.ElementAt(commandViewModel.CurrentStateNum - 1); if (commandViewModel.CurrentStateNum>=commandViewModel.StateList.Count) { commandViewModel.CurrentStateNum = 0; } if(stateViewModel is SuccessStateViewModel) { var state = stateViewModel as SuccessStateViewModel; if (string.IsNullOrEmpty(state.Utterance)) { state.Utterance = ""; } var message = new VoiceCommandUserMessage(); message.SpokenMessage = state.Utterance; message.DisplayMessage = state.Utterance; var response = VoiceCommandResponse.CreateResponse(message); await voiceServiceConnection.ReportSuccessAsync(response); } else if(stateViewModel is ScriptStateViewModel) { var state = stateViewModel as ScriptStateViewModel; if (!string.IsNullOrEmpty(state.Script)) { try { ConnectionData connectionData = new ConnectionData(); connectionData.AcceptPass = viewModel.PassCode; connectionData.Script = state.Script.Replace("\n", ";").Replace("\r", "").Replace("\t", ""); string json = JsonConvert.SerializeObject(connectionData); var byteData = Encoding.UTF8.GetBytes(json); StreamSocket socket = new StreamSocket(); await socket.ConnectAsync(new HostName("127.0.0.1"), SettingManager.ServerPort); var writer = new DataWriter(socket.OutputStream); writer.WriteBytes(byteData); await writer.StoreAsync(); await writer.FlushAsync(); writer.Dispose(); socket.Dispose(); } catch (Exception) { var errorMsg = new VoiceCommandUserMessage(); string msg = "スクリプトの実行を試みましたがサーバーが起動してませんでした"; errorMsg.SpokenMessage = msg; errorMsg.DisplayMessage = msg; var errorResponse = VoiceCommandResponse.CreateResponse(errorMsg); await voiceServiceConnection.ReportFailureAsync(errorResponse); return; } } if (string.IsNullOrEmpty(state.Utterance)) { state.Utterance = ""; } var message = new VoiceCommandUserMessage(); message.SpokenMessage = state.Utterance; message.DisplayMessage = state.Utterance; var response = VoiceCommandResponse.CreateResponse(message); await voiceServiceConnection.ReportSuccessAsync(response); } await DataSaveAsync(viewModel); } }catch(Exception e) { var message = new VoiceCommandUserMessage(); message.SpokenMessage = "何かしらのエラーが起きました"; message.DisplayMessage = e.Message; var response = VoiceCommandResponse.CreateResponse(message); await voiceServiceConnection.ReportSuccessAsync(response); var toast = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText01); ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toast)); } this.serviceDeferral.Complete(); }
public async void Run(IBackgroundTaskInstance taskInstance) { this.serviceDeferral = taskInstance.GetDeferral(); try { var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; if (triggerDetails != null && triggerDetails.Name == "CortanaCommandService") { voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); var voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync(); Debug.WriteLine(voiceCommand.CommandName); MainViewModel viewModel = new MainViewModel(); var vm = await DataLoadAsync(); if (vm != null) { viewModel = vm; } var cols = voiceCommand.CommandName.Split('_'); var commandName = cols[0]; var stateName = cols[1]; var commandViewModel = viewModel.CommandList.First(q => q.Name == commandName); commandViewModel.CurrentStateNum++; var stateViewModel = commandViewModel.StateList.ElementAt(commandViewModel.CurrentStateNum - 1); if (commandViewModel.CurrentStateNum >= commandViewModel.StateList.Count) { commandViewModel.CurrentStateNum = 0; } if (stateViewModel is SuccessStateViewModel) { var state = stateViewModel as SuccessStateViewModel; if (string.IsNullOrEmpty(state.Utterance)) { state.Utterance = ""; } var message = new VoiceCommandUserMessage(); message.SpokenMessage = state.Utterance; message.DisplayMessage = state.Utterance; var response = VoiceCommandResponse.CreateResponse(message); await voiceServiceConnection.ReportSuccessAsync(response); } else if (stateViewModel is ScriptStateViewModel) { var state = stateViewModel as ScriptStateViewModel; if (!string.IsNullOrEmpty(state.Script)) { try { ConnectionData connectionData = new ConnectionData(); connectionData.AcceptPass = viewModel.PassCode; connectionData.Script = state.Script.Replace("\n", ";").Replace("\r", "").Replace("\t", ""); string json = JsonConvert.SerializeObject(connectionData); var byteData = Encoding.UTF8.GetBytes(json); StreamSocket socket = new StreamSocket(); await socket.ConnectAsync(new HostName("127.0.0.1"), SettingManager.ServerPort); var writer = new DataWriter(socket.OutputStream); writer.WriteBytes(byteData); await writer.StoreAsync(); await writer.FlushAsync(); writer.Dispose(); socket.Dispose(); } catch (Exception) { var errorMsg = new VoiceCommandUserMessage(); string msg = "スクリプトの実行を試みましたがサーバーが起動してませんでした"; errorMsg.SpokenMessage = msg; errorMsg.DisplayMessage = msg; var errorResponse = VoiceCommandResponse.CreateResponse(errorMsg); await voiceServiceConnection.ReportFailureAsync(errorResponse); return; } } if (string.IsNullOrEmpty(state.Utterance)) { state.Utterance = ""; } var message = new VoiceCommandUserMessage(); message.SpokenMessage = state.Utterance; message.DisplayMessage = state.Utterance; var response = VoiceCommandResponse.CreateResponse(message); await voiceServiceConnection.ReportSuccessAsync(response); } await DataSaveAsync(viewModel); } }catch (Exception e) { var message = new VoiceCommandUserMessage(); message.SpokenMessage = "何かしらのエラーが起きました"; message.DisplayMessage = e.Message; var response = VoiceCommandResponse.CreateResponse(message); await voiceServiceConnection.ReportSuccessAsync(response); var toast = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText01); ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toast)); } this.serviceDeferral.Complete(); }
/// <summary> /// Background task entrypoint. Voice Commands using the <VoiceCommandService Target="..."> /// tag will invoke this when they are recognized by Cortana, passing along details of the /// invocation. /// /// Background tasks must respond to activation by Cortana within 0.5 seconds, and must /// report progress to Cortana every 5 seconds (unless Cortana is waiting for user /// input). There is no execution time limit on the background task managed by Cortana, /// but developers should use plmdebug (https://msdn.microsoft.com/en-us/library/windows/hardware/jj680085%28v=vs.85%29.aspx) /// on the Cortana app package in order to prevent Cortana timing out the task during /// debugging. /// /// Cortana dismisses its UI if it loses focus. This will cause it to terminate the background /// task, even if the background task is being debugged. Use of Remote Debugging is recommended /// in order to debug background task behaviors. In order to debug background tasks, open the /// project properties for the app package (not the background task project), and enable /// Debug -> "Do not launch, but debug my code when it starts". Alternatively, add a long /// initial progress screen, and attach to the background task process while it executes. /// </summary> /// <param name="taskInstance">Connection to the hosting background service process.</param> public async void Run(IBackgroundTaskInstance taskInstance) { serviceDeferral = taskInstance.GetDeferral(); // Register to receive an event if Cortana dismisses the background task. This will // occur if the task takes too long to respond, or if Cortana's UI is dismissed. // Any pending operations should be cancelled or waited on to clean up where possible. taskInstance.Canceled += OnTaskCanceled; var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; // Load localized resources for strings sent to Cortana to be displayed to the user. cortanaResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources"); // Select the system language, which is what Cortana should be running as. cortanaContext = ResourceContext.GetForViewIndependentUse(); // Get the currently used system date format dateFormatInfo = CultureInfo.CurrentCulture.DateTimeFormat; VoiceCommandResponse response = null; // This should match the uap:AppService and RuleVoiceCommandService references from the // package manifest and VCD files, respectively. Make sure we've been launched by // a Cortana Voice Command. if (triggerDetails != null && triggerDetails.Name == this.GetType().Name) { try { voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails( triggerDetails); voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted; VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync(); HttpClient client = new HttpClient(); switch (voiceCommand.CommandName) { case "turnOnLight": string postBody = JsonConvert.SerializeObject(new Settings { IsOn = false }); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var webResponse = await client.PostAsync("http://hiremotemeetcortana.azurewebsites.net/api/settings", new StringContent(postBody, Encoding.UTF8, "application/json")); if (webResponse.IsSuccessStatusCode) { var turnOnLightMessage = new VoiceCommandUserMessage { DisplayMessage = "Wakeup Light has been turned on ", SpokenMessage = "Wakeup Light has been turned on " }; response = VoiceCommandResponse.CreateResponse(turnOnLightMessage); await voiceServiceConnection.ReportSuccessAsync(response); } else { var turnOnLightMessage = new VoiceCommandUserMessage { DisplayMessage = "Something went wrong", SpokenMessage = "Something went wrong" }; response = VoiceCommandResponse.CreateResponse(turnOnLightMessage); await voiceServiceConnection.ReportFailureAsync(response); } break; case "turnOffLight": string turnOffLightBody = JsonConvert.SerializeObject(new Settings { IsOn = false }); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var saveRurnOffLight = await client.PostAsync("http://hiremotemeetcortana.azurewebsites.net/api/settings", new StringContent(turnOffLightBody, Encoding.UTF8, "application/json")); if (saveRurnOffLight.IsSuccessStatusCode) { var turnOnLightMessage = new VoiceCommandUserMessage { DisplayMessage = "Wakeup Light has been turned off ", SpokenMessage = "Wakeup Light has been turned off " }; response = VoiceCommandResponse.CreateResponse(turnOnLightMessage); await voiceServiceConnection.ReportSuccessAsync(response); } else { var turnOnLightMessage = new VoiceCommandUserMessage { DisplayMessage = "Something went wrong", SpokenMessage = "Something went wrong" }; response = VoiceCommandResponse.CreateResponse(turnOnLightMessage); await voiceServiceConnection.ReportFailureAsync(response); } break; default: // As with app activation VCDs, we need to handle the possibility that // an app update may remove a voice command that is still registered. // This can happen if the user hasn't run an app since an update. LaunchAppInForeground(); break; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Handling Voice Command failed " + ex.ToString()); } } }
async Task ProcessSwitchLightCommandAsync( List <ServiceInfoWithLocation> serviceInfoList, VoiceCommandServiceConnection voiceConnection) { var message = new VoiceCommandUserMessage(); var tiles = new List <VoiceCommandContentTile>(); bool worked = false; if ((serviceInfoList == null) || (serviceInfoList.Count == 0)) { message.SpokenMessage = "I couldn't find any lights at all, sorry"; message.DisplayMessage = "No lights could be found at any location"; } else { var voiceCommand = await voiceConnection.GetVoiceCommandAsync(); var location = ExtractPropertyFromVoiceCommand(voiceCommand, VOICE_COMMAND_LOCATION_KEY); var onOff = ExtractPropertyFromVoiceCommand(voiceCommand, VOICE_COMMAND_ON_OFF_KEY); if (string.IsNullOrEmpty(location)) { message.SpokenMessage = "I couldn't find a location in what you said, sorry"; message.DisplayMessage = "Interpreted text did not contain an audible location"; } else if (string.IsNullOrEmpty(onOff)) { message.SpokenMessage = "I couldn't figure out whether you said on or off, sorry"; message.DisplayMessage = "Not clear around on/off status"; } else { var serviceInfo = serviceInfoList.SingleOrDefault( sinfo => string.Compare(sinfo.Location.Trim(), location.Trim(), true) == 0); if (serviceInfo == null) { message.SpokenMessage = $"I couldn't find any lights in the location {location}, sorry"; message.DisplayMessage = $"No lights in the {location}"; } else { // It may just work... await serviceInfo.Consumer.SwitchAsync(string.Compare(onOff, "on", true) == 0); message.SpokenMessage = $"I think I did it! The light should now be {onOff}"; message.DisplayMessage = $"the light is now {onOff}"; } } } var response = VoiceCommandResponse.CreateResponse(message); if (worked) { await voiceConnection.ReportSuccessAsync(response); } else { await voiceConnection.ReportFailureAsync(response); } }
static async Task ReportErrorAsync( VoiceCommandServiceConnection voiceConnection, string error) { await voiceConnection.ReportFailureAsync(MakeResponse(error)); }
public async void Run(IBackgroundTaskInstance taskInstance) { this.serviceDeferral = taskInstance.GetDeferral(); var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; if (triggerDetails != null) { try { voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); voiceServiceConnection.VoiceCommandCompleted += VoiceServiceConnection_VoiceCommandCompleted; VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync(); switch (voiceCommand.CommandName) { case "AutomationService": { var locationofaction = voiceCommand.Properties["location"][0]; var action = voiceCommand.Properties["action"][0]; var service = voiceCommand.Properties["service"][0]; Message = string.Format("Turned {0} {1} {2}", locationofaction, action, service); var blah = new SharedClasses.GetInterfaces(); var context = blah.GetAutomationList(); SharedClasses.PowerCommand command; if (action.ToLower() == "on") command = SharedClasses.PowerCommand.on; else command = SharedClasses.PowerCommand.off; var commandURI = GetCommandUri(context, locationofaction.ToLower(), service.ToLower(), command); if (!String.IsNullOrEmpty(commandURI)) { HttpClient client = new HttpClient(); var x = await client.GetAsync(new Uri(commandURI)); if (x.IsSuccessStatusCode) { IsSuccessful = true; } else { Message = "Server reported status code " + x.StatusCode; } } else { IsSuccessful = false; Message = "No command found"; } break; } // As a last resort launch the app in the foreground default: LaunchAppInForeground(); break; } } finally { if (this.serviceDeferral != null) { if (IsSuccessful) { var userMessage = new VoiceCommandUserMessage(); userMessage.DisplayMessage = userMessage.SpokenMessage = Message; var response = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportSuccessAsync(response); } else { if (String.IsNullOrEmpty(Message)) Message = "Something went wrong"; var userMessage = new VoiceCommandUserMessage(); userMessage.DisplayMessage = userMessage.SpokenMessage = Message; var response = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportFailureAsync(response); } //Complete the service deferral this.serviceDeferral.Complete(); } } } }
public async void Run(IBackgroundTaskInstance taskInstance) { AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; if (triggerDetails != null && triggerDetails.Name == "VoiceCommandService") { m_VoiceServiceDeferral = taskInstance.GetDeferral(); taskInstance.Canceled += VoiceTaskInstance_Canceled; if (triggerDetails.CallerPackageFamilyName == cortanaFamilyId) { // Being called from Cortana m_VoiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); m_VoiceServiceConnection.VoiceCommandCompleted += VoiceServiceConnection_VoiceCommandCompleted; // GetVoiceCommandAsync establishes initial connection to Cortana, and must be called prior to any // messages sent to Cortana. Attempting to use ReportSuccessAsync, ReportProgressAsync, etc // prior to calling this will produce undefined behavior. VoiceCommand voiceCommand = await m_VoiceServiceConnection.GetVoiceCommandAsync(); switch (voiceCommand.CommandName) { case "setLightsOn": var onProgressResponse = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = "Turning all lights on", SpokenMessage = "Turning all lights on" }); await m_VoiceServiceConnection.ReportProgressAsync(onProgressResponse); if (await g_CommandHandler.SetAllOnAsync()) { // All operations succeeded var successCommandResponse = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = "All lights on", SpokenMessage = "All lights on" }); await m_VoiceServiceConnection.ReportSuccessAsync(successCommandResponse); await NotifyAppOnStateChange(true); } else { var failureCommandResponse = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = "Failed to turn all lights on", SpokenMessage = "Something went wrong. Could not turn all lights on" }); await m_VoiceServiceConnection.ReportFailureAsync(failureCommandResponse); } break; case "setLightsOff": var offProgressResponse = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = "Turning all lights off", SpokenMessage = "Turning all lights off" }); await m_VoiceServiceConnection.ReportProgressAsync(offProgressResponse); if (await g_CommandHandler.SetAllOnAsync()) { // All operations succeeded var successCommandResponse = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = "All lights off", SpokenMessage = "All lights off" }); await m_VoiceServiceConnection.ReportSuccessAsync(successCommandResponse); await NotifyAppOnStateChange(false); } else { var failureCommandResponse = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = "Failed to turn all lights off", SpokenMessage = "Something went wrong. Could not turn all lights off" }); await m_VoiceServiceConnection.ReportFailureAsync(failureCommandResponse); } break; } } if (triggerDetails.CallerPackageFamilyName == Windows.ApplicationModel.Package.Current.Id.FamilyName) { // Being called from the foreground application m_AppServiceDeferral = taskInstance.GetDeferral(); taskInstance.Canceled += AppServiceTask_Canceled; triggerDetails.AppServiceConnection.ServiceClosed += AppServiceConnection_ServiceClosed; g_AppServiceConnection = triggerDetails.AppServiceConnection; } } }
/// <summary> /// Helper method for initalizing the voice service, bridge, and lights. Returns if successful. /// </summary> private async Task<bool> InitializeAsync(AppServiceTriggerDetails triggerDetails) { _voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); _voiceServiceConnection.VoiceCommandCompleted += (s, e) => _deferral.Complete(); _voiceCommand = await _voiceServiceConnection.GetVoiceCommandAsync(); _colors = HsbColor.CreateAll().ToDictionary(x => x.Name); var localStorage = ApplicationData.Current.LocalSettings.Values; _bridge = new Bridge( localStorage["bridgeIp"].ToString(), localStorage["userId"].ToString()); try { _lights = await _bridge.GetLightsAsync(); } catch (Exception) { var response = CreateCortanaResponse("Sorry, I couldn't connect to your bridge."); await _voiceServiceConnection.ReportFailureAsync(response); return false; } if (!_lights.Any()) { var response = CreateCortanaResponse("Sorry, I couldn't find any lights."); await _voiceServiceConnection.ReportFailureAsync(response); return false; } return true; }
public async void Run(IBackgroundTaskInstance taskInstance) { //Take a service deferral so the service isn't terminated. this.serviceDeferral = taskInstance.GetDeferral(); var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; if (triggerDetails != null && triggerDetails.Name == "BookReaderVoiceCommandService") { try { voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); voiceServiceConnection.VoiceCommandCompleted += VoiceCommandCompleted; VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync(); switch (voiceCommand.CommandName) { case "deleteBook": { var userMessage = new VoiceCommandUserMessage(); userMessage.DisplayMessage = "Deleting book"; userMessage.SpokenMessage = "Deleting Book"; var progressReport = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportProgressAsync(progressReport); var book = voiceCommand.Properties["bookName"][0]; userMessage = new VoiceCommandUserMessage(); userMessage.DisplayMessage = book + "deleted"; userMessage.SpokenMessage = "File deleted"; try { var folder = ApplicationData.Current.LocalFolder; var subFolder = await folder.GetFolderAsync("books"); var a = await folder.GetFolderAsync(book + ".txt"); var b = await subFolder.GetFileAsync(book + ".txt"); await a.DeleteAsync(); await b.DeleteAsync(); updateBookPhrases(); await voiceServiceConnection.ReportSuccessAsync(VoiceCommandResponse.CreateResponse(userMessage)); } catch (Exception) { userMessage.DisplayMessage = "Something went wrong, " + book + " not deleted"; userMessage.SpokenMessage = "Something went wrong, File not deleted"; await voiceServiceConnection.ReportFailureAsync(VoiceCommandResponse.CreateResponse(userMessage)); } break; } } } finally { if (this.serviceDeferral != null) { // Complete the service deferral. this.serviceDeferral.Complete(); } } } }
protected override async void OnRun(IBackgroundTaskInstance taskInstance) { serviceDeferral = taskInstance.GetDeferral(); taskInstance.Canceled += OnTaskCanceled; var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; VoiceCommandUserMessage userMessage; VoiceCommandResponse response; try { voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); voiceServiceConnection.VoiceCommandCompleted += VoiceCommandCompleted; VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync(); switch (voiceCommand.CommandName) { case "graphParams": await ShowProgressScreen("Working on it..."); var modelnumber = voiceCommand.Properties["modelnumber"][0]; double lambda = 0; double mu = 0; int model = Models.Point.GetNumberByModel(Models.Point.GetModelByNumber(modelnumber)); if (GetAllParameters(model, voiceCommand, ref lambda, ref mu)) { bool allowed = false; bool unsupported = false; if (model.Equals(1) || model.Equals(2)) { var responseMessage = new VoiceCommandUserMessage() { DisplayMessage = String.Format("Get likelihood results for the model {0} with λ={1} and μ={2}?", modelnumber, lambda, mu), SpokenMessage = String.Format("Do you want me to get likelihood results for the model {0} with these input data?", modelnumber) }; var repeatMessage = new VoiceCommandUserMessage() { DisplayMessage = String.Format("Do you still want me to get likelihood results for the model {0} with λ={1} and μ={2}?", modelnumber, lambda, mu), SpokenMessage = String.Format("Do you still want me to get likelihood results for the model {0} with these input data?", modelnumber) }; response = VoiceCommandResponse.CreateResponseForPrompt(responseMessage, repeatMessage); try { var confirmation = await voiceServiceConnection.RequestConfirmationAsync(response); allowed = confirmation.Confirmed; } catch { } } else { unsupported = true; } if (allowed) { await ShowProgressScreen("Calculating..."); List <VoiceCommandContentTile> resultContentTiles = GetLikelihoodForSelectedModel(lambda, mu, model); userMessage = new VoiceCommandUserMessage() { DisplayMessage = String.Format("Here is your likelihood results for the model {0}", modelnumber), SpokenMessage = "Done and Done! Here is your results" }; response = VoiceCommandResponse.CreateResponse(userMessage, resultContentTiles); response.AppLaunchArgument = modelnumber; await voiceServiceConnection.ReportSuccessAsync(response); } else if (unsupported) { userMessage = new VoiceCommandUserMessage() { DisplayMessage = String.Format("Model {0} is not supported now", modelnumber), SpokenMessage = "Sorry, this model is not supported now" }; response = VoiceCommandResponse.CreateResponse(userMessage); response.AppLaunchArgument = modelnumber; await voiceServiceConnection.ReportFailureAsync(response); } else { userMessage = new VoiceCommandUserMessage() { DisplayMessage = "Okay then", SpokenMessage = "Okay, then" }; response = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportSuccessAsync(response); } } else { userMessage = new VoiceCommandUserMessage() { DisplayMessage = "The arguments is incorrect", SpokenMessage = "Sorry, it seems the arguments is incorrect" }; response = VoiceCommandResponse.CreateResponse(userMessage); response.AppLaunchArgument = ""; await voiceServiceConnection.ReportFailureAsync(response); } break; default: LaunchAppInForeground(); break; } } catch (Exception ex) { Debug.WriteLine(ex.Message); } finally { if (serviceDeferral != null) { //Complete the service deferral serviceDeferral.Complete(); } } }
public async void Run(IBackgroundTaskInstance taskInstance) { _serviceDeferral = taskInstance.GetDeferral(); taskInstance.Canceled += OnTaskCanceled; // Get trigger details var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; if (triggerDetails != null && triggerDetails.Name == "VoiceCommandService") { try { _voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); _voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted; VoiceCommand voiceCommand = await _voiceServiceConnection.GetVoiceCommandAsync(); switch (voiceCommand.CommandName) { case "NearbySights": GeolocationAccessStatus accessStatus; try { // If we call this before the app has granted access, we get an exception accessStatus = await Geolocator.RequestAccessAsync(); } catch { // ensure we have a value accessStatus = GeolocationAccessStatus.Unspecified; } if (accessStatus == GeolocationAccessStatus.Allowed) { var geolocator = new Geolocator(); #if DEBUG // For testing, fake a location in San Francisco Geopoint point = new Geopoint(new BasicGeoposition { Latitude = 37.774930, Longitude = -122.419416 }); if (true) { #else var pos = await geolocator.GetGeopositionAsync(TimeSpan.FromMinutes(5), TimeSpan.FromSeconds(5)); if (pos != null) { Geocoordinate coordinate = pos.Coordinate; #endif var nearest = await GetNearestSights(point); if (nearest != null && nearest.Any()) { await ShowNearestResults(nearest); } else { await ReportFailureToGetSights(); } } else { await ReportFailureToGetCurrentLocation(); } } else { await ReportFailureToGetCurrentLocation(); } break; default: break; } } catch (Exception ex) { var userMessage = new VoiceCommandUserMessage(); userMessage.SpokenMessage = "Sorry, I can't do that right now - something went wrong."; // useful for debugging userMessage.DisplayMessage = ex.Message; var response = VoiceCommandResponse.CreateResponse(userMessage); response.AppLaunchArgument = "LaunchApp"; await _voiceServiceConnection.ReportFailureAsync(response); } } }
public async void Run(IBackgroundTaskInstance taskInstance) { //Service deferral to prevent service termination this.serviceDeferral = taskInstance.GetDeferral(); var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; if (triggerDetails != null && triggerDetails.Name == "MediaPlayerVoiceCommandService") { try { voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); voiceServiceConnection.VoiceCommandCompleted += VoiceCommandCompleted; /* Note: these background commands don't do anything interesting and are simply here to demonstrate * that commands can be run in the background using Cortana as part of this project. It is possible * to add commands relevant to this application such as playing a song in the background however * it's not practical since Cortana must provide progress every 5 seconds and besides the user can * give a command to play a song and simply minimize the app and the song continues to play while * the app is in the suspended state. */ VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync(); switch (voiceCommand.CommandName) { case "backgroundTask": { var userMessage = new VoiceCommandUserMessage(); userMessage.DisplayMessage = "Processing your request"; userMessage.SpokenMessage = "Processing your request"; var progressReport = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportProgressAsync(progressReport); try { userMessage = new VoiceCommandUserMessage(); userMessage.DisplayMessage = "Executing your request"; userMessage.SpokenMessage = "Executing your request"; await voiceServiceConnection.ReportSuccessAsync(VoiceCommandResponse.CreateResponse(userMessage)); } catch (Exception) { userMessage.DisplayMessage = "Something went wrong, terminating process"; userMessage.SpokenMessage = "Something went wrong, terminating process"; await voiceServiceConnection.ReportFailureAsync(VoiceCommandResponse.CreateResponse(userMessage)); } break; } case "backgroundTask2": { var userMessage = new VoiceCommandUserMessage(); userMessage.DisplayMessage = "Processing your request"; userMessage.SpokenMessage = "Processing your request"; var progressReport = VoiceCommandResponse.CreateResponse(userMessage); await voiceServiceConnection.ReportProgressAsync(progressReport); try { userMessage = new VoiceCommandUserMessage(); userMessage.DisplayMessage = "Executing your request"; userMessage.SpokenMessage = "Executing your request"; await voiceServiceConnection.ReportSuccessAsync(VoiceCommandResponse.CreateResponse(userMessage)); } catch (Exception) { userMessage.DisplayMessage = "Something went wrong, terminating process"; userMessage.SpokenMessage = "Something went wrong, terminating process"; await voiceServiceConnection.ReportFailureAsync(VoiceCommandResponse.CreateResponse(userMessage)); } break; } } } finally { if (this.serviceDeferral != null) { //Complete the service deferral this.serviceDeferral.Complete(); } } } }
public async void Run(IBackgroundTaskInstance taskInstance) { m_Deferral = taskInstance.GetDeferral(); AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails; if (triggerDetails != null && triggerDetails.Name == "VoiceCommandService") { taskInstance.Canceled += VoiceTaskInstance_Canceled; if (triggerDetails.CallerPackageFamilyName == cortanaFamilyId) { // Being called from Cortana m_VoiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails); m_VoiceServiceConnection.VoiceCommandCompleted += VoiceServiceConnection_VoiceCommandCompleted; // GetVoiceCommandAsync establishes initial connection to Cortana, and must be called prior to any // messages sent to Cortana. Attempting to use ReportSuccessAsync, ReportProgressAsync, etc // prior to calling this will produce undefined behavior. VoiceCommand voiceCommand = await m_VoiceServiceConnection.GetVoiceCommandAsync(); switch (voiceCommand.CommandName) { case "open": var onProgressResponse = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = "Opening all shades", SpokenMessage = "Please wait until I open all shades" }); await m_VoiceServiceConnection.ReportProgressAsync(onProgressResponse); if (await g_CommandHandler.OpenAllAsync()) { // All operations succeeded var successCommandResponse = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = "Shades are now open", SpokenMessage = "Shades are now open" }); await m_VoiceServiceConnection.ReportSuccessAsync(successCommandResponse); } else { var failureCommandResponse = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = "Failed to open the shades", SpokenMessage = "Something went wrong. Could not open all shades" }); await m_VoiceServiceConnection.ReportFailureAsync(failureCommandResponse); } break; case "close": var offProgressResponse = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = "Closing shades", SpokenMessage = "Please wait until I close all shades" }); await m_VoiceServiceConnection.ReportProgressAsync(offProgressResponse); if (await g_CommandHandler.CloseAllAsync()) { // All operations succeeded var successCommandResponse = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = "Shades are closed", SpokenMessage = "Shades are now closed" }); await m_VoiceServiceConnection.ReportSuccessAsync(successCommandResponse); } else { var failureCommandResponse = VoiceCommandResponse.CreateResponse(new VoiceCommandUserMessage() { DisplayMessage = "Failed to close all shades", SpokenMessage = "Something went wrong. Could not close all shades" }); await m_VoiceServiceConnection.ReportFailureAsync(failureCommandResponse); } break; } } } }