コード例 #1
0
        public TrendsService(IConfiguration config, IIngressService ingressService)
        {
            Context        = new CryptoTideDBContext(config);
            IngressService = ingressService;

            // TODO(Serban): Hacky, will need to keep value in memory and be updated by the data pipeline
            try
            {
                // Errors if max is 0
                LastHour = Context.HourlyTrends.Where(t => t.Date == DateTime.Now.Date).Max(t => t.Hour);
            }
            catch
            {
                LastHour = 0;
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: oscdoctest/OCS-Samples
        private static async Task MainAsync()
        {
            IConfigurationBuilder builder = new ConfigurationBuilder()
                                            .SetBasePath(Directory.GetCurrentDirectory())
                                            .AddJsonFile("appsettings.json");
            IConfiguration configuration = builder.Build();

            // ==== Client constants ====
            string tenantId         = configuration["TenantId"];
            string namespaceId      = configuration["NamespaceId"];
            string address          = configuration["Address"];
            string clientId         = configuration["ClientId"];
            string clientSecret     = configuration["ClientSecret"];
            string topicName        = configuration["TopicName"];
            string mappedClientId   = configuration["MappedClientId"];
            string subscriptionName = configuration["SubscriptionName"];

            //Get Ingress Services to communicate with server
            LoggerCallbackHandler.UseDefaultLogging = false;
            AuthenticationHandler authenticationHandler = new AuthenticationHandler(new Uri(address), clientId, clientSecret);

            IngressService  baseIngressService = new IngressService(new Uri(address), null, HttpCompressionMethod.None, authenticationHandler);
            IIngressService ingressService     = baseIngressService.GetIngressService(tenantId, namespaceId);

            Console.WriteLine($"OCS endpoint at {address}");
            Console.WriteLine();

            Topic        createdTopic        = null;
            Subscription createdSubscription = null;

            try
            {
                // Create a Topic
                Console.WriteLine($"Creating a Topic in Namespace {namespaceId} for Client with Id {mappedClientId}");
                Console.WriteLine();
                Topic topic = new Topic()
                {
                    TenantId    = tenantId,
                    NamespaceId = namespaceId,
                    Name        = topicName,
                    Description = "This is a sample Topic",
                    ClientIds   = new List <string>()
                    {
                        mappedClientId
                    }
                };
                createdTopic = await ingressService.CreateOrUpdateTopicAsync(topic);

                Console.WriteLine($"Created a Topic with Id {createdTopic.Id}");
                Console.WriteLine();

                // Create a Subscription
                Console.WriteLine($"Creating an OCS Subscription in Namespace {namespaceId} for Topic with Id {createdTopic.Id}");
                Console.WriteLine();
                Subscription subscription = new Subscription()
                {
                    TenantId         = tenantId,
                    NamespaceId      = namespaceId,
                    Name             = subscriptionName,
                    Description      = "This is a sample OCS Data Store Subscription",
                    Type             = SubscriptionType.Sds,
                    TopicId          = createdTopic.Id,
                    TopicTenantId    = createdTopic.TenantId,
                    TopicNamespaceId = createdTopic.NamespaceId
                };
                createdSubscription = await ingressService.CreateOrUpdateSubscriptionAsync(subscription);

                Console.WriteLine($"Created an OCS Subscription with Id {createdSubscription.Id}");
                Console.WriteLine();

                // At this point, we are ready to send OMF data to OCS.
                // Please visit https://github.com/osisoft/OMF-Samples/tree/master/Tutorials/CSharp_Sds to learn how to do this.
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
            finally
            {
                try
                {
                    // Delete the Subscription
                    if (createdSubscription != null)
                    {
                        Console.WriteLine($"Deleting the OCS Subscription with Id {createdSubscription.Id}");
                        Console.WriteLine();

                        await ingressService.DeleteSubscriptionAsync(createdSubscription.Id);

                        Console.WriteLine($"Deleted the OCS Subscription with Id {createdSubscription.Id}");
                        Console.WriteLine();
                    }

                    // Delete the Topic
                    if (createdTopic != null)
                    {
                        Console.WriteLine($"Deleting the Topic with Id {createdTopic.Id}");
                        Console.WriteLine();

                        await ingressService.DeleteTopicAsync(createdTopic.Id);

                        Console.WriteLine($"Deleted the Topic with Id {createdTopic.Id}");
                        Console.WriteLine();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                }
            }
        }