public async void Run(IBackgroundTaskInstance taskInstance)
        {
            // Create the deferral by requesting it from the task instance
            serviceDeferral = taskInstance.GetDeferral();

            AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name.Equals("VoiceCommandService"))
            {
                voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);

                VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                // Perform the appropriate command depending on the operation defined in VCD
                switch (voiceCommand.CommandName)
                {
                case "CheckTemperature":
                    VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
                    userMessage.DisplayMessage = "The current temperature is 23 degrees";
                    userMessage.SpokenMessage  = "The current temperature is 23 degrees";

                    VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage, null);
                    await voiceServiceConnection.ReportSuccessAsync(response);

                    break;

                default:
                    break;
                }
            }

            // Once the asynchronous method(s) are done, close the deferral
            serviceDeferral.Complete();
        }
Exemplo n.º 2
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            // add service functionality here
            // inform the system that the background task may
            // continue after the Run method has completed
            this._deferral = taskInstance.GetDeferral();

            // added event handler to handle cancel by user
            taskInstance.Canceled += TaskInstance_Canceled;

            // trigger details gives more information about the
            // task instance
            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if ((triggerDetails != null) &&
                (triggerDetails.Name == "CortanaBackgroundService"))
            {
                // try to get the voice command and respond to it.
                try
                {
                    // Retrieves a VoiceCommandServiceConnection object
                    // from the AppServiceTriggerDetails that
                    // contains info associated with the background
                    // task for the app service
                    voiceServiceConection =
                        VoiceCommandServiceConnection.
                        FromAppServiceTriggerDetails(triggerDetails);

                    // set up the command completed method to indicate completion
                    // calls deferral.complete here
                    voiceServiceConection.VoiceCommandCompleted += VoiceServiceConection_VoiceCommandCompleted;

                    // get the voice command information
                    VoiceCommand voiceCommand = await voiceServiceConection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                    case "howsMyCoffee":
                    {
                        sendUpdateMessageByReturn();
                        break;
                    }

                    default:
                    {
                        launchAppInForeground();
                        break;
                    }
                    }
                }
                finally
                {
                    if (this._deferral != null)
                    {
                        // complete the service deferral
                        this._deferral.Complete();
                    }
                } // end try
            }
        }
Exemplo n.º 3
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
            if (triggerDetails != null && triggerDetails.Name == "BotWorldVoiceCommandService")
            {
                voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                voiceServiceConnection.VoiceCommandCompleted += VoiceServiceConnection_VoiceCommandCompleted;

                VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();
                switch(voiceCommand.CommandName)
                {
                    case "checkScore":
                        {
                            await SendAnswer();
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }

            deferral.Complete();

        }
Exemplo n.º 4
0
        /// <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);
        }
Exemplo n.º 5
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            // Create the deferral by requesting it from the task instance
            serviceDeferral = taskInstance.GetDeferral();

            AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name.Equals("IMCommandVoice"))
            {
                voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);

                VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                // Perform the appropriate command depending on the operation defined in VCD
                switch (voiceCommand.CommandName)
                {
                    case "oldback":
                        VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
                        userMessage.DisplayMessage = "The current temperature is 23 degrees";
                        userMessage.SpokenMessage = "The current temperature is 23 degrees";

                        VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage, null);
                        await voiceServiceConnection.ReportSuccessAsync(response);
                        break;

                    default:
                        break;
                }
            }

            // Once the asynchronous method(s) are done, close the deferral
            serviceDeferral.Complete();
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            deferral = taskInstance.GetDeferral();
            taskInstance.Canceled += TaskInstance_Canceled;
            try
            {
                var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
                voiceServiceConn = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                var command = await voiceServiceConn?.GetVoiceCommandAsync();

                switch (command.CommandName)
                {
                case "RollCommand":
                    await ServiceCommandHandleAsync(command.SpeechRecognitionResult.SemanticInterpretation);

                    break;

                case "ShowAllCommand":
                    await FindCommandHandleAsync(command.SpeechRecognitionResult.SemanticInterpretation);

                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
            finally
            {
                deferral?.Complete();
            }
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral        = taskInstance.GetDeferral();
            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            cortanaResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");

            cortanaContext = ResourceContext.GetForViewIndependentUse();

            dateFormatInfo = CultureInfo.CurrentCulture.DateTimeFormat;

            if (triggerDetails != null && triggerDetails.Name == "JeedomAppVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    var    userMessage = new VoiceCommandUserMessage();
                    string message     = "";


                    // Ajout d'une requet jeedom pour retrouver la commande
                    switch (voiceCommand.CommandName)
                    {
                    case "JeedomInteractList":
                        string CortanaVoiceCommande = voiceCommand.Properties["InteractList"][0];
                        await Jeedom.RequestViewModel.Instance.interactTryToReply(CortanaVoiceCommande);

                        message = Jeedom.RequestViewModel.Instance.InteractReply;
                        break;

                    default:
                        LaunchAppInForeground();
                        break;
                    }

                    userMessage.DisplayMessage = message;
                    userMessage.SpokenMessage  = message;


                    var response = VoiceCommandResponse.CreateResponse(userMessage);
                    response.AppLaunchArgument = message;


                    await voiceServiceConnection.ReportSuccessAsync(response);
                }
                catch (Exception ex)
                {
                    //System.Diagnostics.Debug.WriteLine("Handling Voice Command failed " + ex.ToString());
                }
            }
        }
Exemplo n.º 8
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral = taskInstance.GetDeferral();
            taskInstance.Canceled += TaskInstance_Canceled;

            // Get trigger details , it from Cortana
            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
            if (triggerDetails != null && triggerDetails.Name == "com.poumason.uwp.VCBusService")
            {
                // 取得 VoiceCommandServiceConnection
                vcConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                vcConnection.VoiceCommandCompleted += VcConnection_VoiceCommandCompleted;

                VoiceCommand vCommand = await vcConnection.GetVoiceCommandAsync();
                // 判断 command name
                switch (vCommand.CommandName)
                {
                    case "SearchWhereBusList":
                        foreach (var item in vCommand.Properties)
                        {
                            Debug.WriteLine(String.Format("{0}={1}", item.Key, item.Value));
                        }
                        await HandleSearchWhereBusList();
                        break;
                    case "CheckStartBusStation":
                        ShowHaveOtherBusConfirm();
                        break;
                    default:
                        // 需要注册这个处理,以相容那些不再使用的 VCDs.
                        LaunchAppInForeground();
                        break;
                }
            }
        }
Exemplo n.º 9
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral        = taskInstance.GetDeferral();
            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            try
            {
                voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);

                voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                var interpretation = voiceCommand.SpeechRecognitionResult.SemanticInterpretation;

                await ProcessGenerateFactAsync(interpretation);

                this.serviceDeferral.Complete();
            }
            catch (Exception ex)
            {
            }
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral = taskInstance.GetDeferral();
            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails == null)
            {
                serviceDeferral.Complete();
                return;
            }

            try
            {
                voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                var voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                //Find the command name witch should match the VCD
                if (voiceCommand.CommandName == "buildit_help")
                {
                    var props = voiceCommand.Properties;
                    await CortanaHelpList();
                }
                await Task.Delay(1000);
                await ShowProgressScreen();
            }
            catch
            {
                Debug.WriteLine("Unable to process voice command");
            }
            serviceDeferral.Complete();
        }
        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;

            // This should match the uap:AppService and VoiceCommandService 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 == "AwfulVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    // Depending on the operation (defined in AdventureWorks:AdventureWorksCommands.xml)
                    // perform the appropriate command.
                    switch (voiceCommand.CommandName)
                    {
                        case "didMyThreadsUpdate":
                            await CheckForBookmarksForUpdates();
                            break;
                        case "didMyPmUpdate":
                            await CheckPmsForUpdates();
                            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());
                }
            }
        }
Exemplo n.º 12
0
        /*  ResourceMap cortanaResourceMap;
         * ResourceContext cortanaContext;
         * DateTimeFormatInfo dateFormatInfo;*/

        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            ServiceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += TaskInstance_Canceled;

            var TriggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (TriggerDetails != null && TriggerDetails.Name == "TDTCortanaCommandServices")
            {
                VoiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(TriggerDetails);

                VoiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                // 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 VoiceServiceConnection.GetVoiceCommandAsync();

                switch (voiceCommand.CommandName)
                {
                case "ShowMyTasks":
                    string Style;
                    try
                    {
                        Style = voiceCommand.Properties["Style"][0];
                    }
                    catch
                    {
                        Style = "";
                    }
                    await ShowTasks(Style, voiceCommand.SpeechRecognitionResult.Text.Contains("today"));

                    break;

                case "CompleteSpecificTask":
                    var CompletedTask = voiceCommand.Properties["Tasks"][0];
                    CompleteTask(CompletedTask);
                    break;

                case "DeleteSpecificTask":
                    var DeletedTask = voiceCommand.Properties["Tasks"][0];
                    DeleteTask(DeletedTask);
                    break;

                case "ShowDetailForSpecificTask":
                    var DetailedTask = voiceCommand.Properties["Tasks"][0];
                    ShowDetailsOfTask(DetailedTask);
                    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;
                }
            }
        }
Exemplo n.º 13
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            this.serviceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails =
                taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null &&
                triggerDetails.Name == "CortanaAppService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted +=
                        VoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                    case "drinkAGlass":
                    {
                        var subject = voiceCommand.Properties["subject"][0];
                        var amount  = voiceCommand.Properties["amount"][0];
                        await SendCompletionMessageForFixedAmount(amount, subject);

                        break;
                    }

                    case "drinkAmount":
                    {
                        var amount   = voiceCommand.SpeechRecognitionResult.SemanticInterpretation.Properties["amount"][0];
                        var beverage = voiceCommand.Properties["beverage"][0];
                        await SendCompletionMessageForAmount(amount, beverage);

                        break;
                    }

                    // As a last resort launch the app in the foreground
                    default:
                        LaunchAppInForeground();
                        break;
                    }
                }
                finally
                {
                    //if (this.serviceDeferral != null)
                    //{
                    //    //Complete the service deferral
                    //    this.serviceDeferral.Complete();
                    //}
                }
            }
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            this.serviceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null &&
              triggerDetails.Name == "TalkToMeVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                    voiceServiceConnection.VoiceCommandCompleted += VoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                        case "startMyPresentation":
                            ShowStartMyPresentation();
                            break;

                        case "endMyPresentation":
                            ShowEndMyPresentation();
                            break;

                        case "bankBalance":
                            SendCompletionMessageForBankAccount();
                            break;

                        case "whatDoYouThinkAbout":
                            var name = voiceCommand.Properties["name"][0];
                            SendCompletionMessageForName(name);
                            break;

                        case "birthday":
                            var birthdayName = voiceCommand.Properties["name"][0];
                            SingABirthdaySong(birthdayName);
                            break;

                        default:
                            LaunchAppInForeground();
                            break;
                    }
                }
                finally
                {
                    if (this.serviceDeferral != null)
                    {
                        //Complete the service deferral
                        this.serviceDeferral.Complete();
                    }
                }
            }

        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            //Take a service deferral so the service isn&#39;t terminated.
            this.serviceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails =
                taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null &&
                triggerDetails.Name == "VoiceCommandsService")
            {
                try
                {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += VoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                    case "calculatePropablityDSA":
                    {
                        string e1             = voiceCommand.Properties["eigenschaft1"][0];
                        string e2             = voiceCommand.Properties["eigenschaft2"][0];
                        string e3             = voiceCommand.Properties["eigenschaft3"][0];
                        string tawString      = voiceCommand.Properties["taw"][0];
                        var    voiceParameter = new DSAVoiceCommand(e1, e2, e3);
                        var    calculator     = new DSAPropabilityCaluator(voiceParameter.Eigentschaft1, voiceParameter.Eigentschaft2, voiceParameter.Eigentschaft3);
                        int    taw;
                        if (!int.TryParse(tawString, out taw))
                        {
                            taw = 5;
                        }
                        var probability = calculator.CalcualteDicePropability(taw);
                        SendCompletionMessageForDestination(probability, voiceParameter);
                        break;
                    }

                    // As a last resort, launch the app in the foreground.
                    default:
                        LaunchAppInForeground();
                        break;
                    }
                }
                finally
                {
                    if (this.serviceDeferral != null)
                    {
                        // Complete the service deferral.
                        this.serviceDeferral.Complete();
                    }
                }
            }
        }
Exemplo n.º 16
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral        = taskInstance.GetDeferral();
            taskInstance.Canceled += TaskInstance_Canceled;

            httpClient = new HttpClient();

            AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name == "HuemongousVoiceCommandService")
            {
                voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                voiceServiceConnection.VoiceCommandCompleted += VoiceServiceConnection_VoiceCommandCompleted;

                VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                if (voiceCommand.CommandName == "LightsOnOrOff")
                {
                    string lightState = voiceCommand.Properties["lightState"][0];
                    await HandleLightsOnOrOff(lightState);
                }
                else if (voiceCommand.CommandName == "LightOnOrOff")
                {
                    string lightState     = voiceCommand.Properties["lightState"][0];
                    string lightOrRoom    = voiceCommand.Properties["lightOrRoom"][0];
                    string lightPlurality = voiceCommand.Properties["lightPlurality"][0];
                    await HandleLightOnOrOff(lightState, lightOrRoom, lightPlurality);
                }
                else if (voiceCommand.CommandName == "SpecifyLightOnOrOff")
                {
                    string lightState = voiceCommand.Properties["lightState"][0];
                }
                else if (voiceCommand.CommandName == "SetLightScene")
                {
                    string scene = voiceCommand.Properties["scene"][0];
                }
                else if (voiceCommand.CommandName == "SetLightsColor")
                {
                    string color = voiceCommand.Properties["color"][0];
                    await HandleSetLightsColor(color);
                }
                else if (voiceCommand.CommandName == "SetLightColor")
                {
                    string lightOrRoom    = voiceCommand.Properties["lightOrRoom"][0];
                    string lightPlurality = voiceCommand.Properties["lightPlurality"][0];
                    string color          = voiceCommand.Properties["color"][0];
                }
                else if (voiceCommand.CommandName == "SpecifyLightColor")
                {
                    string color = voiceCommand.Properties["color"][0];
                }
                else
                {
                    Debug.WriteLine("unknown command");
                }
            }
        }
Exemplo n.º 17
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral = taskInstance.GetDeferral();
            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            cortanaResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");

            cortanaContext = ResourceContext.GetForViewIndependentUse();

            dateFormatInfo = CultureInfo.CurrentCulture.DateTimeFormat;

            if (triggerDetails != null && triggerDetails.Name == "DomojeeVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();
                    var userMessage = new VoiceCommandUserMessage();
                    string message = "";


                    // Ajout d'une requet jeedom pour retrouver la commande
                    switch (voiceCommand.CommandName)
                    {
                        case "JeedomInteractList":
                            string CortanaVoiceCommande= voiceCommand.Properties["InteractList"][0];
                            await Jeedom.RequestViewModel.Instance.interactTryToReply(CortanaVoiceCommande);
                            message = Jeedom.RequestViewModel.Instance.InteractReply;
                            break;
                        default:
                            LaunchAppInForeground();
                            break;
                    }

                    userMessage.DisplayMessage = message;
                    userMessage.SpokenMessage = message;
                    

            var response = VoiceCommandResponse.CreateResponse(userMessage);
            response.AppLaunchArgument = message;


                await voiceServiceConnection.ReportSuccessAsync(response);
            }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Handling Voice Command failed " + ex.ToString());
                }
            }
        }
Exemplo n.º 18
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            this.serviceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null &&
                triggerDetails.Name == "TalkToMeVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                    voiceServiceConnection.VoiceCommandCompleted += VoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                    case "startMyPresentation":
                        ShowStartMyPresentation();
                        break;

                    case "endMyPresentation":
                        ShowEndMyPresentation();
                        break;

                    case "bankBalance":
                        SendCompletionMessageForBankAccount();
                        break;

                    case "whatDoYouThinkAbout":
                        var name = voiceCommand.Properties["name"][0];
                        SendCompletionMessageForName(name);
                        break;

                    case "birthday":
                        var birthdayName = voiceCommand.Properties["name"][0];
                        SingABirthdaySong(birthdayName);
                        break;

                    default:
                        LaunchAppInForeground();
                        break;
                    }
                }
                finally
                {
                    if (this.serviceDeferral != null)
                    {
                        //Complete the service deferral
                        this.serviceDeferral.Complete();
                    }
                }
            }
        }
Exemplo n.º 19
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            //Take a service deferral so the service isn't terminated
            this.serviceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails =
                taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null &&
                triggerDetails.Name == "CortanaAppService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted +=
                        VoiceCommandCompleted;

                    VoiceCommand voiceCommand = await
                                                voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                    case "toggleSwitch":
                    {
                        var pref =
                            voiceCommand.Properties["pref"][0];
                        var switchname =
                            voiceCommand.Properties["switch"][0];
                        var state =
                            voiceCommand.Properties["state"][0];
                        await ToggleSwitch(pref, switchname, state);

                        break;
                    }

                    // As a last resort launch the app in the foreground
                    default:
                        LaunchAppInForeground();
                        break;
                    }
                }
                finally
                {
                    if (this.serviceDeferral != null)
                    {
                        //Complete the service deferral
                        this.serviceDeferral.Complete();
                    }
                }
            }
        }
Exemplo n.º 20
0
        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;


            // Select the system language, which is what Cortana should be running as.
            _cortanaContext = ResourceContext.GetForViewIndependentUse();

            // This should match the uap:AppService and VoiceCommandService 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 == "CortanaDialogFlow")
            {
                try
                {
                    _voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    _voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    // 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 _voiceServiceConnection.GetVoiceCommandAsync();

                    // Depending on the operation (defined in TestCortana:TestCortanaCommands.xml)
                    // perform the appropriate command.
                    switch (voiceCommand.CommandName)
                    {
                    case "changeAmbiance":
                        await SendCompletionMessageForAmbiance();

                        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());
                }
            }
        }
        /// <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;

            // This should match the uap:AppService and VoiceCommandService 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 == "AdventureWorksVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    // Depending on the operation (defined in AdventureWorks:AdventureWorksCommands.xml)
                    // perform the appropriate command.
                    switch (voiceCommand.CommandName)
                    {
                    case "whenIsTripToDestination":
                        var destination = voiceCommand.Properties["destination"][0];
                        await SendCompletionMessageForDestination(destination);

                        break;

                    case "cancelTripToDestination":
                        var cancelDestination = voiceCommand.Properties["destination"][0];
                        await SendCompletionMessageForCancellation(cancelDestination);

                        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());
                }
            }
        }
Exemplo n.º 22
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            _serviceDeferral = taskInstance.GetDeferral();


            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name == "CortanaBGService")
            {
                try
                {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += VoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                    case "blinds":
                    {
                        foreach (var prop in voiceCommand.Properties)
                        {
                            Debug.WriteLine(String.Format("prop {0} = {1}", prop.Key, prop.Value.ToString()));
                        }

                        var state = voiceCommand.Properties["blindState"][0];
                        var blind = voiceCommand.Properties["blindId"][0];

                        await ChangeBlindState(blind, state);

                        break;
                    }

                    // As a last resort launch the app in the foreground
                    default:
                        _serviceDeferral.Complete();
                        break;
                    }
                }
                catch (Exception)
                {
                    Debugger.Break();
                }
                finally
                {
                    if (_serviceDeferral != null)
                    {
                        //Complete the service deferral
                        _serviceDeferral.Complete();
                        _serviceDeferral = null;
                    }
                }
            }
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral        = taskInstance.GetDeferral();
            taskInstance.Canceled += OnTaskCancelled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            cortanaResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");
            cortanaContext     = ResourceContext.GetForViewIndependentUse();

            if (triggerDetails != null && triggerDetails.Name == "VoiceCommandService")
            {
                try
                {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    var client = await this.GetClient();

                    // Depending on the operation (defined in the VoiceCommands.xml file)
                    // perform the appropriate command.
                    switch (voiceCommand.CommandName)
                    {
                    case "turnOnItem":
                        var onTarget = voiceCommand.Properties["target"][0];
                        await SendCompletionMessageForOnOff(client, onTarget, true);

                        break;

                    case "turnOffItem":
                        var offTarget = voiceCommand.Properties["target"][0];
                        await SendCompletionMessageForOnOff(client, offTarget, false);

                        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());
                }
            }
        }
Exemplo n.º 24
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            //Take a service deferral so the service isn&#39;t terminated.
            this.serviceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += VoiceCommandCanceled;

            var triggerDetails =
                taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null &&
                triggerDetails.Name == "ContactsVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted +=
                        VoiceCommandCompleted;

                    VoiceCommand voiceCommand = await
                                                voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                    case "zeige":
                    {
                        var contactName =
                            voiceCommand.Properties["nutzer"][0];
                        SendCompletionMessageForContact(contactName);
                        break;
                    }

                    // As a last resort, launch the app in the foreground.
                    default:
                        LaunchAppInForeground();
                        break;
                    }
                }
                finally
                {
                    if (this.serviceDeferral != null)
                    {
                        // Complete the service deferral.
                        this.serviceDeferral.Complete();
                    }
                }
            }
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            taskDeferall = taskInstance.GetDeferral();

            //we add OnTaskCanceled event
            taskInstance.Canceled += OnTaskCanceled;

            //grab triggerDetails and cast on ApppService type 
            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
            try
            {
                //here we creating connection which has all our information
                voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);

                //Add CompletedEvent without it Cortand will hang up very often that's why Cortans won't know when task is completed
                //I divided  Completed into two ways  one: TaskCompleted and second VoiceCommand Completed to let Cortana know when task and command is completed 
                voiceServiceConnection.OnVoiceCommandCompleted += OnVoiceCommandCompleted;

                //voiceCommand is part of voiceSerivceConnection  I'm gonna to use it for....
                VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                //grab interpretation
                //Interpreatin is very important to inform Cortan what we want, it's all we care about 
                var interpretation = voiceCommand.SpeechRecognitionResult.SemanticInterpretation;
                VoiceCommandType commandType = (VoiceCommandType)Enum.Parse(typeof(VoiceCommandType), voiceCommand.CommandName, true);
                switch(commandType)
                {
                    case VoiceCommandType.InterestingFactQueryCommand:
                        await ProcessInterestingFactAsync(interpretation);
                        break;
                    case VoiceCommandType.WeekOfYearQueryCommand:
                        await ProcessWeekOfYearAsync(interpretation);
                        break;

                    case VoiceCommandType.FindBusinessQueryCommand:
                        ProcessFindBusinessAsync(interpretation);
                        break;

                    default:
                        break;
                }

            }
            catch(Exception ex)
            {

            }



        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            //service deferral so that the  app service is not terminated while handling the voice command.
            this.serviceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails =
                taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name == "HelloCortanaVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted +=
                        VoiceCommandCompleted;

                    VoiceCommand voiceCommand = await
                                                voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                    case "showInCanvas":
                    {
                        var destination =
                            voiceCommand.Properties["destination"][0];
                        SendCompletionMessageForDestination(destination);
                        break;
                    }


                    default:
                        LaunchAppInForeground();
                        break;
                    }
                }
                finally
                {
                    if (this.serviceDeferral != null)
                    {
                        // Complete the service deferral.
                        this.serviceDeferral.Complete();
                    }
                }
            }
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name == "HolVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection =
                                    VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                                        triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;


                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                        case "SayHello":

                            var userMessage = new VoiceCommandUserMessage();
                            userMessage.DisplayMessage = "お店で合言葉話してね。";
                            userMessage.SpokenMessage = "ごきげんよう。";

                            var response = VoiceCommandResponse.CreateResponse(userMessage);

                            await voiceServiceConnection.ReportSuccessAsync(response);

                            break;


                        default:
                            break;
                    }


                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Handling Voice Command failed " + ex.ToString());
                }
            }


        }
Exemplo n.º 28
0
        /* Unused, because this project does not yet need localised resources
         * /// <summary>
         * /// ResourceMap containing localized strings for display in Cortana.
         * /// </summary>
         * ResourceMap cortanaResourceMap;
         *
         * /// <summary>
         * /// The context for localized strings.
         * /// </summary>
         * ResourceContext cortanaContext;
         */

        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            //Use to let Cortana know what's up
            serviceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += OnTaskCanceled;

            AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name == "AdventureWorksVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    // Perform the appropriate command (defined in Manto:MantoCortanaCommands.xml)
                    switch (voiceCommand.CommandName)
                    {
                    case "whenIsTripToDestination":
                        var destination = voiceCommand.Properties["destination"][0];
                        await SendCompletionMessageForDestination(destination);

                        break;

                    case "cancelTripToDestination":
                        var cancelDestination = voiceCommand.Properties["destination"][0];
                        await SendCompletionMessageForCancellation(cancelDestination);

                        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());
                }
            }
        }
Exemplo n.º 29
0
        /// <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.
        /// </summary>
        /// <param name="taskInstance">Connection to the hosting background service process.</param>
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            cortanaResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Strings");
            cortanaContext     = ResourceContext.GetForViewIndependentUse();
            dateFormatInfo     = CultureInfo.CurrentCulture.DateTimeFormat;

            if (triggerDetails != null && triggerDetails.Name == "VoiceCommandService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    // 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 voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                    case "enterLivingRoom":
                    case "leaveLivingRoom":
                        await HandleRoomEvent(voiceCommand.CommandName);

                        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());
                }
            }
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            try
            {
                _resourceLoader = ResourceLoader.GetForViewIndependentUse();
            }
            catch (Exception ex)
            {
                // todo: do something
            }
            _serviceDeferral = taskInstance.GetDeferral();

            // If cancelled, set deferal
            // Mets le déféral si annulation
            taskInstance.Canceled += (sender, reason) => _serviceDeferral?.Complete();

            // Get the details of the event that trigered the service
            // Obtient les détails de l'évenement qui à démarré le service
            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            // Check if it is service name set in VCD
            // Regarde si c'est le nom du service qui est mis dans le VCD
            if (triggerDetails?.Name == "PresidentsService")
            {
                _voiceCommandServiceConnection =
                    VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                // Set deferal when voice command is completed
                // Mets le deferal quand la commande vocale est terminée
                _voiceCommandServiceConnection.VoiceCommandCompleted += (sender, args) => _serviceDeferral?.Complete();
                // Get voice command
                // Obtient la commande vocale
                var voicecommand = await _voiceCommandServiceConnection.GetVoiceCommandAsync();

                switch (voicecommand.CommandName)
                {
                    case "whichPresidentYear":
                        var year = voicecommand.Properties["year"][0];
                        await SendProgressMessageAsync(string.Format(GetString(Strings.LookingYear), year));
                        await SearchPresidentForYearAsync(year);
                        break;
                    case "showTerm":
                        var president = voicecommand.Properties["president"][0];
                        await SendProgressMessageAsync(string.Format(GetString(Strings.LookingTerms), president));
                        await SearchTermOfPresidentAsync(president);
                        break;
                }
            }
        }
Exemplo n.º 31
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral = taskInstance.GetDeferral();

            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();


            if (triggerDetails != null && triggerDetails.Name == "HolVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                    case "SayHello":
                        var userMessage = new VoiceCommandUserMessage();
                        userMessage.DisplayMessage = "Hello!";
                        userMessage.SpokenMessage  = "Your app says hi. It is having a great time.";
                        var response = VoiceCommandResponse.CreateResponse(userMessage);
                        await voiceServiceConnection.ReportSuccessAsync(response);

                        break;

                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Handling Voice Command failed " + ex.ToString());
                }
            }
        }
Exemplo n.º 32
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            //Take a service deferral so the service isn&#39;t terminated.
            this.serviceDeferral = taskInstance.GetDeferral();


            var triggerDetails =
                taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name == "VoiceService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted +=
                        VoiceCommandCompleted;

                    VoiceCommand voiceCommand = await
                                                voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                    case "howIsMood":
                    {
                        Task.Delay(1000);
                        SendCompletionMessageForDestination("lahm");
                        break;
                    }

                    // As a last resort, launch the app in the foreground.
                    default:
                        LaunchAppInForeground();
                        break;
                    }
                }
                finally
                {
                    if (this.serviceDeferral != null)
                    {
                        // Complete the service deferral.
                        this.serviceDeferral.Complete();
                    }
                }
            }
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            _deferral = taskInstance.GetDeferral();

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
            if (triggerDetails != null && triggerDetails.Name == "FeedReaderVoiceCommandService")
            {
                _voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                _voiceServiceConnection.VoiceCommandCompleted += VoiceServiceConnection_VoiceCommandCompleted;
                var voiceCommand = await _voiceServiceConnection.GetVoiceCommandAsync();
                if (voiceCommand?.CommandName == "ShowLatestNews")
                {
                    await ShowLatestNews();
                }
            }
        }
Exemplo n.º 34
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            // inform the system that the background task may continue after the 
            // Run method has completed
            this._deferral = taskInstance.GetDeferral();

            taskInstance.Canceled += TaskInstance_Canceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if( (triggerDetails != null) &&
                (triggerDetails.Name == "superGameVoiceService"))
            {
                try
                {
                    voiceServiceConection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);

                    voiceServiceConection.VoiceCommandCompleted += VoiceServiceConection_VoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                        case "highScorer":
                            {
                       
                                sendCompletionMessageForHighScorer();
                                break;
                            }
                        default:
                            {
                                launchAppInForeground();
                                break;
                            }
                    }
                }
                finally 
                {
                    if( this._deferral != null)
                    {
                        // complete the service deferral
                        this._deferral.Complete();
                    }
                }

            } // end if (triggerDetails
        }
Exemplo n.º 35
0
        protected override async void OnRun(IBackgroundTaskInstance taskInstance)
        {
            this.serviceDeferral = taskInstance.GetDeferral();

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            // get the voiceCommandServiceConnection from the tigger details
            voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);

            VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

            VoiceCommandResponse response;

            // switch statement to handle different commands
            switch (voiceCommand.CommandName)
            {
                case "sendMessage":
                    // get the message the user has spoken
                    var message = voiceCommand.Properties["message"][0];
                    //var bot = new Bot();

                    // get response from bot
                    string firstResponse = "";
                        //await bot.SendMessageAndGetResponseFromBot(message);

                    // create response messages for Cortana to respond
                    var responseMessage = new VoiceCommandUserMessage();
                    var responseMessage2 = new VoiceCommandUserMessage();
                    responseMessage.DisplayMessage =
                        responseMessage.SpokenMessage = firstResponse;
                    responseMessage2.DisplayMessage =
                        responseMessage2.SpokenMessage = "did you not hear me?";

                    // create a response and ask Cortana to respond with success
                    response = VoiceCommandResponse.CreateResponse(responseMessage);
                    await voiceServiceConnection.ReportSuccessAsync(response);

                    break;
            }

            if (this.serviceDeferral != null)
            {
                //Complete the service deferral
                this.serviceDeferral.Complete();
            }

        }
Exemplo n.º 36
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            try {
                if (triggerDetails != null)
                {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandComplete;
                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    var person = voiceCommand.Properties["person"][0];

                    var userMessage = new VoiceCommandUserMessage();
                    userMessage.DisplayMessage = "Which workflow would you like to trigger?";
                    userMessage.SpokenMessage  = "Which workflow would you like to trigger?";
                    var workflowTiles = new List <VoiceCommandContentTile>()
                    {
                        new VoiceCommandContentTile()
                        {
                            ContentTileType   = VoiceCommandContentTileType.TitleWith68x68IconAndText,
                            Image             = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///GhostRider.VoiceCommands/Images/GreyTile.scale-100.png")),
                            Title             = "Create New Page",
                            AppLaunchArgument = $"person={person}",
                            TextLine1         = "Creates a new basic page"
                        },
                        new VoiceCommandContentTile()
                        {
                            ContentTileType   = VoiceCommandContentTileType.TitleWith68x68IconAndText,
                            Image             = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Images/GreyTile.scale-100.png")),
                            Title             = "Conquer the World",
                            AppLaunchArgument = $"person={person}",
                            TextLine1         = "Destroys all of your enemies"
                        },
                    };

                    var response = VoiceCommandResponse.CreateResponse(userMessage, workflowTiles);
                    await this.voiceServiceConnection.ReportSuccessAsync(response);

                    //await DetermineAction(voiceCommand);
                }
            }
            finally {
                deferral.Complete();
            }
        }
Exemplo n.º 37
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral = taskInstance.GetDeferral();

            // Does a ton of like, super important stuff
            
            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name == "CoffeeVoiceService")
            {
                try
                {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                        case "makeMeCoffeeBitch":
                            {
                                //await ShowProgressScreen("Working");
                                //WhatKindOfCoffee();

                                GetSassy();
                                break;
                            }

                        // As a last resort, launch the app in the foreground.
                        default:
                            LaunchAppInForeground();
                            break;
                    }
                }
                finally
                {
                    if (this.serviceDeferral != null)
                    {
                        // Complete the service deferral.
                        this.serviceDeferral.Complete();
                    }
                }
            }
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral = taskInstance.GetDeferral();

            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();


            if (triggerDetails != null && triggerDetails.Name == "HolVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                        case "SayHello":
                            var userMessage = new VoiceCommandUserMessage();
                            userMessage.DisplayMessage = "Hello!";
                            userMessage.SpokenMessage = "Your app says hi. It is having a great time.";
                            var response = VoiceCommandResponse.CreateResponse(userMessage);
                            await voiceServiceConnection.ReportSuccessAsync(response);
                            break;
                        default:
                            break;
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Handling Voice Command failed " + ex.ToString());
                }
            }

        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral = taskInstance.GetDeferral();
            taskInstance.Canceled += OnTaskCancelled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
            cortanaResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");
            cortanaContext = ResourceContext.GetForViewIndependentUse();

            if (triggerDetails != null && triggerDetails.Name == "VoiceCommandService")
            {
                try
                {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    var client = await this.GetClient();

                    // Depending on the operation (defined in the VoiceCommands.xml file)
                    // perform the appropriate command.
                    switch (voiceCommand.CommandName)
                    {
                        case "turnOnItem":
                            var onTarget = voiceCommand.Properties["target"][0];
                            await SendCompletionMessageForOnOff(client, onTarget, true);
                            break;
                        case "turnOffItem":
                            var offTarget = voiceCommand.Properties["target"][0];
                            await SendCompletionMessageForOnOff(client, offTarget, false);
                            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());
                }
            }
        }
Exemplo n.º 40
0
        /* Unused, because this project does not yet need localised resources
        /// <summary>
        /// ResourceMap containing localized strings for display in Cortana.
        /// </summary>
        ResourceMap cortanaResourceMap;

        /// <summary>
        /// The context for localized strings.
        /// </summary>
        ResourceContext cortanaContext;
        */

        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            //Use to let Cortana know what's up
            serviceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += OnTaskCanceled;

            AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name == "AdventureWorksVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    // Perform the appropriate command (defined in Manto:MantoCortanaCommands.xml)
                    switch (voiceCommand.CommandName)
                    {
                        case "whenIsTripToDestination":
                            var destination = voiceCommand.Properties["destination"][0];
                            await SendCompletionMessageForDestination(destination);
                            break;
                        case "cancelTripToDestination":
                            var cancelDestination = voiceCommand.Properties["destination"][0];
                            await SendCompletionMessageForCancellation(cancelDestination);
                            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());
                }
            }
        }
Exemplo n.º 41
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            //Take a service deferral so the service isn't terminated
            this.serviceDeferral = taskInstance.GetDeferral();
            data = await GetRecommendation();

            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name == "MGTVVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                    voiceServiceConnection.VoiceCommandCompleted += VoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                        case "ShowSomeMovies":
                            string content = string.Empty;
                            if (voiceCommand.Properties.ContainsKey("content"))
                            {
                                content = voiceCommand.Properties["content"][0];
                            }
                            SendCompletionMessageForSearchContent(content);
                            break;

                        // As a last resort launch the app in the foreground
                        default:
                            LaunchAppInForeground();
                            break;
                    }
                }
                finally
                {
                    if (this.serviceDeferral != null)
                    {
                        //Complete the service deferral
                        this.serviceDeferral.Complete();
                    }
                }
            }
        }
Exemplo n.º 42
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            _taskDerral = taskInstance.GetDeferral();
            var details = taskInstance.TriggerDetails as AppServiceTriggerDetails;

                        // 验证是否调用了正确的app service
                        if(details == null || details.Name != "CortanaService")
            {
                _taskDerral.Complete();
                return;
            }

            _serviceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(details);
                        var cmd = await _serviceConnection.GetVoiceCommandAsync();

            _taskDerral.Complete();
        }
    async Task ProcessVoiceCommandAsync(List<ServiceInfoWithLocation> serviceInfoList,
      VoiceCommandServiceConnection voiceConnection)
    {
      var command = await voiceConnection.GetVoiceCommandAsync();

      switch (command.CommandName)
      {
        case VOICE_COMMAND_SHOW_LIGHTS:
          await ProcessShowLightsCommandAsync(serviceInfoList, voiceConnection);
          break;
        case VOICE_COMMAND_SWITCH_LIGHT:
          await ProcessSwitchLightCommandAsync(serviceInfoList, voiceConnection);
          break;
        default:
          break;
      }
    }
Exemplo n.º 44
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name == "HolVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;


                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                    case "SayHello":

                        var userMessage = new VoiceCommandUserMessage();
                        userMessage.DisplayMessage = "お店で合言葉話してね。";
                        userMessage.SpokenMessage  = "ごきげんよう。";

                        var response = VoiceCommandResponse.CreateResponse(userMessage);

                        await voiceServiceConnection.ReportSuccessAsync(response);

                        break;


                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Handling Voice Command failed " + ex.ToString());
                }
            }
        }
Exemplo n.º 45
0
        protected override async void OnRun(IBackgroundTaskInstance taskInstance)
        {
            this.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 "getPatientData":
                        userMessage = new VoiceCommandUserMessage();
                        userMessage.SpokenMessage = "Here is the Patient Data";

                        var responseMessage = new VoiceCommandUserMessage();
                        responseMessage.DisplayMessage = responseMessage.SpokenMessage = "Patient Name: John Spartan\nAge: 47\nBlood Type: O+\nPatient ID: 000S00117";

                        response = VoiceCommandResponse.CreateResponse(responseMessage);
                        await voiceServiceConnection.ReportSuccessAsync(response);

                        break;

                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            finally
            {
                if (this.serviceDeferral != null)
                {
                    //Complete the service deferral
                    this.serviceDeferral.Complete();
                }
            }
        }
Exemplo n.º 46
0
        /// <summary>
        /// The background task entrypoint.
        ///
        /// 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/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.
        ///
        /// The Cortana UI is dismissed if Cortana loses focus.
        /// The background task is also dismissed even if being debugged.
        /// Use of Remote Debugging is recommended in order to debug background task behaviors.
        /// 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)
        {
            // Create the deferral by requesting it from the task instance
            serviceDeferral = taskInstance.GetDeferral();

            AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name.Equals("VitWifiVoiceCommandService"))
            {
                voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);

                VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                // Perform the appropriate command depending on the operation defined in VCD
                switch (voiceCommand.CommandName)
                {
                case "Login":
                    string x = NetworkNames.ToString();
                    VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
                    userMessage.DisplayMessage = string.Format("The current networks is {0} ", x);
                    userMessage.SpokenMessage  = string.Format("The current networks is {0} ", x);

                    VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage, null);
                    await voiceServiceConnection.ReportSuccessAsync(response);

                    break;

                case "Logout":
                    string logoutMessage = NetworkNames.ToString();
                    VoiceCommandUserMessage userLogoutMessage = new VoiceCommandUserMessage();
                    userLogoutMessage.DisplayMessage = string.Format("The current networks is {0} ", logoutMessage);
                    userLogoutMessage.SpokenMessage  = string.Format("The current networks is {0} ", logoutMessage);
                    VoiceCommandResponse logoutResponse = VoiceCommandResponse.CreateResponse(userLogoutMessage, null);
                    await voiceServiceConnection.ReportSuccessAsync(logoutResponse);

                    break;

                default:
                    break;
                }
            }

            // Once the asynchronous method(s) are done, close the deferral
            serviceDeferral.Complete();
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            _deferral = taskInstance.GetDeferral();

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name == "FeedReaderVoiceCommandService")
            {
                _voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                _voiceServiceConnection.VoiceCommandCompleted += VoiceServiceConnection_VoiceCommandCompleted;
                var voiceCommand = await _voiceServiceConnection.GetVoiceCommandAsync();

                if (voiceCommand?.CommandName == "ShowLatestNews")
                {
                    await ShowLatestNews();
                }
            }
        }
Exemplo n.º 48
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral        = taskInstance.GetDeferral();
            taskInstance.Canceled += OnTaskCanceled;
            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name == "CodecampSessionsVoiceCommandService")
            {
                voiceServiceConnection =
                    VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                        triggerDetails);

                voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                await _agendaService.GetSessionsAsync();

                switch (voiceCommand.CommandName)
                {
                case "sayPresentationDescription":
                    var userMessage = new VoiceCommandUserMessage();
                    userMessage.DisplayMessage = "You already forgot? You are going to talk about how I can help developers to create voice activated apps";
                    userMessage.SpokenMessage  = "You already forgot? You are going to talk about how I can help developers to create voice activated apps. By the way...asshole, stop forcing me to help you with this stupid presentation. You're lucky I can't use curse words";
                    var response = VoiceCommandResponse.CreateResponse(userMessage);
                    await voiceServiceConnection.ReportSuccessAsync(response);

                    break;

                case "findSessionsWithCortana":
                    var tags = voiceCommand.SpeechRecognitionResult.SemanticInterpretation.Properties["search"][0];
                    await FindSessionsByTag(tags);

                    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;
                }
            }
        }
Exemplo n.º 49
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            
            addressTable = MobileService.GetTable<Address>();
            serviceDeferral = taskInstance.GetDeferral();
            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null &&
              triggerDetails.Name == "AlternaService")
            {
                try
                {
                    voiceServiceConnection =
                      VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                        triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted +=
                      VoiceCommandCompleted;

                    VoiceCommand voiceCommand = await
                    voiceServiceConnection.GetVoiceCommandAsync();

                    switch (voiceCommand.CommandName)
                    {
                        case "showLocations":
                            {
                                SendCompletionMessageForLocations();
                                break;
                            }

                        // As a last resort launch the app in the foreground
                        default:
                            LaunchAppInForeground();
                            break;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Handling Voice Command failed " + ex.ToString());
                }
            }
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral        = taskInstance.GetDeferral();
            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            // This should match the uap:AppService and VoiceCommandService references from the
            // package manifest and VCD files, respectively. Make sure we've been launched by
            // a Cortana Voice Command.
            if (triggerDetails.Name == "PingPongVoiceCommandService102040")
            {
                try
                {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();


                    switch (voiceCommand.CommandName)
                    {
                    case "sendPingPong":
                        var t = voiceCommand.Properties["type"][0];
                        await DoItAsync(t);
                        await SendCompletionMessageForDestination(t + $" ({code})");

                        break;

                    default:
                        LaunchAppInForeground();
                        break;
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Handling Voice Command failed " + ex.ToString());
                }
                finally
                {
                    CleanUpDeferral();
                }
            }
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral = taskInstance.GetDeferral();
            taskInstance.Canceled += OnTaskCanceled;
            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
            if (triggerDetails != null && triggerDetails.Name == "CodecampSessionsVoiceCommandService")
            {
                voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                await _agendaService.GetSessionsAsync();
                switch (voiceCommand.CommandName)
                {
                    case "sayPresentationDescription":
                        var userMessage = new VoiceCommandUserMessage();
                        userMessage.DisplayMessage = "You already forgot? You are going to talk about how I can help developers to create voice activated apps";
                        userMessage.SpokenMessage = "You already forgot? You are going to talk about how I can help developers to create voice activated apps. By the way...asshole, stop forcing me to help you with this stupid presentation. You're lucky I can't use curse words";
                        var response = VoiceCommandResponse.CreateResponse(userMessage);
                        await voiceServiceConnection.ReportSuccessAsync(response);
                        break;
                    case "findSessionsWithCortana":
                        var tags = voiceCommand.SpeechRecognitionResult.SemanticInterpretation.Properties["search"][0];
                        await FindSessionsByTag(tags);
                        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;
                }
            }
        }
Exemplo n.º 52
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            BackgroundTaskDeferral _defferal = taskInstance.GetDeferral();

            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetals = taskInstance.TriggerDetails as AppServiceTriggerDetails;
            if (triggerDetals != null && triggerDetals.Name == "ArcherVM.CortanaPlusEndpoint")
            {
                try
                {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetals);
                    voiceServiceConnection.VoiceCommandCompleted += VoiceServiceConnection_VoiceCommandCompleted;
                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();
                    switch (voiceCommand.CommandName)
                    {
                        case "openArcherVM":
                            var app = voiceCommand.Properties["App"][0];


                            var userMessage = new VoiceCommandUserMessage();
                            userMessage.DisplayMessage = "Here's your web app.";
                            userMessage.SpokenMessage = "Let me get Malaika to open up " + app + " for you.";
                            VoiceCommandResponse vcr = VoiceCommandResponse.CreateResponse(userMessage);
                            break;





                    }

                    }catch (Exception e)
                { }
            }

        }
        /// <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;

            // This should match the uap:AppService and VoiceCommandService 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 == "BandOnTheRunVoiceCommandService")
            {
                try
                {
                    voiceServiceConnection =
                        VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
                            triggerDetails);

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();
                    VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();

                    // Depending on the operation (defined in AdventureWorks:AdventureWorksCommands.xml)
                    // perform the appropriate command.
                    switch (voiceCommand.CommandName)
                    {
                        case "showbandinformation":
                            //hardcoded - needs to be hooked into real data flow.
                            userMessage.DisplayMessage = "Band 1 \n" +
                                                         "status: connected\n" +
                                                          "Motion: Jogging\n" +
                                                          "Speed: 10kph\n" +
                                                          "Skin Temp: 37\n" +
                                                           "UV: medium";
                            userMessage.SpokenMessage = "Showing band information";
;
                            var response = VoiceCommandResponse.CreateResponse(userMessage);
                            await voiceServiceConnection.ReportSuccessAsync(response);
                    
                            break;
                 
                        default:
                     
                            break;
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Handling Voice Command failed " + ex.ToString());
                }
                finally
                {
                    if (this.serviceDeferral != null)
                    {
                        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());
                }
            }
        }
Exemplo n.º 55
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            string textSpoken = string.Empty;
            serviceDeferral = taskInstance.GetDeferral();
            taskInstance.Canceled += TaskInstance_Canceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (triggerDetails != null && triggerDetails.Name == "CortanaPediaVoiceCommandService")
            {

                try
                {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                    voiceServiceConnection.VoiceCommandCompleted += VoiceServiceConnection_VoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    // Get the name of the voice command and the text spoken
                    string voiceCommandName = voiceCommand.SpeechRecognitionResult.RulePath[0];
                    textSpoken = voiceCommand.SpeechRecognitionResult.Text;

                    // The commandMode is either "voice" or "text", and it indicates how the voice command was 
                    //entered by the user.
                    // Apps should respect "text" mode by providing feedback in a silent form.
                    // string commandMode = SemanticInterpretation("commandMode", voiceCommand.SpeechRecognitionResult);


                    var Query = voiceCommand.SpeechRecognitionResult.SemanticInterpretation.Properties["SearchTerms"][0];

                    switch (voiceCommand.CommandName)
                    {
                        case "CortanaPediaImages":
                            {
                                ShowResultsInCortana(Query);
                                break;
                            }

                    }

                }
                catch (Exception)
                {

                    throw;
                }
                finally
                {

                    if (this.serviceDeferral != null)
                    {
                        this.serviceDeferral.Complete();
                    }

                }


            }



        }
Exemplo n.º 56
0
        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();
        }
    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)
        {
            Country selectedcountry;

            // We need deferal
            // On a besoin de deferal
            _serviceDeferral = taskInstance.GetDeferral();

            // If cancelled, set deferal
            // Mets le déféral si annulation
            taskInstance.Canceled += (sender, reason) => _serviceDeferral?.Complete();

            // Get the details of the event that trigered the service
            // Obtient les détails de l'évenement qui à démarré le service
            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            // Check if it is service name set in VCD
            // Regarde si c'est le nom du service qui est mis dans le VCD
            if (triggerDetails?.Name == "FlagoramaService")
            {
                _voiceCommandServiceConnection =
                    VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
                // Set deferal when voice command is completed
                // Mets le deferal quand la commande vocale est terminée
                _voiceCommandServiceConnection.VoiceCommandCompleted += (sender, args) => _serviceDeferral?.Complete();
                // Get voice command
                // Obtient la commande vocale
                var voicecommand = await _voiceCommandServiceConnection.GetVoiceCommandAsync();

                switch (voicecommand.CommandName)
                {
                    case "showFlagList":
                        // Show flag list
                        selectedcountry = await ShowFlagListAsync();
                        if (selectedcountry != null)
                        {
                            // If one flag is selected, ask confirmation
                            if (await PromptForConfirmationAsync(selectedcountry))
                            {
                                // Send success message with selected flag
                                await SendSuccessMessageAsync(selectedcountry);
                            }
                        }
                        break;
                    case "showFlag":
                        await SendErrorMessageAsync();
                        await Task.Delay(2000);
                        // Get country flags with selected name
                        var flags = Countries.List.Where(
                                d => d.Name.ToLower().Contains(voicecommand.Properties["flag"][0].ToLower())).ToList();
                        // If more than one, disambiguate
                        if (flags.Count() == 1)
                            selectedcountry = flags.First();
                        else
                        {
                            selectedcountry = await DisambiguateCountry(flags);
                        }
                        // Show progress message for 2 seconds, then show flag
                        await SendProgressMessageAsync(selectedcountry);
                        await Task.Delay(20000);
                        await SendSuccessMessageAsync(selectedcountry);
                        break;
                }
            }
        }
        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();
                    }
                }
            }
        }
        /// <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;

            // This should match the uap:AppService and VoiceCommandService references from the 
            // package manifest and VCD files, respectively. Make sure we've been launched by
            // a Cortana Voice Command.
            string actualName = triggerDetails.Name;
            string expectedName = typeof(TheVoiceCommandService).ToString();
            bool launchedByCortanaVoiceCommand = triggerDetails != null && expectedName.Contains( actualName );
            if ( launchedByCortanaVoiceCommand ) {
                try {
                    voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails( triggerDetails );
                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;
                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();


                    switch (voiceCommand.CommandName) {
                        default:
                            LaunchAppInForeground();
                            break;
                    }
                }
                catch (Exception ex) {
                    System.Diagnostics.Debug.WriteLine("Handling Voice Command failed " + ex.ToString());
                }
            }
        }