コード例 #1
0
 public AFDataObserver(AFAttributeList attrList, ProcessAFDataPipeEventDelegate processEvent, int pollInterval = 5000)
 {
     AttributeList = attrList;
     DataPipe      = new AFDataPipe();
     _threadSleepTimeInMilliseconds = pollInterval;
     _processEvent = processEvent;
 }
コード例 #2
0
ファイル: AddStreamExample.cs プロジェクト: bzshang/AFSDK-Rx
        public void Run()
        {
            AFElement element = AFObject.FindObject(@"\\BSHANGE6430s\Sandbox\Reactive") as AFElement;
            AFAttributeList attrList = new AFAttributeList(element.Attributes);

            AFDataPipe dataPipe = new AFDataPipe();
            var errors = dataPipe.AddSignups(attrList);

            IObservable<AFDataPipeEvent> obsDataPipe = dataPipe.CreateObservable();

            IObservable<AFDataPipeEvent> sinusoidSnapshot = obsDataPipe
                .Where(evt => evt.Value.Attribute.Name == "SINUSOID" && evt.Action == AFDataPipeAction.Add);

            IObservable<AFDataPipeEvent> cdt158Snapshot = obsDataPipe
                .Where(evt => evt.Value.Attribute.Name == "CDT158" && evt.Action == AFDataPipeAction.Add);

            IObservable<AFDataPipeEvent> sumStream = sinusoidSnapshot.Add(cdt158Snapshot);

            IDisposable subscription = sumStream.Subscribe(evt =>
            {
                Console.WriteLine("Timestamp: {0}, SINUSOID + CDT158: {1}", evt.Value.Timestamp.ToString("HH:mm:ss.ffff"), evt.Value.Value);
            });

            Console.WriteLine("Press any key to unsubscribe");
            Console.ReadKey();

            subscription.Dispose();
            dataPipe.Dispose();
        }
コード例 #3
0
        // Define other instance members here

        public AssetRankProvider(AFAttributeTemplate attrTemplate)
        {
            AttributeTemplate = attrTemplate;
            DataPipe          = new AFDataPipe();

            // Your code here
            // 1. Initialize an internal data structure to store the latest values keyed by element
        }
コード例 #4
0
 public AFDataObserver(IList <AFAttribute> attributes, Action <AFValue> onNextAction)
 {
     _attributes   = attributes;
     _onNextAction = onNextAction;
     _dataPipe     = new AFDataPipe();
     _tokenSource  = new CancellationTokenSource();
     _ct           = _tokenSource.Token;
 }
コード例 #5
0
        static void Main(string[] args)
        {
            // Connect to the AF server and database
            PISystem myAF = new PISystems()["DNG-AF2014"];
            AFDatabase myDB = myAF.Databases["Dev Support"];

            // Create attribute list for attributes to be signed up for AFDataPipe
            AFElement myElement = myDB.Elements["CDR"];
            AFAttribute myAttribute1 = myElement.Attributes["cdt158"];
            AFAttribute myAttribute2 = myElement.Attributes["SQLDRTest"];
            AFAttribute myAttribute3 = myElement.Attributes["SQLDRTest2"];
            IList<AFAttribute> attrList = new List<AFAttribute>();
            attrList.Add(myAttribute1);
            attrList.Add(myAttribute2);
            attrList.Add(myAttribute3);

            // Get new events continuously until users enter any key in the console window
            using (AFDataPipe myDataPipe = new AFDataPipe())
            {
                myDataPipe.AddSignups(attrList);
                IObserver<AFDataPipeEvent> observer = new DataPipeObserver();
                myDataPipe.Subscribe(observer);
                bool more = false;

                // create a cancellation source to terminate the update thread when the user is done
                CancellationTokenSource cancellationSource = new CancellationTokenSource();
                Task task = Task.Run(() =>
                {
                    // keep polling while the user hasn't requested cancellation
                    while (!cancellationSource.IsCancellationRequested)
                    {
                        // Get updates from pipe and process them
                        AFErrors<AFAttribute> myErrors = myDataPipe.GetObserverEvents(out more);

                        // wait for 1 second using the handle provided by the cancellation source
                        cancellationSource.Token.WaitHandle.WaitOne(1000);
                    }

                }, cancellationSource.Token);

                Console.ReadKey();
                Console.WriteLine("Exiting updates");
                cancellationSource.Cancel();

                // wait for the task to complete before taking down the pipe
                task.Wait();
            }
        }
コード例 #6
0
        static void Main(string[] args)
        {
            // Connect to the AF server and database
            PISystem   myAF = new PISystems()["DNG-AF2014"];
            AFDatabase myDB = myAF.Databases["Dev Support"];

            // Create attribute list for attributes to be signed up for AFDataPipe
            AFElement           myElement    = myDB.Elements["CDR"];
            AFAttribute         myAttribute1 = myElement.Attributes["cdt158"];
            AFAttribute         myAttribute2 = myElement.Attributes["SQLDRTest"];
            AFAttribute         myAttribute3 = myElement.Attributes["SQLDRTest2"];
            IList <AFAttribute> attrList     = new List <AFAttribute>();

            attrList.Add(myAttribute1);
            attrList.Add(myAttribute2);
            attrList.Add(myAttribute3);

            // Get new events continuously until users enter any key in the console window
            using (AFDataPipe myDataPipe = new AFDataPipe())
            {
                myDataPipe.AddSignups(attrList);
                IObserver <AFDataPipeEvent> observer = new DataPipeObserver();
                myDataPipe.Subscribe(observer);
                bool more = false;

                // create a cancellation source to terminate the update thread when the user is done
                CancellationTokenSource cancellationSource = new CancellationTokenSource();
                Task task = Task.Run(() =>
                {
                    // keep polling while the user hasn't requested cancellation
                    while (!cancellationSource.IsCancellationRequested)
                    {
                        // Get updates from pipe and process them
                        AFErrors <AFAttribute> myErrors = myDataPipe.GetObserverEvents(out more);

                        // wait for 1 second using the handle provided by the cancellation source
                        cancellationSource.Token.WaitHandle.WaitOne(1000);
                    }
                }, cancellationSource.Token);

                Console.ReadKey();
                Console.WriteLine("Exiting updates");
                cancellationSource.Cancel();

                // wait for the task to complete before taking down the pipe
                task.Wait();
            }
        }
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                if (_tokenSource != null)
                {
                    _tokenSource.Cancel();

                    try
                    {
                        if (_mainTask != null)
                        {
                            _mainTask.Wait();
                        }
                    }
                    catch (AggregateException e)
                    {
                        foreach (var v in e.InnerExceptions)
                        {
                            if (!(v is TaskCanceledException))
                            {
                                Console.WriteLine("Exception in the main task : {0}", v);
                            }
                        }
                        Console.WriteLine();
                    }
                    finally
                    {
                        _tokenSource.Dispose();
                    }
                }

                if (DataPipe != null)
                {
                    DataPipe.Dispose();
                }
            }

            _tokenSource = null;
            DataPipe     = null;
            _disposed    = true;
        }
        public AssetRankProvider(AFAttributeTemplate attrTemplate)
        {
            if (attrTemplate.Type != typeof(double) &&
                attrTemplate.Type != typeof(float) &&
                attrTemplate.Type != typeof(Int32))
            {
                throw new ArgumentException("Cannot rank attributes with value type {0}", attrTemplate.Type.Name);
            }

            AttributeTemplate = attrTemplate;

            _afValueComparer = new Comparison <AFValue>(CompareAFValue);

            DataPipe    = new AFDataPipe();
            _lastValues = new Dictionary <AFElement, AFValue>();

            _tokenSource = new CancellationTokenSource();
            _ct          = _tokenSource.Token;
        }
コード例 #9
0
        // TODO: Updated code for handling piped-events in AF-SDK
        //private void PipeOnOnNewValue()
        //{
        //    List<IMeasurement> measurements = new List<IMeasurement>();

        //    m_connection.Execute(server =>
        //    {
        //        PIEventObject eventobject;
        //        PointValue pointvalue;

        //        for (int i = 0; i < m_pipe.Count; i++)
        //        {
        //            eventobject = m_pipe.Take();

        //            // we will publish measurements for every action except deleted (possible dupes on updates)
        //            if (eventobject.Action != EventActionConstants.eaDelete)
        //            {
        //                try
        //                {
        //                    pointvalue = (PointValue)eventobject.EventData;

        //                    double value = Convert.ToDouble(pointvalue.PIValue.Value);
        //                    MeasurementKey key = m_tagKeyMap[pointvalue.PIPoint.Name];

        //                    Measurement measurement = new Measurement();
        //                    measurement.Key = key;
        //                    measurement.Timestamp = pointvalue.PIValue.TimeStamp.LocalDate.ToUniversalTime();
        //                    measurement.Value = value;
        //                    measurement.StateFlags = MeasurementStateFlags.Normal;

        //                    if (measurement.Timestamp > m_lastReceivedTimestamp.Ticks)
        //                        m_lastReceivedTimestamp = measurement.Timestamp;

        //                    measurements.Add(measurement);
        //                }
        //                catch
        //                {
        //                    /* squelch any errors on digital state data that can't be converted to a double */
        //                }
        //            }
        //        }
        //    });

        //    if (measurements.Any())
        //    {
        //        OnNewMeasurements(measurements);
        //        m_processedMeasurements += measurements.Count;
        //    }
        //}

        /// <summary>
        /// Disposes members for garbage collection
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (m_tagKeyMap != null)
            {
                m_tagKeyMap.Clear();
            }
            m_tagKeyMap = null;

            if ((object)m_connection != null)
            {
                m_connection.Dispose();
            }

            m_connection = null;
            m_points     = null;
            m_pipe       = null;

            m_measurements.Clear();
            m_measurements = null;

            if (m_dataThread != null)
            {
                m_dataThread.Abort();
                m_dataThread = null;
            }

            if (m_publishTimer != null)
            {
                m_publishTimer.Stop();
                m_publishTimer.Elapsed -= m_publishTimer_Tick;
                m_publishTimer.Dispose();
                m_publishTimer = null;
            }
        }
コード例 #10
0
 /// <summary>
 /// Creates a datapipe handler for an AFDataPipe
 /// </summary>
 /// <param name="observer">The observer object that will receive the data changes</param>
 public DataPipeHandler(IObserver <AFDataPipeEvent> observer)
 {
     _AFPipe = new AFDataPipe();
     _AFPipe.Subscribe(observer);
 }
コード例 #11
0
 public void Dispose()
 {
     DataPipe.Dispose();
     DataPipe = null;
 }