AF SDK - Some code snippets for the OSIsoft.AF.PI namespace

Blog Post created by andreas Employee on Jun 28, 2012

After Rick's post I realized that there are still some code snippets I wanted to share with you. The following snippets will show you simple tasks you used to do with the PI SDK like creating a tag, writing data or signing up for events.




So let's start with connecting to the PI Server:

// the PI server
OSIsoft.AF.PI.PIServers myPIServers = new OSIsoft.AF.PI.PIServers();
OSIsoft.AF.PI.PIServer myPIServer = myPIServers.DefaultPIServer;

As usual I am using my default server only to keep things simple


As Rick mentioned already - things are slightly different between AF SDK and PI SDK. So for the point attributes, AF SDK expects a dictionary:



string strSolution = "AFSDK_RDA_CodeSnippets";
Dictionary<string, object> myAttributes = new Dictionary<string, object>();
myAttributes.Add("CompMin", 0);          // CompMin 0
myAttributes.Add("CompMax", 600);        // CompMax 10 min
myAttributes.Add("CompDev", 60);         // CompDev 60
myAttributes.Add("Compressing", 1);      // Compressing on
myAttributes.Add("ExcMin", 0);           // ExceptionMin 0
myAttributes.Add("ExcMax", 0);           // ExceptionMax 0
myAttributes.Add("ExcDev", 0);           // ExceptionDeviation 0
myAttributes.Add("Pointsource", strSolution);

now we are ready to create the PI point:

OSIsoft.AF.PI.PIPoint myPIPoint = myPIServer.CreatePIPoint("myTagname", myAttributes);

Let us generate a list of values: 

List<OSIsoft.AF.Asset.AFValue> myValues = new List<OSIsoft.AF.Asset.AFValue>;
DateTime myTimeStamp = DateTime.Now.AddSeconds(-1000);
Random myRandom = new Random(myTimeStamp.Second);

for (int cc = 0; cc < 1000; cc++)
        myValues.Add(new OSIsoft.AF.Asset.AFValue(myRandom.Next(0, 100),
                                                  new OSIsoft.AF.Time.AFTime(myTimeStamp)));
        myTimeStamp = myTimeStamp.AddSeconds(1);

 and send them to PI:


myPIPoint.UpdateValues(myValues, OSIsoft.AF.Data.AFUpdateOption.Insert);


 A frequent topic for the PI SDK are the EventPipes. In the AF SDK we have this mechanism as well – the PIDataPipe. This time we are not looking for a single tag, we are getting a PointList:


OSIsoft.AF.PI.PIPointList myPIPoints = new OSIsoft.AF.PI.PIPointList(OSIsoft.AF.PI.PIPoint.FindPIPoints(myPIServer, "*", strSolution));


 Let us sign up for snapshot events:


OSIsoft.AF.PI.PIDataPipe myPIDataPipe = new OSIsoft.AF.PI.PIDataPipe(OSIsoft.AF.Data.AFDataPipeType.Snapshot);


And for the rest of the day I keep watching events coming into my system:


long lValCount = 0;
while (Console.KeyAvailable == false)
    OSIsoft.AF.AFListResults<OSIsoft.AF.PI.PIPoint, OSIsoft.AF.Data.AFDataPipeEvent> myEvents;
    myEvents = myPIDataPipe.GetUpdateEvents(1000000);
    if (myEvents.Count > 0)
        lValCount += myEvents.Count;
        Console.WriteLine("{0,7:N0} events ({1,10:N0}) ({2,8:N0} events/s).", 

So - if you have not tried the new AF SDK 2.5 - go and download the beta and start exploring!