AnsweredAssumed Answered

Issue with AFTimeRange Instance

Question asked by DeepSkyMan on Jul 29, 2020
Latest reply on Jul 29, 2020 by Rick_Davin_3.0

Hi there I am breaking new ground with the pipoint.summaries method and am having a problem with the associated time range.  In the following exampe I need to return the average for a 30min period 08:00 to 08:30, however when I ask for a time range the method returns a time range that is out by an addtional hour (09:00 to 09:30).  Curiously at the end of the For Each loop when I assign the value, timestamp and tag to a PIValue instance the time stamp reverts to the period commencing 08:00, however this is incorrect with respect to the data which logically is from 09:00 to 09:30.  I need to return a collection of PIValues as returning AFValues would break an existing legacy interface.  Here is the code:

 

    Public Function GetHalfHourPiDataFiltered() As String
        Dim result As String = ""
        Dim afvals As AFValues
        Dim afVal As AFValue
        Dim piValues As PIValues = New PIValues
        Dim piValue As PIValue = Nothing
        Dim offSetOfForThisTime As DateTime = Nothing
        Dim piVal As New PIValue
        Dim ts As New TimeSpan(0, 30, 0)
        Dim endtime As DateTime
        Dim starttime As DateTime
        Dim timeRng As AFTimeRange
        Dim Inter As AFTimeSpan
        Dim afValsToSerialize As New AFValues
        Dim tsh As New TimeSpan(1, 0, 0)

        Try
            offSetOfForThisTime = GetNearestTimeStamp(_forThisTime, 30, utils.eRoundingDirection.up)
            endtime = offSetOfForThisTime '#7/22/2020 08:30:00 AM#
            starttime = endtime.Subtract(ts) '#7/22/2020 08:00:00 AM#
            ' Create the Attribute
            Dim myAttribute As New AFAttribute(_myDB)
            'Now refresh the tags
            RESTRefreshTags()
            'Compile the collection of PI Data
            'For each point source
            For Each piPointSource In _spenPntSrcs
                ''For each tag in the point source
                For Each piPoint In piPointSource.PointList
                    myAttribute.Name = piPoint.Name
                    myAttribute.Type = GetType(Double)
                    piValue = New PIValue
                    timeRng = New AFTimeRange(starttime, endtime) '{22/07/2020 09:00:00 - 22/07/2020 09:30:00}
                    Inter = New AFTimeSpan(0, 0, 0, 0, 30)
                    Dim summarizedvals = piPoint.Summaries(timeRng, Inter, AFSummaryTypes.Average, AFCalculationBasis.TimeWeighted, AFTimestampCalculation.Auto)
                    afvals = summarizedvals(AFSummaryTypes.Average)
                    afVal = afvals.Item(0)
                    If afVal.ValueTypeCode = TypeCode.Decimal Or afVal.ValueTypeCode = TypeCode.Single Or afVal.ValueTypeCode = TypeCode.Double Then
                        piValue.Value = Math.Round(afVal.Value, 4)
                    ElseIf afVal.ValueTypeCode = TypeCode.Object Or afVal.ValueTypeCode = TypeCode.String Then
                        piValue.Value = afVal.ToString
                    End If
                    piValue.PointName = afVal.PIPoint.Name
                    piValue.TimeStamp = afVal.Timestamp 'Results in a discrepancy of one hour here between pi.value.timestamp ("22/07/2020 08:00:00") AND  afval.timestamp {22/07/2020 09:00:00}
                    piValues.Add(piValue)
                Next
            Next
            Return ToJSON(piValues)
        Catch ex As Exception
            Throw
        Finally
            piValue = Nothing
            piValues = Nothing
            afVal = Nothing
            afvals = Nothing
            _piServer = Nothing
            _piPoints = Nothing
            _piPointSource = Nothing
            _spenPntSrcs = Nothing
        End Try
    End Function

Outcomes