9 Replies Latest reply on Jul 5, 2011 9:35 AM by MichaelvdV@Atos

    Unregister a PI tag for eventing

    sandeep.narang

      Hi All

       

      I have used eventpipes to handle value changed events. My issue is that , on click of a button, I want to stop the value change events. I have tried to detach the OnNewValueChange() event like this :

       
      myEvents.OnNewValue -= myValHandler as PISDK._DEventPipeEvents_OnNewValueEventHandler;
      

      But id did not work.

       


      Can you please suggest me the workaround for this?

       


      Thanks

       

       Sachin

        • Re: Unregister a PI tag for eventing
          MichaelvdV@Atos

          Have a look at the IEventPipe2 interface. It has a AddSignUp and RemoveSignUp method. Its important to use these, because they will also remove the eventpipes on the server.

           

          I'm not sure about your notation, are you using C#?

           
                      //Adds the eventhandler
                      EventPipe.Events.OnNewValue += new PISDK._DEventPipeEvents_OnNewValueEventHandler(Events_OnNewValue);
          
                      //Removes the eventhandler
                      EventPipe.Events.OnNewValue -= new PISDK._DEventPipeEvents_OnNewValueEventHandler(Events_OnNewValue);
          
                      //Short notation to add eventhandler
                      EventPipe.Events.OnNewValue += Events_OnNewValue;
          
                      //Short notation to remove eventhandler
                      EventPipe.Events.OnNewValue -= Events_OnNewValue;
          
            • Re: Unregister a PI tag for eventing
              sandeep.narang

              Hi Michael

               

              Thanks for the post reply. Can you please send me a sample code snippet to listen for new changed events using AddSignUp and removeSignUp. I looked for an example but could not find it.

               

              I would appreciate your help.

               

              Thanks

               

              Sachin

                • Re: Unregister a PI tag for eventing
                  hanyong

                  Hi Sachin,

                   

                  There is a section in Chapter 7 of PI Application Development Course that goes through how to listen for new changed events. Together with the course materials, There is also a exercise template (Visual Studio project) as well as a solution that you can refer to. Just browse through the course material pack to find it. You can download the course materials from Training Center following the highlighted link:

                   

                  2548.AppDev.jpg

                   

                   

                    • Re: Unregister a PI tag for eventing
                      sandeep.narang

                      Hi

                       

                      I looked up at the training material but could not find my answer to original question

                       

                      i.e. How to unhook the events from event pipe. Michael told me to use AddSignUp method. I used the same

                       
                      PointList pListFinal = new PointList();
                      
                      PISDK.IEventPipe2 iEP2;
                      
                      iEP2.AddSignUp(pListFinal,null);
                      
                      iEP2 = (PISDK.IEventPipe2)myPipe; Delegate myValHandler = new 
                      
                      PISDK._DEventPipeEvents_OnNewValueEventHandler(myEvents_OnNewValue);
                      
                                         myEvents.OnNewValue += myValHandler as PISDK._DEventPipeEvents_OnNewValueEventHandler;
                      

                      but got some external component error.

                       

                      Please assist me in this implementation as this is really crucial for me.

                        • Re: Unregister a PI tag for eventing
                          hanyong

                          Hi Sachin,

                           

                          In your code, you are calling iEP2.AddSignUp() before pointing the iEP2 to an instance of EventPipe, is it done in the earlier part of your code. other than using IEventPipe2.AddSignUp() method, you can also do something like the following:

                          PISDK.PointList _ptlt;
                          PISDK.EventPipe _evtp;
                          PISDK._DEventPipeEvents_Event _Intf_EventPipeEvents;
                          object _NewValueHandler;

                          //code to get your PointList collection
                          ..... 
                           
                          // get EventPipe that tracks value changes to the points in the PointList (_ptlt) 
                          _evtp = _ptlt.Data.ArchiveEventPipe;  
                           
                          // add delegate 
                          EventPipeTypeToCreate = PISDK.EventPipeTypeConstants.eptPointList;
                          _Intf_EventPipeEvents = (PISDK._DEventPipeEvents_Event)_evtp;
                           
                          _NewValueHandler = new PISDK._DEventPipeEvents_OnNewValueEventHandler(EventPipe_OnNewData);
                          _Intf_EventPipeEvents.OnNewValue += (PISDK._DEventPipeEvents_OnNewValueEventHandler)_NewValueHandler;

                          This would sign up for the EventPipe for value changes of the points in the PointList and handle it in the EventPipe_OnNewData

                            • Re: Unregister a PI tag for eventing
                              hanyong

                              Small correction: the line on defining the EventPipeTypeToCreate should be:

                              EventPipeTypeToCreate = PISDK.EventPipeTypeConstants.eptPointListArchive

                              This is not used in the code snippet that I've provided but you can use it to track the type of EventPipe that is created.

                                • Re: Unregister a PI tag for eventing
                                  sandeep.narang

                                  I have implemented a WCF service which has following methods:

                                   

                                   

                                   

                                  1.  Register(string serverName, string tagName)

                                   

                                  2.  UnRegister(tagName)

                                   

                                   *******************

                                   

                                  public class Service1 : IService1
                                  {
                                      private PISDK.Server myPIServer;
                                      private static List<KeyValuePair<string, KeyValuePair<string, SessionStateClass>>> lstEventPipeStatus = new List<KeyValuePair<string, KeyValuePair<string, SessionStateClass>>>();
                                      private PISDK.EventPipe myPipe = default(PISDK.EventPipe);
                                      private PISDK._DEventPipeEvents_Event myEvents;

                                   

                                   public bool Register(string serverName, string tagName)
                                      {
                                          try
                                          {
                                              PISDK.IEventPipe2 iEP2;
                                              PointList pListFinal = new PointList();
                                              PISDK.PISDK mySDK = new PISDK.PISDK();

                                   

                                              if (!string.IsNullOrEmpty(serverName))
                                              {
                                                  myPIServer = mySDK.Servers[serverName];
                                              }
                                              else
                                              {
                                                  myPIServer = mySDK.Servers.DefaultServer;
                                              }

                                   

                                              if (!myPIServer.Connected)
                                                  myPIServer.Open("Trusted_Connection=true");

                                   

                                              if (myPIServer.Connected)
                                              {
                                                  myPipe = pListFinal.Data.EventPipe;
                                                  iEP2 = (PISDK.IEventPipe2)myPipe;
                                                  myEvents = (PISDK._DEventPipeEvents_Event)myPipe;
                                                  iEP2.AddSignUp(pListFinal,null);

                                   

                                                  Delegate myValHandler = new PISDK._DEventPipeEvents_OnNewValueEventHandler(myEvents_OnNewValue);
                                                  myEvents.OnNewValue += myValHandler as PISDK._DEventPipeEvents_OnNewValueEventHandler;

                                   

                                                  // THE FOOLOWING CODE IS TO MAINTAIN THE NECESSARY OBJECTS SO THAT THEY CAN BE USED TO UNREGISTER A TAG.
                                      SessionStateClass objSessionStateClass = new SessionStateClass();
                                      objSessionStateClass.EventPipe = myPipe;
                                      objSessionStateClass.MyValHandler = myValHandler;
                                      objSessionStateClass.PointList = pListFinal;
                                      lstEventPipeStatus.Add(new KeyValuePair<string, KeyValuePair<string, SessionStateClass>>(groupName, new KeyValuePair<string, SessionStateClass>("Running", objSessionStateClass)));
                                     }
                                    }
                                    catch (Exception ex)
                                    {
                                   
                                    }
                                   }

                                   

                                   private void myEvents_OnNewValue()
                                   {
                                    // Some Processing goes here
                                   }

                                   

                                   bool Unregister(string tagName)
                                   {
                                       EventPipe mPipe;
                                       PISDK.IEventPipe2 iEP2;

                                   

                                    foreach (KeyValuePair<string, KeyValuePair<string, SessionStateClass>> kvp in lstEventPipeStatus)
                                       {
                                           if (kvp.Key.Equals(groupName))
                                     {
                                            mPipe = kvp.Value.Value.EventPipe;
                                      kvp.Value.Key.Replace("Running", "Not Running");
                                      myEvents = (PISDK._DEventPipeEvents_Event)mPipe;
                                      iEP2 = (PISDK.IEventPipe2)mPipe;
                                      iEP2.RemoveSignUp(kvp.Value.Value.PointList, null);
                                      ((PISDK._DEventPipeEvents_Event)mPipe).OnNewValue -= new _DEventPipeEvents_OnNewValueEventHandler(myEvents_OnNewValue);
                                      break;
                                     }
                                    }
                                    
                                    return true;
                                   }
                                  }

                                   

                                  ********************

                                   

                                  Please review and let me know if I m doing something wrong. The basic solution i need is how to unregister the tag

                                   

                                   

                                   

                                   

                              • Re: Unregister a PI tag for eventing
                                MichaelvdV@Atos

                                Could you post the error message?

                                 

                                From your code snippet, it looks like you are calling the 'AddSignUp' method on the iEP2 variable before you assigned it (with the casted myPipe). You should get a NullReferenceException there...