Exemple #1
0
        /// <summary>
        /// Listens on the Azure queue realtimedevicequeue[ENVRIONMENT]
        /// </summary>
        /// <param name="gateway">The Gateway to pull controller statuses from</param>
        /// <param name="log">Azure L</param>
        public static void ProcessRealtimeQueueMessage([QueueTrigger(Constants.REAL_TIME_DEVICE_QUEUE_TRIGGER)] Gateway gateway, TextWriter log)
        {
            try
            {
                var ingressService = new IngressService(log);
                var airMeshService = new AirMeshService(log);

                foreach (var controllerStatus in airMeshService.GetControllerStatusForGateway(gateway))
                {
                    // Check if we have registered this device, if not let's do it...
                    if (!RegisteredDeviceService.IsDeviceRegistered(controllerStatus.UUID))
                    {
                        // TODO: Confirm this pattern ->  Should we poll devices at startup, and get them registered before
                        RegisteredDeviceService.RegisterDevice(controllerStatus.UUID);
                        ingressService.PushDataToPlatform(D2CAPI.DeviceTree, controllerStatus.Controller.controller_id, controllerStatus.GetDeviceTree());
                    }

                    // Push Realtimes...
                    ingressService.PushDataToPlatform(D2CAPI.Realtimes, controllerStatus.Controller.controller_id, controllerStatus.GetRealTimes());

                    // Push Trends...
                    ingressService.PushDataToPlatform(D2CAPI.Trends, controllerStatus.Controller.controller_id, controllerStatus.GetTrends());
                }
            }
            catch (Exception ex)
            {
                // TODO: Improve logging text
                log.WriteAsync(ex.ToString());
            }
        }
Exemple #2
0
        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();
                }
            }
        }