PIUnitBatchList / PIUnitBatch Example

 

Option Explicit

Private Sub Command1_Click()
   Dim module As PIModule
   Dim startTime As New PITime
   Dim endTime As New PITime
   Dim unitBatch As PIUnitBatch
   Dim ubList As PIUnitBatchList
   Dim batch As PIBatch
   Dim i As Long
'
'  Create a Batch
'
   endTime.SetToCurrent
   startTime.UTCSeconds = endTime.UTCSeconds - 10  ' 10 second batch
   Set batch = PISDK.Servers.DefaultServer.PIBatchDB.Add("Batch1", "Product1", "Recipe1", startTime, endTime)
   
   On Error Resume Next
   Set module = PISDK.Servers.DefaultServer.PIModuleDB.PIModules.Add("PIUnitBatchesTest")
   Set module = PISDK.Servers.DefaultServer.PIModuleDB.PIModules.Item("PIUnitBatchesTest")
   module.IsPIUnit = True  ' make it a unit
   On Error GoTo 0
'
'  Add 3 UnitBatches to this module, and then add them to a batch at the same time
'
   endTime.SetToCurrent
   endTime.UTCSeconds = endTime.UTCSeconds - 5  ' start 5 seconds ago
   For i = 1 To 3
      startTime.UTCSeconds = endTime.UTCSeconds - 1    ' 1 second batch to avoid overlapping problems
      Set unitBatch = module.AddPIUnitBatch("UB1", "Product", startTime, endTime)
      batch.PIUnitBatches.Insert unitBatch   ' add it to this batch
      endTime = endTime + 1   ' next unitBatch
   Next
'
'  Get the unitBatches back in a list by searching
'
   endTime.SetToCurrent
   startTime.UTCSeconds = endTime.UTCSeconds - 3600   ' get the unitbatches for the last hour
   Set ubList = module.PIUnitBatchSearch(startTime, endTime)
'
'  List out the UnitBatches from the list
'
   List1.Clear
   LoadUnitBatchList ubList
'
'  List the UnitBatches from the UnitBatches collection of the PIBatch
'
   List2.Clear
   LoadUnitBatches batch.PIUnitBatches

End Sub

Private Sub LoadUnitBatchList(ubList As PIUnitBatchList)

   Dim tempStr As String
   Dim unitBatch As PIUnitBatch

   For Each unitBatch In ubList
      tempStr = unitBatch.BatchID & " " & unitBatch.Product & " " & unitBatch.startTime.LocalDate _
               & " " & unitBatch.endTime.LocalDate & " " & unitBatch.PIBatch.BatchID
      List1.AddItem tempStr
   Next

End Sub

Private Sub LoadUnitBatches(unitBatches As PIUnitBatches)

   Dim tempStr As String
   Dim unitBatch As PIUnitBatch

   For Each unitBatch In unitBatches
      tempStr = unitBatch.BatchID & " " & unitBatch.Product & " " & unitBatch.startTime.LocalDate _
               & " " & unitBatch.endTime.LocalDate & " " & unitBatch.PIBatch.BatchID
      List2.AddItem tempStr
   Next

End Sub

	
	
	
Enabling Operational Intelligence