IPICalculation Example

 

A couple examples of using the various methods of IPICalculation




Private Sub Calculate_Click()
On Error GoTo errhandler

' This example demonstrates how to evaluate an expression with natural sampling
' Then evaluate a second expression at the same timestamps as the first example.

Dim srv As Server
Dim ipiCalc As IPICalculation
Dim vals1 As PIValues, vals2 As PIValues, vals3 As PIValues
Dim expr1 As String, expr2 As String, startTime As String, endTime As String
Dim testTimes As Variant, cnt As Long, I As Long

Dim bCheck As Boolean, bValCheck As Boolean

    startTime = "y" ' yesterday midnight
    endTime = "y+1h"
    expr1 = "if 'cdm158' = ""auto"" then 1 else 0"
    expr2 = "'sinusoid' + 'cdt158'"

    Set srv = Servers("localhost")
    Set ipiCalc = srv                        ' Get pointer to IPICalculation Interface
 

' note that the SampleInterval argument is ignored if stRecordedValues is the SampleType
    Set vals1 = ipiCalc.Calculate(startTime, endTime, expr1, stRecordedValues, "")
    cnt = vals1.Count

    ReDim testTimes(1 To cnt) As Double
' Generate time stamps array
    For I = 1 To cnt
        testTimes(I) = vals1(I).TimeStamp.UTCSeconds
    Next I
    Set vals2 = ipiCalc.TimedCalculate(testTimes, expr2)
 

' Verify that the timedCalculate was performed at the same timestamps as the Calculate call.
    If vals2.Count <> vals1.Count Then
        MsgBox "timedCalculate did not return correct cnt"
        Exit Sub
    Else
        bCheck = False
        For I = 1 To cnt
            If (Abs(vals1(I).TimeStamp.UTCSeconds - vals2(I).TimeStamp.UTCSeconds) > 0.0001) Then
                bCheck = True
            End If
        Next I
        If (bCheck) Then
            MsgBox "timedcalculate did not have same timestamps"
            Exit Sub
        End If
    End If

 

' can just pass the pivalues as the timestamps to the TimedCalculate call.
    Set vals3 = ipiCalc.TimedCalculate(vals1, expr2)

' Verify that the two timedCalculate calls have the same results
    If vals2.Count <> vals3.Count Then
        MsgBox "timedCalculate with pivalues did not return correct cnt"
        Exit Sub
    Else
        bCheck = False
        bValCheck = False
        For I = 1 To cnt
            If (Abs(vals3(I).TimeStamp.UTCSeconds - vals2(I).TimeStamp.UTCSeconds) > 0.0001) Then
                bCheck = True
            End If
            If (Abs(vals3(I).Value - vals2(I).Value) > 0.01) Then
                bValCheck = True
            End If
        Next I
        If (bCheck) Then
            MsgBox "timedcalculate with pivalues did not have same timestamps"
        End If
        If (bValCheck) Then
            MsgBox "timedcalculate with pivalues did not have same values"
        End If
    End If

    Exit Sub

errhandler:
    MsgBox (Err.Description)
    Exit Sub

End Sub
 

 


Private Sub Summaries2_Click()
On Error GoTo errhandler

' This example demonstrates the Summaries2 and FilteredSummaries methods in the IPIData2
' interface as well as the PercentTrue method in the IPICalculation interface.


' First use Summaries2 to compute the daily time-weighted total for sinusoid for three days.
' Then call FilteredSummaries for all summary types for the same periods with a filter expression
'     depending on a different tag.
' Also call PercentTrue for the same filter expression for the same periods.
' Demonstrate that the amounts of time when the filter expression is true computed from PercentTrue
'     and those computed from FilteredSummaries using time-weighted Count are the same.
' Demonstrate that there is a difference in the daily time-weighted total when filter is applied.
' Finally, demonstrate how to access the additional summary result attributes for daily Maximum.

Dim srv As Server
Dim pt As PIPoint
Dim pdata As PIData
Dim ipid2 As IPIData2, ipiCalc As IPICalculation
Dim nvsSum As NamedValues, nvsFilSum As NamedValues
Dim valsum As PIValues, valfilsum As PIValues, valfiltime As PIValues, valtimetrue As PIValues
Dim filexpr As String, startTime As String, endTime As String, duration As String
Dim nv As NamedValue, msg As String
Dim filtime As Double, stime As PITime, etime As PITime, maxtime As PITime
Dim stime2 As PITime, etime2 As PITime

    startTime = "y-2d" ' three days ago
    endTime = "t" ' today
    duration = "1d" ' one day calculation period
    filexpr = "('ba:phase.1' = ""phase1"")"

    Set srv = Servers("localhost")
    Set ipiCalc = srv                         ' get pointer to IPICalculation Interface
    Set pt = srv.PIPoints("sinusoid")  ' perform summary calls on tag sinusoid
    Set pdata = pt.Data
    Set ipid2 = pdata                        ' get pointer to IPIData2 Interface

' calculate daily total for three days on sinusoid
    Set nvsSum = ipid2.Summaries2(startTime, endTime, duration, asTotal, cbTimeWeighted)
' extract the pivalues for total from the result
    Set valsum = nvsSum("Total").Value
' calculate the daily summary for sinusoid only when the batches are at phase1
' do all the summary types in one call
' note that fstExpRecordedValues is only supported by PI 3.4 and above
    Set nvsFilSum = ipid2.FilteredSummaries(startTime, endTime, duration, _
                                                                  filexpr, asAll, cbTimeWeighted, fstExpRecordedValues)
' extract the time-weighted total and Count from the FilteredSummaries result
    Set valfilsum = nvsFilSum("Total").Value
' The time-weighted Count returns the number of seconds when the calculation is active
' (i.e. pass the filter test)
    Set valfiltime = nvsFilSum("Count").Value
' Calculate the actual amount of time the batches are at phase1 using the PercentTrue method
    Set valtimetrue = ipiCalc.PercentTrue(startTime, endTime, duration, filexpr, fstExpRecordedValues)

' Check and Compare the results
    If (valsum.Count <> 3 Or valfilsum.Count <> 3 Or valfiltime.Count <> 3 Or _
         valtimetrue.Count <> 3) Then
        MsgBox "Result count is not correct"
        Exit Sub
    End If
    Dim I As Integer
    For I = 1 To 3
' start time and end time of each calculation period are stored in the
' pivalue.ValueAttributes for PercentTrue
        Set nv = valtimetrue(I).ValueAttributes("EarliestTime")
        Set stime = nv.Value
        Set nv = valtimetrue(I).ValueAttributes("MostRecentTime")
        Set etime = nv.Value
' Same start time and end time information from the pivalues generated
' by the Summaries methods
        Set nv = valsum(I).ValueAttributes("EarliestTime")
        Set stime2 = nv.Value
        Set nv = valsum(I).ValueAttributes("MostRecentTime")
        Set etime2 = nv.Value
' the start time and the end time from all these pivalues should be the same
' because the time specification are the same
        If (Abs(stime.UTCSeconds - stime2.UTCSeconds) > 0.0001 Or _
             Abs(etime.UTCSeconds - etime2.UTCSeconds) > 0.0001) Then
            MsgBox "Start time or End time not match among the pivalues"
            Exit Sub
        End If
' amount of time when filter is active from PercentTrue result is (percent true) * (period) / 100.
        filtime = valtimetrue(I).Value * (etime.UTCSeconds - stime.UTCSeconds) / 100#
' compare filtime with the time-weighted Count result from FilteredSummaries call
        If (Abs(filtime - valfiltime(I).Value) > 2) Then
            msg = "At period " & CStr(I) & " filter time result does not match"
            MsgBox msg
        End If
        Debug.Print I, stime.LocalDate, etime.LocalDate, _
                          valsum(I).Value, valfilsum(I).Value, filtime, valfiltime(I).Value
' print percent good for the total in the period
        Debug.Print "percend good: ", valsum(I).ValueAttributes("Percentgood").Value, _
                                                    valfilsum(I).ValueAttributes("Percentgood").Value
        Debug.Print " "
    Next I

' Examine the daily Maximum sinusoid when the batch is at phase1
    Set valfilsum = nvsFilSum("Maximum").Value
    Debug.Print "period", "Maxtime", "MaxValue"
    For I = 1 To 3
' extract the maximum time from the pivalue ValueAttributes
        Set nv = valfilsum(I).ValueAttributes("TimeOfMaxVal")
        Set maxtime = nv.Value
        Debug.Print I, maxtime.LocalDate, valfilsum(I).Value
    Next I

' Will not find Maximum in the NameValues returned by the Summaries2 call
' because we only ask for total
    On Error Resume Next
    Set nv = nvsSum("Maximum")
    If (Err.Number <> 0) Then
        Err.Clear
        MsgBox "It is OK not to find Maximum data when we only ask for Total"
    Else
        MsgBox "Something is wrong, get Maximum data when we only ask for Total"
    End If
' Will not find TimeOfMaxVal in PIvalues of a Total summary result
    Set nv = valsum(1).ValueAttributes("TimeOfMaxVal")
    If (Err.Number <> 0) Then
        Err.Clear
        MsgBox "It is OK not to find TimeOfMaxVal attribute in a PIvalue for a total"
    Else
        MsgBox "Something is wrong, get TimeOfMaxVal attribute in a PIvalue for a total"
    End If
    Exit Sub

errhandler:
    MsgBox (Err.Description)
    Exit Sub
End Sub
 

Enabling Operational Intelligence