YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Question about PI Server connection Options · View
martinergb
#1 Posted : Friday, February 04, 2011 4:35:29 PM
Rank: Newbie
Groups: Member

Joined: 2/4/2011
Posts: 1
Hi,
I want to connect to a server, but i found that there is only one way to do this by getting the server from Servers collection (like Servers["localhost"] or Servers.DefaultServer). And the server must be on the known list. So, are there any ways to connect to a server with server path, port, username and password specified at runtime? (I use .Net)
And, in my application, I want to create more than one connection to the same server (eg. 1st connection to read point A, 2nd connection to read point B). But when I close the 1st connection, the 2nd connection is closed too. Any solutions?
Thanks in advance.
Sponsor  
 

OSIsoft vCampus is a subscription-based, online offering that consists of providing everything people need to develop applications on the PI System.
We invite you to take a "tour" of the OSIsoft Virtual Campus - also feel free to consult the FAQ  or contact OSIsoft vCampus for more details.
erburrell
#2 Posted : Wednesday, March 30, 2011 8:00:05 PM
Rank: Member
Groups: Member

Joined: 4/2/2009
Posts: 19
Location: Mississippi, USA
Out of curiosity, is there a reason to open more than one connection to the server? If the user is at the same workstation, then one connection should suffice.

Ed.
sunflower
#3 Posted : Thursday, July 07, 2011 12:05:11 PM
Rank: Newbie
Groups: Member

Joined: 6/28/2011
Posts: 3
I wanted to know if we can connect to PI server by entering username and password at run time
erburrell
#4 Posted : Thursday, July 07, 2011 1:37:04 PM
Rank: Member
Groups: Member

Joined: 4/2/2009
Posts: 19
Location: Mississippi, USA
Here is a class that I use to access and retrieve PI data. You could easily assign the values of the PI server, username and password in the class file if you desire, or you can allow a user to enter the data at run time, and pass the information to PI. Feel free to use it if you like it. If you make some significant improvements, I would appreciate any feedback if possible.

Regards,

Ed

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PITools
{
    partial class PiTool
    {
        #region class definitions

        private string _connectionServer;           // The PI Server being connected to.
        public string ConnectionServer
        {
            get { return _connectionServer; }
            set { _connectionServer = value; }
        }

        private string _connectionUser;             // The User Name to be used.
        public string ConnectionUser
        {
            get { return _connectionUser; }
            set { _connectionUser = value; }
        }

        private string _connectionPassword;         // The User Password to be used.
        public string ConnectionPassword
        {
            get { return _connectionPassword; }
            set { _connectionPassword = value; }
        }


        private PISDK.Server _pIserver;
        private PISDKCommon.PIAsynchStatus _piStatus;
        private PISDK.PIPoint _piTag;
        private PISDK.PIValue _piValue;
        private PISDK.PIValues _piValues;
        private PISDK.DigitalState _piDigState;

        #endregion

        public PiTool(string ServerName, string PiUserName, string PiPassword)
        {
            ConnectionServer = ServerName;
            ConnectionUser = PiUserName;
            ConnectionPassword = PiPassword;
        }

        public bool ConnectToPi()
        {
            bool connectResult = false;

            PISDK.PISDKClass SDK = new PISDK.PISDKClass();
            try
            {
                if (_pIserver == null)
                    _pIserver = SDK.Servers[ConnectionServer];
                try
                {
                    if (!_pIserver.Connected)
                        _pIserver.Open("UID=" + ConnectionUser + ";PWD=" + ConnectionPassword);
                    connectResult = _pIserver.Connected;
                }
                catch
                {
                    connectResult = false;
                }
            }
            catch
            {
                connectResult = false;
            }

            return connectResult;
        }

        public decimal GetValueSnapShot(string TagName)
        {
            if (ConnectToPi())
            {
                _piTag = _pIserver.PIPoints[TagName];
                _piValue = _piTag.Data.Snapshot;

                return Convert.ToDecimal(_piValue.Value);
            }

            return 0;
        }

        public decimal GetArchiveValue(string TagName, string PiTime)
        {
            _piStatus = null;

            if (ConnectToPi())
            {
                _piTag = _pIserver.PIPoints[TagName];
                _piValue = _piTag.Data.ArcValue(PiTime, PISDK.RetrievalTypeConstants.rtInterpolated, _piStatus);

                return Convert.ToDecimal(_piValue.Value);
            }

            return 0;
        }

        /// <summary>
        /// Designed to return the time weighted average over a period of time.  Best to be uysed for hourly averages.
        /// </summary>
        /// <param name="TagName"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public decimal GetAverageValueForTimePeriod(string TagName, DateTime StartTime, DateTime EndTime)
        {
            decimal totalizer = 0.0M;
            double timeCounter = 0.0;

            PITimeServer.PITime piStartTime = new PITimeServer.PITime();
            PITimeServer.PITime piEndTime = new PITimeServer.PITime();

            piStartTime.LocalDate = StartTime;
            piEndTime.LocalDate = EndTime;

            if (ConnectToPi())
            {
                _piTag = _pIserver.PIPoints[TagName];
                _piValues = _piTag.Data.RecordedValues(piStartTime, piEndTime, PISDK.BoundaryTypeConstants.btInside, null, PISDK.FilteredViewConstants.fvShowFilteredState, _piStatus);

                for (int i = 1; i < _piValues.Count; i++)
                {
                    totalizer = totalizer + Convert.ToDecimal(_piValues[i - 1].Value) * (decimal)(_piValues[i].TimeStamp.LocalDate - _piValues[i - 1].TimeStamp.LocalDate).TotalSeconds;
                    timeCounter = timeCounter + (_piValues[i].TimeStamp.LocalDate - _piValues[i - 1].TimeStamp.LocalDate).TotalSeconds;
                }
            }

            if (timeCounter > 0)
                return totalizer / (decimal)timeCounter;

            return 0.0M;
        }

        public string GetDigSnapShot(string TagName)
        {
            if (ConnectToPi())
            {
                _piTag = _pIserver.PIPoints[TagName];
                _piValue = _piTag.Data.Snapshot;
                _piDigState = (PISDK.DigitalState)_piValue.Value;

                return _piDigState.Name;
            }

            return string.Empty;
        }

        public string GetDigArchiveValue(string TagName, string PiTime)
        {
            _piStatus = null;

            if (ConnectToPi())
            {
                _piTag = _pIserver.PIPoints[TagName];
                _piValue = _piTag.Data.ArcValue(PiTime, PISDK.RetrievalTypeConstants.rtInterpolated, _piStatus);
                _piDigState = (PISDK.DigitalState)_piValue.Value;

                return _piDigState.Name;
            }

            return string.Empty;
        }

        public DateTime FindGigValueChangeTime(string SearchValue, string PiTag, DateTime StartTime, DateTime EndTime)
        {
            PITimeServer.PITime startTime = new PITimeServer.PITime();
            PITimeServer.PITime endTime = new PITimeServer.PITime();

            startTime.LocalDate = StartTime;
            endTime.LocalDate = EndTime;

            if (ConnectToPi())
            {
                PISDK.PIPoint piTag = _pIserver.PIPoints[PiTag];
                PISDK.DigitalState tagState = null;

                PISDK.PIValues PiTagVals = piTag.Data.RecordedValues(startTime,
                                                                     endTime,
                                                                     PISDK.BoundaryTypeConstants.btAuto,
                                                                     null,
                                                                     PISDK.FilteredViewConstants.fvShowFilteredState,
                                                                     null);

                foreach (PISDK.PIValue tagVal in PiTagVals)
                {
                    if (tagVal.IsGood())
                    {
                        tagState = (PISDK.DigitalState)tagVal.Value;
                        if (tagState.Name == SearchValue)
                        {
                            return tagVal.TimeStamp.LocalDate;
                        }
                    }
                    else
                    {
                        // Bad Value
                    }
                }
            }

            return EndTime;
        }
    }
}

Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.