Ejemplo n.º 1
0
        /// <summary>
        /// Query to subtract the oldest value of a OPC UA node
        /// from the newest value in the search span
        /// </summary>
        /// <param name="searchSpan">Date and time span for the query</param>
        /// <param name="appUri">The OPC UA server application Uri</param>
        /// <param name="nodeId">The node id in the OPC UA server namespace</param>
        /// <returns>The difference</returns>
        public async Task <double> DiffQuery(DateTimeRange searchSpan, string appUri, string nodeId)
        {
            try
            {
                PredicateStringExpression expression = new PredicateStringExpression(
                    String.Format(OpcServerNodePredicate, appUri, nodeId));

                BaseLimitClause firstMember = new TopLimitClause(1,
                                                                 new[]
                {
                    new SortClause(new BuiltInPropertyReferenceExpression(BuiltInProperty.Timestamp),
                                   SortOrderType.Asc)
                });
                BaseLimitClause lastMember = new TopLimitClause(1,
                                                                new[]
                {
                    new SortClause(new BuiltInPropertyReferenceExpression(BuiltInProperty.Timestamp),
                                   SortOrderType.Desc)
                });

                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                Task <IEnumerable <IEvent> > firstTask = RDXQueryClient.GetEventsAsync(
                    searchSpan,
                    expression,
                    firstMember,
                    _cancellationToken);
                Task <IEnumerable <IEvent> > lastTask = RDXQueryClient.GetEventsAsync(
                    searchSpan,
                    expression,
                    lastMember,
                    _cancellationToken);
                await Task.WhenAll(new Task[] { firstTask, lastTask });

                IEnumerable <IEvent> firstEvent = firstTask.Result;
                IEnumerable <IEvent> lastEvent  = lastTask.Result;
                stopwatch.Stop();
                RDXTrace.TraceInformation("DiffQuery queries took {0} ms", stopwatch.ElapsedMilliseconds);

                long first = GetValueOfProperty <long>(firstEvent.First <IEvent>(), OpcMonitoredItemValue);
                long last  = GetValueOfProperty <long>(lastEvent.First <IEvent>(), OpcMonitoredItemValue);
                return(last - first);
            }
            catch (Exception e)
            {
                RDXTrace.TraceError("DiffQuery Exception {0}", e.Message);
                return(0);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Helper to query for last event of given station and nodeid
        /// </summary>
        private Task <IEnumerable <IEvent> > GetLatestEvent(DateTime endTime, string appUri, string nodeId)
        {
            PredicateStringExpression expression = new PredicateStringExpression(
                String.Format(OpcServerNodePredicate, appUri, nodeId));
            DateTimeRange   searchSpan = new DateTimeRange(endTime.Subtract(TimeSpan.FromDays(360)), endTime);
            BaseLimitClause lastMember = new TopLimitClause(1,
                                                            new[]
            {
                new SortClause(new BuiltInPropertyReferenceExpression(BuiltInProperty.Timestamp),
                               SortOrderType.Desc)
            });

            return(RDXQueryClient.GetEventsAsync(
                       searchSpan,
                       expression,
                       lastMember,
                       _cancellationToken));
        }