12 Replies Latest reply on Jul 8, 2011 3:48 AM by schugh@nalco.com

    ACE code is not working as expected- It is regarding addition  of our data to MDB and also to  ACE context to perform our custom calculation

    schugh@nalco.com

      Kindly advise . This code is not able to add the MDB stuff to ACE context

       
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      
          Dim mySDK As New PISDK.PISDK
          Dim srv As Server
      
          Dim membrane_name As String
          membrane_name = membraneName.Text
      
          Dim train_name As String
          train_name = TrainName.Text
          Dim tag1 As String
          tag1 = Tag1Text.Text
          Dim tag2 As String
          tag2 = Tag2Text.Text
          Dim tag3 As String
          tag3 = Tag3Text.Text
      
          Dim myMod1 As PISDK.PIModule
          Dim myMod2 As PISDK.PIModule
          Dim myMod3 As PISDK.PIModule
      
          srv = mySDK.Servers("usnap3dpi90")
          srv.Open()
      
          Try
              ' Add "Meters" module to root
              myMod1 = srv.PIModuleDB.PIModules.Add("Nalco Membranes")
          Catch ex As Exception
              myMod1 = srv.PIModuleDB.PIModules("Nalco Membranes")
              MsgBox("Nalco Membranes already exists")
          End Try
      
          Try
              ' Add "Train1" module under the "Meters" module
              myMod2 = myMod1.PIModules.Add(membrane_name)
          Catch ex As Exception
              myMod2 = myMod1.PIModules(membrane_name)
              MsgBox(membrane_name + " " + "already exists")
          End Try
      
          Try
              ' Add "Train1" module under the "Meters" module
              myMod3 = myMod2.PIModules.Add(train_name)
          Catch ex As Exception
              myMod3 = myMod2.PIModules(train_name)
              MsgBox(train_name + " " + "already exists")
          End Try
      
          Dim pt1 As PISDK.PIPoint
          Dim pt2 As PISDK.PIPoint
          Dim pt3 As PISDK.PIPoint
      
          pt1 = srv.PIPoints(tag1)
          pt2 = srv.PIPoints(tag2)
          pt3 = srv.PIPoints(tag3)
          Try
              myMod3.PIAliases.Add("Tag1", pt1)
              myMod3.PIAliases.Add("Tag2", pt2)
              myMod3.PIAliases.Add("Tag3", pt3)
          Catch ex As Exception
              MsgBox("Aliases already exists")
          End Try
      
          Dim mdbFunc As New PIACEMDBFunctions
          Dim ScheduleInfo As New NamedValues
          Dim ContextPath As String
          ContextPath = "\\usnapedpi90\default\NoSQL\Test\\\usnapedpi90\Nalco Membranes\" + membrane_name + "\" + train_name
          ScheduleInfo.Add("Priority", 3)
          ScheduleInfo.Add("Type", 1)
          ScheduleInfo.Add("Latency", 15)
          ScheduleInfo.Add("Tag1", tag1)
      
          Try
              PIACEMDBFunctions.AddPIACEContextSchedule(ContextPath, ScheduleInfo)
           Catch ex As Exception
              MsgBox(ex.Message)
          End Try
      
      End Sub
      

       

        • Re: ACE code is not working as expected- It is regarding addition  of our data to MDB and also to  ACE context to perform our custom calculation
          Ahmad Fattahi

          These are methods to access MDB in your PI ACE code. It is assuming that there are no privilege problems to the MDB. The sample codes should be a showing how to get the job done.

           

          Method 1: in order to access the PI MDB, you must add a reference to OSIsoft.PISDK. The common way of accessing it is to simply reference the module and its properties directly from within the PI ACE Calculation. However, this might cause SVCHOST.EXE process to use a lot of CPU. Here is a sample code:

           
              Public Overrides Sub ACECalculations()
                  Dim pimdbContext As PISDK.PIModule
                  pimdbContext = OSIsoft.PI.ACE.PIACEBIFunctions.GetPIModuleFromPath(Context)
                  Output_Tag.Value = pimdbContext.PIProperties("Prop_1").Value + pimdbContext.PIProperties("Prop_2").Value + _ 
                  pimdbContext.PIProperties("Prop_3").Value + pimdbContext.PIProperties("Prop_4").Value + _ 
                  pimdbContext.PIProperties("Prop_5").Value
              End Sub
          
              Protected Overrides Sub InitializePIACEPoints()
           Output_Tag = GetPIACEPoint("Output_Tag")
              End Sub
          
              '
              ' User-written module dependent initialization code
              '
              Protected Overrides Sub ModuleDependentInitialization()
              End Sub
          

           

           

          Method 2: To mitigate the CPU issue above you can use IRefresh interface. For this, you must add a reference to OSIsoft.PISDKCommon.dll. Here is the complete PI ACE code:

           
          Imports OSIsoft.PI.ACE
          Imports OSIsoft.PI.ACE.PIACEBIFunctions 'For accessing the module from context path
          Imports PISDK   'For accessing the MDB
          
          Public Class Mod_4
              Inherits PIACENetClassModule
              Private Output_Tag As PIACEPoint
              '
              '      Tag Name/VB Variable Name Correspondence Table
              ' Tag Name                                VB Variable Name
              ' ------------------------------------------------------------
              '
              ' Output_Tag                                        Output_Tag
          
              Dim pimdbContext As PISDK.PIModule
              Dim Prop_1, Prop_2, Prop_3, Prop_4, Prop_5 As PISDK.PIProperty
              Dim iRefr As PISDKCommon.IRefresh   'Requires OSIsoft.PISDKCommon
          
              Public Overrides Sub ACECalculations()
                  iRefr.Refresh()         ' Refresh the module an its properties
                  Output_Tag.Value = Prop_1.Value + Prop_2.Value + Prop_3.Value + Prop_4.Value + Prop_5.Value
              End Sub
          
              Protected Overrides Sub InitializePIACEPoints()
           Output_Tag = GetPIACEPoint("Output_Tag")
              End Sub
          
              '
              ' User-written module dependent initialization code
              '
              Protected Overrides Sub ModuleDependentInitialization()
                  pimdbContext = OSIsoft.PI.ACE.PIACEBIFunctions.GetPIModuleFromPath(Context) 'Set the module
                  iRefr = pimdbContext    ' Prepare the IRefresh interface
                  Prop_1 = pimdbContext.PIProperties("Prop_1")    'Set the properties
                  Prop_2 = pimdbContext.PIProperties("Prop_2")
                  Prop_3 = pimdbContext.PIProperties("Prop_3")
                  Prop_4 = pimdbContext.PIProperties("Prop_4")
                  Prop_5 = pimdbContext.PIProperties("Prop_5")
              End Sub
          
              '
              ' User-written module dependent termination code
              '
              Protected Overrides Sub ModuleDependentTermination()
              End Sub
          End Class
          

           

            • Re: ACE code is not working as expected- It is regarding addition  of our data to MDB and also to  ACE context to perform our custom calculation
              schugh@nalco.com

              Thank you for your response.Could you please share if you have any sample code to add a context to PI ACE manager..its a bit urgent.for.me. A.quick response will be highly appriciated...i got a couple of.examples from tech support but they are not working as expected. If i can take your 20/30 mins on the exceptions ..our dev will become smooth...

                • Re: ACE code is not working as expected- It is regarding addition  of our data to MDB and also to  ACE context to perform our custom calculation
                  dtakara

                  Hi Sandeep,

                   

                  Please take a look at this post from Ahmad in another thread and let us know if this is what you were looking for.

                    • Re: ACE code is not working as expected- It is regarding addition  of our data to MDB and also to  ACE context to perform our custom calculation
                      schugh@nalco.com

                      Hi Daniel \Ahmad

                       

                      I tried to execute as per the instructions below and i didnt get any error and even i didnt get any success..

                       

                      Here is my code in C#.

                       

                      Kindly advise if I am doing anyting wrong here..

                       

                      using System;

                       

                      using System.Collections.Generic;

                       

                      using System.Linq;

                       

                      using System.Text;

                       

                      using System.Collections;

                       

                      using PIACECommon;

                       

                      using PISDKCommon;

                       

                      namespace MyAceManager

                       

                      {

                       

                      class MyAceManagerMain

                       

                      {

                       

                      private const int OneMinute = 60000;

                       

                      static void Main(string[] args)

                       

                      {

                       

                         PIACEModuleDBFunctions mdbFunc = new PIACEModuleDBFunctions();

                       

                         NamedValues ScheduleInfo = new NamedValues();

                       

                         string ContextPath = null;

                       

                         ContextPath = @"\\" + "usnap3dpi90" + @"\" + "NoSQL2" + @"\" + "test2" + @"\" + @"\\"+"Nalco Membranes"+  @"\" +"testmembrane" +  @"\"+ "train1"  ;

                       

                         ScheduleInfo.Add("Priority", 3);

                       

                         ScheduleInfo.Add("Type", 1);

                       

                         ScheduleInfo.Add("Latency", 15);

                       

                         ScheduleInfo.Add("Tag1", "cdm158");

                       

                         try

                       

                         {

                       

                             mdbFunc.AddPIACEContextSchedule(ContextPath, ScheduleInfo);

                       

                         }

                       

                         catch (Exception ex )

                       

                         {

                       

                             Console.WriteLine(ex);

                       

                         }

                       

                      }

                       

                      }

                       

                      }

                        • Re: ACE code is not working as expected- It is regarding addition  of our data to MDB and also to  ACE context to perform our custom calculation
                          nagarwal

                          Sandeep,

                           

                          Could you please verify that the ContextPath parameter is defined correctly. For PI ACE 2010 R2, you need to specify this using the following format:

                           

                          <server name>\<scheduler name>\<executable name>\<module name>\<context name> where <context name> points to the fully resolved path of the PIModule to which the ACE context is mapped. For example:

                           

                          PI Server: mypiserver

                           

                          Scheduler node: default

                           

                          Exe: HeatExchangerCalcs

                           

                          Module: HeatDutyCalc

                           

                          Context: \\mypiserver\HeatExchangers\Exchanger1

                           

                          ContextPath = mypiserver\default\HeatExchangerCalcs\HeatDutyCalc\\\mypiserver\HeatExchangers\Exchanger1

                           

                          Hope this helps.

                           

                          Regards,

                           

                          Nitin

                            • Re: ACE code is not working as expected- It is regarding addition  of our data to MDB and also to  ACE context to perform our custom calculation
                              nagarwal

                              Ok, I tested the following code and it seems to work. The key may be missing highlighted line in your code.

                               

                              -Nitin

                               

                               

                               
                              Imports PISDKCommon
                              Imports OSIsoft.PIACECommon
                              
                              Module Module1
                              
                                  Sub Main()
                                      Dim objMDBFunctions As New PIACEModuleDBFunctions
                                      
                              objMDBFunctions.SetACENet = True
                                      Dim strServerName As String = "mypiserver"
                                      Dim strSchedulerName As String = "default"
                                      Dim strExeName As String = "HeatExchangerCalcs1"
                                      Dim strModuleName As String = "HeatDutyCalc"
                                      Dim strContextName As String = "\\mypiserver\HeatExchangers\Exchanger11"
                              
                                      Dim strContextPath As String = strServerName + "\" + strSchedulerName + "\" + strExeName + "\" + strModuleName + "\" + strContextName
                                      Dim nvs As New NamedValues
                                      nvs.Add("Priority", 3)
                                      nvs.Add("Type", 1)
                                      nvs.Add("Latency", 15)
                                      nvs.Add("Tag1", "cdm158")
                              
                                      Try
                                          objMDBFunctions.AddPIACEContextSchedule(strContextPath, nvs)
                                      Catch ex As Exception
                                          Console.Write(ex.Message)
                                      End Try
                              
                                  End Sub
                              
                              End Module
                              

                               

                                • Re: ACE code is not working as expected- It is regarding addition  of our data to MDB and also to  ACE context to perform our custom calculation
                                  schugh@nalco.com

                                  I got this working Nitin. Can you advise us on how to add info about recalculation tags prgramatically ...can we do it the same way we did it for scheduling info...do we have api's for that

                                    • Re: ACE code is not working as expected- It is regarding addition  of our data to MDB and also to  ACE context to perform our custom calculation
                                      nagarwal

                                      Hi Sandeep,

                                       

                                      I am glad you got it working. For specifying recalculation tags, I believe you need to use the EditPIACEContextRecalcInfo method. Similar to AddPIACEContextSchedule method, you need to create a namedvalues collection (as shown in code sample).

                                       
                                            Dim nvRecalcInfo As New PISDKCommon.NamedValues()
                                            nvRecalcInfo.Add("Priority", enuPriorityCode.pcNormal)
                                            nvRecalcInfo.Add("Type", enuScheduleType.stNaturalScheduled)
                                            nvRecalcInfo.Add("Latency", 15)
                                            nvRecalcInfo.Add("Tag1", "TColdIn1")
                                            nvRecalcInfo.Add("Tag2", "THotIn1")
                                            nvRecalcInfo.Add("Tag3", "TColdOut1")
                                            nvRecalcInfo.Add("Tag4", "THotOut1")
                                      
                                            objMDBFunctions.EditPIACEContextRecalcInfo(strContextPath, nvRecalcInfo)
                                      

                                       

                                       

                                      You should check the vCampus Live! 2010 presentation - Delivering PI ACE applications in .NET (http://vcampus.osisoft.com/live_2010/m/live2010/7809.aspx) which covered programmatic manipulation of ACE contexts. We also posted the sample code from that presentation in the Download Center>Extras>PI ACE Context Manager Example.

                                        • Re: ACE code is not working as expected- It is regarding addition  of our data to MDB and also to  ACE context to perform our custom calculation
                                          schugh@nalco.com

                                          Hi

                                           

                                          I am unable to execute PI ACE mannual recalc code.. Kindly help at the earliest.

                                           
                                          class consoleapplication1
                                          
                                          {
                                          
                                          private const int OneMinute = 60000;
                                          
                                          static void Main(string[] args)
                                          
                                          {
                                          
                                             string piServerName = "usnap3dpi90";
                                          
                                             string aceExecutable = "NoSQL2";
                                          
                                             string aceModule = "test2";
                                          
                                             string aceContextName = @"\\usnap3dpi90\NalcoMembranes\testmembrane1\train1";
                                          
                                             string fullContext = @"\\" + piServerName + @"\"+"default"+@"\" + aceExecutable + @"\" + aceModule + @"\" + aceContextName;
                                          
                                                 string aceContextName1 = @"\\usnap3dpi90\NalcoMembranes\testmembrane1\train1";
                                          
                                             string fullContext1 = @"\\" + piServerName + @"\"+"default"+@"\" + aceExecutable + @"\" + aceModule + @"\" + aceContextName1;
                                          
                                             PIACEModuleDBFunctions mdbFunc = new PIACEModuleDBFunctions();
                                          
                                             mdbFunc.SetACENet = true;
                                          
                                             //NamedValues ScheduleInfo = new NamedValues();
                                          
                                             //ScheduleInfo.Add("Priority", 3);
                                          
                                             //ScheduleInfo.Add("Type", 1);
                                          
                                             //ScheduleInfo.Add("Latency", 15);
                                          
                                             //ScheduleInfo.Add("Tag1", "Tag1");
                                          
                                             //NamedValues RecalcInfo = new NamedValues();
                                          
                                             //RecalcInfo.Add("Tag1", "Tag1");
                                          
                                             try
                                          
                                             {
                                          
                                                 //mdbFunc.AddPIACEContextSchedule(fullContext, ScheduleInfo);
                                          
                                                 //mdbFunc.EditPIACEContextRecalcInfo(fullContext, RecalcInfo);
                                          
                                                 DateTime startdate = new DateTime(11, 07, 07, 10, 00, 00);
                                          
                                                 DateTime d1 = startdate.ToUniversalTime();
                                          
                                                 string s1= String.Format("{0:d-MMM-yy HH:mm:ss}", startdate);
                                          
                                                 double d2 = Convert.ToDouble(Convert.ToDateTime(s1).ToOADate());
                                          
                                                 DateTime enddate = new DateTime(11, 07, 07, 10, 30, 00);
                                          
                                                 DateTime d3 = enddate.ToUniversalTime();
                                          
                                                 string s2 = String.Format("{0:d-MMM-yy HH:mm:ss}", d3);
                                          
                                                 double d4 = Convert.ToDouble(Convert.ToDateTime(s2).ToOADate());
                                          
                                                 mdbFunc.AddManualRecalculation(fullContext1, d2,d4);
                                          
                                             }
                                          
                                             catch (Exception ex )
                                          
                                             {
                                          
                                                 Console.WriteLine(ex);
                                          
                                             }
                                          
                                          }
                                          
                                          }
                                          

                                           

                                           

                                          This code is not doing anyting to the output tag for the time range sent from code....

                                           

                                          Calculation is tag1+ tag2 only....

                                            • Re: ACE code is not working as expected- It is regarding addition  of our data to MDB and also to  ACE context to perform our custom calculation
                                              nagarwal

                                              Sandeep, 

                                               

                                               

                                               

                                              I am note sure if the parameters d2 and d4 are set correctly. You need to specify the StartTime and EndTime parameters as UTC seconds since 1-1-1970. You could compute that in any of the following two ways.

                                               

                                               

                                               

                                               

                                               
                                                    'Method1: Direct conversion 
                                                    Dim objReferenceTime As New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
                                                    Dim dtStartTime1 As New DateTime(2011, 7, 7, 10, 0, 0)
                                                    dtStartTime1 = dtStartTime1.ToUniversalTime
                                              
                                                    Dim dtEndTime1 As New DateTime(2011, 7, 7, 11, 0, 0)
                                                    dtEndTime1 = dtEndTime1.ToUniversalTime
                                              
                                                    Dim dblStartTime1, dblEndTime1 As Double
                                              
                                                    dblStartTime1 = dtStartTime1.Subtract(objReferenceTime).TotalSeconds
                                                    dblEndTime1 = dtEndTime1.Subtract(objReferenceTime).TotalSeconds
                                              
                                                    'Method2: Using PITime object
                                                    Dim ptStartTime As New PITimeServer.PITime
                                                    Dim ptEndtime As New PITimeServer.PITime
                                                    Dim dtStartTime2 As New DateTime(2011, 7, 7, 10, 0, 0)
                                                    Dim dtEndTime2 As New DateTime(2011, 7, 7, 11, 0, 0)
                                                    ptStartTime.LocalDate = dtStartTime2
                                                    ptEndtime.LocalDate = dtEndTime2
                                              
                                                    Dim dblStartTime2, dblEndTime2 As Double
                                                    dblStartTime2 = ptStartTime.UTCSeconds
                                                    dblEndTime2 = ptEndtime.UTCSeconds
                                              

                                               

                                               

                                               

                                               

                                               

                                               

                                              Regards

                                               

                                              -Nitin

                                               

                                               

                                               

                                               

                                              • Re: ACE code is not working as expected- It is regarding addition  of our data to MDB and also to  ACE context to perform our custom calculation
                                                dtakara

                                                Hi Sandeep,

                                                 

                                                Here is some C# code I just tested successfully to trigger the manual ACE recalculation programmatically:

                                                 
                                                using System;
                                                using PIACECommon;
                                                using PITimeServer;
                                                
                                                namespace ConsoleApplication1
                                                {
                                                    class Program
                                                    {
                                                        static void Main(string[] args)
                                                        {
                                                            string piServerName = "CGUIMARAESD630A";
                                                            string aceExecutable = "RecalcTest01Exe";
                                                            string aceModule = "CM01";
                                                            string aceContextName = @"\\CGUIMARAESD630A\MyFactory\Tank A";
                                                            string fullContext = @"\\" + piServerName + @"\" + "default" + @"\" + aceExecutable + @"\" + aceModule + @"\" + aceContextName;
                                                            Console.Write(fullContext);
                                                            PIACEModuleDBFunctions mdbFunc = new PIACEModuleDBFunctions();
                                                            mdbFunc.SetACENet = true;
                                                            try
                                                            {
                                                                DateTime startdate = new DateTime(2011, 7, 7, 10, 0, 0);
                                                                PITime t1 = new PITime();
                                                                t1.LocalDate = startdate;
                                                                DateTime enddate = new DateTime(2011, 7, 7, 10, 30, 0);
                                                                PITime t2 = new PITime();
                                                                t2.LocalDate = enddate;
                                                                mdbFunc.AddManualRecalculation(fullContext, t1.UTCSeconds, t2.UTCSeconds);
                                                            }
                                                            catch (Exception ex)
                                                            {
                                                                Console.WriteLine(ex);
                                                            }
                                                        }
                                                    }
                                                } 
                                                

                                                You need to add a reference to the following dll files in your project:

                                                • Program Files\PIPC\Library\PITimeServer.dll
                                                • Program Files\PIPC\ACE\PIACECommon.dll

                                                Of course, you also adapt the names of the ACE executable, ACE class module, PI Server and calculation context.