PIArchiveEditor Control Example

See Also

 

In this example, the public properties, methods and events of the PIArchiveEditor control are exercised.

Create a new Windows Forms project under Visual Studio.NET and site the PIArchiveEditor control on the form (see "how to" instructions).  Size the control so that it is fully visible.

Add 4 buttons and edit the Text properties as shown in the screen capture below. Make sure that they are named as button1 thru button4 from left to right. Double click each button in order to create "click" event handlers for them.

Select the Events tabs of the PIArchiveEditor Properties window and define/double-click on the errorEvent.

 

Add the following "using" statements:

    using OSIsoft.PISDK.Controls;

 

Define the following class variables:

    private PISDK.PISDK piSDK = null; // main PISDK object

 

Then replace the "empty" event handler methods with the following code:

 

    /// <summary>

    /// Archive editor raised an error.

    /// Show a messagebox with error information

    /// </summary>

    /// <param name="sender">PIArchiveEditor control</param>

    /// <param name="e">Error event arguments</param>

    private void piArchiveEditor1_errorEvent(object sender, ErrEventArgs e)

    {

        string msgText = "";

        if (e.errCode != 0)

            msgText = "Error Code: " + e.errCode.ToString() + "; ";

        msgText += "Err Message=" + e.message + "; Origin = " + e.origin;

        msgText += "; From " + e.sender;

        MessageBox.Show(this, msgText, "PIArchiveEditor Error");

    }

 

    /// <summary>

    /// Button1 event handler

    /// Get current data from the control. Make sure that there are events to get.

    /// This example just accesses the information, nothing is done with it.

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    private void button1_Click(object sender, System.EventArgs e)

    {

        // get the events

        PIArchiveEventList evList = piArchiveEditor1.EventList;

        int eventCount = evList.eventCount; // # retrieved

        // enumarate events

        foreach (PIArchEventData piEvent in evList)

        {

            object evValue = piEvent.eventValue; // get value

            PITimeServer.PITime pTime = piEvent.eventTime; // get time

            // get attributes

            bool substituted = piEvent.substituted;

            bool questionable = piEvent.questionable;

            object annot = piEvent.annotation; // annotation

            string annotType = "NULL"; // assume annotation is null

            if (annot is PISDK.PIAnnotations)

                annotType = "PIAnnotations"; // it is PIAnnotations collection

            else

                    annotType = "String"; // VARIANT string

            // Now do something usefull with this event....

        }

        MessageBox.Show(this, "EventList retrieved, event count was " +

            evList.eventCount.ToString(), "Button1 click");

    }

 

    /// <summary>

    /// Button2 event handler

    /// Get current/relevant information from the control and show in Messagebox

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    private void button2_Click(object sender, System.EventArgs e)

    {  // show tag, timerange, event count and selected value

        string msg = "Tagname=";

        if (piArchiveEditor1.PITag is PISDK.PIPoint)

            msg += ((PISDK.PIPoint)piArchiveEditor1.PITag).Name; // get tagname from point

        else if (piArchiveEditor1.PITag is string)

            msg += (string)piArchiveEditor1.PITag; // it is string..

        msg += " Even Count=" + piArchiveEditor1.EventList.eventCount.ToString()+ Environment.NewLine;

        if (piArchiveEditor1.CurrentValue != null)

            msg += "Selected value=" + piArchiveEditor1.CurrentValue.Value.ToString()+ Environment.NewLine;

        else

            msg += "Selected value is undefined" + Environment.NewLine;

        msg += "StartTime=" + piArchiveEditor1.StartTime.LocalDate.ToString() +

                "; EndTime=" + piArchiveEditor1.EndTime.LocalDate.ToString();

        MessageBox.Show(this, msg, "PIArchiveEditor data");

        // show filter params

        msg = "Filter=" + piArchiveEditor1.FilterExpression + Environment.NewLine; // expression

        msg += "BoundaryType=" + piArchiveEditor1.BoundaryType.ToString();

        msg += "; FilteredView=" + piArchiveEditor1.FilteredView.ToString() + Environment.NewLine;

        MessageBox.Show(this, msg, "PIArchiveEditor Filter setup");

    }

 

    /// <summary>

    /// Button3 event handler

    /// Read-only button clicked. Reverse the current action,

    /// i.e. allow or disallow edits.

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    private void button3_Click(object sender, System.EventArgs e)

    {

        if (piArchiveEditor1.AllowValueEdit)

        { // make it readonly

            piArchiveEditor1.AllowValueEdit = false; // prevent value edits

            piArchiveEditor1.AllowAnnotations = false; // and annotations

            button3.Text = "Allow Edits";

        }

        else

        { // allow edits

            piArchiveEditor1.AllowValueEdit = true;

            piArchiveEditor1.AllowAnnotations = true;

            button3.Text = "Set ReadOnly";

        }

    }

 

    /// <summary>

    /// Button 4 event handler

    /// Set "servers" button clicked. Reverse the action, i.e.

    /// start using server picklist or define a custom list

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    private void button4_Click(object sender, System.EventArgs e)

    {

        if (piArchiveEditor1.UseSrvPickList)

        { // Define a server list

            piArchiveEditor1.UseSrvPickList = false; // don't use picklist

            button4.Text = "Use PickList";

            if (piSDK == null)

                piSDK = new PISDK.PISDKClass(); // create PISDK only once

            // Add two first servers from known servers table

            int servIndex = 0; // reset server index

            string[] serverList; // empty server list

            if (piSDK.Servers.Count > 1)

                serverList = new string[2]; // allocate 2

            else

                serverList = new string[1]; // aloocate 1

            // Servers don't support indexing by number - use foreach instead

            foreach (PISDK.Server piServer in piSDK.Servers)

            {

                serverList[servIndex] = piServer.Name; // add this server

                if (++servIndex > 1 || piSDK.Servers.Count < 2)

                    break; // servers added - break

            }

            piArchiveEditor1.ServerList = serverList; // set the list

        }

        else

        { // start using picklist

            // First remove a server from the list (example only)

            string srvName = piArchiveEditor1.ServerList[0]; // get 1'st server

            piArchiveEditor1.modifyServerList(srvName, false); // remove it

            piArchiveEditor1.UseSrvPickList = true;

            button4.Text = "Set ServerList";

        }

    }

 

Compile and run the project. The form should with no data. Specify, or select, a valid tag for the editor control and press the "Get Events" button to retrieve data. Press each button to test the functionality.

To test the error event, specify an invalid tagname and press the "Get Events" button on the control.

 

Requirements

    Assembly: OSIsoft.PISDK.Controls.PIArchiveEditor (in OSIsoft.PISDK.Controls.PIArchiveEditor .dll)


Enabling Operational Intelligence