Ejemplo n.º 1
0
        private void m_unsynchronizedSubscriber_NewMeasurements(object sender, EventArgs <ICollection <IMeasurement> > e)
        {
            if (0 == Interlocked.Exchange(ref m_processingUnsynchronizedMeasurements, 1))
            {
                try
                {
                    foreach (IMeasurement measurement in e.Argument)
                    {
                        double tempValue = measurement.AdjustedValue;

                        if (!double.IsNaN(tempValue) && !double.IsInfinity(tempValue)) // Process data only if it is not NaN or infinity.
                        {
                            ChartPlotterDynamic.Dispatcher.Invoke(DispatcherPriority.Normal, (Action) delegate
                            {
                                if (m_yAxisDataCollection.Count == 0)
                                {
                                    for (int i = 0; i < NumberOfPointsToPlot; i++)
                                    {
                                        m_yAxisDataCollection.Enqueue(tempValue);
                                    }

                                    m_yAxisBindingCollection = new EnumerableDataSource <double>(m_yAxisDataCollection);
                                    m_yAxisBindingCollection.SetYMapping(y => y);

                                    m_lineGraph = ChartPlotterDynamic.AddLineGraph(new CompositeDataSource(m_xAxisBindingCollection, m_yAxisBindingCollection), Color.FromArgb(255, 25, 25, 200), 1, "");
                                }
                                else
                                {
                                    double oldValue;

                                    if (m_yAxisDataCollection.TryDequeue(out oldValue))
                                    {
                                        m_yAxisDataCollection.Enqueue(tempValue);
                                    }
                                }
                                m_yAxisBindingCollection.RaiseDataChanged();
                            });
                        }
                    }
                }
                finally
                {
                    Interlocked.Exchange(ref m_processingUnsynchronizedMeasurements, 0);
                }
            }
        }
Ejemplo n.º 2
0
        private void m_subscriber_NewMeasurements(object sender, EventArgs <ICollection <IMeasurement> > e)
        {
            if (0 == Interlocked.Exchange(ref m_processingSynchronizedMeasurements, 1))
            {
                try
                {
                    if (m_historicalPlayback && m_waitingForData)
                    {
                        m_waitingForData = false;

                        Dispatcher.BeginInvoke(new Action(delegate
                        {
                            ModeMessage.Text = "Historical";
                        }));
                    }

                    bool refreshMeasurementValueBelowChart = false;
                    if (DateTime.UtcNow.Ticks - m_lastRefreshTime > m_refreshRate)
                    {
                        m_lastRefreshTime = DateTime.UtcNow.Ticks;
                        refreshMeasurementValueBelowChart = true;
                    }

                    foreach (IMeasurement newMeasurement in e.Argument)
                    {
                        m_timeStampList.Enqueue(newMeasurement.Timestamp.ToString("HH:mm:ss.fff"));

                        if (m_timeStampList.Count > m_numberOfDataPointsToPlot)
                        {
                            string oldValue;
                            m_timeStampList.TryDequeue(out oldValue);
                        }

                        double tempValue    = newMeasurement.AdjustedValue;
                        Guid   tempSignalID = newMeasurement.ID;
                        if (!double.IsNaN(tempValue) && !double.IsInfinity(tempValue)) // Process data only if it is not NaN or infinity.
                        {
                            ConcurrentQueue <double> tempValueCollection;
                            if (m_yAxisDataCollection.TryGetValue(tempSignalID, out tempValueCollection)) // If value collection already exists, then just replace oldest value with newest.
                            {
                                double oldValue;
                                if (tempValueCollection.TryDequeue(out oldValue))
                                {
                                    tempValueCollection.Enqueue(tempValue);
                                }
                            }
                            else // It is probably a new measurement user wants to subscribe to.
                            {
                                RealTimeMeasurement measurement;

                                // Check if user has selected this measurement. Because user may have unselected this but request may not have been processed completely.
                                if (m_selectedMeasurements.TryGetValue(tempSignalID, out measurement))
                                {
                                    tempValueCollection = new ConcurrentQueue <double>();
                                    for (int i = 0; i < m_numberOfDataPointsToPlot; i++)
                                    {
                                        tempValueCollection.Enqueue(tempValue);
                                    }

                                    m_yAxisDataCollection.TryAdd(tempSignalID, tempValueCollection);
                                    EnumerableDataSource <double> tempDataSource = new EnumerableDataSource <double>(tempValueCollection);
                                    m_yAxisBindingCollection.TryAdd(tempSignalID, tempDataSource);
                                    tempDataSource.SetYMapping(y => y);

                                    ChartPlotterDynamic.Dispatcher.Invoke(DispatcherPriority.Normal, (Action) delegate
                                    {
                                        int colorIndex;
                                        Math.DivRem(m_yAxisBindingCollection.Count, 10, out colorIndex);
                                        LineGraph lineGraph = null;

                                        if (measurement.SignalAcronym == "FREQ")
                                        {
                                            lineGraph = ChartPlotterDynamic.AddLineGraph(new CompositeDataSource(m_xAxisBindingCollection, tempDataSource), m_lineColors[colorIndex], 1, measurement.SignalReference);
                                        }
                                        else if (measurement.SignalAcronym == "IPHA" || measurement.SignalAcronym == "VPHA")
                                        {
                                            lineGraph = PhaseAnglePlotter.AddLineGraph(new CompositeDataSource(m_xAxisBindingCollection, tempDataSource), m_lineColors[colorIndex], 1, measurement.SignalReference);
                                        }
                                        else if (measurement.SignalAcronym == "VPHM")
                                        {
                                            lineGraph = VoltagePlotter.AddLineGraph(new CompositeDataSource(m_xAxisBindingCollection, tempDataSource), m_lineColors[colorIndex], 1, measurement.SignalReference);
                                        }
                                        else if (measurement.SignalAcronym == "IPHM")
                                        {
                                            lineGraph = CurrentPlotter.AddLineGraph(new CompositeDataSource(m_xAxisBindingCollection, tempDataSource), m_lineColors[colorIndex], 1, measurement.SignalReference);
                                        }

                                        if ((object)lineGraph != null)
                                        {
                                            m_lineGraphCollection.TryAdd(tempSignalID, lineGraph);
                                            measurement.Foreground = (SolidColorBrush)lineGraph.LinePen.Brush;
                                        }
                                    });
                                }
                            }

                            if (refreshMeasurementValueBelowChart)
                            {
                                lock (m_selectedMeasurements)
                                {
                                    RealTimeMeasurement measurement;
                                    if (m_selectedMeasurements.TryGetValue(tempSignalID, out measurement))
                                    {
                                        measurement.Value   = tempValue.ToString("0.###");
                                        measurement.TimeTag = newMeasurement.Timestamp.ToString("HH:mm:ss.fff");
                                        measurement.Quality = newMeasurement.ValueQualityIsGood() ? "GOOD" : "UNKNOWN";
                                    }
                                }
                            }
                        }
                    }
                }
                finally
                {
                    Interlocked.Exchange(ref m_processingSynchronizedMeasurements, 0);
                }
            }
        }