AnsweredAssumed Answered

Getting Notification Instances from a target element

Question asked by John_Fors on Jul 28, 2016
Latest reply on Jul 29, 2016 by gregor

I can't seem to get my code to retrieve instance(s) of a Notification that I may display the results.  I've isolated an element with notifications historized as can be seen using PI-Explorer.

With the code below I get an Exception fault on the line "GetLastInstance" or previously "GetInstances" (line 28, 30) .  Same exception for either method (see below code).  Any pointers or comments greatly appreciated.

 

  1.       private void SearchNotifications3()
  2.         {
  3.             AFDatabase myAFDB;
  4.             AFElement tmpElement;
  5.             AFFindElement AFfecntr;
  6.             AFNamedCollection<AFNotification> myNots;
  7.             OSIsoft.AF.Time.AFTime StartTime = new OSIsoft.AF.Time.AFTime("*");
  8.             OSIsoft.AF.Time.AFTime EndTime = new OSIsoft.AF.Time.AFTime("*-7d");
  9.             // Example AFNotification.FindNotificationsByElement
  10.             // Get an AFElement where we know we have notifications
  11.             AFfecntr = new AFFindElement(); // new helper form with find control
  12.             myAFDB = afDatabasePicker1.AFDatabase;
  13.             AFfecntr.afFE.Database = myAFDB;
  14.             tmpElement = AFfecntr.afFE.GetElement("HMRoot\\PIL1");
  15.             ANNotificationList notificationList = new ANNotificationList
  16.                      (tmpElement.GetNotifications(AFSortField.StartTime, AFSortOrder.Ascending, 32));
  17.             myNots = tmpElement.GetNotifications(AFSortField.StartTime, AFSortOrder.Ascending, 32);
  18.             foreach (AFNotification myNot in myNots)
  19.             {
  20.                 // transmute the AFNotification to an ANNotification object
  21.                 ANNotification myAnNotification = new ANNotification(myNot);
  22.                 ANInstance myinstance = myAnNotification.GetLastInstance();
  23.                 // now we should be able to GetInstances
  24.                 ANInstances instances = myAnNotification.GetInstances(StartTime, EndTime);
  25.                 foreach (ANInstance myInstance in instances)
  26.                 {
  27.                     LogMsg(myNot.Target.ToString() + " " +
  28.                             myInstance.StartTime.LocalTime.ToString() + "  " +
  29.                             myInstance.EndTime.LocalTime.ToString() + "  " +
  30.                             myInstance.State.Name );
  31.                 }
  32.             }
  33.         }

Exception information: 

System.TypeInitializationException was unhandled

  HResult=-2146233036

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

  Source=OSIsoft.PIAnalytics.SDK

  TypeName=OSIsoft.AN.Notification.ANNotificationHistoryManager

  StackTrace:

       at OSIsoft.AN.Notification.ANNotificationHistoryManager.GetLastInstance(AFNotification notification, ANGetInstanceMode activeInstance, ANGetInstanceMode nonActiveInstance)

       at OSIsoft.AN.Notification.ANNotification.GetLastInstance() in c:\Builds\152\PIANO\Notifications 1.2.1205 Nightly\Sources\1.2.1205\Library\ANSDK\Notification\ANNotification.cs:line 454

       at MdbInterfaceAF.MDBImport.SearchNotifications3() in C:\Projects\MdbInterfaceImportAF\MdbInterfaceAF\MdbInterfaceAF\MDBImport.cs:line 127

       at MdbInterfaceAF.MDBImport.ImportBtn_Click(Object sender, EventArgs e) in C:\Projects\MdbInterfaceImportAF\MdbInterfaceAF\MdbInterfaceAF\MDBImport.cs:line 84

       at System.Windows.Forms.Control.OnClick(EventArgs e)

       at System.Windows.Forms.Button.OnClick(EventArgs e)

       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

       at System.Windows.Forms.Control.WndProc(Message& m)

       at System.Windows.Forms.ButtonBase.WndProc(Message& m)

       at System.Windows.Forms.Button.WndProc(Message& m)

       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)

       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

       at System.Windows.Forms.Application.Run(Form mainForm)

       at MdbInterfaceAF.Program.Main() in C:\Projects\MdbInterfaceImportAF\MdbInterfaceAF\MdbInterfaceAF\Program.cs:line 19

       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)

       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)

       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)

       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

       at System.Threading.ThreadHelper.ThreadStart()

  InnerException:

       HResult=-2147467262

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

       Source=OSIsoft.PIAnalytics.SDK

       StackTrace:

            at OSIsoft.AN.Notification.ANNotificationHistoryManager..cctor() in c:\Builds\152\PIANO\Notifications 1.2.1205 Nightly\Sources\1.2.1205\Library\ANSDK\Notification\ANNotificationHistoryManager.cs:line 60

       InnerException:

Outcomes