public TimeSeriesPoint CreateTimeSeriesPoint(JToken timeSeriesPoint, string dataType)
        {
            string   tsp_Value         = "";
            string   tsp_Quality       = "";
            DateTime tsp_TimeStamp_UTC = DateTime.Now;

            foreach (JProperty timeSeriesPointValue in timeSeriesPoint)
            {
                switch (timeSeriesPointValue.Name.ToString())
                {
                case "Value":
                    tsp_Value = timeSeriesPointValue.Value.ToString();
                    break;

                case "Quality":
                    tsp_Quality = timeSeriesPointValue.Value.ToString();
                    break;

                case "TimeStamp":
                    tsp_TimeStamp_UTC = Convert.ToDateTime(timeSeriesPointValue.Value.ToString());
                    break;
                }
            }

            TimeSeriesPoint tsp = TimeSeriesPoint.CreatePoint(tsp_Value, tsp_TimeStamp_UTC, tsp_Quality, dataType);

            return(tsp);
        }
        public void GetFlagFromAllFlagsAndSetThatFlagInTimeSeriesPoint()
        {
            var             l   = Quality.AllFlags;
            TimeSeriesPoint tsp = new TimeSeriesPoint(default(DateTime), 1.0);

            tsp.SetQuality(l[0].Value);
        }
示例#3
0
        private TimeSeriesPoint LastNonZeroPoint(List <TimeSeriesPoint> points)
        {
            points.Reverse();
            TimeSeriesPoint lastPoint = FirstNonZeroPoint(points);

            Log.DebugFormat("LastNonZeroPoint at timestamp = {0}, value = {1}", lastPoint.Timestamp.ToString(Common._DateFormat), lastPoint.Value);
            return(lastPoint);
        }
        internal void GetCimQuantityQuality_WhenEnergyQuantity_ReturnQuantityQuality(TimeSeriesPoint sut)
        {
            // Arrange
            sut.QuantityMissing = false;
            sut.QuantityQuality = FixedTestQuantityQuality;

            // Act
            var result = sut.GetCimQuantityQuality();

            // Assert
            Assert.Equal(FixedTestQuantityQuality, result);
        }
        internal void GetCimEnergyQuantity_WhenQuantityMissing_ReturnZero(TimeSeriesPoint sut)
        {
            // Arrange
            sut.QuantityMissing = true;
            sut.EnergyQuantity  = FixedTestQuantity;

            // Act
            var result = sut.GetCimEnergyQuantity();

            // Assert
            Assert.Equal(0.0m, result);
        }
示例#6
0
        public static List <TimeSeriesPoint> IntPoints()
        {
            var point = new List <TimeSeriesPoint>
            {
                TimeSeriesPoint.CreatePoint("2",
                                            DateTime.UtcNow,
                                            "Bad",
                                            "int")
            };

            return(point);
        }
示例#7
0
        public static List <TimeSeriesPoint> DoublePoints()
        {
            var point = new List <TimeSeriesPoint>
            {
                TimeSeriesPoint.CreatePoint("2.32532",
                                            DateTime.UtcNow,
                                            "Good",
                                            "double"
                                            )
            };

            return(point);
        }
        private static async Task <TimeSeriesPoint> ProcessPointsAsync(XmlReader reader)
        {
            var point = new TimeSeriesPoint();

            do
            {
                if (reader.NodeType != XmlNodeType.Element && reader.NodeType != XmlNodeType.EndElement)
                {
                    continue;
                }

                // TODO Add support for the calculated "Time" field, see TimeSeriesSplitter
                if (reader.Is("Position", B2BNamespace))
                {
                    var value = await reader.ReadElementContentAsStringAsync()
                                .ConfigureAwait(false);

                    point.Position = int.Parse(value, CultureInfo.InvariantCulture);
                }
                else if (reader.Is("EnergyQuantity", B2BNamespace))
                {
                    // Note: If EnergyQuantity is there, QuantiyMissing is not
                    var value = await reader.ReadElementContentAsStringAsync()
                                .ConfigureAwait(false);

                    point.EnergyQuantity = decimal.Parse(value, CultureInfo.InvariantCulture);
                }
                else if (reader.Is("QuantityQuality", B2BNamespace))
                {
                    point.QuantityQuality = await reader.ReadElementContentAsStringAsync()
                                            .ConfigureAwait(false);
                }
                else if (reader.Is("QuantityMissing", B2BNamespace))
                {
                    // Note: If the QuantityMissing element is there, EnergyQuantity is not and QuantityQuality is irrelevant
                    await reader.ReadElementContentAsStringAsync()
                    .ConfigureAwait(false);

                    // If QuantityMissing is there, it means the quality is missing (regardless of value), which is mapped to a CIM value
                    point.QuantityMissing = true;
                }
                else if (reader.Is("IntervalEnergyObservation", B2BNamespace, XmlNodeType.EndElement))
                {
                    break;
                }
            }while (await reader.ReadAsync().ConfigureAwait(false));

            return(point);
        }
示例#9
0
        /// <summary>
        /// set a new current value
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newPoint"></param>
        /// <param name="destinationID"></param>
        protected void setCurrentValue(string destinationID, TimeSeriesPoint newPoint)
        {
            try
            {
                ConnectRedisAndConnectDatabase(4);
            }
            catch (RedisConnectionException)
            {
                Console.WriteLine("unable to connect enter corrent hostname and/or password");
                throw;
            }

            redisDatabase.StringSet(destinationID, JsonConvert.SerializeObject(newPoint));
            DisconnectDatabase();
        }
示例#10
0
        private TimeSeriesPoint FirstNonZeroPoint(List <TimeSeriesPoint> points)
        {
            TimeSeriesPoint firstDataPoint = points[0];

            foreach (TimeSeriesPoint p in points)
            {
                if (p.Value.HasValue && p.Value.Value > 0)
                {
                    firstDataPoint = p;
                    break;
                }
            }
            Log.DebugFormat("FirstNonZeroPoint at timestamp = {0}, value = {1}", firstDataPoint.Timestamp.ToString(Common._DateFormat), firstDataPoint.Value);
            return(firstDataPoint);
        }
        private static void WriteTimeSeriesPoint(
            Utf8JsonWriter writer, TimeSeriesPoint point, RSM012Fragments fragments)
        {
            writer.WriteStartObject();

            writer.WriteNumber("Position", point.Position);
            writer.WriteNumber("Quantity", point.GetCimEnergyQuantity());
            writer.WriteString("Quality", point.GetCimQuantityQuality());
            writer.WriteString(
                "Time",
                Iso8601Duration.GetObservationTime(
                    fragments.ObservationTimeSeriesPeriod?.Start,
                    fragments.ObservationTimeSeriesPeriod?.ResolutionDuration,
                    point.Position).ToString());

            writer.WriteEndObject();
        }
示例#12
0
        public static string FormatPointValue(TimeSeriesPoint point, StatisticType statType)
        {
            string missingStr = "***";
            bool   fix        = true;

            if (!point.Value.HasValue)
            {
                return(missingStr);
            }

            if (statType == StatisticType.Count)
            {
                return(FormatDoubleValue(point.Value.Value, fix, 0, missingStr));
            }
            else if (statType == StatisticType.Sum)
            {
                return(FormatSumValue(point.Value.Value, missingStr));
            }

            return(FormatDoubleValue(point.Value.Value, fix, 3, missingStr));
        }
示例#13
0
 /// <summary>
 /// set a current value object
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="destinationId"></param>
 /// <param name="obj"></param>
 public void SetCurrentValue(string destinationId, TimeSeriesPoint obj)
 {
     setCurrentValue(destinationId, obj);
     Disconnect();
 }
示例#14
0
 private static bool IsPointWithinTimeRange(TimeSeriesPoint point, TimeRange timeRange)
 {
     return(timeRange.StartTime <= point.Timestamp.DateTimeOffset && point.Timestamp.DateTimeOffset < timeRange.EndTime);
 }
示例#15
0
        public async Task TimeSeriesInsightsQuery_GetSeriesLifecycle()
        {
            // Arrange
            TimeSeriesInsightsClient tsiClient    = GetClient();
            DeviceClient             deviceClient = await GetDeviceClient().ConfigureAwait(false);

            // Figure out what the Time Series Id is composed of
            TimeSeriesModelSettings modelSettings = await tsiClient.ModelSettings.GetAsync().ConfigureAwait(false);

            // Create a Time Series Id where the number of keys that make up the Time Series Id is fetched from Model Settings
            TimeSeriesId tsiId = await GetUniqueTimeSeriesInstanceIdAsync(tsiClient, modelSettings.TimeSeriesIdProperties.Count)
                                 .ConfigureAwait(false);

            try
            {
                // Send some events to the IoT hub
                await QueryTestsHelper.SendEventsToHubAsync(
                    deviceClient,
                    tsiId,
                    modelSettings.TimeSeriesIdProperties.ToArray(),
                    10)
                .ConfigureAwait(false);

                // Act

                // Query for temperature events with two calculations. First with the temperature value as is, and the second
                // with the temperature value multiplied by 2.
                DateTimeOffset now       = Recording.UtcNow;
                DateTimeOffset endTime   = now.AddMinutes(10);
                DateTimeOffset startTime = now.AddMinutes(-10);

                var temperatureNumericVariable = new NumericVariable(
                    new TimeSeriesExpression($"$event.{QueryTestsHelper.Temperature}"),
                    new TimeSeriesExpression("avg($value)"));
                var temperatureNumericVariableTimesTwo = new NumericVariable(
                    new TimeSeriesExpression($"$event.{QueryTestsHelper.Temperature} * 2"),
                    new TimeSeriesExpression("avg($value)"));
                var temperatureTimesTwoVariableName = $"{QueryTestsHelper.Temperature}TimesTwo";

                var querySeriesRequestOptions = new QuerySeriesRequestOptions();
                querySeriesRequestOptions.InlineVariables[QueryTestsHelper.Temperature]    = temperatureNumericVariable;
                querySeriesRequestOptions.InlineVariables[temperatureTimesTwoVariableName] = temperatureNumericVariableTimesTwo;

                // This retry logic was added as the TSI instance are not immediately available after creation
                await TestRetryHelper.RetryAsync <AsyncPageable <QueryResultPage> >(async() =>
                {
                    QueryAnalyzer querySeriesEventsPages = tsiClient.Queries.CreateSeriesQueryAnalyzer(
                        tsiId,
                        startTime,
                        endTime,
                        querySeriesRequestOptions);

                    await foreach (Page <TimeSeriesPoint> seriesEventsPage in querySeriesEventsPages.GetResultsAsync().AsPages())
                    {
                        seriesEventsPage.Values.Should().HaveCount(10);
                        for (int index = 0; index < seriesEventsPage.Values.Count; index++)
                        {
                            TimeSeriesPoint point = seriesEventsPage.Values[index];
                            point.Timestamp.Should().BeAfter(startTime).And.BeBefore(endTime);
                            point.GetUniquePropertyNames().Should().HaveCount(3);
                            point.GetUniquePropertyNames().Should().Contain((property) => property == QueryTestsHelper.Temperature)
                            .And
                            .Contain((property) => property == temperatureTimesTwoVariableName);

                            // Assert that the values for the Temperature property is equal to the values for the other property, multiplied by 2
                            var temperatureTimesTwoValue = (double?)point.GetValue(temperatureTimesTwoVariableName);
                            var temperatureValue         = (double?)point.GetValue(QueryTestsHelper.Temperature);
                            temperatureTimesTwoValue.Should().Be(temperatureValue * 2);
                        }
                    }

                    return(null);
                }, MaxNumberOfRetries, s_retryDelay);

                // Query for all the series events using a timespan
                QueryAnalyzer querySeriesEventsPagesWithTimespan = tsiClient
                                                                   .Queries
                                                                   .CreateSeriesQueryAnalyzer(tsiId, TimeSpan.FromMinutes(10), null, querySeriesRequestOptions);

                await foreach (Page <TimeSeriesPoint> seriesEventsPage in querySeriesEventsPagesWithTimespan.GetResultsAsync().AsPages())
                {
                    seriesEventsPage.Values.Should().HaveCount(10);
                    foreach (TimeSeriesPoint point in seriesEventsPage.Values)
                    {
                        point.GetUniquePropertyNames().Should().HaveCount(3);
                    }
                }

                // Query for temperature and humidity
                var humidityNumericVariable = new NumericVariable(
                    new TimeSeriesExpression("$event.Humidity"),
                    new TimeSeriesExpression("avg($value)"));
                querySeriesRequestOptions.InlineVariables[QueryTestsHelper.Humidity] = humidityNumericVariable;
                querySeriesRequestOptions.ProjectedVariables.Add(QueryTestsHelper.Temperature);
                querySeriesRequestOptions.ProjectedVariables.Add(QueryTestsHelper.Humidity);
                await TestRetryHelper.RetryAsync <AsyncPageable <QueryResultPage> >(async() =>
                {
                    QueryAnalyzer querySeriesEventsPages = tsiClient.Queries.CreateSeriesQueryAnalyzer(tsiId, startTime, endTime, querySeriesRequestOptions);

                    await foreach (Page <TimeSeriesPoint> seriesEventsPage in querySeriesEventsPages.GetResultsAsync().AsPages())
                    {
                        seriesEventsPage.Values.Should().HaveCount(10);
                        foreach (TimeSeriesPoint point in seriesEventsPage.Values)
                        {
                            point.Timestamp.Should().BeAfter(startTime).And.BeBefore(endTime);
                            point.GetUniquePropertyNames().Should().HaveCount(2)
                            .And
                            .Contain((property) => property == QueryTestsHelper.Temperature)
                            .And
                            .Contain((property) => property == QueryTestsHelper.Humidity);
                        }
                    }

                    return(null);
                }, MaxNumberOfRetries, s_retryDelay);

                // Send 2 events with a special condition that can be used later to query on
                IDictionary <string, object> messageBase = QueryTestsHelper.BuildMessageBase(modelSettings.TimeSeriesIdProperties.ToArray(), tsiId);
                messageBase[QueryTestsHelper.Temperature] = 1.2;
                messageBase[QueryTestsHelper.Humidity]    = 3.4;
                string messageBody = JsonSerializer.Serialize(messageBase);
                var    message     = new Message(Encoding.ASCII.GetBytes(messageBody))
                {
                    ContentType     = "application/json",
                    ContentEncoding = "utf-8",
                };

                Func <Task> sendEventAct = async() => await deviceClient.SendEventAsync(message).ConfigureAwait(false);

                sendEventAct.Should().NotThrow();

                // Send it again
                sendEventAct.Should().NotThrow();

                // Query for the two events with a filter
                querySeriesRequestOptions.Filter = "$event.Temperature.Double = 1.2";
                await TestRetryHelper.RetryAsync <AsyncPageable <QueryResultPage> >(async() =>
                {
                    QueryAnalyzer querySeriesEventsPages = tsiClient.Queries.CreateSeriesQueryAnalyzer(tsiId, startTime, endTime, querySeriesRequestOptions);
                    await foreach (Page <TimeSeriesPoint> seriesEventsPage in querySeriesEventsPages.GetResultsAsync().AsPages())
                    {
                        seriesEventsPage.Values.Should().HaveCount(2);
                        foreach (TimeSeriesPoint point in seriesEventsPage.Values)
                        {
                            point.GetUniquePropertyNames().Should().HaveCount(2);
                            var temperatureValue = (double?)point.GetValue(QueryTestsHelper.Temperature);
                            temperatureValue.Should().Be(1.2);
                        }
                    }

                    return(null);
                }, MaxNumberOfRetries, s_retryDelay);

                // Query for the two events with a filter, but only take 1
                querySeriesRequestOptions.MaximumNumberOfEvents = 1;
                QueryAnalyzer querySeriesEventsPagesWithFilter = tsiClient.Queries.CreateSeriesQueryAnalyzer(tsiId, startTime, endTime, querySeriesRequestOptions);
                await foreach (Page <TimeSeriesPoint> seriesEventsPage in querySeriesEventsPagesWithFilter.GetResultsAsync().AsPages())
                {
                    seriesEventsPage.Values.Should().HaveCount(1);
                }
            }
            finally
            {
                deviceClient?.Dispose();
            }
        }