RyanBrown

SDK 101 - EventPipes

Discussion created by RyanBrown on May 18, 2012
Latest reply on May 21, 2012 by MvanderVeeken

Hi Everyone,

 

Bit late in the week than I would have liked for this post but it’s been a very busy one for me.

 

What I want to show you today is something that I think is brilliant. It’s the EventPipes. What the EventPipes let you do is to monitor a PI server for things that are changing and react to them. As an example you might want to monitor a tag for new events and do something when that happens.

Keeping with the format I’ve chosen for these posts I’ve written a simple console application to demonstrate this that hopefully you can try out and see how it works. I’ve not really done any error handling in this one as I wanted to keep the code example short with just the main parts included.

Description

 

Okay so this is a simple console application that will monitor the sinusoid tag. It runs and waits for new events to appear for the tag. Once it sees a new event it will output some information on the console that includes the timestamp and value of the new event. For this example you need to add two references ( OsISoft.PISDK, OSIsoft.PITimeServer)

 

 

 
using System;
using PISDK;
using PITimeServer;

namespace SimpleEventPipe
{
    class Program
    {
        // These need declared here for the event handler
        static _DEventPipeEvents_Event sinusoidEvents;
        static EventPipe sinusoidEvPipe;

        static void Main(string[] args)
        {
            PISDK.PISDK myPiSdk = new PISDK.PISDK();

            Server myPiServer = myPiSdk.Servers["localhost"];

            if (!myPiServer.Connected)
            {
                // Okay so not handling exceptions here but keeping this one simple
                myPiServer.Open();
            }


            // Just going to monitor sinusoid direct so hard coded here.
            PIPoint myPoint = myPiServer.PIPoints["sinusoid"];
            
            sinusoidEvPipe = myPoint.Data.EventPipe;

            sinusoidEvents = (_DEventPipeEvents_Event)sinusoidEvPipe;

            // Need to define a new event handler to call when a new value is added to sinusoid
            sinusoidEvents.OnNewValue += new _DEventPipeEvents_OnNewValueEventHandler(sinusoidNewEvent);

            // Now wait for something to happen. Put in a ReadKey obviously so it doesn't just close!
            Console.WriteLine("Waiting for events. Just press and key when you want to exit the app.");
            Console.WriteLine();
            Console.ReadKey();

        }

        private static void sinusoidNewEvent()
        {
            PIEventObject sinusoidObject;

            for (int i = 0; i < sinusoidEvPipe.Count; i++)
            {
                // Get the event
                sinusoidObject = sinusoidEvPipe.Take();

                // Get the value and timestamp into a string for display on console
                PointValue sinusoidVal = (PointValue)sinusoidObject.EventData;
                string value = sinusoidVal.PIValue.Value.ToString();
                string changedTime = sinusoidVal.PIValue.TimeStamp.LocalDate.ToString();

                // So let's put the stuff onto the console for viewing
                Console.WriteLine("Sinusoid changed at {0} to value {1}", changedTime, value);
            }
        }
    }
}

 

 

 

 

 

 

If you run the above application then I’d encourage you to open up SMT and change the value of sinusoid to see the event being handled in the console.

So my example is very simple but that’s been done on purpose. I hope it gives you an idea of how it works and you should use your imagination to think of what cool things you can do in your event handler.

I personally think this is an awesome thing to be able to do and will be playing about with some ideas of my own.

 

Hopefully you found this post useful and I’m now trying to think up my next topic

 

Ryan

Outcomes