コード例 #1
0
ファイル: Program.cs プロジェクト: vmacari/Qi-Samples
        private static async Task MainAsync()
        {
            IConfigurationBuilder builder = new ConfigurationBuilder()
                                            .SetBasePath(Directory.GetCurrentDirectory())
                                            .AddJsonFile("appsettings.json");
            IConfiguration configuration = builder.Build();

            // ==== Client constants ====
            var tenantId    = configuration["TenantId"];
            var namespaceId = configuration["NamespaceId"];
            var address     = configuration["Address"];
            var resource    = configuration["Resource"];
            var clientId    = configuration["ClientId"];
            var clientKey   = configuration["ClientKey"];

            // ==== Metadata IDs ====
            string streamId        = "SampleStream";
            string typeId          = "SampleType";
            string behaviorId      = "SampleBehavior";
            string targetTypeId    = "SampleType_Target";
            string targetIntTypeId = "SampleType_TargetInt";
            string autoViewId      = "SampleAutoView";
            string manualViewId    = "SampleManualView";

            // Get Qi Services to communicate with server
            QiSecurityHandler securityHandler = new QiSecurityHandler(resource, tenantId, clientId, clientKey);

            QiService qiService       = new QiService(new Uri(address), securityHandler);
            var       metadataService = qiService.GetMetadataService(tenantId, namespaceId);
            var       dataService     = qiService.GetDataService(tenantId, namespaceId);

            LoggerCallbackHandler.UseDefaultLogging = false;


            Console.WriteLine(@"---------------------------------------------------");
            Console.WriteLine(@"________  .__      _______  ______________________");
            Console.WriteLine(@"\_____  \ |__|     \      \ \_   _____/\__    ___/");
            Console.WriteLine(@" /  / \  \|  |     /   |   \ |    __)_   |    |   ");
            Console.WriteLine(@"/   \_/.  \  |    /    |    \|        \  |    |   ");
            Console.WriteLine(@"\_____\ \_/__| /\ \____|__  /_______  /  |____|   ");
            Console.WriteLine(@"       \__>    \/         \/        \/           ");
            Console.WriteLine(@"---------------------------------------------------");
            Console.WriteLine();
            Console.WriteLine($"Qi endpoint at {address}");
            Console.WriteLine();

            try
            {
                // create a QiType
                Console.WriteLine("Creating a QiType");
                QiType type = QiTypeBuilder.CreateQiType <WaveData>();
                type.Id = typeId;
                type    = await metadataService.GetOrCreateTypeAsync(type);

                // create a QiStream
                Console.WriteLine("Creating a QiStream");
                var stream = new QiStream
                {
                    Id          = streamId,
                    Name        = "Wave Data Sample",
                    TypeId      = type.Id,
                    Description = "This is a sample QiStream for storing WaveData type measurements"
                };
                stream = await metadataService.GetOrCreateStreamAsync(stream);

                // insert data
                Console.WriteLine("Inserting data");

                // insert a single event
                var wave = GetWave(0, 200, 2);
                await dataService.InsertValueAsync(stream.Id, wave);

                // insert a list of events
                var waves = new List <WaveData>();
                for (var i = 2; i <= 18; i += 2)
                {
                    waves.Add(GetWave(i, 200, 2));
                }
                await dataService.InsertValuesAsync(stream.Id, waves);

                // get last event
                Console.WriteLine("Getting latest event");
                var latest = await dataService.GetLastValueAsync <WaveData>(streamId);

                Console.WriteLine(latest.ToString());
                Console.WriteLine();

                // get all events
                Console.WriteLine("Getting all events");
                var allEvents = (List <WaveData>) await dataService.GetWindowValuesAsync <WaveData>(streamId, "0", "180");

                Console.WriteLine($"Total events found: {allEvents.Count}");
                foreach (var evnt in allEvents)
                {
                    Console.WriteLine(evnt.ToString());
                }
                Console.WriteLine();

                // update events
                Console.WriteLine("Updating events");

                // update one event
                var updatedWave = UpdateWave(allEvents.First(), 4);
                await dataService.UpdateValueAsync(stream.Id, updatedWave);

                // update all events, adding ten more
                var updatedCollection = new List <WaveData>();
                for (int i = 2; i < 40; i = i + 2)
                {
                    updatedCollection.Add(GetWave(i, 400, 4));
                }
                await dataService.UpdateValuesAsync(stream.Id, updatedCollection);

                allEvents = (List <WaveData>) await dataService.GetWindowValuesAsync <WaveData>(stream.Id, "0", "180");

                Console.WriteLine("Getting updated events");
                Console.WriteLine($"Total events found: {allEvents.Count}");

                foreach (var evnt in allEvents)
                {
                    Console.WriteLine(evnt.ToString());
                }
                Console.WriteLine();

                // replacing events
                Console.WriteLine("Replacing events");

                // replace one event
                var replaceEvent = allEvents.First();
                replaceEvent.Sin = 0.717;
                replaceEvent.Cos = 0.717;
                replaceEvent.Tan = Math.Sqrt(2 * (0.717 * 0.717));

                await dataService.ReplaceValueAsync <WaveData>(streamId, replaceEvent);

                // replace all events
                foreach (var evnt in allEvents)
                {
                    evnt.Sin = 5.0 / 2;
                    evnt.Cos = 5 * Math.Sqrt(3) / 2;
                    evnt.Tan = 5 / Math.Sqrt(3);
                }

                await dataService.ReplaceValuesAsync <WaveData>(streamId, allEvents);

                Console.WriteLine("Getting replaced events");
                var replacedEvents = (List <WaveData>) await dataService.GetWindowValuesAsync <WaveData>(streamId, "0", "180");

                Console.WriteLine($"Total events found: {replacedEvents.Count}");
                foreach (var evnt in replacedEvents)
                {
                    Console.WriteLine(evnt.ToString());
                }
                Console.WriteLine();

                // Stream behaviors
                Console.WriteLine("QiStreamBehaviors determine whether Qi interpolates or extrapolates data at the requested index location");
                Console.WriteLine();

                // Stream behaviors modify retrieval.  We will retrieve three events using the default behavior, Continuous
                var retrieved = await dataService
                                .GetRangeValuesAsync <WaveData>(stream.Id, "1", 3, QiBoundaryType.ExactOrCalculated);

                Console.WriteLine("Default (Continuous) stream behavior, requesting data starting at index location '1', Qi will interpolate this value:");
                foreach (var value in retrieved)
                {
                    Console.WriteLine($"Order: {value.Order}, Radians: {value.Radians}");
                }
                Console.WriteLine();

                // create a Discrete stream behavior
                var behavior = new QiStreamBehavior
                {
                    Id   = behaviorId,
                    Mode = QiStreamMode.Discrete
                };
                behavior = await metadataService.GetOrCreateBehaviorAsync(behavior);

                // update the stream
                stream.BehaviorId = behavior.Id;
                await metadataService.CreateOrUpdateStreamAsync(stream);

                retrieved = await dataService
                            .GetRangeValuesAsync <WaveData>(stream.Id, "1", 3, QiBoundaryType.ExactOrCalculated);

                Console.WriteLine("Discrete stream behavior, Qi does not interpolate and returns the data starting at the next index location containing data:");
                foreach (var value in retrieved)
                {
                    Console.WriteLine($"Order: {value.Order}, Radians: {value.Radians}");
                }
                Console.WriteLine();

                // Stream views
                Console.WriteLine("QiViews");

                // create target types
                var targetType = QiTypeBuilder.CreateQiType <WaveDataTarget>();
                targetType.Id = targetTypeId;

                var targetIntType = QiTypeBuilder.CreateQiType <WaveDataInteger>();
                targetIntType.Id = targetIntTypeId;

                await metadataService.CreateOrUpdateTypeAsync(targetType);

                await metadataService.CreateOrUpdateTypeAsync(targetIntType);

                // create views
                var autoView = new QiView()
                {
                    Id           = autoViewId,
                    SourceTypeId = typeId,
                    TargetTypeId = targetTypeId
                };

                // create explicit mappings
                var vp1 = new QiViewProperty()
                {
                    SourceId = "Order", TargetId = "OrderTarget"
                };
                var vp2 = new QiViewProperty()
                {
                    SourceId = "Sin", TargetId = "SinInt"
                };
                var vp3 = new QiViewProperty()
                {
                    SourceId = "Cos", TargetId = "CosInt"
                };
                var vp4 = new QiViewProperty()
                {
                    SourceId = "Tan", TargetId = "TanInt"
                };

                var manualView = new QiView()
                {
                    Id           = manualViewId,
                    SourceTypeId = typeId,
                    TargetTypeId = targetIntTypeId,
                    Properties   = new List <QiViewProperty>()
                    {
                        vp1, vp2, vp3, vp4
                    }
                };

                await metadataService.CreateOrUpdateViewAsync(autoView);

                await metadataService.CreateOrUpdateViewAsync(manualView);

                Console.WriteLine("Here is some of our data as it is stored on the server:");
                foreach (var evnt in retrieved)
                {
                    Console.WriteLine($"Sin: {evnt.Sin}, Cos: {evnt.Cos}, Tan {evnt.Tan}");
                }
                Console.WriteLine();

                // get autoview data
                var autoViewData = await dataService.GetRangeValuesAsync <WaveDataTarget>(stream.Id, "1", 3, QiBoundaryType.ExactOrCalculated, autoViewId);

                Console.WriteLine("Specifying a view with a QiType of the same shape returns values that are automatically mapped to the target QiType's properties:");

                foreach (var value in autoViewData)
                {
                    Console.WriteLine($"SinTarget: {value.SinTarget} CosTarget: {value.CosTarget} TanTarget: {value.TanTarget}");
                }
                Console.WriteLine();

                // get manaulview data
                Console.WriteLine("QiViews can also convert certain types of data, here we return integers where the original values were doubles:");
                var manualViewData = await dataService.GetRangeValuesAsync <WaveDataInteger>(stream.Id, "1", 3, QiBoundaryType.ExactOrCalculated, manualViewId);

                foreach (var value in manualViewData)
                {
                    Console.WriteLine($"SinInt: {value.SinInt} CosInt: {value.CosInt} TanInt: {value.TanInt}");
                }
                Console.WriteLine();

                // get QiViewMap
                Console.WriteLine("We can query Qi to return the QiViewMap for our QiView, here is the one generated automatically:");
                var autoViewMap = await metadataService.GetViewMapAsync(autoViewId);

                PrintViewMapProperties(autoViewMap);

                Console.WriteLine("Here is our explicit mapping, note QiViewMap will return all properties of the Source Type, even those without a corresponding Target property:");
                var manualViewMap = await metadataService.GetViewMapAsync(manualViewId);

                PrintViewMapProperties(manualViewMap);

                // delete values
                Console.WriteLine("Deleting values from the QiStream");

                // delete one event
                await dataService.RemoveValueAsync(stream.Id, 0);

                // delete all events
                await dataService.RemoveWindowValuesAsync(stream.Id, 1, 200);

                retrieved = await dataService.GetWindowValuesAsync <WaveData>(stream.Id, "0", "200");

                if (retrieved.ToList <WaveData>().Count == 0)
                {
                    Console.WriteLine("All values deleted successfully!");
                }
                Console.WriteLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("Cleaning up");
                // Delete the stream, types, views and behavior
                Console.WriteLine("Deleteing stream");
                await metadataService.DeleteStreamAsync(streamId);

                Console.WriteLine("Deleteing types");
                await metadataService.DeleteTypeAsync(typeId);

                await metadataService.DeleteTypeAsync(targetTypeId);

                await metadataService.DeleteTypeAsync(targetIntTypeId);

                Console.WriteLine("Deleteing views");
                await metadataService.DeleteViewAsync(autoViewId);

                await metadataService.DeleteViewAsync(manualViewId);

                Console.WriteLine("Deleteing behavior");
                await metadataService.DeleteBehaviorAsync(behaviorId);

                Console.WriteLine("done");
                Console.ReadKey();
            }
        }
コード例 #2
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();
            });
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: jacnel/Qi-Samples
        private static void Main(string[] args)
        {
            // ==== Client constants ====
            var tenant   = ConfigurationManager.AppSettings["Tenant"];
            var address  = ConfigurationManager.AppSettings["Address"];
            var resource = ConfigurationManager.AppSettings["Resource"];
            var appId    = ConfigurationManager.AppSettings["AppId"];
            var appKey   = ConfigurationManager.AppSettings["AppKey"];

            var namespaceId = ConfigurationManager.AppSettings["Namespace"];

            // ==== Setup the namespace.  The namespace provides isolation within a Tenant. ====
            var admin = QiService.GetAdministrationService(new Uri(address), tenant, new QiSecurityHandler(resource, tenant, appId, appKey));

            var sampleNamespace = new QiNamespace(namespaceId);

            sampleNamespace = admin.GetOrCreateNamespaceAsync(sampleNamespace).GetAwaiter().GetResult();

            // ==== Setup the type and stream ====
            var config = QiService.GetMetadataService(new Uri(address), tenant, namespaceId, new QiSecurityHandler(resource, tenant, appId, appKey));

            QiType type = QiTypeBuilder.CreateQiType <WaveData>();

            type = config.GetOrCreateTypeAsync(type).GetAwaiter().GetResult();

            var stream = new QiStream
            {
                Id          = Guid.NewGuid().ToString(),
                Name        = "Wave Data Sample",
                TypeId      = type.Id,
                Description = "This is a sample QiStream for storing WaveData type measurements"
            };

            stream = config.GetOrCreateStreamAsync(stream).GetAwaiter().GetResult();

            // ==== Perform a number of CRUD opreations ====
            var client = QiService.GetDataService(new Uri(address), tenant, namespaceId, new QiSecurityHandler(resource, tenant, appId, appKey));

            // Insert a single event into a stream
            var wave = GetWave(0, 200, 2);

            client.InsertValueAsync(stream.Id, wave).GetAwaiter().GetResult();

            // Update will add the event as well
            wave.Order += 1;
            client.UpdateValueAsync(stream.Id, wave).GetAwaiter().GetResult();

            // Inserting a collection of events into a stream (more efficient)
            var waves = new List <WaveData>();

            for (var i = 2; i <= 200; ++i)
            {
                waves.Add(GetWave(i, 200, 2));
            }

            client.InsertValuesAsync(stream.Id, waves).GetAwaiter().GetResult();

            // Retrieve events for a the range.  Indexes are expected in string format.  Order property is is the Key
            IEnumerable <WaveData> retrieved = client.GetWindowValuesAsync <WaveData>(stream.Id, "0", "200").GetAwaiter().GetResult();

            Console.WriteLine("Retrieved {0} events", retrieved.Count());
            Console.WriteLine("\tfrom {0} {1}", retrieved.First().Order, retrieved.First().Sin);
            Console.WriteLine("\tto {0} {1}", retrieved.Last().Order, retrieved.Last().Sin);

            // Update some events
            UpdateWave(retrieved.First(), 4);
            client.UpdateValueAsync(stream.Id, retrieved.First()).GetAwaiter().GetResult();

            foreach (var value in waves)
            {
                UpdateWave(value, 4);
            }

            client.UpdateValuesAsync(stream.Id, waves.ToList()).GetAwaiter().GetResult();

            retrieved = client.GetWindowValuesAsync <WaveData>(stream.Id, "0", "200").GetAwaiter().GetResult();
            Console.WriteLine("Updated {0} events", retrieved.Count());
            Console.WriteLine("\tfrom {0} {1}", retrieved.First().Order, retrieved.First().Sin);
            Console.WriteLine("\tto {0} {1}", retrieved.Last().Order, retrieved.Last().Sin);

            // Stream behaviors modify retrieval
            // Retrieve a range of three values using the default behavior.  The default behavior is Continuous,
            // so ExactOrCalculated should bring back interpolated values
            retrieved = client.GetRangeValuesAsync <WaveData>(stream.Id, "1", 0, 3, false, QiBoundaryType.ExactOrCalculated).GetAwaiter().GetResult();
            Console.WriteLine("Default behavior {0} events", retrieved.Count());
            foreach (var value in retrieved)
            {
                Console.WriteLine("\t{0} {1}", value.Order, value.Sin);
            }

            // Modify the stream behavior to StepwiseContinuousLeading
            var stepwiseContinuousLeading = new QiStreamBehavior
            {
                Id   = Guid.NewGuid().ToString(),
                Mode = QiStreamMode.StepwiseContinuousLeading
            };

            stepwiseContinuousLeading = config.GetOrCreateBehaviorAsync(stepwiseContinuousLeading).GetAwaiter().GetResult();

            stream.BehaviorId = stepwiseContinuousLeading.Id;
            config.UpdateStreamAsync(stream.Id, stream).GetAwaiter().GetResult();

            retrieved = client.GetRangeValuesAsync <WaveData>(stream.Id, "1", 0, 3, false, QiBoundaryType.ExactOrCalculated).GetAwaiter().GetResult();
            Console.WriteLine("StepwiseContinuousLeading behavior {0} events", retrieved.Count());
            foreach (var value in retrieved)
            {
                Console.WriteLine("\t{0} {1}", value.Order, value.Sin);
            }

            // Delete the first event in the stream, the event at index zero
            client.RemoveValueAsync(stream.Id, 0).GetAwaiter().GetResult();

            // Delete the rest of the Events
            client.RemoveWindowValuesAsync(stream.Id, 1, 200).GetAwaiter().GetResult();

            retrieved = client.GetWindowValuesAsync <WaveData>(stream.Id, "0", "200").GetAwaiter().GetResult();
            Console.WriteLine("After delete {0} events remain", retrieved.Count());

            // Delete the stream and behavior
            config.DeleteStreamAsync(stream.Id).GetAwaiter().GetResult();
            config.DeleteBehaviorAsync(stepwiseContinuousLeading.Id).GetAwaiter().GetResult();

            // It is less common to remove the type and namespace
            config.DeleteTypeAsync(type.Id).GetAwaiter().GetResult();
            // admin.DeleteNamespaceAsync(sampleNamespace.Id).GetAwaiter().GetResult();
        }