11 Replies Latest reply on Apr 11, 2018 8:15 AM by John Messinger

    Latest PI Notifcations code example

    Tfenn

      Hi there are a lot of old posts on Notifications SDK.  Where can I find code examples using the latest SDK for Notifications.  Basically needing to programitically retreive the notification history for a particular AF element.

       

       

       

      Thanks - Tony

        • Re: Latest PI Notifcations code example
          Mike Zboray

          There are samples in the PI Notifications Developer Tools for Notifications 2012. It is available in the Download Center under the "PI Analytics" category. There is a sample project called "ListNotificationInstanceCount" which you should take a look at. It will list the notification history items (these are called "Instances" in the SDK) for all notifications in a database. Just replace the AFNotification.FindNotifications search with AFNotification.FindNotificationsByElement to get only the notifications targeting a specific element.

            • Re: Latest PI Notifcations code example
              Tfenn

              Thanks - gone thru these but there doesnt seem to be an example (or indication in the help) how to get the 'body' of a notification.  I want to be able to list the subject and/or body of the message without the code knowing what that is.  Can you point me to the property/method to call?

               

              thanks

                • Re: Latest PI Notifcations code example
                  Tfenn

                  Can someone please give me an example of using the new AF SDK to access the notification history over a specified time period and obtain result parameters such as StartTime, EndTime, Trigger Condition, IsAcknowledged etc.

                   

                  The PI Notifications Developer Tools for Notifications 2012 does not show how to process Notification results history, but concentrates on building Notifications and looking at Notification configuration results.   I cannot find a good example of processing actual Notification history results.

                   

                  I can only find examples of processing notification history as ANNotitificationInstances as per example in vcampus.osisoft.com/.../6618.aspx  

                   

                  Is this still the preferred way to do this ??

                    • Re: Latest PI Notifcations code example
                      mhamel

                      @Tony: This is still the way to go to browse the history of notifications. I have added also an example of code illustratring what you have asked.

                       
                      'Get a reference on the database.        
                      _MyDB = _PISystem.Databases("My AF Database")
                      
                      'Contain all AFNotification objects that meet the criteria applied to the FindNotifications metho.
                      Dim MyAFNotificationsList As AFNamedCollectionList(Of Notification.AFNotification) = Nothing
                      'Transform the AFNotification objects from the AF to AN world.
                      Dim MyANNotificationList As ANNotificationList = Nothing
                      'Contain the list of all notifications accross all AFNotification objects.
                      Dim NotificationInstancesList As List(Of ANNotificationInstances) = Nothing
                      'Contain all avent instances of a given notification
                      Dim NotificationInstances As ANNotificationInstances = Nothing
                      'This object will hold the events were creation/closure of a notification have taken place or
                      'events were information was sent throughout delivery channel such as the e-mail.
                      Dim NotificationEventInstance As ANInstance = Nothing
                      Dim StartTime As Time.AFTime = New Time.AFTime("*-7d")
                      Dim EndTime As Time.AFTime = New Time.AFTime("*")        
                      Dim MessageTemplate As String = "Notification {0} last triggered at {1} and is {2}"
                      
                      'Get the AFNotification objects, equivalent to a SELECT TOP 100...
                      MyAFNotificationsList = OSIsoft.AF.Notification.AFNotification.FindNotifications(_MyDB, "*", AFSearchField.Name, AFSortField.Name, AFSortOrder.Ascending, 100)
                      'Transform them from AF to AN world.
                      MyANNotificationList = New ANNotificationList(MyAFNotificationsList)
                      'Get the list of all notification instances accross all AFNotification objects between the start and end time.
                      NotificationInstancesList = MyANNotificationList.GetInstances(StartTime, EndTime)
                      
                      'Browse each instance when a notification took place.
                      For Each NotificationInstances In NotificationInstancesList
                           'Browse event instances to 
                           If NotificationInstances.Instances IsNot Nothing And NotificationInstances.Instances.Count > 0 Then
                      
                                For Each NotificationEventInstance In NotificationInstances.Instances
                                     'Process your StartTime, EndTime, IsAcknowledged properties
                                     Console.WriteLine( _
                                               String.Format(MessageTemplate, _
                                                                MyAFNotificationsList(NotificationInstances.NotificationID).Name, _
                                                                NotificationEventInstance.StartTime.ToString("yyyy-MM-dd HH:mm:ss"), _
                                                                IIf(NotificationEventInstance.IsAcknowledged, "acknowledged", "unacknowledged")))
                                Next
                           End If
                      Next
                      

                       

                       

                      The trigger condition is not exposed at this level. You will need to validate against the AnalysisRule object to find out.

                        • Re: Latest PI Notifcations code example
                          Tfenn

                          Great that confirmed what I thought.  I was confused with the mix of ANNotification and AFNotification.

                          • Re: Latest PI Notifcations code example
                            Mike Zboray

                            Can you expand on what you mean by "list the subject and/or body of the message" because there are multiple ways to interpret this.

                             

                            A message using delivery formats combines data from configuration-time and run-time in the message that is sent to subscribers. Do you want to show

                             

                            1) The configuration-time information in a user interface. Similar to the Design in Message tab in the PSE. There may be an existing control that can do this.

                             

                            2) Raw configuration-time data in a serialized format.

                             

                            3) A 'test' similar to the Previews available in the Message tab of PSE. The configuration-time information is substituted with dummy information.

                             

                            4) The actual message that was sent to a subscriber for a specific notification using run-time data.

                             

                            Item 2 is an implementation detail and is subject to change. Item 4 is not possible because the messages are constructed in-memory and are not historized.

                            • Re: Latest PI Notifcations code example
                              Tfenn

                              Thanks for the reply.  This seems to be working... most of the time.  

                               

                              However with this (c#) code

                               

                                             ANNotificationList notificationList = new ANNotificationList(AFNotification.FindNotifications(AFDb, null, AFSearchField.Name, AFSortField.Name, AFSortOrder.Ascending, 10));

                               

                                             List<ANNotificationInstances> notificationInstances = notificationList.GetInstances(start);

                               

                              I intermittently get an error on the second line

                               

                              The type initializer for 'OSIsoft.AN.Notification.ANNotificationHistoryManager' threw an exception.

                               

                              Unable to cast object of type 'System.__ComObject' to type 'PISDK.PISDKClass'.

                               

                              However the notificationList  does contain items (3 in this case).  As I say - this doesnt always happen... and the underlying data is always the same.

                               

                              Note - this is running within asp.net on an iis if this makes a difference

                               

                              thanks again

                                • Re: Latest PI Notifcations code example
                                  mhamel

                                  @Tony: Are you using the .NET Framework 4.0 in your project? If it is the case, can you validate that you are not using the option Embed Interop for the PI SDK assemblies (OSIsoft.PISDK.dll, OSIsoft.PISDKCommon.dll, OSIsoft.PITimeserver.dll,et c.)? Are you referencing the COM Type libraries directly rather than the interops?

                                   

                                  More explanations on this problem can be found on this thread. It can also be related to MTA/STA problem. The PI SDK in general is STA (single-threaded COM apartment), you will need some extra attention when working with PI SDK in a MTA threading environment. By default, IIS the Microsoft Web Server runs virtual application in MTA.

                                   

                                  This thread, this one and this one can also help you.

                                    • Re: Latest PI Notifcations code example
                                      Tfenn

                                      Thanks for the reply.  We're actually using .net 4.5... with visual studio 2012 within an MVC4 project.  The issue occurs both on the development machine inside a local copy of IIS express (during debugging) and on the production PI server which has IIS installed. In both cases there is probably only one connection being made during testing.

                                       

                                      There are references to the OSIsoft.PISDK dlls, Embed Interop is now false, and I've ensured that they are using the ones in

                                       

                                      C:\Program Files\PIPC\PISDK\PublicAssemblies

                                       

                                      apart from PISDKDlg where there is only a 32bit version in the (x86) folder...

                                       

                                      however the problem above appeared in a class that didnt ref these SDKs- only the AF and AN ones which dont have the Interop issue?  It seems odd to get the error

                                       

                                      Unable to cast object of type 'System.__ComObject' to type 'PISDK.PISDKClass'.

                                       

                                      when the call was for an AN item - "The type initializer for 'OSIsoft.AN.Notification.ANNotificationHistoryManager' threw an exception."

                                       

                                      Would the MTA issue cause an intermittent failure of reading?  The above line appears to work 50% of the time...

                                       

                                      I've done some research and this page has a suggestion for effectively producing an STA within MVC

                                       

                                      www.west-wind.com/.../Creating-STA-COM-compatible-ASPNET-Applications

                                       

                                      We're looking at whether to do this or find an alternative way of storing the notifications - possibly with an additional end point.

                                       

                                      Any thoughts or suggestions you have would be helpful!

                                       

                                      thanks again

                                  • Re: Latest PI Notifcations code example
                                    Henryk Schneider

                                    Is ANNotificationList no longer supported in the latest AF SDK (2.9.2) ?