Created
August 4, 2013 23:01
-
-
Save DevJohnC/6152415 to your computer and use it in GitHub Desktop.
Adjutant shutdown speech command example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void SetupSpeechCommands() | |
| { | |
| var recognizer = Speech.GetRecognizer(); | |
| if (recognizer == null) | |
| { | |
| throw new SpeechRecognitionException("Recognizer not available."); | |
| } | |
| var locationNames = new List<string>(); | |
| foreach (var location in Locations.Get()) | |
| locationNames.Add(location.Name); | |
| var computerNames = new List<string>(); | |
| foreach (var computer in Computer.Lookup(NodeType.Client | NodeType.Satellite)) | |
| computerNames.Add(computer.DeviceName); | |
| var builder = new GrammarBuilder(); | |
| builder.Append("shutdown"); | |
| var computersGrammar = new GrammarBuilder(); | |
| computersGrammar.Append(new Choices(computerNames.ToArray())); | |
| builder.Append(new SemanticResultKey("computer", computersGrammar)); | |
| var locationsGrammar = new GrammarBuilder(); | |
| locationsGrammar.Append(new Choices(locationNames.ToArray())); | |
| builder.Append(new SemanticResultKey("location", locationsGrammar)); | |
| var shutdownLocationCommand = new VoiceCommand(); | |
| shutdownLocationCommand.Grammar = new Grammar(builder); | |
| shutdownLocationCommand.Recognized += (sender, e) => | |
| { | |
| var shutdownList = new List<Computer>(); | |
| if (e.Result.Semantics.ContainsKey("computer")) | |
| { | |
| shutdownList.Add(Computer.Lookup(e.Result.Semantics["computer"].Text)); | |
| } | |
| else if (e.Result.Semantics.ContainsKey("location")) | |
| { | |
| var location = Locations.Get(e.Result.Semantics["location"].Text); | |
| var computers = Computer.Lookup(location); | |
| foreach(var computer in computers) | |
| shutdownList.Add(computer); | |
| } | |
| foreach (var target in shutdownList) | |
| target.Shutdown(); | |
| }; | |
| recognizer.AddGrammar(shutdownLocationCommand); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment