Exemple #1
0
        private void HandleNewMeasurementsRequest(MeasurementKey[] Keys)
        {
            OnStatusMessage("Received request for {0} keys...", new object[] { Keys.Count() });

            if (!IsConnected)
                AttemptConnection();

            var query = from row in DataSource.Tables["ActiveMeasurements"].AsEnumerable()
                        from key in Keys
                        where row["ID"].ToString().Split(':')[1] == key.ID.ToString()
                        select new { Key = key, AlternateTag = row["ALTERNATETAG"].ToString(), PointTag = row["POINTTAG"].ToString() };

            StringBuilder tagFilter = new StringBuilder();
            foreach (var row in query)
            {
                string tagname = row.PointTag;
                if (!String.IsNullOrWhiteSpace(row.AlternateTag))
                    tagname = row.AlternateTag;

                if (!m_tagKeyMap.ContainsKey(tagname))
                {
                    m_tagKeyMap.AddOrUpdate(tagname, row.Key, (k, v) => row.Key);
                }

                if (tagFilter.Length > 0)
                    tagFilter.Append(" OR ");

                tagFilter.Append(string.Format("tag='{0}'", tagname));
            }

            m_points = m_server.GetPoints(tagFilter.ToString());

            // event pipes are only applicable if enabled in connection string and this is a real time session, not playback
            bool useEventPipes = m_useEventPipes && StartTimeConstraint == DateTime.MinValue && StopTimeConstraint == DateTime.MaxValue;
            if (useEventPipes)
            {
                try
                {
                    if (m_pipe != null)
                        ((_DEventPipeEvents_Event)m_pipe).OnNewValue -= (PISDK._DEventPipeEvents_OnNewValueEventHandler)PipeOnOnNewValue;

                    m_pipe = m_points.Data.EventPipe;
                    ((_DEventPipeEvents_Event)m_pipe).OnNewValue += (PISDK._DEventPipeEvents_OnNewValueEventHandler)PipeOnOnNewValue;
                }
                catch (ThreadAbortException) { throw; }
                catch (Exception e)
                {
                    useEventPipes = false; // try to run with polling instead of event pipes;
                    OnProcessException(e);
                }
            }

            if (!useEventPipes)
            {
                // warn that we are going to use a different configuration here...
                if (m_useEventPipes)
                    OnStatusMessage("WARNING: PI adapter switching from event pipes to polling due to error or start/stop time constraints.");

                // set up a new thread to do some long calls to PI and set up threads, timers, etc for polling
                StopGettingData();
                ThreadPool.QueueUserWorkItem(StartGettingData, tagFilter);
            }

            m_useEventPipes = useEventPipes;
        }