private FrameworkElement CreatePieCharts(DeviceStatisticsHelper info)
        {
            Grid chartgrid = new Grid();

            chartgrid.RowDefinitions.Add(new RowDefinition());
            chartgrid.RowDefinitions.Add(new RowDefinition());
            chartgrid.ColumnDefinitions.Add(new ColumnDefinition());
            chartgrid.ColumnDefinitions.Add(new ColumnDefinition());

            Chart chart = new Chart();

            chart.Title       = info.Device.FriendlyName + " - Network Layer";
            chart.Style       = (Style)FindResource("compactChart");
            chart.LegendStyle = (System.Windows.Style)FindResource("HiddenLegend");
            BarSeries bs = new BarSeries();

            bs.IndependentValuePath = "Key";
            bs.DependentValuePath   = "Value";
            bs.ItemsSource          = info.NetworkLayer;
            chart.Series.Add(bs);
            chartgrid.Children.Add(chart);
            chart.SetValue(Grid.RowProperty, 0);
            chart.SetValue(Grid.ColumnProperty, 0);
            chart.SetValue(Grid.ColumnSpanProperty, 2);

            LabeledPieChart pchart = new LabeledPieChart();

            pchart.Style = (Style)FindResource("compactLabeledPieChart");
            LabeledPieSeries ps = new LabeledPieSeries();

            pchart.Title                 = info.Device.FriendlyName + " - Transport Layer";
            ps.IndependentValuePath      = "Key";
            ps.DependentValuePath        = "Value";
            ps.ItemsSource               = info.TransportLayer;
            ps.PieChartLabelItemTemplate = (DataTemplate)FindResource("pieChartLabelDataTemplate");
            ps.LabelDisplayMode          = DisplayMode.AutoMixed;
            pchart.Series.Add(ps);
            chartgrid.Children.Add(pchart);
            pchart.SetValue(Grid.RowProperty, 1);
            pchart.SetValue(Grid.ColumnProperty, 0);

            pchart                       = new LabeledPieChart();
            pchart.Style                 = (Style)FindResource("compactLabeledPieChart");
            ps                           = new LabeledPieSeries();
            pchart.Title                 = info.Device.FriendlyName + " - Application Layer";
            ps.IndependentValuePath      = "Key";
            ps.DependentValuePath        = "Value";
            ps.ItemsSource               = info.ApplicationLayer;
            ps.PieChartLabelItemTemplate = (DataTemplate)FindResource("pieChartLabelDataTemplate");
            ps.LabelDisplayMode          = DisplayMode.AutoMixed;
            pchart.Series.Add(ps);
            chartgrid.Children.Add(pchart);
            pchart.SetValue(Grid.RowProperty, 1);
            pchart.SetValue(Grid.ColumnProperty, 1);

            return(chartgrid);
        }
Beispiel #2
0
        private void RegisterDeviceStatistics()
        {
            long now = m_useLocalClockAsRealTime ? DateTime.UtcNow.Ticks : 0L;

            Dictionary<Guid, DeviceStatisticsHelper<SubscribedDevice>> subscribedDevicesLookup;
            List<DeviceStatisticsHelper<SubscribedDevice>> subscribedDevices;
            ISet<string> subscribedDeviceNames;
            ISet<string> definedDeviceNames;

            DataSet dataSource;
            DataRow[] measurementRows;
            Guid signalID;

            try
            {
                dataSource = DataSource;

                if ((object)dataSource == null || !dataSource.Tables.Contains("InputStreamDevices"))
                {
                    if ((object)m_statisticsHelpers != null)
                    {
                        foreach (DeviceStatisticsHelper<SubscribedDevice> statisticsHelper in m_statisticsHelpers)
                            statisticsHelper.Device.Dispose();
                    }

                    m_statisticsHelpers = new List<DeviceStatisticsHelper<SubscribedDevice>>();
                    m_subscribedDevicesLookup = new Dictionary<Guid, DeviceStatisticsHelper<SubscribedDevice>>();
                }
                else
                {
                    subscribedDevicesLookup = new Dictionary<Guid, DeviceStatisticsHelper<SubscribedDevice>>();
                    subscribedDevices = new List<DeviceStatisticsHelper<SubscribedDevice>>();
                    subscribedDeviceNames = new HashSet<string>();
                    definedDeviceNames = new HashSet<string>();

                    foreach (DataRow deviceRow in dataSource.Tables["InputStreamDevices"].Select($"ParentID = {ID}"))
                        definedDeviceNames.Add($"LOCAL${deviceRow["Acronym"].ToNonNullString()}");

                    if ((object)m_statisticsHelpers != null)
                    {
                        foreach (DeviceStatisticsHelper<SubscribedDevice> statisticsHelper in m_statisticsHelpers)
                        {
                            if (definedDeviceNames.Contains(statisticsHelper.Device.Name))
                            {
                                subscribedDevices.Add(statisticsHelper);
                                subscribedDeviceNames.Add(statisticsHelper.Device.Name);
                            }
                            else
                            {
                                statisticsHelper.Device.Dispose();
                            }
                        }
                    }

                    foreach (string definedDeviceName in definedDeviceNames)
                    {
                        if (!subscribedDeviceNames.Contains(definedDeviceName))
                        {
                            DeviceStatisticsHelper<SubscribedDevice> statisticsHelper = new DeviceStatisticsHelper<SubscribedDevice>(new SubscribedDevice(definedDeviceName));
                            subscribedDevices.Add(statisticsHelper);
                            statisticsHelper.Reset(now);
                        }
                    }

                    if (dataSource.Tables.Contains("ActiveMeasurements"))
                    {
                        foreach (DeviceStatisticsHelper<SubscribedDevice> statisticsHelper in subscribedDevices)
                        {
                            measurementRows = dataSource.Tables["ActiveMeasurements"]
                                .Select($"SignalReference LIKE '{Regex.Replace(statisticsHelper.Device.Name, @"^LOCAL\$", "")}-%' AND SignalType <> 'STAT'");

                            foreach (DataRow measurementRow in measurementRows)
                            {
                                if (Guid.TryParse(measurementRow["SignalID"].ToNonNullString(), out signalID))
                                {
                                    // In some rare cases duplicate signal ID's have been encountered (likely bad configuration),
                                    // as a result we use a GetOrAdd instead of an Add
                                    subscribedDevicesLookup.GetOrAdd(signalID, statisticsHelper);

                                    switch (measurementRow["SignalType"].ToNonNullString())
                                    {
                                        case "FLAG":
                                            statisticsHelper.Device.StatusFlagsID = signalID;
                                            break;

                                        case "FREQ":
                                            statisticsHelper.Device.FrequencyID = signalID;
                                            break;

                                        case "DFDT":
                                            statisticsHelper.Device.DeltaFrequencyID = signalID;
                                            break;
                                    }
                                }
                            }
                        }
                    }

                    m_subscribedDevicesLookup = subscribedDevicesLookup;
                    m_statisticsHelpers = subscribedDevices;
                }

                FixExpectedMeasurementCounts();
            }
            catch (Exception ex)
            {
                OnProcessException(new InvalidOperationException($"Unable to register device statistics due to exception: {ex.Message}", ex));
            }
        }