//This method handle the click event on the Stop Detection button private void Stop_Detection_Button_Click(object sender, RoutedEventArgs e) { //Critical Section that have to be executed atomically lock (_lockObject) { //Instantly stop the execution of the C++ code Synchronizer.StopEngine(configuration); //Stopping the timer needed to update the graphs update_Graph.Stop(); update_Graph.Tick -= Update_Graph_handler; update_Stats.Stop(); update_Stats.Tick -= Update_Stats_handler; //Here i put a point to create a discontinues range of interval CoT_devices.Values.Add(new DateTimePoint(last_detection, double.NaN)); //Change the button visibility in the window and the status of the detection this.stop_detection_button.Visibility = Visibility.Hidden; this.restart_detection_button.Visibility = Visibility.Visible; no_detection = true; detection_mode = Features.Detection_Status.Temporarly_Stopped; this.detection_status.Text = detection_mode.ToString().Replace("_", " "); this.detection_status.Foreground = Brushes.Orange; //Starting the timer needed to understand if we are in presence of timeout timeout.Tick += Timeout_handler; timeout.Interval = new TimeSpan(0, 5, 0); timeout.Start(); } return; }
//This is the method that have to be called when there is a DB error connection public void Handle_DB_Error() { //Critical section before closing this window after a DB error lock (_lockObject) { Synchronizer.StopEngine(configuration); this.StopAllTimers(); //Here I change the window status label and layout detection_mode = Features.Detection_Status.Stopped; this.detection_status.Text = detection_mode.ToString(); this.detection_status.Foreground = Brushes.Red; } if (Features.openedWindow.Equals(Features.Window_type.Statistic)) { MessageBoxResult result = MessageBox.Show("We are sorry!\n" + "Something went wrong with the connection with the database!\n" + "Do you want to try to re-connect to the database?", "Reconnection to database", MessageBoxButton.YesNo); if (result == MessageBoxResult.No) { this.Close(); Environment.Exit(-1); } else { this.BackToMain(); } } }
//This method do all the operations needed in case of c++ errors private void Handle_Cpp_Error(Synchronizer.Reason type, string errorMessage) { lock (_lockObject) { if (Features.openedWindow.Equals(Features.Window_type.Statistic)) { Synchronizer.StopEngine(configuration); //Here I stop all the timers usedd for updates this.StopAllTimers(); detection_mode = Features.Detection_Status.Stopped; this.detection_status.Text = detection_mode.ToString(); this.detection_status.Foreground = Brushes.Red; MessageBoxResult result = MessageBox.Show("We are sorry, something went wrong!\n" + "Do you want to go back to main?", "Reconnection to database", MessageBoxButton.YesNo); if (result.Equals(MessageBoxResult.No)) { this.Close(); Environment.Exit(-1); } else { this.BackToMain(); } } } }
//This method handle the timeout event raised by the timer setted by the stop detection button click private void Timeout_handler(object sender, EventArgs e) { //Update of the detection status detection_mode = Features.Detection_Status.Stopped; this.detection_status.Text = detection_mode.ToString(); this.detection_status.Foreground = Brushes.Red; //Clear the DM representation DM_positions.Values.Clear(); //Turn the numbers and the percentage to 0 local_MACs.Text = "0"; distinct_MACs.Text = "0"; percentage_local_MACs.Text = "0 %"; }
//This method handle the click event on the Close button private void Close_Button_Click(object sender, RoutedEventArgs e) { if (no_detection) { //Stopping all the timers to avoid any possible event call this.StopAllTimers(); } else { MessageBoxResult question = MessageBox.Show("The application is still detecting data coming from the boards!\n" + "Would you stop anyway the detection and go back to main?", "Continue the detection", MessageBoxButton.YesNo); if (question == MessageBoxResult.Yes) { //Critical Section that have to be executed atomically lock (_lockObject) { //Instantly stop the C++ code and taking the error handler away before closing Synchronizer.StopEngine(configuration); //Change the status of the detection detection_mode = Features.Detection_Status.Stopped; this.detection_status.Text = detection_mode.ToString(); this.detection_status.Foreground = Brushes.Red; //Stopping all the timers to avoid any possible event call this.StopAllTimers(); } } else { return; } } this.BackToMain(); }
//This method handle the click event on the Restart Detection button private void Restart_Detection_Button_Click(object sender, RoutedEventArgs e) { //Critical Section that have to be executed atomically lock (_lockObject) { try { //Stopping the timer needed to understand if we are in presence of timeout timeout.Stop(); timeout.Tick -= Timeout_handler; //Instantly re-starting the execution of the C++ code Synchronizer.StartEngine(configuration, Handle_Cpp_Error); //Change the button visibility in the window this.restart_detection_button.Visibility = Visibility.Hidden; this.stop_detection_button.Visibility = Visibility.Visible; no_detection = false; detection_mode = Features.Detection_Status.Running; this.detection_status.Text = detection_mode.ToString(); this.detection_status.Foreground = Brushes.Green; //Update the activity timeline dictionary activity_timeline = Log.GetEngineActivityTimeline(configuration, true); //Update the maximum of all the sliders double max = activity_timeline.Sum(t => t.Value.TotalSeconds); DM_slider.Maximum = max; CoT_rangeSlider.Maximum = max; FM_rangeSlider.Maximum = max; //Updating the tick frequency of the sliders DM_slider.TickFrequency = Features.percentageUpgradeSteps * DM_slider.Maximum; CoT_rangeSlider.TickFrequency = Features.percentageUpgradeSteps * CoT_rangeSlider.Maximum; FM_rangeSlider.TickFrequency = Features.percentageUpgradeSteps * FM_rangeSlider.Maximum; //Here I update the splitters Statistic_Auxiliary.SplittersDisplay(this, activity_timeline); //Check if I have to update the graphs if (DM_realtime) { DM_slider.Value = DM_slider.Maximum; } if (CoT_realtime) { //Here I compute the last activity range KeyValuePair <DateTime, TimeSpan> lastActivityRange = activity_timeline.OrderByDescending(t => t.Key).First(); //Here I update the CoT slider values, without the raising of the event CoT_rangeSlider.HigherValue = CoT_rangeSlider.Maximum; CoT_rangeSlider.LowerValue = activity_timeline.Values.Sum(cot => cot.TotalSeconds) - lastActivityRange.Value.TotalSeconds; } //Here I set the new last detection to the period of restart detection last_detection = DateTime.Now; //Re-starting the timer needed to update the graphs this.ActiveUpdateTimers(); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); Console.WriteLine(ex.Message); Handle_DB_Error(); } } return; }
//This method initialize the entire statistic window private void OnLoadEvent(object sender, RoutedEventArgs e) { try { Synchronizer.StartEngine(configuration, Handle_Cpp_Error); //Changing the detection mode and representation detection_mode = Features.Detection_Status.Running; this.detection_status.Text = detection_mode.ToString(); this.detection_status.Foreground = Brushes.Green; no_detection = false; //Setting the default current view at start current_view = this.device_distribution.Name; //Store the activity timeline activity_timeline = Log.GetEngineActivityTimeline(configuration, true); } catch (System.Data.Entity.Core.ProviderIncompatibleException dex) { Console.WriteLine(dex.StackTrace); Console.WriteLine(dex.Message); Handle_DB_Error(); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); Console.WriteLine(ex.Message); //Here I stop the engine of the C++ code Synchronizer.StopEngine(configuration); MessageBox.Show("An unknown error occur during the initialization of the statistic window!\n" + "The application will be closed!"); Environment.Exit(-1); } //Initialization of all the axis and series of the views Axis_Initialize(); LineSeries_Initialize(); //Here I compute the last activity range KeyValuePair <DateTime, TimeSpan> lastActivityRange = activity_timeline.OrderByDescending(t => t.Key).First(); //Initialize the device monitoring representation for (int i = 0; i < configuration_boards.Count; i++) { DM_polygon.Values.Add(new ObservablePoint(configuration_boards[i].X, configuration_boards[i].Y)); } if (configuration_boards.Count > 2) { DM_polygon.Values.Add(new ObservablePoint(configuration_boards[0].X, configuration_boards[0].Y)); } Axis_Focus_Point(); //Only if the mode is Modify or Load we display the previous data detected if (opening_mode == Features.Window_Mode.Load || opening_mode == Features.Window_Mode.Modify) { DateTime lastActivity = activity_timeline.OrderBy(t => t.Key).Last().Key; foreach (KeyValuePair <DateTime, TimeSpan> tuple in activity_timeline.OrderBy(t => t.Key)) { //Initialize all the sliders in the representations DM_slider.Maximum += tuple.Value.TotalSeconds; CoT_rangeSlider.Maximum += tuple.Value.TotalSeconds; FM_rangeSlider.Maximum += tuple.Value.TotalSeconds; //Initialize the counting over time representation if (tuple.Key != lastActivity && tuple.Value.TotalSeconds >= Features.delayUpdateGraphs) { int iteration = (int)tuple.Value.TotalSeconds / Features.delayUpdateGraphs; for (int i = 1; i <= iteration; i++) { try { last_detection = tuple.Key.AddSeconds(i * Features.delayUpdateGraphs); devices_position = dataCaching.LastPosition_List(last_detection); CoT_devices.Values.Add(new DateTimePoint(last_detection, devices_position.Count)); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); Console.WriteLine(ex.Message); Handle_DB_Error(); } } CoT_devices.Values.Add(new DateTimePoint(last_detection, double.NaN)); } } //Updating the tick frequency of the sliders DM_slider.TickFrequency = Features.percentageUpgradeSteps * DM_slider.Maximum; CoT_rangeSlider.TickFrequency = Features.percentageUpgradeSteps * CoT_rangeSlider.Maximum; FM_rangeSlider.TickFrequency = Features.percentageUpgradeSteps * FM_rangeSlider.Maximum; //Here I add the splitters Statistic_Auxiliary.SplittersDisplay(this, activity_timeline); //Here I initialize the statistics at startup try { //Here I generate the data needed for update the 3 statistics int totalDistinctMACs, local_MACs, distinct_MACs, percentage_MACs; totalDistinctMACs = dataCaching.TotalMAC_DistinctNumber(); local_MACs = dataCaching.LocalMAC_Number(); distinct_MACs = dataCaching.LocalMAC_DistinctNumber(); if (local_MACs == 0) { percentage_MACs = 0; } else { percentage_MACs = (int)(distinct_MACs * 100 / local_MACs); } //Here I update the text of the labels showing the statistics this.total_distinct_MACs.Text = totalDistinctMACs.ToString(); this.local_MACs.Text = local_MACs.ToString(); this.distinct_MACs.Text = distinct_MACs.ToString(); this.percentage_local_MACs.Text = percentage_MACs.ToString() + " %"; } catch (Exception ex) { Console.WriteLine(ex.StackTrace); Console.WriteLine(ex.Message); Handle_DB_Error(); } } else { //Initialize the date of the representations double max = activity_timeline.Sum(t => t.Value.TotalSeconds); DM_slider.Maximum = max; CoT_rangeSlider.Maximum = max; FM_rangeSlider.Maximum = max; //Here I update the Min and Max values of the Axis of the CoT graph at startup Statistic_Auxiliary.UpdateCoTAxisConfiguration(this, activity_timeline); } //Here I update DM_slider thumb DM_slider.Value = DM_slider.Maximum; //Here I update the CoT and FM slider values double sum = activity_timeline.Sum(x => x.Value.TotalSeconds); CoT_rangeSlider.HigherValue = CoT_rangeSlider.Maximum; CoT_rangeSlider.LowerValue = sum - lastActivityRange.Value.TotalSeconds; DateTime date_min = Statistic_Auxiliary.Translate_Slider_Value(FM_rangeSlider.LowerValue, activity_timeline); DateTime date_max = Statistic_Auxiliary.Translate_Slider_Value(FM_rangeSlider.HigherValue, activity_timeline); FM_rangeSlider.HigherValue = FM_rangeSlider.Maximum; FM_rangeSlider.LowerValue = sum - lastActivityRange.Value.TotalSeconds; FM_date_start.Text = date_min.ToString(); FM_date_end.Text = date_max.ToString(); last_detection = DateTime.Now; //Set the operation for the first update graph event call this.ActiveUpdateTimers(); return; }