5 Replies Latest reply on Nov 25, 2015 9:33 AM by pthivierge

    using AFSDK output the Max of one tag to the value of another

    robert.low

      I am trying to do the .Net code below

       

              afval1 = New AFValue

              afval1.Timestamp = mynewdate

              afval1.Value = myReportNbr.Summary(myaftimestr, AFSummaryTypes.Maximum, AFCalculationBasis.TimeWeighted, AFTimestampCalculation.Auto).Max

              myRxPINbr.UpdateValue(afval1, AFUpdateOption.Insert, AFBufferOption.DoNotBuffer)

       

      but get an error assigning the value. How do you set the value of one tag = the Maximum of another?

        • Re: using AFSDK output the Max of one tag to the value of another
          Rick Davin

          Hey Robert.

           

          I presume you are using the AFData.Summary method though your signature doesn't quite match.  Reading myaftimestr makes me think of one AFTime value when the signature wants an AFTimeRange.

           

          What is returned is IDictionary, so using a dotted .Max won't cut it.  Instead it would be bracketed [AFSummaryTypes.Max].Value just to set the value.

           

          If you still have issues, perhaps post the error message.

            • Re: using AFSDK output the Max of one tag to the value of another
              robert.low

              the time range is set and the call returns the value I expect. the problem is the left side of the = sign. I am trying to set the afval1.Value and that is generating the error. I have code above and below that are simply grabbing the current.value of PI tags, all work fine. Trying to use the summary call is failing.

               

              I was wondering if I need to use a different variable assignment to grab the max value first?

                • Re: using AFSDK output the Max of one tag to the value of another
                  Rick Davin

                  I am rusty on my VB.NET so typing this free form:

                   

                  max = myReportNbr.Summary(myaftimestr, AFSummaryTypes.Maximum, AFCalculationBasis.TimeWeighted, AFTimestampCalculation.Auto)[AFSummaryTypes.Maximum]
                  
                  afval1 = New AFValue(max.Value, myNewDate, max.UOM, max.Status)
                  
                  myRxPINbr.UpdateValue(afval1, AFUpdateOption.Insert, AFBufferOption.DoNotBuffer)
                  

                   

                  Or this could work:

                   

                  afval1 = myReportNbr.Summary(myaftimestr, AFSummaryTypes.Maximum, AFCalculationBasis.TimeWeighted, AFTimestampCalculation.Auto)[AFSummaryTypes.Maximum]
                  
                  afval1.Timestamp = myNewDate
                  
                  myRxPINbr.UpdateValue(afval1, AFUpdateOption.Insert, AFBufferOption.DoNotBuffer)
                  

                   

                  Here is an old link I did comparing the PISDK summary methods to AFSDK.

                   

                  Keep in mind the summary call can fail, perhaps if there are no events within the time range.  You would still get an AFValue back, but it would have a bad status and the .Value would have an error message that doesn't necessarily map to a SYSTEM state.  If your attribute uses the PIPoint data reference, it may not like the assignment because its either a good number convertible to a Float32 or else a bad SYSTEM state.

                  1 of 1 people found this helpful
              • Re: using AFSDK output the Max of one tag to the value of another
                Eugene Lee

                Just wanted to add on that if you want to work with PIPoints (tags) directly in AF SDK. You can use the PIPoint class, bypassing the AFData class completely.

                • Re: using AFSDK output the Max of one tag to the value of another
                  pthivierge

                  Hello All,

                   

                  It seems that it would be useful to understand what type of object is afval1 in this situation.

                   

                  I have put together an example, in VB.NET that should help you to get started:

                   

                  Example that that takes max value of "maxTagName" and write the result into updateTagName

                   

                   Public Sub GetMaxAndUpdate(servername As String, maxTagName As String, updateTagName As String)
                  
                          Dim maxTag As PIPoint = Nothing
                          Dim updateTag As PIPoint = Nothing
                          Dim maxValue As AFValue = Nothing
                  
                  
                          ' gets the PI Server (PI Data Archive)
                          Dim server = PIServer.FindPIServer(servername)
                  
                          If server = Nothing Then
                              Throw New NullReferenceException("The specified PI Server could not be found")
                          End If
                  
                          ' getting the tags
                          maxTag = PIPoint.FindPIPoint(server,maxTagName)
                          updateTag = PIPoint.FindPIPoint(server,updateTagName)
                  
                  
                         ' Getting the maximum value of the last day
                          Dim timeRange = New AFTimeRange(startTime:="*-1d", endTime:="*")
                          Dim summaryResults As IDictionary(Of AFSummaryTypes, AFValue) = Nothing
                          summaryResults = maxTag.Summary(timeRange, AFSummaryTypes.Maximum, AFCalculationBasis.TimeWeighted, AFTimestampCalculation.Auto)
                  
                          maxValue = summaryResults(AFSummaryTypes.Maximum)
                  
                          ' writing the max value into the second tag, we use a timestamp of now
                          ' here, as the AFValue is not an AF Attribute, we pass Nothing in the constructor for the AFAttribute object
                          updateTag.UpdateValue( New AFValue(Nothing,maxValue.Value,DateTime.Now) , AFUpdateOption.Insert)
                          
                          ' waiting a bit before we read the result in the server to be certain result is written
                          Thread.Sleep(1000)
                          dim result=updateTag.CurrentValue()
                          Console.WriteLine(String.Format("Found Max Value for {0}: Max {1} at {2}",maxTagName,maxValue.Value.ToString(),maxValue.Timestamp))
                          Console.WriteLine(String.Format("Value of {0}: {1} - {2}",updateTagName,result.Value.ToString(),result.Timestamp))
                  
                      End Sub
                  

                   

                  Hope this helps