コード例 #1
0
        private DeviceClient CreateDeviceClient(string contextName, TransportType?transportType)
        {
            DeviceClient deviceClient;

            var config = service.LoadConfig();

            IoTHubContext context = null;

            if (!config.IoTHubContexts.TryGetValue(contextName, out context))
            {
                return(null);
            }

            if (transportType != null)
            {
                var transportSettings = CreateTransportSettingsFromName(transportType.Value, null);
                deviceClient = DeviceClient.CreateFromConnectionString(context.DeviceConnectionString, new ITransportSettings[] { transportSettings });
            }
            else
            {
                deviceClient = DeviceClient.CreateFromConnectionString(context.DeviceConnectionString);
            }

            return(deviceClient);
        }
コード例 #2
0
        //private async Task WatchMessages(int limit, int messageTimeout)
        //{

        //    string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName;

        //    if (currentContext.ConsumerGroup != null)
        //    {
        //        consumerGroup = currentContext.ConsumerGroup;
        //    }

        //    await using (var consumer = new EventHubConsumerClient(consumerGroup, currentContext.ConnectionString, currentContext.EventHubName))
        //    {
        //        EventPosition startingPosition = EventPosition.Latest;
        //        using var cancellationSource = new System.Threading.CancellationTokenSource();

        //        int maxWaitTime = messageTimeout == 0 ? 30 : messageTimeout;
        //        cancellationSource.CancelAfter(TimeSpan.FromSeconds(maxWaitTime));

        //        string[] partitionIds = await consumer.GetPartitionIdsAsync();

        //        var partitions = new IAsyncEnumerable<PartitionEvent>[partitionIds.Length];

        //        for (int i = 0; i < partitionIds.Length; i++)
        //        {
        //            partitions[i] = consumer.ReadEventsFromPartitionAsync(partitionIds[i], startingPosition, cancellationSource.Token);
        //        }

        //        var mergedPartitions = AsyncEnumerable.Merge<PartitionEvent>(partitions);

        //        var maxMessages = Math.Max(1, limit);

        //        try
        //        {

        //            Console.WriteLine("Waiting for messages..");
        //            //for (int i = 0; i < maxMessages; i++)
        //            //{
        //            //    var singlePe = mergedPartitions.Take<PartitionEvent>(maxMessages);

        //            //    await foreach (var pe in singlePe)
        //            //    {
        //            //        Console.WriteLine($"Event received {Encoding.UTF8.GetString(pe.Data.Body.ToArray())}");
        //            //    }
        //            //}

        //            int messageCount = 0;
        //            await foreach (var pe in mergedPartitions.Take<PartitionEvent>(maxMessages))
        //            {

        //                Console.WriteLine($"Event received on partition {pe.Partition.PartitionId} with body {Encoding.UTF8.GetString(pe.Data.Body.ToArray())}");
        //                messageCount = messageCount + 1;
        //                if (messageCount >= maxMessages)
        //                {
        //                    Console.WriteLine($"Total messages received: {messageCount}");
        //                    break;
        //                }
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            Console.WriteLine($"{ex}");
        //        }
        //        //await foreach (PartitionEvent receivedEvent in consumer.ReadEventsFromPartitionAsync(partitionId, startingPosition, cancellationSource.Token))
        //        //{
        //        //    // At this point, the loop will wait for events to be available in the partition.  When an event
        //        //    // is available, the loop will iterate with the event that was received.  Because we did not
        //        //    // specify a maximum wait time, the loop will wait forever unless cancellation is requested using
        //        //    // the cancellation token.

        //        //}
        //    }
        //}


        //private async Task FollowMessages(int messageTimeout)
        //{

        //    string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName;

        //    if (currentContext.ConsumerGroup != null)
        //    {
        //        consumerGroup = currentContext.ConsumerGroup;
        //    }

        //    await using (var consumer = new EventHubConsumerClient(consumerGroup, currentContext.ConnectionString, currentContext.EventHubName))
        //    {
        //        EventPosition startingPosition = EventPosition.Latest;
        //        using var cancellationSource = new System.Threading.CancellationTokenSource();

        //        //int maxWaitTime = messageTimeout == 0 ? 30 : messageTimeout;
        //        //cancellationSource.CancelAfter(TimeSpan.FromSeconds(maxWaitTime));

        //        string[] partitionIds = await consumer.GetPartitionIdsAsync();

        //        var partitions = new IAsyncEnumerable<PartitionEvent>[partitionIds.Length];

        //        for (int i = 0; i < partitionIds.Length; i++)
        //        {
        //            partitions[i] = consumer.ReadEventsFromPartitionAsync(partitionIds[i], startingPosition, cancellationSource.Token);
        //        }

        //        var mergedPartitions = AsyncEnumerable.Merge<PartitionEvent>(partitions);



        //        try
        //        {

        //            Console.WriteLine("Following messages..");
        //            //for (int i = 0; i < maxMessages; i++)
        //            //{
        //            //    var singlePe = mergedPartitions.Take<PartitionEvent>(maxMessages);

        //            //    await foreach (var pe in singlePe)
        //            //    {
        //            //        Console.WriteLine($"Event received {Encoding.UTF8.GetString(pe.Data.Body.ToArray())}");
        //            //    }
        //            //}

        //            int messageCount = 0;
        //            await foreach (var pe in mergedPartitions)
        //            {

        //                Console.WriteLine($"Event received on partition {pe.Partition.PartitionId} with body {Encoding.UTF8.GetString(pe.Data.Body.ToArray())}");
        //                messageCount = messageCount + 1;

        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            Console.WriteLine($"{ex}");
        //        }
        //        //await foreach (PartitionEvent receivedEvent in consumer.ReadEventsFromPartitionAsync(partitionId, startingPosition, cancellationSource.Token))
        //        //{
        //        //    // At this point, the loop will wait for events to be available in the partition.  When an event
        //        //    // is available, the loop will iterate with the event that was received.  Because we did not
        //        //    // specify a maximum wait time, the loop will wait forever unless cancellation is requested using
        //        //    // the cancellation token.

        //        //}
        //    }
        //}


        public void SetContext(string name,
                               string eventhubEndpoint,
                               string eventHubName,
                               string storageConnection,
                               string consumerGroup,
                               string deviceConnectionString)
        {
            Console.WriteLine($"Setting context {name} with eventhub endpoint {eventhubEndpoint} and hub {eventHubName}");
            var           config = service.LoadConfig();
            IoTHubContext ctxt   = null;

            if (!config.IoTHubContexts.TryGetValue(name, out ctxt))
            {
                ctxt = new IoTHubContext();
            }

            if (eventhubEndpoint != null)
            {
                ctxt.EventHubEndpoint = eventhubEndpoint;
            }

            if (eventHubName != null)
            {
                ctxt.EventHubName = eventHubName;
            }

            if (storageConnection != null)
            {
                ctxt.StorageConnectionString = storageConnection;
            }

            if (consumerGroup != null)
            {
                ctxt.ConsumerGroup = consumerGroup;
            }

            if (deviceConnectionString != null)
            {
                ctxt.DeviceConnectionString = deviceConnectionString;
            }

            config.IoTHubContexts[name.ToLower()] = ctxt;

            service.UpdateConfig(config);
        }
コード例 #3
0
 public IoTHubProducerCommands(IoTHubContext context, ConfigService service)
 {
     this.currentContext = context;
     this.service        = service;
 }