Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Diagnostics; // Used to access the "Performance Counter".
- using System.Speech.Recognition; // Used to access the Speech API.
- using System.Speech.Synthesis; // Used to access the Speech
- namespace Terminal_47_Alpha
- {
- /// <summary>
- /// Interaction logic for SystemMonitor.xaml
- /// </summary>
- public partial class SystemMonitor : UserControl
- {
- // -------------------------------------------------------------------------------------------------------------------------------------------------------------------
- // Speech Recognition
- private SpeechRecognitionEngine recognitionEngine; // Defines a new instance of the SpeechRecognitionEngine.
- // Speech Synthesis
- SpeechSynthesizer julie = new SpeechSynthesizer(); // Creates the julie instance for Speech Synthesis.
- int Volume = 100; // Sets the Volume level for Speech Synthesis output.
- int Rate = -1; // Sets the Speed for Speech Synthesis output.
- // PerformanceCounter
- string CPUUsage = ""; // Stores the current CPU Usage in Percent.
- string CPUTemp = ""; // Stores the current CPU Temperature in Celcius.
- string RAMUsage = ""; // Stores the current Memory Usage in Percent.
- string RAMAvailable = ""; // Stores the amount of available Memory in Percent.
- PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true); // Pulls the "Processor Usage" from the Performance Counter.
- PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes", true); // Pulls the amount of "Available Memory" from the Performance Counter.
- // commandCodes
- string activeCommandCode = "";
- // -------------------------------------------------------------------------------------------------------------------------------------------------------------------
- public SystemMonitor() // Main Method.
- {
- SystemMonitorTimer(null, null); // Calls the SystemMonitorTimer method.
- loadSpeechRecognitionEngine(null, null); // Calls the loadSpeechRecognitionEngine method.
- // startEngine(null, null); // Starts the SpeechRecognitionEngine.
- this.InitializeComponent(); // Loads Visual Elements. DO NOT REMOVE.
- }
- // -------------------------------------------------------------------------------------------------------------------------------------------------------------------
- // Creates the SystemMonitorTimer
- private void SystemMonitorTimer(object sender, EventArgs e)
- {
- System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer(); // Creates Timer (to refresh the Debug Menu)
- myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1, 0); // (days, hours, minutes, seconds, milliseconds) Int32 datatype. Currently set to update every 995ms (was intially 1000ms, but was changed to reduce lag).
- myDispatcherTimer.Tick += new EventHandler(RefreshData);
- myDispatcherTimer.Start();
- }
- // Called by the SystemMonitorTimer
- private void RefreshData(object sender, EventArgs e)
- {
- GetCPUUsage(null, null);
- GetRAMUsage(null, null);
- // GetHDDUsage(null, null); //Future feature. Gets the usage data for locally attached storage.
- }
- // Used to change the time string. Add's a 0 if the time has only one digit.
- string twoDigit(string input)
- {
- if (input.Length < 2)
- return "o" + " " + input;
- else
- return input;
- }
- // Gets the CPU Usage.
- public void GetCPUUsage(object sender, EventArgs e)
- {
- CPUUsage = Convert.ToInt32(cpuCounter.NextValue()).ToString();
- }
- // Gets the Memory Usage.
- public void GetRAMUsage(object sender, EventArgs e)
- {
- double totalRAM = 1024; // Total Available RAM in Megabytes. TO-DO: Make this configurable outside of program source. 8GB = 8192, 1GB = 1024.
- double availableRAM = Convert.ToInt32(ramCounter.NextValue()); // Gets currently Available Memory in Megabytes.
- double freeRAM = 0; // Stores Available RAM in Megabytes. Used to convert to percentage.
- int percentFree = 0; // RAM Available in percent (%). Must use ".ToString()" when displaying in a textblock.
- int percentUsed = 0; // RAM Usage in percent (%). Must use ".ToString()" when displaying in a textblock.
- freeRAM = (availableRAM / totalRAM) * 100.0; // Find the amount of RAM available and convert to a percent.
- percentFree = Convert.ToInt32(freeRAM); // Stores the percentage of RAM available. "Convert.ToInt" removes the decimal places from the percentage.
- percentUsed = (100 - percentFree); // Stores the percentage of RAM used.
- RAMAvailable = percentFree.ToString(); // Sends the Available RAM percentage with the RAMAvaiable variable.
- RAMUsage = percentUsed.ToString(); // Sends the Used RAM percentage with the RAMUsage variable.
- }
- // Gets the HDD Usage.
- public void GetHDDUsage(object sender, EventArgs e)
- {
- }
- // -------------------------------------------------------------------------------------------------------------------------------------------------------------------
- // Loads the SpeechRecognitionEngine.
- public void loadSpeechRecognitionEngine(object sender, EventArgs e)
- {
- recognitionEngine = new SpeechRecognitionEngine(); // Creates a new instance of the SpeechRecognitionEngine.
- recognitionEngine.SetInputToDefaultAudioDevice(); // Sets the SpeechReognitionEngine Input to the System Default (controlled by OS).
- recognitionEngine.LoadGrammar(CreateMainGrammarObject()); // Loads the Main XML Grammar File.
- recognitionEngine.LoadGrammarCompleted +=new EventHandler<LoadGrammarCompletedEventArgs>(recognitionEngine_LoadGrammarCompleted); // Specifies the Event Handler for the LoadGrammarCompleted event.
- recognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognitionEngine_SpeechRecognized); // Specifies the Event Handler for SpeechRecognized event.
- recognitionEngine.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(recognitionEngine_SpeechRecognitionRejected); // Specifies the Event Handler for SpeechRecognized event.
- recognitionEngine.AudioSignalProblemOccurred += new EventHandler<AudioSignalProblemOccurredEventArgs>(recognitionEngine_AudioSignalProblemOccurred); // Specifies the Event Handler for AudioSignalProblem event.
- }
- // Defines the MainGrammar.
- public Grammar CreateMainGrammarObject()
- {
- Grammar mainGrammar = new Grammar(".//Resources//Config//MainGrammar.xml");
- return mainGrammar;
- }
- // Defines the CommandCodeGrammar.
- public Grammar CreateCommandCodeGrammarObject()
- {
- Grammar commandCodeGrammar = new Grammar(".//Resources//Config//CommandCodeGrammar.xml");
- return commandCodeGrammar;
- }
- // Starts the SpeechRecognitionEngine.
- private void startEngine(object sender, EventArgs e)
- {
- recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
- }
- // Stops the SpeechRecognitionEngine.
- private void stopEngine(object sender, EventArgs e)
- {
- recognitionEngine.RecognizeAsyncStop();
- }
- // Unloads All Loaded Grammars
- private void unloadAllGrammars(object sender, EventArgs e)
- {
- recognitionEngine.UnloadAllGrammars();
- }
- // Called when the LoadGrammarCompleted event is raised.
- void recognitionEngine_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
- {
- }
- // Called when the SpeechRecognized event is raised.
- void recognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
- {
- // Defines what to do when speech is recognized.
- confidenceLevel.Text = e.Result.Confidence.ToString(); // Outputs the Confidence level to a textblock on the UC.
- txtOutput.Text += e.Result.Text;
- txtOutput.Text += Environment.NewLine;
- if (e.Result.Confidence < 0.940f) // Checks to see if Confidence is below the set threshold.
- {
- computerRejected(null, null); // Play the wave file.
- //MessageBox.Show("Confidence hasn't been met" + e.Result.Confidence.ToString()); -- Uncomment for debugging purposes.
- }
- else
- {
- string keyword = "computer ";
- var hearHello = e.Result.Text.ToString() == keyword + "hello";
- var hearGoodMorning = e.Result.Text.ToString() == keyword + "good morning";
- if (hearHello)
- {
- computerAcknowledge(null, null);
- HearGreetingHello(null, null);
- }
- else if (hearGoodMorning)
- {
- computerAcknowledge(null, null);
- }
- }
- }
- // Called when the SpeechRecognitionRejected event is raised.
- void recognitionEngine_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
- {
- }
- // Called when the AudioSignalProblemOccurred event is raised.
- void recognitionEngine_AudioSignalProblemOccurred(object sender, AudioSignalProblemOccurredEventArgs e)
- {
- }
- // Sound Player for Acknowledgement.
- private void computerAcknowledge(object sender, EventArgs e)
- {
- System.Media.SoundPlayer soundAcknowledge = new System.Media.SoundPlayer(); // Creates a sound player for the audio anouncements. Used for Static Audio File Playing.
- soundAcknowledge.SoundLocation = ".\\Resources\\Sounds\\Alerts\\comp_ackn.wav"; // Sets the wave file to play.
- soundAcknowledge.PlaySync(); // Plays the wave file.
- }
- // Sound Player for Rejected.
- private void computerRejected(object sender, EventArgs e)
- {
- System.Media.SoundPlayer soundRejected = new System.Media.SoundPlayer(); // Creates a sound player for the audio anouncements. Used for Static Audio File Playing.
- soundRejected.SoundLocation = ".\\Resources\\Sounds\\Alerts\\comp_deny.wav"; // Sets the wave file to play.
- soundRejected.PlaySync(); // Plays the wave file.
- }
- // -------------------------------------------------------------------------------------------------------------------------------------------------------------------
- // Hear Command Code Accepted Acknowledgement.
- private void commandCodeAccepted(object sender, EventArgs e)
- {
- julie.Volume = Volume;
- julie.Rate = Rate;
- julie.SpeakAsync("Command Code Accepted.");
- }
- // Hear Command Code Rejected Acknowledgement.
- private void commandCodeDenied(object sender, EventArgs e)
- {
- julie.Volume = Volume;
- julie.Rate = Rate;
- julie.SpeakAsync("Command Code Denied.");
- }
- // Hear the CPU Usage.
- private void HearCPUUsage(object sender, EventArgs e)
- {
- julie.Volume = Volume;
- julie.Rate = Rate;
- if ((activeCommandCode == "level_1") | (activeCommandCode == "level_2") | (activeCommandCode == "level_3")) // Checks to see what command code the user has.
- {
- julie.SpeakAsync("Core processor usage is at " + CPUUsage.ToString() + "percent."); // Synthesised speech.
- }
- }
- // Hear the Memory Usage.
- private void HearRAMUsage(object sender, EventArgs e)
- {
- julie.Volume = Volume;
- julie.Rate = Rate;
- julie.SpeakAsync("Core memory usage is at " + RAMUsage.ToString() + "percent."); // Synthesised speech.
- }
- // Hear the Time.
- private void HearCurrentTime(object o, EventArgs sender)
- {
- string hour = twoDigit(DateTime.Now.Hour.ToString()); // Gets the hour.
- string minute = twoDigit(DateTime.Now.Minute.ToString()); // Gets the minute.
- string second = twoDigit(DateTime.Now.Second.ToString()); // Gets the second.
- string combined = hour + " " + minute; // Combines the time into a string with white space to avoid the time being synthesised as one word.
- julie.Volume = Volume;
- julie.Rate = Rate;
- julie.SpeakAsync("The time is " + combined + "hours"); // Synthesised speech.
- }
- // Plays the Hello Greeting.
- private void HearGreetingHello(object sender, EventArgs e)
- {
- julie.Volume = Volume;
- julie.Rate = Rate;
- julie.SpeakAsync("Hello" + Config.currentUsernameSetting);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement