Пример #1
0
        public async Task CreateRouteAsync(RequestContext context)
        {
            var client = new AzureDigitalTwinsAPIClient(new TokenCredentials(context.Token.RawData));

            client.BaseUri = new Uri($"https://{context.Host}/");

            _log.LogInformation($"Creating route for {context.InstanceName}");
            await client.EventRoutes.AddAsync(RouteId, new Sdk.Models.EventRoute(EndpointId, filter: "true"));
        }
        public static async Task Run([EventHubTrigger("iothub-wsgrp", Connection = "EventHubConnectionAppSetting")] EventData[] events, ILogger log)
        {
            var exceptions = new List <Exception>();

            if (client == null)
            {
                await Authenticate(log);
            }
            else
            {
                foreach (EventData eventData in events)
                {
                    try
                    {
                        if (!flush)
                        {
                            string  messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
                            JObject messageData = JObject.Parse(messageBody);
                            string  deviceId    = eventData.SystemProperties["iothub-connection-device-id"].ToString();
                            await ProcessDataForDevice(messageData, deviceId, log);
                        }

                        await Task.Yield();
                    }
                    catch (Exception e)
                    {
                        // We need to keep processing the rest of the batch - capture this exception and continue.
                        // Also, consider capturing details of the message that failed processing so it can be processed again later.
                        exceptions.Add(e);
                        client = null;
                    }
                }

                // Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure.

                if (exceptions.Count > 1)
                {
                    throw new AggregateException(exceptions);
                }

                if (exceptions.Count == 1)
                {
                    throw exceptions.Single();
                }
            }
        }
Пример #3
0
        public async Task <bool> CheckAccessAsync()
        {
            if (Token == null)
            {
                return(false);
            }

            var client = new AzureDigitalTwinsAPIClient(new TokenCredentials(Token.RawData));

            client.BaseUri = new Uri($"https://{Host}/");

            try
            {
                await client.DigitalTwinModels.ListAsync(digitalTwinModelsListOptions : new DigitalTwinModelsListOptions(maxItemCount: 1));

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        public async static Task Authenticate(ILogger log)
        {
            var    azureServiceTokenProvider = new AzureServiceTokenProvider();
            string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync(adtAppId);

            var wc = new System.Net.Http.HttpClient();

            wc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

            try
            {
                TokenCredentials tk = new TokenCredentials(accessToken);
                client = new AzureDigitalTwinsAPIClient(tk)
                {
                    BaseUri = new Uri(adtInstanceUrl)
                };
                log.LogInformation($"Azure Digital Twins client connection created.");
            }
            catch (Exception)
            {
                log.LogError($"Azure Digital Twins client connection failed.");
            }
        }