Пример #1
0
        /// <summary>
        /// Returns <see cref="PIConnection"/> to the pool.
        /// </summary>
        /// <param name="connection"><see cref="PIConnection"/> to return to the pool.</param>
        /// <exception cref="InvalidOperationException">Provided PIConnection does not belong to this connection pool.</exception>
        public void ReturnPooledConnection(PIConnection connection)
        {
            if ((object)connection == null)
            {
                return;
            }

            lock (m_connectionPool)
            {
                if (!m_connectionPool.Contains(connection))
                {
                    throw new InvalidOperationException("Provided PIConnection does not belong to this connection pool");
                }

                if (connection.AccessCount > 0)
                {
                    connection.AccessCount--;
                }

                if (connection.AccessCount < 1)
                {
                    if (m_connectionPool.Count > m_minimumPoolSize && m_connectionPool.Remove(connection))
                    {
                        connection.Disconnected -= connection_Disconnected;
                        connection.Dispose();
                    }
                }
            }
        }
Пример #2
0
        protected override void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                try
                {
                    if (disposing)
                    {
                        if ((object)m_connection != null)
                        {
                            m_connection.Disconnected -= m_connection_Disconnected;
                            m_connection.Dispose();
                            m_connection = null;
                        }

                        m_dataPipe?.Dispose();

                        if ((object)m_eventTimer != null)
                        {
                            m_eventTimer.Elapsed -= m_eventTimer_Elapsed;
                            m_eventTimer.Dispose();
                        }
                    }
                }
                finally
                {
                    m_disposed = true;          // Prevent duplicate dispose.
                    base.Dispose(disposing);    // Call base class Dispose().
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Connects to the configured PI server.
        /// </summary>
        protected override void AttemptConnection()
        {
            m_processedMappings     = 0;
            m_processedMeasurements = 0;
            m_totalProcessingTime   = 0;

            m_connection = new PIConnection
            {
                ServerName     = m_serverName,
                UserName       = m_userName,
                Password       = m_password,
                ConnectTimeout = m_connectTimeout
            };

            m_connection.Disconnected += m_connection_Disconnected;
            m_connection.Open();

            m_mappedPIPoints.Clear();

            lock (m_pendingMappings)
            {
                m_pendingMappings.Clear();
            }

            m_mapRequestQueue.Clear();
            m_mapRequestQueue.Start();
            m_archiveQueue.Start();

            // Kick off meta-data refresh
            RefreshMetadata();
        }
Пример #4
0
        /// <summary>
        /// Gets a connection from the pool or creates a new one if all current connections are being used at peak access.
        /// </summary>
        /// <param name="serverName">Name of the PI server for the adapter's PI connection.</param>
        /// <param name="userName">Name of the PI user ID for the adapter's PI connection.</param>
        /// <param name="password">Password used for the adapter's PI connection.</param>
        /// <param name="connectTimeout">Timeout interval (in milliseconds) for the adapter's connection.</param>
        /// <returns>A <see cref="PIConnection"/> from the pool.</returns>
        /// <exception cref="InvalidOperationException">Failed to get a pooled PI connection.</exception>
        public PIConnection GetPooledConnection(string serverName, string userName = null, string password = null, int connectTimeout = PIConnection.DefaultConnectTimeout)
        {
            PIConnection connection = null;

            // We dynamically allocate pooled PI server connections each having a maximum accessibility count.
            // PI's threading model can handle many connections each archiving a small volume of points, but
            // falls behind under load when archiving a large volume of points from a single connection.
            try
            {
                lock (m_connectionPool)
                {
                    while ((object)connection == null)
                    {
                        // Get next connection from the pool with lowest accessibility count
                        if (m_connectionPool.Count > 0)
                        {
                            PIConnection[] availableConnections = m_connectionPool.Where(c => c.AccessCount < m_accessCountPerConnection).ToArray();

                            if (availableConnections.Length > 0)
                            {
                                connection = availableConnections.Aggregate((currentMin, nextItem) => (object)nextItem != null && currentMin.AccessCount < nextItem.AccessCount ? currentMin : nextItem);
                            }
                        }

                        if ((object)connection == null)
                        {
                            // Add pooled connections in groups for better distribution
                            for (int i = 0; i < m_minimumPoolSize; i++)
                            {
                                // Create a new connection
                                connection = new PIConnection
                                {
                                    ServerName     = serverName,
                                    UserName       = userName,
                                    Password       = password,
                                    ConnectTimeout = connectTimeout
                                };

                                // Since PI doesn't detect disconnection until an operation is attempted,
                                // we must monitor for disconnections from the pooled connections as well
                                connection.Disconnected += connection_Disconnected;
                                connection.Open();

                                // Add the new connection to the server pool
                                m_connectionPool.Add(connection);
                            }
                        }
                    }

                    // Increment current connection access count
                    connection.AccessCount++;
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(string.Format("Failed to get a pooled PI connection: {0}", ex.Message), ex);
            }

            return(connection);
        }
Пример #5
0
 /// <summary>
 /// Disconnects from the configured PI server if a connection is open
 /// </summary>
 protected override void AttemptDisconnection()
 {
     if ((object)m_connection != null)
     {
         m_connection.Dispose();
         m_connection = null;
     }
 }
Пример #6
0
        /// <summary>
        /// Connects to the configured PI server.
        /// </summary>
        protected override void AttemptConnection()
        {
            m_processedMeasurements = 0;

            m_connection = new PIConnection
            {
                ServerName     = m_serverName,
                UserName       = m_userName,
                Password       = m_password,
                ConnectTimeout = m_connectTimeout
            };

            m_connection.Open();
        }
Пример #7
0
        /// <summary>
        /// Attempts to disconnect from this <see cref="PIAdapters.PIPBInputAdapter"/>.
        /// </summary>
        protected override void AttemptDisconnection()
        {
            if ((object)m_readTimer != null)
            {
                m_readTimer.Enabled = false;

                lock (m_readTimer)
                {
                    m_dataReader = null;
                }
            }

            if ((object)m_connection != null)
            {
                m_connection.Dispose();
                m_connection = null;
            }
        }
Пример #8
0
        /// <summary>
        /// Connects to the configured PI server.
        /// </summary>
        protected override void AttemptConnection()
        {
            m_connection = new PIConnection
            {
                ServerName     = this.ServerName,
                UserName       = this.UserName,
                Password       = this.Password,
                ConnectTimeout = this.ConnectTimeout
            };

            m_connection.Disconnected += m_connection_Disconnected;
            m_connection.Open();

            m_dataPipe = new PIDataPipe(AFDataPipeType.Snapshot);
            //m_dataPipe.Subscribe(m_dataUpdateObserver);

            if (AutoStart && (object)OutputMeasurements != null && OutputMeasurements.Any())
            {
                SubscribeToPointUpdates(this.OutputMeasurementKeys());
            }
        }
Пример #9
0
        /// <summary>
        /// Closes this <see cref="PIOutputAdapter"/> connections to the PI server.
        /// </summary>
        protected override void AttemptDisconnection()
        {
            m_archiveQueue.Stop();

            m_mapRequestQueue.Stop();
            m_mapRequestQueue.Clear();

            m_mappedPIPoints.Clear();

            lock (m_pendingMappings)
            {
                m_pendingMappings.Clear();
            }

            if ((object)m_connection != null)
            {
                m_connection.Disconnected -= m_connection_Disconnected;
                m_connection.Dispose();
                m_connection = null;
            }
        }
Пример #10
0
        /// <summary>
        /// Disconnects from the configured PI server if a connection is open
        /// </summary>
        protected override void AttemptDisconnection()
        {
            m_eventTimer.Enabled = false;

            if ((object)m_dataPoints != null)
            {
                m_dataPoints.Clear();
                m_dataPoints = null;
            }

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

            if ((object)m_connection != null)
            {
                m_connection.Dispose();
                m_connection = null;
            }
        }
Пример #11
0
        /// <summary>
        /// Attempts to connect to this <see cref="PIAdapters.PIPBInputAdapter"/>.
        /// </summary>
        protected override void AttemptConnection()
        {
            // This adapter is only engaged for history, so we don't process any data unless a temporal constraint is defined
            if (this.TemporalConstraintIsDefined())
            {
                // Turn off read timer if it's active
                m_readTimer.Enabled = false;

                m_connection = new PIConnection
                {
                    ServerName     = m_serverName,
                    UserName       = m_userName,
                    Password       = m_password,
                    ConnectTimeout = m_connectTimeout
                };

                m_connection.Open();

                // Start the data reader on its own thread so connection attempt can complete in a timely fashion...
                ThreadPool.QueueUserWorkItem(StartDataReader);
            }
        }
Пример #12
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="PIOutputAdapter"/> object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                try
                {
                    if (disposing)
                    {
                        if ((object)m_mapRequestQueue != null)
                        {
                            m_mapRequestQueue.Dispose();
                        }

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

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

                        if ((object)m_mappedPIPoints != null)
                        {
                            m_mappedPIPoints.Clear();
                        }
                    }
                }
                finally
                {
                    m_disposed = true;          // Prevent duplicate dispose.
                    base.Dispose(disposing);    // Call base class Dispose().
                }
            }
        }
Пример #13
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;
            }
        }