Exemple #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);
            }
        }
Exemple #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));
        }
Exemple #3
0
        /// <summary>
        /// Query for aggregation of count, min, max, sum and average for all active
        /// nodes of the given OPC UA server in the given search span as time histogram
        /// with interval
        /// </summary>
        /// <param name="searchSpan">Date and time span for the query</param>
        /// <param name="appUri">The OPC UA server application Uri</param>
        /// <param name="interval">Interval for Date Time Histogram</param>
        /// <returns>The aggregated nodes</returns>
        public async Task <AggregateResult> GetAllAggregatedNodesWithInterval(DateTimeRange searchSpan, string appUri, string nodeId, TimeSpan interval)
        {
            try
            {
                string id = TimeSpanToId(interval);

                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                PredicateStringExpression predicate = new PredicateStringExpression(
                    String.Format(OpcServerNodePredicate, appUri, nodeId));
                Aggregate aggregate = new Aggregate(
                    Expression.UniqueValues(OpcMonitoredItemId, PropertyType.String, opcMaxMonitoredItemId),
                    new Aggregate(Expression.DateHistogram(BuiltInProperty.Timestamp, IntervalSize.FromId(id)),
                                  new Aggregate(
                                      Expression.Count(),
                                      Expression.Min(OpcMonitoredItemValue, PropertyType.Double),
                                      Expression.Max(OpcMonitoredItemValue, PropertyType.Double),
                                      Expression.Average(OpcMonitoredItemValue, PropertyType.Double),
                                      Expression.Sum(OpcMonitoredItemValue, PropertyType.Double)
                                      )));

                AggregatesResult aggregateResults = await RDXQueryClient.GetAggregatesAsync(
                    searchSpan,
                    predicate,
                    new[] { aggregate },
                    _cancellationToken);

                // Since there was 1 top level aggregate in request, there is 1 aggregate result.
                AggregateResult aggregateResult = aggregateResults[0];

                stopwatch.Stop();
                RDXTrace.TraceInformation("GetAllAggregatedNodes query took {0} ms", stopwatch.ElapsedMilliseconds);

                return(aggregateResult);
            }
            catch (Exception e)
            {
                RDXTrace.TraceError("GetAllAggregatedNodes: Exception {0}", e.Message);
                return(null);
            }
        }
Exemple #4
0
        /// <summary>
        /// Query for aggregation of a given measure for the
        /// given node and given OPC UA server in the given 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>
        /// <param name="measure"></param>
        public async Task <double> GetAggregatedNode(DateTimeRange searchSpan, string appUri, string nodeId, AggregateExpression measure)
        {
            try
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                PredicateStringExpression predicate = new PredicateStringExpression(
                    String.Format(OpcServerNodePredicate, appUri, nodeId));
                Aggregate aggregate = new Aggregate(
                    measures: new AggregateExpression[] { measure }
                    );

                AggregatesResult aggregateResults = await RDXQueryClient.GetAggregatesAsync(
                    searchSpan,
                    predicate,
                    new[] { aggregate },
                    _cancellationToken);

                // Since there was 1 top level aggregate in request, there is 1 aggregate result.
                AggregateResult aggregateResult = aggregateResults[0];

                stopwatch.Stop();
                RDXTrace.TraceInformation("AggregateQuery query took {0} ms", stopwatch.ElapsedMilliseconds);

                if (aggregateResult.Measures == null)
                {
                    return(0.00);
                }

                return((double)aggregateResult.Measures[0]);
            }
            catch (Exception e)
            {
                RDXTrace.TraceError("AggregateQuery: Exception {0}", e.Message);
                return(0);
            }
        }