public void ArchiveQueryTest(string pointMask, string startTime, string endTime, double minValue, double maxValue)
        {
            var now = AFTime.Now;
            var st  = new AFTime(startTime, now);
            var et  = new AFTime(endTime, now);

            Output.WriteLine($"Start to execute PI Data Archive queries on PI Points matching [{pointMask}] " +
                             $"between [{st}] and [{et}].");

            IList <IEnumerable <PIPointQuery> > queries = PIPointQuery.ParseQuery(Fixture.PIServer, pointMask);
            IEnumerable <PIPoint>          pointList    = PIPoint.FindPIPoints(Fixture.PIServer, queries).ToList();
            IDictionary <string, AFValues> events       = Fixture.ReadPIEvents(pointList, st, et);

            // Verify all event values are in the expected range
            foreach (var ptvaluespair in events)
            {
                foreach (var val in ptvaluespair.Value.Where(val => val.IsGood))
                {
                    var convertedValue = Convert.ToDouble(val.Value, CultureInfo.InvariantCulture);
                    Assert.True(convertedValue >= minValue && convertedValue <= maxValue,
                                $"[{ptvaluespair.Key}] has a value [{val.Value}] outside of expected data range of " +
                                $"[{minValue} ~ {maxValue}]");
                }
            }

            Output.WriteLine($"Found {events.Sum(kvp => kvp.Value.Count)} PI events.");
        }
Exemplo n.º 2
0
        public IEnumerable <PIPoint> Search(string query)
        {
            var queries = PIPointQuery.ParseQuery(_server, query);
            var points  = PIPoint.FindPIPoints(_server, queries);

            return(points);
        }
        public void PIPointSearchTest()
        {
            PIServer piServer = PIFixture.PIServer;
            int      numberOfPointsToCreate = 10;
            var      pointPrefix            = "PIPointSearchTest_Point";

            try
            {
                // Create PI Points
                Output.WriteLine($"Creating PI Points with prefix [{pointPrefix}].");
                var points = PIFixture.CreatePIPoints($"{pointPrefix}#", numberOfPointsToCreate);

                // Assign range of values to defined tags
                for (int i = 0; i < points.Count(); i++)
                {
                    points.ElementAt(i).UpdateValue(new AFValue(Math.Pow(-1, i + 1), null), 0);

                    // Set the Step attribute of half of the PI Points to true and half to false
                    points.ElementAt(i).SetAttribute(PICommonPointAttributes.Step, Convert.ToBoolean(1 * ((i + 1) % 2)));
                    points.ElementAt(i).SaveAttributes();
                }

                // Search PI Points with queries
                var searchQuery = $"Name:'{pointPrefix}*' value:>0";
                Output.WriteLine($"Searching for PI Points with query [{searchQuery}].");
                var parsedQuery       = PIPointQuery.ParseQuery(piServer, searchQuery);
                var searchPointsCount = PIPoint.FindPIPoints(piServer, parsedQuery).Count();
                AssertEventually.True(
                    () => PIPoint.FindPIPoints(piServer, parsedQuery).Count() == (numberOfPointsToCreate / 2),
                    TimeSpan.FromSeconds(10),
                    TimeSpan.FromSeconds(0.5),
                    $"The PI Points count do not match. Expected: {numberOfPointsToCreate / 2}, Actual: {searchPointsCount}.");

                searchQuery = $"Name:'{pointPrefix}*'";
                Output.WriteLine($"Searching for PI Points with query [{searchQuery}].");
                parsedQuery       = PIPointQuery.ParseQuery(piServer, searchQuery);
                searchPointsCount = PIPoint.FindPIPoints(piServer, parsedQuery).Count();
                AssertEventually.True(
                    () => PIPoint.FindPIPoints(piServer, parsedQuery).Count() == numberOfPointsToCreate,
                    TimeSpan.FromSeconds(10),
                    TimeSpan.FromSeconds(0.5),
                    $"The PI Points count do not match. Expected: {numberOfPointsToCreate}, Actual: {searchPointsCount}.");

                searchQuery = $"Name:'{pointPrefix}*' step:=0";
                Output.WriteLine($"Searching for PI Points with query [{searchQuery}].");
                parsedQuery       = PIPointQuery.ParseQuery(piServer, searchQuery);
                searchPointsCount = PIPoint.FindPIPoints(piServer, parsedQuery).Count();
                AssertEventually.True(
                    () => PIPoint.FindPIPoints(piServer, parsedQuery).Count() == (numberOfPointsToCreate / 2),
                    TimeSpan.FromSeconds(10),
                    TimeSpan.FromSeconds(0.5),
                    $"The PI Points count do not match. Expected: {numberOfPointsToCreate / 2}, Actual: {searchPointsCount}.");
            }
            finally
            {
                PIFixture.DeletePIPoints("PIPointSearchTest_Point*", Output);
            }
        }
        public void PointSearchTest(string query, int expectedCount)
        {
            Output.WriteLine($"Search for PI Points using the query [{query}]. Expect to find {expectedCount} PI Points.");
            IList <IEnumerable <PIPointQuery> > queries = PIPointQuery.ParseQuery(Fixture.PIServer, query);
            IEnumerable <PIPoint> pointList             = PIPoint.FindPIPoints(Fixture.PIServer, queries).ToList();
            int actualCount = pointList.Count();

            Assert.True(actualCount == expectedCount,
                        $"Query [{query}] resulted in {actualCount} PI Points, expected {expectedCount}.");
        }
        public PIPointsProvider(string query, PIServer server)
        {
            var queries = PIPointQuery.ParseQuery(server, query);

            if (queries == null || queries.Count < 1)
            {
                throw new Exception("The query passed was invalid, it would not find any PI Point");
            }

            Points = PIPoint.FindPIPoints(server, queries);
        }
        public void SineWaveEventsTest()
        {
            string query = $"name:{PIFixture.SineWavePointNameMask} AND PointSource:{PIFixture.TestPIPointSource}";

            Output.WriteLine($"Start to verify the SineWave wave pattern in PI Points matching [{query}] in the past one day.");
            IList <IEnumerable <PIPointQuery> > queries = PIPointQuery.ParseQuery(Fixture.PIServer, query);
            IEnumerable <PIPoint>          pointList    = PIPoint.FindPIPoints(Fixture.PIServer, queries).ToList();
            IDictionary <string, AFValues> sineEvents   = Fixture.ReadPIEvents(pointList, new AFTime("*-1d"), AFTime.Now);

            int totalCheckedEventCount = 0;

            foreach (KeyValuePair <string, AFValues> kvp in sineEvents)
            {
                // Use Distinct() in case there are duplicate archived events
                var timedEvents = kvp.Value.Distinct().ToDictionary(val => val.Timestamp.LocalTime, val => val.ValueAsDouble());

                // Start for the oldest event and check events one by one
                foreach (KeyValuePair <DateTime, double> evt in timedEvents)
                {
                    var timestampAfterAHalfWave = evt.Key.AddSeconds(PIFixture.SineWaveLengthInHours * 1800);
                    var timestampAfterOneWave   = evt.Key.AddSeconds(PIFixture.SineWaveLengthInHours * 3600);

                    if (timedEvents.ContainsKey(timestampAfterAHalfWave) && timedEvents.ContainsKey(timestampAfterOneWave))
                    {
                        // Based on the SineWave pattern, the current value should be equal to -1 * the value
                        // after a half wave and the value after a full wave.
                        Assert.True(
                            Math.Abs(evt.Value - (-1 * timedEvents[timestampAfterAHalfWave])) < PIFixture.Epsilon,
                            $"Found events in [{kvp.Key}] at [{evt.Key}] and [{timestampAfterAHalfWave}] not matching the SineWave pattern.");
                        Assert.True(
                            Math.Abs(evt.Value - timedEvents[timestampAfterOneWave]) < PIFixture.Epsilon,
                            $"Found events in [{kvp.Key}] at [{evt.Key}] and [{timestampAfterOneWave}] not matching the SineWave pattern.");

                        totalCheckedEventCount++;
                    }
                    else
                    {
                        // If the data set does not contain a key of timestampAfterOneWave, it means we have reached the end of set.
                        break;
                    }
                }
            }

            Output.WriteLine($"Successfully verified a total of {totalCheckedEventCount} SineWave values in {sineEvents.Count} PI Points.");
        }
Exemplo n.º 7
0
        public void Run()
        {
            PIServers piServers = new PIServers();
            PIServer  piServer  = piServers["<PISERVER>"];

            // Use PICommonPointAttributes so we don't have to remember the strings for point attributes.
            PIPointQuery compressionFilter = new PIPointQuery {
                AttributeName  = PICommonPointAttributes.Compressing,
                AttributeValue = "1",
                Operator       = AFSearchOperator.Equal
            };

            PIPointQuery nameFilter = new PIPointQuery
            {
                AttributeName  = PICommonPointAttributes.PointSource,
                AttributeValue = "R",
                Operator       = AFSearchOperator.Equal
            };

            IEnumerable <string> attributesToLoad = new[]
            {
                PICommonPointAttributes.Compressing,
                PICommonPointAttributes.Descriptor,
                PICommonPointAttributes.PointSource,
                PICommonPointAttributes.Span,
                PICommonPointAttributes.Zero
            };

            IEnumerable <PIPoint> points = PIPoint.FindPIPoints(piServer, new[] { compressionFilter, nameFilter }, attributesToLoad);

            foreach (PIPoint pt in points)
            {
                Console.WriteLine("Name: {0}", pt.GetAttribute(PICommonPointAttributes.Tag));
                Console.WriteLine("Compressing: {0}", pt.GetAttribute(PICommonPointAttributes.Compressing));
                Console.WriteLine("Descriptor: {0}", pt.GetAttribute(PICommonPointAttributes.Descriptor));
                Console.WriteLine("PointSource: {0}", pt.GetAttribute(PICommonPointAttributes.PointSource));
                Console.WriteLine("Span: {0}", pt.GetAttribute(PICommonPointAttributes.Span));
                Console.WriteLine("Zero: {0}", pt.GetAttribute(PICommonPointAttributes.Zero));
                Console.WriteLine();
            }
        }
        public void Run()
        {
            PIServers piServers = new PIServers();
            PIServer piServer = piServers["<PISERVER>"];

            // Use PICommonPointAttributes so we don't have to remember the strings for point attributes.
            PIPointQuery compressionFilter = new PIPointQuery {
                AttributeName = PICommonPointAttributes.Compressing,
                AttributeValue = "1",
                Operator = AFSearchOperator.Equal };

            PIPointQuery nameFilter = new PIPointQuery
            {
                AttributeName = PICommonPointAttributes.PointSource,
                AttributeValue = "R",
                Operator = AFSearchOperator.Equal
            };

            IEnumerable<string> attributesToLoad = new[]
            {
                PICommonPointAttributes.Compressing,
                PICommonPointAttributes.Descriptor,
                PICommonPointAttributes.PointSource,
                PICommonPointAttributes.Span,
                PICommonPointAttributes.Zero
            };

            IEnumerable<PIPoint> points = PIPoint.FindPIPoints(piServer, new[] { compressionFilter, nameFilter }, attributesToLoad);

            foreach (PIPoint pt in points)
            {
                Console.WriteLine("Name: {0}", pt.GetAttribute(PICommonPointAttributes.Tag));
                Console.WriteLine("Compressing: {0}", pt.GetAttribute(PICommonPointAttributes.Compressing));
                Console.WriteLine("Descriptor: {0}", pt.GetAttribute(PICommonPointAttributes.Descriptor));
                Console.WriteLine("PointSource: {0}", pt.GetAttribute(PICommonPointAttributes.PointSource));
                Console.WriteLine("Span: {0}", pt.GetAttribute(PICommonPointAttributes.Span));
                Console.WriteLine("Zero: {0}", pt.GetAttribute(PICommonPointAttributes.Zero));
                Console.WriteLine();
            }
        }
Exemplo n.º 9
0
        private static async Task MainAsync()
        {
            string accountId    = ConfigurationManager.AppSettings["accountId"];
            string namespaceId  = ConfigurationManager.AppSettings["namespaceId"];
            string address      = ConfigurationManager.AppSettings["address"];
            string resource     = ConfigurationManager.AppSettings["resource"];
            string clientId     = ConfigurationManager.AppSettings["clientId"];
            string clientSecret = ConfigurationManager.AppSettings["clientSecret"];
            string piServerName = ConfigurationManager.AppSettings["PIDataArchive"];

            var qiService = new QiService(new Uri(address),
                                          new QiSecurityHandler(resource, accountId, clientId, clientSecret));

            var metadataService = qiService.GetMetadataService(accountId, namespaceId);
            var dataService     = qiService.GetDataService(accountId, namespaceId);

            var piServer = new PIServers()[piServerName];

            piServer.Connect();

            PIPointQuery nameFilter = new PIPointQuery
            {
                AttributeName  = PICommonPointAttributes.Tag,
                AttributeValue = _options.TagMask,
                Operator       = OSIsoft.AF.Search.AFSearchOperator.Equal
            };

            IEnumerable <string> attributesToRetrieve = new[]
            {
                PICommonPointAttributes.Descriptor,
                PICommonPointAttributes.EngineeringUnits,
                PICommonPointAttributes.PointSource
            };

            var piPoints =
                (await PIPoint.FindPIPointsAsync(piServer, new[] { nameFilter }, attributesToRetrieve)).ToList();

            if (!piPoints.Any())
            {
                Console.WriteLine($"No points found matching the tagMask query!");
                return;
            }
            Console.WriteLine($"Found {piPoints.Count} points matching mask: {_options.TagMask}");

            //create types
            await PIQiTypes.CreateOrUpdateTypesInOcsAsync(metadataService);

            //delete existing streams if requested
            if (_options.Mode == CommandLineOptions.DataWriteModes.clearExistingData)
            {
                Parallel.ForEach(piPoints, piPoint => DeleteStreamBasedOnPIPointAsync(piPoint, metadataService).Wait());
            }

            Parallel.ForEach(piPoints, piPoint => CreateStreamBasedOnPIPointAsync(piPoint, attributesToRetrieve, metadataService).Wait());
            Console.WriteLine($"Created or updated {piPoints.Count()} streams.");

            //for each PIPoint, get the data of interest and write it to OCS
            Parallel.ForEach(piPoints, piPoint =>
            {
                //Indices must be unique in OCS so we get rid of duplicate values for a given timestamp
                var timeRange = new AFTimeRange(_options.StartTime, _options.EndTime);
                var afValues  = piPoint.RecordedValues(timeRange, AFBoundaryType.Inside, null, true)
                                .GroupBy(value => value.Timestamp)
                                .Select(valuesAtTimestamp => valuesAtTimestamp.Last()) //last event for particular timestamp
                                .Where(val => val.IsGood)                              //System Digital States (e.g. Shutdown, IO Timeout, etc...) are ignored
                                .ToList();

                WriteDataToOcsAsync(piPoint, afValues, dataService).Wait();
            });
        }