Exemplo n.º 1
0
        public override void Initialize(MeterDataSet meterDataSet)
        {
            Dictionary <Channel, List <DataGroup> >    trendingGroups;
            HashSet <Tuple <int, SeriesID, DateTime> > existingTrendingPoints;

            Dictionary <string, DataSeries> seriesLookup;
            DataSeries minSeries;
            DataSeries maxSeries;
            DataSeries avgSeries;

            List <TrendingDataSummary> summaries;
            TrendingDataSummary        summary;

            m_trendingDataSummaries = new Dictionary <Channel, List <TrendingDataSummary> >();
            trendingGroups          = meterDataSet.GetResource <TrendingGroupsResource>().TrendingGroups;

            using (Historian historian = new Historian(m_historianSettings.Server, m_historianSettings.InstanceName))
            {
                existingTrendingPoints = FindExistingTrendingPoints(meterDataSet, historian);
            }

            foreach (KeyValuePair <Channel, List <DataGroup> > trendingGroup in trendingGroups)
            {
                summaries = m_trendingDataSummaries.GetOrAdd(trendingGroup.Key, channel => new List <TrendingDataSummary>());

                foreach (DataGroup dataGroup in trendingGroup.Value)
                {
                    seriesLookup = dataGroup.DataSeries.ToDictionary(series => series.SeriesInfo.SeriesType.Name);
                    seriesLookup.TryGetValue("Minimum", out minSeries);
                    seriesLookup.TryGetValue("Maximum", out maxSeries);
                    seriesLookup.TryGetValue("Average", out avgSeries);

                    // By the time we exit this if statement,
                    // the min series will have been defined
                    if ((object)minSeries == null)
                    {
                        if ((object)avgSeries != null)
                        {
                            // Use the average series as the min series.
                            // Max series may still be undefined
                            minSeries = avgSeries;
                        }
                        else if ((object)maxSeries != null)
                        {
                            // We know the min and average series are both undefined,
                            // so use the max series for all of them
                            minSeries = maxSeries;
                            avgSeries = maxSeries;
                        }
                        else if (seriesLookup.TryGetValue("Values", out minSeries) || seriesLookup.TryGetValue("Instantaneous", out minSeries))
                        {
                            // All series are undefined, but there exists an instantaneous series
                            // for this channel which can assume the role of min, max, and average
                            maxSeries = minSeries;
                            avgSeries = minSeries;
                        }
                        else
                        {
                            // There is no way to tell what series could reasonably
                            // represent the min, max, and average for this channel
                            continue;
                        }
                    }

                    // By the time we exit this if statement,
                    // the max series will have been defined
                    if ((object)maxSeries == null)
                    {
                        if ((object)avgSeries != null)
                        {
                            // Use the average series as the max series
                            maxSeries = avgSeries;
                        }
                        else
                        {
                            // Both the average and max series are undefined
                            // so use the min series for all of them
                            maxSeries = minSeries;
                            avgSeries = minSeries;
                        }
                    }

                    for (int i = 0; i < dataGroup.Samples; i++)
                    {
                        summary = new TrendingDataSummary();

                        // Get the date-time of the summary
                        summary.Time = minSeries.DataPoints[i].Time;

                        // Get the min and the max of the current sample
                        summary.Minimum = minSeries.DataPoints[i].Value;
                        summary.Maximum = maxSeries.DataPoints[i].Value;

                        // If a series containing average values is not available,
                        // use the average of the max and the min
                        if ((object)avgSeries != null)
                        {
                            summary.Average = avgSeries.DataPoints[i].Value;
                        }
                        else
                        {
                            summary.Average = (summary.Minimum + summary.Maximum) / 2.0D;
                        }

                        if (double.IsNaN(summary.Minimum) || double.IsNaN(summary.Maximum) || double.IsNaN(summary.Average))
                        {
                            continue;
                        }

                        summary.IsDuplicate = SeriesIDs.Any(seriesID => !existingTrendingPoints.Add(Tuple.Create(trendingGroup.Key.ID, seriesID, summary.Time)));

                        summaries.Add(summary);
                    }
                }
            }
        }
Exemplo n.º 2
0
 private IEnumerable <ulong> GetAllMeasurementIDs(int channel)
 {
     return(SeriesIDs.Select(series => ToPointID(channel, (int)series)));
 }