예제 #1
0
        private void Engine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            // Handle special Action Start.
            if (string.Equals(e.Result.Text, InitiateCommandsPhrase, StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine("Acknowledged...");
                ComputerFeedbackPlayer.PlayComputerInit();
                CommandInitiated = true;
            }
            // Handle special action Cancel Override.
            else if (string.Equals(e.Result.Text, CancelProgramCommand, StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine("Program Quit Detected.");
                speechCancellationTokenSource.Cancel();
            }
            else
            {
                if (!CommandInitiated)
                {
                    // Ignore matching commands if Commands are not initiated by start command.
                    return;
                }

                var semantics = e.Result.Semantics;

                if (!semantics.ContainsKey(CommandSubjectSemanticKey))
                {
                    Console.WriteLine("Grammar recognized but no subject was found on which to take an action.");
                    //throw new Exception("Grammar recognized but no subject was found on which to take an action.");
                }

                var subject = e.Result.Semantics[CommandSubjectSemanticKey];

                // TODO this should be configured not hardcoded for when there are more than just light actions.
                if (Equals(subject.Value, LightActionSpec.LightSubjectSemanticValue))
                {
                    ComputerFeedbackPlayer.PlayComputerAck();

                    if (!semantics.ContainsKey(LightIdentifierSemanticKey) || !semantics.ContainsKey(LightActionSemanticKey))
                    {
                        Console.WriteLine("A command with the light subject must contain a light identifier and action semantic.");
                        //throw new Exception("A command with the light subject must contain a light identifier and action semantic.");
                    }

                    // TODO consider also making this configured by the action spec itself.  Something like expected Semantic Keys.
                    var identifier = e.Result.Semantics[LightIdentifierSemanticKey];
                    var action     = e.Result.Semantics[LightActionSemanticKey];

                    LightActionSpec.ExecuteAction((string)action.Value, (string)identifier.Value);

                    Console.WriteLine("Grammer match: {0}", e.Result.Text);
                    Console.WriteLine("subject:{0}, action:{1}, identifier:{2}", subject.Value, action.Value, identifier.Value);
                    Console.WriteLine("With Confidence {0}", e.Result.Confidence);
                }
                else
                {
                    Console.WriteLine("Subject value {0} was found but can not be bound to an action", subject.Value);
                    //throw new Exception(string.Format("Subject value {0} was found but can not be bound to an action", subject.Value));
                }
            }

            // Do not share a variable with the above code, this is to protect against long processing times.
            SetNewSpeechTime(DateTime.UtcNow);
        }