6 Replies Latest reply on Jul 14, 2016 11:24 AM by howardh

    ProcessBook VB Update Tag with Annotation and Display Annotation

    howardh

      I am developing condition monitoring managed from a Element Relative ProcessBook display using analysis in AF. The issue for me is I am not very good with VB and can normally 'fudge' my way through but this time I need some help.

       

      The PB has a: date picker (ThisDisplay.DateReset.Value), time picker (ThisDisplay.TimeReset.Value), annotation input textBox (TextAnnotate) and a RichTextBox (TextAnnDisplay) that displays what was just annotated with the user, annotation date/time and annotation. I attached an image at the bottom

       

      The process is: User selects an element which populates the PB, since the pump was changed they select date/time and enter reason for change (annotation) then clicks Update Reset Time button. The code checks to make sure 'something' has been annotated then proceeds writing the date/time to Reset PI tag type string which by analysis is parsed to timestamp then to float for calculations, then writes the timestamp to a PI tag for later retrieval to display the last annotation for the selected element. User can also disable either runtime or cycles dependant on what is needed by setting setpoint and visual to 0. Display will indicate disabled. The last part of the code writes the annotation with the username, date/time annotated and annotation value to the RichTextBox. All values will update dependant on element is selected.

       

      CODE

       

      Sub UpdateReset()

      Dim srv As server
      Dim tagname As String
      Dim tag As PIPoint
      Dim Timestamp As String
      Dim tagValue As Variant
      Dim i As Integer
      Dim startTime As String
      Dim inte As Variant
      Dim vrDat As Variant
      Dim vrStatus As Variant
      Dim element As String

      ' Make sure 'some' text is entered for annotation
      If TextAnnotate.Text = "" Then
          MsgBox " You must update the 'Reset' Annotation field with a valid reason to proceed"
          Exit Sub
      End If

      Set srv = Servers.DefaultServer

      element = ThisDisplay.Value11.GetValue(vrDat, vrStatus)
      tagname = "P2.Runtime." & ThisDisplay.Value11.GetValue(vrDat, vrStatus) & "_Reset"
      tagValue = ThisDisplay.DateReset.Value & " " & ThisDisplay.TimeReset.Value
      startTime = ThisDisplay.DateReset.Value & " " & ThisDisplay.TimeReset.Value

      If MsgBox("Are you sure you want to use  " & tagValue & "  for the date and time  " & element & "  was maintained?", _
      vbYesNo + vbQuestion, "Runtime Reset Confirm") = vbNo Then Exit Sub

      Set tag = srv.PIPoints(tagname)
      tag.Data.UpdateValue tagValue, startTime, dmInsertDuplicates

         Dim oSDK As New PISDK.PISDK
         Dim myAnns As New PISDK.PIAnnotations
         Dim textAnn As PISDK.PIAnnotation
         Dim val As PISDK.PIValue
         Dim time As New PITimeServer.PITime
         Dim nvs As New PISDKCommon.NamedValues
         Set val = New PISDK.PIValue

         time.SetToCurrent
         Set val.Timestamp = time
         val.Value = tagValue

      '   Set textAnn = myAnns.Add("textann", "textann", TextAnnotate.Value, False, "String")
         Set textAnn = myAnns.Add("textann1", "textann", TextAnnotate.Value, False, "string")
         nvs.Add "Annoations", 1
         nvs.Add "Annotations", myAnns
         Set val.ValueAttributes = nvs
         tag.Data.UpdateValue val, 0, PISDK.DataMergeConstants.dmReplaceDuplicates

      MsgBox "Reset Time write sucessful. It may take a minute for all values to update", 0, "PI Reset Time Change"
          
      TextAnnotate.Text = ""
      'DateReset.Value = ""
      'TimeReset.Value = ""

         Dim valValores As PIValues
         Dim oNVsRetrievalAttributes As New NamedValues
         Dim oNVValueAttribute As NamedValue2
         Dim oPIAnnotations As PIAnnotations
         Dim strAnnotationCr As String
         Dim strAnnotationCD As String
         Dim strAnnotationMr As String
         Dim strAnnotationMD As String

      'set the retrieval attributes so the RecordedValues query knows to retrieve the annotation information.
         oNVsRetrievalAttributes.Add "Annotations", True
         Set tag.Data.RetrievalAttributes = oNVsRetrievalAttributes

      'get the PIValue objects with the time range that have annotations
         Set valValores = tag.Data.RecordedValues(ThisDisplay.DateReset.Value & " " & ThisDisplay.TimeReset.Value, "", btOutside, "IsSet('" & tag.Name & "', ""Annotated"")", fvRemoveFiltered)
        
         Dim NumItems As Integer
          NumItems = valValores.Count

      Set tagValue = valValores(NumItems)
      'get the annotations
         Set oPIAnnotations = tagValue.ValueAttributes.Item("annotations").Value
      'get the first annotation
         strAnnotationCr = oPIAnnotations.Item(1).Creator
         strAnnotationCD = oPIAnnotations.Item(1).CreationDate
         strAnnotationMr = oPIAnnotations.Item(1).Value

      ' Converts Unix time
         Dim intDays As Integer, intHours As Integer, intMins As Integer, intSecs As Integer

          intDays = strAnnotationCD \ 86400
          intHours = (strAnnotationCD Mod 86400) \ 3600
          intMins = (strAnnotationCD Mod 3600) \ 60
          intSecs = strAnnotationCD Mod 60
         
          strAnnotationCDc = DateSerial(1970, 1, intDays + 1) + TimeSerial(intHours, intMins, intSecs)

         'MsgBox "User: " & strAnnotationCr & vbNewLine & "Updated: " & strAnnotationCDc & vbNewLine & "" & vbNewLine & strAnnotationMr
      TextAnnDisplay.Text = "User: " & strAnnotationCr & vbNewLine & "Updated: " & strAnnotationCDc & vbNewLine & "" & vbNewLine & strAnnotationMr

      End Sub

       

       

      ProcessBook display with code

       

      Message was edited by: Dale Rhodes - Updated VBA code to match what is current

       

      Message was edited by: Dale Rhodes Added Unix time conversion

       

      Message was edited by: Dale Rhodes Corrected 'tag.Data.RecordedValues' filter to support variable

       

      Message was edited by: Dale Rhodes - Final update to working code