//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; }
//Managing the resize of the window private void ResizedWindow_Event(object sender, EventArgs e) { Statistic_Auxiliary.SplittersDisplay(this, activity_timeline); }
//This method handle the event raised by the timer setted for the update of the grid views private void Update_Graph_handler(object sender, EventArgs e) { DateTime now = DateTime.Now; //Here I update all the sliders DM_slider.Maximum += now.Subtract(last_detection).TotalSeconds; CoT_rangeSlider.Maximum += now.Subtract(last_detection).TotalSeconds; FM_rangeSlider.Maximum += now.Subtract(last_detection).TotalSeconds; //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 timespan of the last entry of the activity timeline dictionary KeyValuePair <DateTime, TimeSpan> lastActivityRange = activity_timeline.OrderByDescending(t => t.Key).First(); DateTime lastActivityDate = lastActivityRange.Key; activity_timeline[lastActivityDate] = now.Subtract(lastActivityDate); //Here I update the splitters Statistic_Auxiliary.SplittersDisplay(this, activity_timeline); //Here I save the last detection of reference last_detection = DateTime.Now; //Here I update the slider of the DM representation if (DM_realtime) { DM_slider.Value = DM_slider.Maximum; } //Here I add some data to the counting over time graph try { 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(); } //Here I update the slider of the CoT representation if (CoT_realtime) { CoT_rangeSlider.HigherValue = CoT_rangeSlider.Maximum; if (activity_timeline[lastActivityDate].TotalSeconds < Features.MaxMinDifferenceRange) { CoT_rangeSlider.LowerValue = activity_timeline.Values.Sum(cot => cot.TotalSeconds) - activity_timeline[lastActivityDate].TotalSeconds; } else { CoT_rangeSlider.LowerValue = activity_timeline.Values.Sum(cot => cot.TotalSeconds) - Features.MaxMinDifferenceRange; } } //At the end of all operations this handler set again the timer after what to retrive the new data update_Graph.Interval = new TimeSpan(0, 0, Features.delayUpdateGraphs); update_Graph.Start(); }
//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; }