Ejemplo n.º 1
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Read IoT Hub device to cloud messages. Ctrl-C to exit.\n");

            _eventHubClient = EventHubClient.CreateFromConnectionString(ConnectionString);

            var runtimeInfo = _eventHubClient.GetRuntimeInformationAsync().GetAwaiter().GetResult();
            var partitions  = runtimeInfo.PartitionIds;

            var cts = new CancellationTokenSource();

            Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                cts.Cancel();
                Console.WriteLine("Exiting...");
            };

            var tasks = new List <Task>();

            foreach (var partition in partitions)
            {
                tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
            }

            Task.WaitAll(tasks.ToArray());
        }
Ejemplo n.º 2
0
        public static async Task DoRD2C(string[] args)
        {
            //_Sensors = new List<Sensor>();
            s_eventHubsCompatibleEndpoint = Configuration["EVENT_HUBS_COMPATIBILITY_ENDPOINT"];
            s_eventHubsCompatiblePath     = Configuration["EVENT_HUBS_COMPATIBILITY_PATH"];
            s_iotHubSasKey     = Configuration["EVENT_HUBS_SAS_KEY"];     // "{your service primary key}";
            s_iotHubSasKeyName = Configuration["SHARED_ACCESS_KEY_NAME"]; // "service, iothubowner";

            System.Diagnostics.Debug.WriteLine("IoT Hub Quickstarts - Read device to cloud messages. Ctrl-C to exit.\n");


            System.Diagnostics.Debug.WriteLine("Using Env Var EVENT_HUBS_COMPATIBILITY_ENDPOINT = " + s_eventHubsCompatibleEndpoint);
            System.Diagnostics.Debug.WriteLine("Using Env Var EVENT_HUBS_COMPATIBILITY_PATH = " + s_eventHubsCompatiblePath);
            System.Diagnostics.Debug.WriteLine("Using Env Var EVENT_HUBS_SAS_KEY = " + s_iotHubSasKey);
            System.Diagnostics.Debug.WriteLine("Using Env Var SHARED_ACCESS_KEY_NAME = " + s_iotHubSasKeyName);

            //System.Diagnostics.Debug.WriteLine("\nDo you want to Hide System Properties sent by IoT Hub? [Y]es Default No");
            //var ch = Console.ReadKey();
            //if ((ch.KeyChar == 'Y') || (ch.KeyChar == 'y'))
            //{
            //    show_system_properties = false;
            //}

            show_system_properties = false;
            System.Diagnostics.Debug.WriteLine("Press Enter to continue when the Simulated-Device is sending messages.");
            //Console.ReadLine();

            // Create an EventHubClient instance to connect to the
            // IoT Hub Event Hubs-compatible endpoint.
            var connectionString = new EventHubsConnectionStringBuilder(new Uri(s_eventHubsCompatibleEndpoint), s_eventHubsCompatiblePath, s_iotHubSasKeyName, s_iotHubSasKey);



            s_eventHubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString());

            // Create a PartitionReciever for each partition on the hub.
            //var xx = await s_eventHubClient.GetPartitionRuntimeInformationAsync("1");
            var runtimeInfo = await s_eventHubClient.GetRuntimeInformationAsync();

            var d2cPartitions = runtimeInfo.PartitionIds;

            CancellationTokenSource cts = new CancellationTokenSource();

            Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                cts.Cancel();
                System.Diagnostics.Debug.WriteLine("Exiting...");
            };

            var tasks = new List <Task>();

            foreach (string partition in d2cPartitions)
            {
                tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
            }

            // Wait for all the PartitionReceivers to finsih.
            Task.WaitAll(tasks.ToArray());
        }
Ejemplo n.º 3
0
        private static async Task MainAsync(string[] args)
        {
            Console.WriteLine("IoT Hub Quickstarts - Read device to cloud messages. Ctrl-C to exit.\n");

            // Create an EventHubClient instance to connect to the
            // IoT Hub Event Hubs-compatible endpoint.
            var connectionString = "Endpoint=sb://pwlodek-ceres-eventhub.servicebus.windows.net/;SharedAccessKeyName=listen;SharedAccessKey=;EntityPath=weather-telemetry";

            s_eventHubClient = EventHubClient.CreateFromConnectionString(connectionString);

            // Create a PartitionReciever for each partition on the hub.
            var runtimeInfo = await s_eventHubClient.GetRuntimeInformationAsync();

            var d2cPartitions = runtimeInfo.PartitionIds;

            CancellationTokenSource cts = new CancellationTokenSource();

            Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                cts.Cancel();
                Console.WriteLine("Exiting...");
            };

            var tasks = new List <Task>();

            foreach (string partition in d2cPartitions)
            {
                tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
            }

            // Wait for all the PartitionReceivers to finsih.
            Task.WaitAll(tasks.ToArray());
        }
Ejemplo n.º 4
0
        public static async Task RunAzureTelemetryReaderTest(AzureTelemetryReaderOpts opts)
        {
            _logger = LoggerFactory.Create(builder => { builder.AddConsole(); })
                      .CreateLogger <AzureTelemetryReaderCmd>();

            _eventHubClient = EventHubClient.CreateFromConnectionString(opts.EventHubConnectionString);

            var runtimeInfo = await _eventHubClient.GetRuntimeInformationAsync();

            var d2CPartitions = runtimeInfo.PartitionIds;

            _logger.LogInformation("Starting reading messages from the Azure IoT Hub... Press Ctrl-C to cancel");
            var cts = new CancellationTokenSource();

            Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                cts.Cancel();
                _logger.LogInformation("Exiting...");
            };

            var tasks = new List <Task>();

            foreach (string partition in d2CPartitions)
            {
                tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
            }

            // Wait for all the PartitionReceivers to finsih.
            Task.WaitAll(tasks.ToArray());
        }
        private static async Task Main(string[] args)
        {
            Console.WriteLine("Lendo mensagens da coisa. Ctrl-C para sair.\n");

            var connectionString = new EventHubsConnectionStringBuilder(new Uri(_eventHubsEndpointCompativel), _eventHubsCaminhoCompativel, _iotHubNomeChaveSasKey, _iotHubChaveSas);

            _leitorHubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString());

            var runtimeInfo = await _leitorHubClient.GetRuntimeInformationAsync();

            var d2cPartitions = runtimeInfo.PartitionIds;

            CancellationTokenSource cts = new CancellationTokenSource();

            Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                cts.Cancel();
                Console.WriteLine("Exiting...");
            };

            var tasks = new List <Task>();

            foreach (string partition in d2cPartitions)
            {
                tasks.Add(ReceberMensagensDaCoisaAsync(partition, cts.Token));
            }

            Task.WaitAll(tasks.ToArray());
        }
        static async Task Main(string[] args)
        {
            Console.WriteLine("Read temperature messages. Ctrl-C to exit.\n");

            IConfiguration config = new ConfigurationBuilder()
                                    .AddJsonFile("conf.json", true, true)
                                    .Build();

            var connectionString = new EventHubsConnectionStringBuilder(new Uri(config["EventHubsCompatibleEndpoint"]), config["EventHubsCompatiblePath"], config["IotHubSasKeyName"], config["IotHubSasKey"]);

            s_eventHubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString());

            var runtimeInfo = await s_eventHubClient.GetRuntimeInformationAsync();

            var d2cPartitions = runtimeInfo.PartitionIds;

            CancellationTokenSource cts = new CancellationTokenSource();

            Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                cts.Cancel();
            };

            var tasks = new List <Task>();

            foreach (string partition in d2cPartitions)
            {
                tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
            }

            Task.WaitAll(tasks.ToArray());

            Console.ReadLine();
        }
        public static void CreateListenerPalAndReceiveMessages()
        {
            var builder = new EventHubsConnectionStringBuilder(Configuration.IoTHub.EventHubString)
            {
                EntityPath = Configuration.IoTHub.EventHubCompatibleName
            };

            EventHubClient eventHubClient          = EventHubClient.CreateFromConnectionString(builder.ToString());
            var            eventRuntimeInformation = eventHubClient.GetRuntimeInformationAsync().Result;
            var            eventHubPartitionsCount = eventRuntimeInformation.PartitionCount;
            string         consumerGroupName       = Configuration.IoTHub.EventHubConsumerGroup;

            foreach (string partitionId in eventRuntimeInformation.PartitionIds)
            {
                try
                {
                    PartitionReceiver receiver = eventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, DateTime.Now.AddMinutes(-LookbackTimeInMinutes));
                    s_log.WriteLine($"EventHub receiver created for partition {partitionId}, listening from {LookbackTimeInMinutes}");

                    new Task(async() =>
                    {
                        while (true)
                        {
                            IEnumerable <EventData> eventDatas = await receiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(OperationTimeoutInSeconds)).ConfigureAwait(false);
                            ProcessEventData(eventDatas);
                        }
                    }).Start();
                }
                catch (EventHubsException ex)
                {
                    s_log.WriteLine($"{nameof(EventHubTestListener)}.{nameof(CreateListenerPalAndReceiveMessages)}: Cannot create receiver for partitionID {partitionId}: {ex}");
                }
            }
        }
Ejemplo n.º 8
0
        public async void connect()
        {
            if (running)
            {
                return;
            }
            running     = true;
            CancelToken = new CancellationTokenSource();

            var connectionStringBuilder = new EventHubsConnectionStringBuilder(Config.Instance["ConnectionString"])
            {
                EntityPath = Config.Instance["IotHubD2cEndpoint"]
            };


            EventHubClientConnector = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

            var tasks = new List <Task>();
            var runTimeInformation = await EventHubClientConnector.GetRuntimeInformationAsync();

            foreach (string partitionId in runTimeInformation.PartitionIds)
            {
                Debug.WriteLine("Found partition {0}\n", partitionId);
                tasks.Add(ReceiveMessagesFromDeviceAsync(partitionId, CancelToken.Token));
            }
            //Task.WaitAll(tasks.ToArray());
        }
Ejemplo n.º 9
0
        public static async Task Run(ActionReceivedText OnSvcStatusUpdateD = null, ActionReceivedText OnSvcRecvTextD = null)
        {
            OnSvcStatusUpdate = OnSvcStatusUpdateD;
            OnSvcRecvText     = OnSvcRecvTextD;

            System.Diagnostics.Debug.WriteLine("IoT Hub Telemetry - Read device to cloud messages.");
            OnSvcStatusUpdate?.Invoke("IoT Hub Telemetry - Read device to cloud messages.");
            // Create an EventHubClient instance to connect to the
            // IoT Hub Event Hubs-compatible endpoint.
            //var connectionString1 = new EventHubsConnectionStringBuilder(new Uri(s_eventHubsCompatibleEndpoint), s_eventHubsCompatiblePath, s_iotHubSasKeyName, s_iotHubSasKey);


            //Get some of event cs properties from hub cs

            string iotHubSasKeyName = Azure_IoTHub_Connections.MyConnections.IotHubKeyName;


            EventHubsConnectionStringBuilder EventHubConnectionString = null;

            if (Azure_IoTHub_Connections.MyConnections.EHMethod1)
            {
                EventHubConnectionString = new EventHubsConnectionStringBuilder(Azure_IoTHub_Connections.MyConnections.EventHubsConnectionString);
            }
            else
            {
                EventHubConnectionString = new EventHubsConnectionStringBuilder(
                    new Uri(Azure_IoTHub_Connections.MyConnections.EventHubsCompatibleEndpoint),
                    Azure_IoTHub_Connections.MyConnections.EventHubsCompatiblePath,
                    Azure_IoTHub_Connections.MyConnections.IotHubKeyName,
                    Azure_IoTHub_Connections.MyConnections.EventHubsSasKey);
            }


            s_eventHubClient = EventHubClient.CreateFromConnectionString(EventHubConnectionString.ToString());

            // Create a PartitionReciever for each partition on the hub.
            var runtimeInfo = await s_eventHubClient.GetRuntimeInformationAsync();

            var d2cPartitions = runtimeInfo.PartitionIds;

            cts = new CancellationTokenSource();

            //System.Diagnostics.Debug.CancelKeyPress += (s, e) =>
            //{
            //    e.Cancel = true;
            //    cts.Cancel();
            //    System.Diagnostics.Debug.WriteLine("Exiting...");
            //};

            var tasks = new List <Task>();

            foreach (string partition in d2cPartitions)
            {
                tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
            }

            // Wait for all the PartitionReceivers to finsih.
            Task.WaitAll(tasks.ToArray());
            OnSvcStatusUpdate?.Invoke("Telemetry: All Threads Done.");
        }
        public static async Task <EventHubTestListener> CreateListenerPal(string deviceName)
        {
            PartitionReceiver receiver = null;
            Stopwatch         sw       = new Stopwatch();

            sw.Start();

            var builder = new EventHubsConnectionStringBuilder(Configuration.IoTHub.EventHubString)
            {
                EntityPath = Configuration.IoTHub.EventHubCompatibleName
            };

            EventHubClient eventHubClient          = EventHubClient.CreateFromConnectionString(builder.ToString());
            var            eventRuntimeInformation = await eventHubClient.GetRuntimeInformationAsync().ConfigureAwait(false);

            var    eventHubPartitionsCount = eventRuntimeInformation.PartitionCount;
            string partition         = EventHubPartitionKeyResolver.ResolveToPartition(deviceName, eventHubPartitionsCount);
            string consumerGroupName = Configuration.IoTHub.EventHubConsumerGroup;

            while (receiver == null && sw.Elapsed.TotalMinutes < MaximumWaitTimeInMinutes)
            {
                try
                {
                    receiver = eventHubClient.CreateReceiver(consumerGroupName, partition, DateTime.Now.AddMinutes(-LookbackTimeInMinutes));
                }
                catch (EventHubsException ex)
                {
                    s_log.WriteLine($"{nameof(EventHubTestListener)}.{nameof(CreateListener)}: Cannot create receiver: {ex}");
                }
            }

            sw.Stop();

            return(new EventHubTestListener(receiver));
        }
Ejemplo n.º 11
0
        private static async Task Main(string[] args)
        {
            Console.WriteLine("IoT Hub Quickstarts - Read device to cloud messages. Ctrl-C to exit.\n");

            // Create an EventHubClient instance to connect to the
            // IoT Hub Event Hubs-compatible endpoint.
            var connectionString = new EventHubsConnectionStringBuilder(new Uri(s_eventHubsCompatibleEndpoint), s_eventHubsCompatiblePath, s_iotHubSasKeyName, s_iotHubSasKey);

            s_eventHubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString());

            // Create a PartitionReciever for each partition on the hub.
            var runtimeInfo = await s_eventHubClient.GetRuntimeInformationAsync();

            var d2cPartitions = runtimeInfo.PartitionIds;

            CancellationTokenSource cts = new CancellationTokenSource();

            Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                cts.Cancel();
                Console.WriteLine("Exiting...");
            };

            var tasks = new List <Task>();

            foreach (string partition in d2cPartitions)
            {
                tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
            }

            // Wait for all the PartitionReceivers to finsih.
            Task.WaitAll(tasks.ToArray());
        }
        public async Task <IEnumerable <string> > GetPartitionIdsAsync()
        {
            if (this.partitionIds == null)
            {
                EventHubClient eventHubClient = null;
                try
                {
                    eventHubClient = EventHubClient.CreateFromConnectionString(this.host.EventHubConnectionString);
                    var runtimeInfo = await eventHubClient.GetRuntimeInformationAsync().ConfigureAwait(false);

                    this.partitionIds = runtimeInfo.PartitionIds.ToList();
                }
                catch (Exception e)
                {
                    throw new EventProcessorConfigurationException("Encountered error while fetching the list of EventHub PartitionIds", e);
                }
                finally
                {
                    if (eventHubClient != null)
                    {
                        await eventHubClient.CloseAsync().ConfigureAwait(false);
                    }
                }

                ProcessorEventSource.Log.EventProcessorHostInfo(this.host.Id, $"PartitionCount: {this.partitionIds.Count}");
            }

            return(this.partitionIds);
        }
        private async Task <StatusResultServiceModel> PingEventHubAsync()
        {
            var result = new StatusResultServiceModel(false, "EventHub check failed");
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(
                this.servicesConfig.EventHubConnectionString)
            {
                EntityPath = this.servicesConfig.EventHubName
            };

            EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(
                connectionStringBuilder.ToString());

            try
            {
                await eventHubClient.GetRuntimeInformationAsync();

                result.Message   = "Alive and well!";
                result.IsHealthy = true;
            }
            catch (Exception e)
            {
                this.log.Error(result.Message, () => new { e });
            }
            return(result);
        }
Ejemplo n.º 14
0
        public override async void StartListening()
        {
            try
            {
                var connectionString =
                    new EventHubsConnectionStringBuilder(
                        new Uri(GapConfig.EventHubsCompatibleEndpoint),
                        GapConfig.EventHubsCompatiblePath,
                        GapConfig.IotHubSasKeyName,
                        GapConfig.IotHubSasKey);

                eventHubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString());

                // Create a PartitionReciever for each partition on the hub.
                var runtimeInfo = await eventHubClient.GetRuntimeInformationAsync();

                var d2cPartitions = runtimeInfo.PartitionIds;

                // TODO: listening on partition 0 only.
                receivers =
                    d2cPartitions
                    .Select(d2c => eventHubClient.CreateReceiver("$Default", d2c, EventPosition.FromEnqueuedTime(DateTime.Now)))
                    .ToList();

                listenerTimer = ThreadPoolTimer.CreatePeriodicTimer(BackgoundListen, timerPeriod);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }
Ejemplo n.º 15
0
        static void Main()
        {
            var connectionString = new EventHubsConnectionStringBuilder(new Uri(s_eventHubsCompatibleEndpoint), s_eventHubsCompatiblePath, s_iotHubSasKeyName, s_iotHubSasKey);
            s_eventHubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString());

            // Create a PartitionReciever for each partition on the hub.
            var runtimeInfo = s_eventHubClient.GetRuntimeInformationAsync();
            var d2cPartitions = runtimeInfo.Result.PartitionIds;

            CancellationTokenSource cts = new CancellationTokenSource();

            System.Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                cts.Cancel();
                Console.WriteLine("Exiting...");
            };

            var tasks = new List<Task>();
            foreach (string partition in d2cPartitions)
            {
                tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
            }

            // Wait for all the PartitionReceivers to finsih.
            Task.WaitAll(tasks.ToArray());
        }
        private async Task <PartitionReceiver> CreateEventHubReceiver(string deviceName)
        {
            PartitionReceiver eventHubReceiver = null;
            Stopwatch         sw = new Stopwatch();

            sw.Start();

            var builder = new EventHubsConnectionStringBuilder(Configuration.IoTHub.EventHubString)
            {
                EntityPath = Configuration.IoTHub.EventHubCompatibleName
            };

            EventHubClient eventHubClient          = EventHubClient.CreateFromConnectionString(builder.ToString());
            var            eventRuntimeInformation = await eventHubClient.GetRuntimeInformationAsync().ConfigureAwait(false);

            var    eventHubPartitionsCount = eventRuntimeInformation.PartitionCount;
            string partition         = EventHubPartitionKeyResolver.ResolveToPartition(deviceName, eventHubPartitionsCount);
            string consumerGroupName = Configuration.IoTHub.EventHubConsumerGroup;

            while (eventHubReceiver == null && sw.Elapsed.Minutes < 1)
            {
                try
                {
                    eventHubReceiver = eventHubClient.CreateReceiver(consumerGroupName, partition, DateTime.Now.AddMinutes(-5));
                }
                catch (QuotaExceededException ex)
                {
                    Debug.WriteLine(ex);
                }
            }

            sw.Stop();

            return(eventHubReceiver);
        }
Ejemplo n.º 17
0
        static async void Start()
        {
            Console.WriteLine("Receive messages. Ctrl-C to exit.\n");

            eventHubClient = EventHubClient.CreateFromConnectionString(connectionString);

            var d2cPartitions = (await eventHubClient.GetRuntimeInformationAsync()).PartitionIds;

            CancellationTokenSource cts = new CancellationTokenSource();

            System.Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                cts.Cancel();
                Console.WriteLine("Exiting...");
            };

            var tasks = new List <Task>();

            foreach (string partition in d2cPartitions)
            {
                tasks.Add(ReceiveMessagesFromDeviceAsync(partition));
            }

            //    tasks.Add(ReceiveMessagesFromDeviceAsync("3"));
            Task.WaitAll(tasks.ToArray());
        }
Ejemplo n.º 18
0
        static async Task Main(string[] args)
        {
            var eventHubConnectionString = $"Endpoint=sb://event-hub-examples.servicebus.windows.net/;SharedAccessKeyName={SHARED_ACCESS_KEY_NAME};SharedAccessKey={SHARED_ACCESS_KEY};EntityPath={EVENT_HUB_NAME}";

            _client = EventHubClient.CreateFromConnectionString(eventHubConnectionString);
            var runtimeInformation = await _client.GetRuntimeInformationAsync();

            var partitionInformation = runtimeInformation.PartitionIds;

            var partitionSender = _client.CreatePartitionSender(partitionInformation[0]);

            var newEventData = new SimpleDataModel()
            {
                Age       = 35,
                FirstName = "User",
                LastName  = "Named"
            };

            await partitionSender.SendAsync(newEventData.ToEventData());

            var newEventData2 = new
            {
                Age       = 15,
                FirstName = "Pracka",
                LastName  = "Meen"
            };

            await _client.SendAsync(EventDataMapper.MapToEventData(newEventData2));
        }
Ejemplo n.º 19
0
        public static void Main(string[] args)
        {
            colorMessage("Wine Cellar Operator\n", ConsoleColor.Yellow);

            // Create an EventHubClient instance to connect to the IoT Hub Event Hubs-compatible endpoint.
            var connectionString = new EventHubsConnectionStringBuilder(new Uri(s_eventHubsCompatibleEndpoint), s_eventHubsCompatiblePath, s_iotHubSasKeyName, s_iotHubSasKey);

            s_eventHubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString());

            // Create a PartitionReceiver for each partition on the hub.
            var runtimeInfo   = s_eventHubClient.GetRuntimeInformationAsync().GetAwaiter().GetResult();
            var d2cPartitions = runtimeInfo.PartitionIds;

            // A registry manager is used to access the digital twins.
            registryManager = RegistryManager.CreateFromConnectionString(s_serviceConnectionString);
            SetTwinProperties().Wait();

            // Create a ServiceClient to communicate with service-facing endpoint on your hub.
            s_serviceClient = ServiceClient.CreateFromConnectionString(s_serviceConnectionString);
            InvokeMethod().GetAwaiter().GetResult();

            // Create receivers to listen for messages.
            var tasks = new List <Task>();

            foreach (string partition in d2cPartitions)
            {
                tasks.Add(ReceiveMessagesFromDeviceAsync(partition));
            }

            // Wait for all the PartitionReceivers to finish.
            Task.WaitAll(tasks.ToArray());
        }
        public async Task StartAsync(EventHubClient client, CancellationToken token)
        {
            var groups = await _nsm.GetConsumerGroupsAsync(EventHubName);
            groups.ToList()
                .ForEach(g => ConsumerGroups.Add(g.Name));

            var defaultConsumerGroup = ConsumerGroups.First();

            var runtime = await client.GetRuntimeInformationAsync();
            runtime.PartitionIds
                .Select(partitionId =>
                {
                    Func<Task<PartitionDescription>> f = () => _nsm.GetEventHubPartitionAsync(EventHubName, defaultConsumerGroup, partitionId);
                    return new PartitionViewModel(partitionId, client, f);
                })
                .ToList()
                .ForEach(p => Partitions.Add(p));

            foreach (var partition in Partitions)
            {
                partition.WhenEventReceived.Subscribe(x => { _aggregatedEventCount.OnNext(x); });

                _listeners.Add(partition.StartAsync(CancellationToken.None));
            }

            _aggregatedEventCount
                .Buffer(TimeSpan.FromSeconds(5))
                .TimeInterval()
                .Select(x => x.Value.Sum() / x.Interval.TotalSeconds)
                .Subscribe(rate => RatePerSecond = rate);
        }
        /// <summary>
        /// Method to run Health Check for Azure Event hub
        /// </summary>
        /// <param name="serviceKey"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override LightHealth.HealthCheck HealthCheck(string serviceKey, string value)
        {
            try
            {
                if (config == null)
                {
                    this.GetConnection(string.Empty);
                }

                foreach (var item in _eventCache)
                {
                    if (!string.IsNullOrEmpty(item.Value.HubName))
                    {
                        var connectionStringBuilder = new EventHubsConnectionStringBuilder(config.ConnectionString)
                        {
                            EntityPath = item.Value.HubName
                        };

                        EventHubClient eventHub = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
                        var            a        = eventHub.GetRuntimeInformationAsync();
                        eventHub.Close();
                    }
                }

                return(LightHealth.HealthCheck.Healthy);
            }
            catch
            {
                return(LightHealth.HealthCheck.Unhealthy);
            }
        }
Ejemplo n.º 22
0
        public async Task <List <EventData> > GetMessagesForDevice(string deviceId, DateTime startTime, int maxPerPartition = 100, int waitTimeSecs = 5)
        {
            var messages = new List <EventData>();

            EventHubClient    eventHubClient    = EventHubClient.CreateFromConnectionString(this.eventHubConnectionString);
            PartitionReceiver partitionReceiver = eventHubClient.CreateReceiver(
                EventHubConsumerGroup,
                EventHubPartitionKeyResolver.ResolveToPartition(deviceId, (await eventHubClient.GetRuntimeInformationAsync()).PartitionCount),
                EventPosition.FromEnqueuedTime(startTime));

            // Retry a few times due to weird behavior with ReceiveAsync() not returning all messages available
            for (int i = 0; i < 3; i++)
            {
                IEnumerable <EventData> events = await partitionReceiver.ReceiveAsync(maxPerPartition, TimeSpan.FromSeconds(waitTimeSecs));

                if (events != null)
                {
                    messages.AddRange(events);
                }

                if (i < 3)
                {
                    await Task.Delay(TimeSpan.FromSeconds(5));
                }
            }

            await partitionReceiver.CloseAsync();

            await eventHubClient.CloseAsync();

            return(messages);
        }
Ejemplo n.º 23
0
        protected async Task VerifyDataOnIoTHub(string moduleId)
        {
            Console.WriteLine($"Verifying data on IoTHub from {moduleId}");

            // First Verify if module is already running.
            await this.bootstrapper.VerifyModuleIsRunning(moduleId);

            var builder = new EventHubsConnectionStringBuilder(this.eventhubCompatibleEndpointWithEntityPath)
            {
                TransportType = this.eventHubClientTransportType
            };

            Console.WriteLine($"Receiving events from device '{this.context.DeviceId}' on Event Hub '{builder.EntityPath}'");

            EventHubClient eventHubClient =
                EventHubClient.CreateFromConnectionString(builder.ToString());

            this.proxy.ForEach(p => eventHubClient.WebProxy = p);

            PartitionReceiver eventHubReceiver = eventHubClient.CreateReceiver(
                "$Default",
                EventHubPartitionKeyResolver.ResolveToPartition(
                    this.context.DeviceId,
                    (await eventHubClient.GetRuntimeInformationAsync()).PartitionCount),
                EventPosition.FromEnd());

            // TODO: [Improvement] should verify test results without using event hub, which introduce latency.
            var result = new TaskCompletionSource <bool>();

            using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(20))) // This long timeout is needed in case event hub is slow to process messages
            {
                using (cts.Token.Register(() => result.TrySetCanceled()))
                {
                    eventHubReceiver.SetReceiveHandler(
                        new PartitionReceiveHandler(
                            eventData =>
                    {
                        eventData.SystemProperties.TryGetValue("iothub-connection-device-id", out object devId);
                        eventData.SystemProperties.TryGetValue("iothub-connection-module-id", out object modId);

                        if (devId != null && devId.ToString().Equals(this.context.DeviceId) &&
                            modId != null && modId.ToString().Equals(moduleId))
                        {
                            result.TrySetResult(true);
                            return(true);
                        }

                        return(false);
                    }));

                    await result.Task;
                }
            }

            Console.WriteLine("VerifyDataOnIoTHub completed.");
            await eventHubReceiver.CloseAsync();

            await eventHubClient.CloseAsync();
        }
Ejemplo n.º 24
0
        public async Task LoadData()
        {
            //Deserialize last received message
            using (SerializeDataOnFile sdf = new SerializeDataOnFile(this.FileNameSerializeLastOffset))
            {
                Dictionary <string, object> lastRowValuesAsObjects = sdf.GetDictionary();
                lastRowValues = new Dictionary <string, LastReceivedMessage>();
                foreach (var item in lastRowValuesAsObjects)
                {
                    LastReceivedMessage lastItem;
                    lastItem.EnqueuedOffset  = Convert.ToInt64(((string)item.Value).Split(',')[0]);
                    lastItem.EnqueuedTimeUtc = Convert.ToDateTime(((string)item.Value).Split(',')[1]);
                    lastRowValues.Add(item.Key, lastItem);
                }
            }

            //Load
            receivedRows = new List <RowRead>();

            var connectionStringBuilder = new EventHubsConnectionStringBuilder(this.ConnectionStringEventHubCompatible)
            {
                EntityPath = this.EntityEventHubCompatible
            };

            eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

            var runTimeInformation = await eventHubClient.GetRuntimeInformationAsync();

            foreach (string partition in runTimeInformation.PartitionIds)
            {
                // It checks if lastRowValues contains for current partition last received message
                if (!lastRowValues.ContainsKey(partition))
                {
                    LastReceivedMessage lastReceivedMessage;
                    lastReceivedMessage.EnqueuedTimeUtc = this.StartTimeInHours == 0 ? DateTime.UtcNow.AddHours(-24) : DateTime.UtcNow.AddHours(this.StartTimeInHours);
                    lastReceivedMessage.EnqueuedOffset  = -1; //Starting offset for EventHub. Note: For IoT Hub, starting offset is 0.
                    lastRowValues.Add(partition, lastReceivedMessage);
                }

                // Receiving
                var receiver = eventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partition, lastRowValues[partition].EnqueuedOffset.ToString());
                await ReceiveMessagesFromDeviceAsync(receiver);
            }

            valuesRead = receivedRows.ToArray();

            //Serialize last received message
            using (SerializeDataOnFile sdf = new SerializeDataOnFile(this.FileNameSerializeLastOffset))
            {
                Dictionary <string, object> lastRowValuesAsObjects = new Dictionary <string, object>();
                foreach (var item in lastRowValues)
                {
                    lastRowValuesAsObjects.Add(item.Key, $"{item.Value.EnqueuedOffset},{item.Value.EnqueuedTimeUtc}");
                }
                sdf.SetDictionary(lastRowValuesAsObjects);
            }
        }
        public async Task <IActionResult> Post([FromRoute] string name, [FromBody] EventsProcessorApplicationParams parameters)
        {
            // Determine the number of IoT Hub partitions.
            // The events processing service will be created with the same number of partitions.
            EventHubClient             eventHubClient = EventHubClient.CreateFromConnectionString(parameters.IotHubConnectionString, "messages/events");
            EventHubRuntimeInformation eventHubInfo   = await eventHubClient.GetRuntimeInformationAsync();

            // Application parameters are passed to the Events Processing application instance.
            NameValueCollection appInstanceParameters = new NameValueCollection();

            appInstanceParameters["IotHubConnectionString"]        = parameters.IotHubConnectionString;
            appInstanceParameters["IotHubProcessOnlyFutureEvents"] = parameters.IotHubProcessOnlyFutureEvents;
            appInstanceParameters["PublishDataServiceURLs"]        = parameters.PublishDataServiceURLs;

            ApplicationDescription application = new ApplicationDescription(
                new Uri($"{Names.EventsProcessorApplicationPrefix}/{name}"),
                Names.EventsProcessorApplicationTypeName,
                parameters.Version,
                appInstanceParameters);

            // Create a named application instance
            await this.fabricClient.ApplicationManager.CreateApplicationAsync(application, this.operationTimeout, this.appLifetime.ApplicationStopping);

            // Next, create named instances of the services that run in the application.
            ServiceUriBuilder serviceNameUriBuilder = new ServiceUriBuilder(application.ApplicationName.ToString(), Names.EventsProcessorRouterServiceName);

            StatefulServiceDescription service = new StatefulServiceDescription()
            {
                ApplicationName            = application.ApplicationName,
                HasPersistedState          = true,
                MinReplicaSetSize          = 3,
                TargetReplicaSetSize       = 3,
                PartitionSchemeDescription = new UniformInt64RangePartitionSchemeDescription(eventHubInfo.PartitionCount, 0, eventHubInfo.PartitionCount - 1),
                ServiceName     = serviceNameUriBuilder.Build(),
                ServiceTypeName = Names.EventsProcessorRouterServiceTypeName
            };

            await this.fabricClient.ServiceManager.CreateServiceAsync(service, this.operationTimeout, this.appLifetime.ApplicationStopping);

            if (parameters.PublishDataServiceURLs != null && parameters.PublishDataServiceURLs.Length > 0)
            {
                // Next, create named instances of the services that run in the application.
                serviceNameUriBuilder = new ServiceUriBuilder(application.ApplicationName.ToString(), Names.EventsProcessorExtenderServiceName);

                StatelessServiceDescription extenderService = new StatelessServiceDescription()
                {
                    ApplicationName            = application.ApplicationName,
                    InstanceCount              = 1,
                    PartitionSchemeDescription = new SingletonPartitionSchemeDescription(),
                    ServiceName     = serviceNameUriBuilder.Build(),
                    ServiceTypeName = Names.EventsProcessorExtenderServiceTypeName
                };

                await this.fabricClient.ServiceManager.CreateServiceAsync(extenderService, this.operationTimeout, this.appLifetime.ApplicationStopping);
            }
            return(this.Ok());
        }
Ejemplo n.º 26
0
        protected async Task VerifyDataOnIoTHubAsync()
        {
            // Leaf device without parent not expected to send messages
            if (!this.edgeDeviceId.HasValue)
            {
                return;
            }

            var builder = new EventHubsConnectionStringBuilder(this.eventhubCompatibleEndpointWithEntityPath)
            {
                TransportType = this.eventHubClientTransportType
            };

            Console.WriteLine($"Receiving events from device '{this.context.Device.Id}' on Event Hub '{builder.EntityPath}'");

            EventHubClient eventHubClient =
                EventHubClient.CreateFromConnectionString(builder.ToString());

            this.proxy.ForEach(p => eventHubClient.WebProxy = p);

            PartitionReceiver eventHubReceiver = eventHubClient.CreateReceiver(
                "$Default",
                EventHubPartitionKeyResolver.ResolveToPartition(
                    this.context.Device.Id,
                    (await eventHubClient.GetRuntimeInformationAsync()).PartitionCount),
                EventPosition.FromEnqueuedTime(DateTime.Now.AddMinutes(-5)));

            var result = new TaskCompletionSource <bool>();

            using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3)))
            {
                using (cts.Token.Register(() => result.TrySetCanceled()))
                {
                    eventHubReceiver.SetReceiveHandler(
                        new PartitionReceiveHandler(
                            eventData =>
                    {
                        eventData.SystemProperties.TryGetValue("iothub-connection-device-id", out var devId);

                        if (devId != null && devId.ToString().Equals(this.context.Device.Id, StringComparison.Ordinal) &&
                            Encoding.UTF8.GetString(eventData.Body).Contains(this.context.MessageGuid, StringComparison.Ordinal))
                        {
                            result.TrySetResult(true);
                            return(true);
                        }

                        return(false);
                    }));

                    await result.Task;
                }
            }

            await eventHubReceiver.CloseAsync();

            await eventHubClient.CloseAsync();
        }
        public async Task SendDataAsync(CoffeeMachineData data)
        {
            EventData evData     = CreateEventData(data);
            var       partitions = await _ehClient.GetRuntimeInformationAsync();

            var partitionId = await _ehClient.GetPartitionRuntimeInformationAsync(partitions.PartitionIds.First());

            await _ehClient.SendAsync(evData);
        }
 private async Task<EventHubReceiver> CreateEventHubReceiver(string deviceName)
 {
     EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(hubConnectionString, "messages/events");
     var eventHubPartitions = await eventHubClient.GetRuntimeInformationAsync();
     var eventHubPartitionsCount = eventHubPartitions.PartitionCount;
     string partition = EventHubPartitionKeyResolver.ResolveToPartition(deviceName, eventHubPartitionsCount);
     string consumerGroupName = Configuration.IoTHub.ConsumerGroup;
     return eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, DateTime.Now, TestUtil.EventHubEpoch++);
 }
Ejemplo n.º 29
0
        void InitPartitions(EventHubClient client)
        {
            string[] partitionList = client.GetRuntimeInformationAsync().Result.PartitionIds;

            foreach (string partitionId in partitionList)
            {
                var receiver = client.CreateReceiver("$default", partitionId, PartitionReceiver.EndOfStream);
                receiver.SetReceiveHandler(this);
            }
        }
Ejemplo n.º 30
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            var info = await eventHubClient.GetRuntimeInformationAsync();

            var partitions = info.PartitionIds;
            var cts        = new CancellationTokenSource();

            var tasks = partitions.Select(partition => ReceiveMessagesFromDeviceAsync(partition, cts.Token));

            tasks.ToArray();
        }
Ejemplo n.º 31
0
        public async Task ReceiveMessagesFromDeviceAsync(CancellationToken cancellationToken = default)
        {
            var information = await eventHubClient.GetRuntimeInformationAsync();

            var tasks = new List <Task>();

            foreach (string partition in information.PartitionIds)
            {
                tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cancellationToken));
            }
        }
        public async Task<IEnumerable<string>> AllocateAsync(EventHubClient eventHubClient, FabricClient fabricClient)
        {
            var runtimeInformation = await eventHubClient.GetRuntimeInformationAsync();
            var eventHubPartitions = runtimeInformation.PartitionIds.OrderBy(p => p).ToArray();
            var serviceFabricPartitions = (await fabricClient.QueryManager.GetPartitionListAsync(_serviceName))
                                           .Select(p => p.PartitionInformation.Id.ToString())
                                           .OrderBy(p => p).ToArray();

            // when there are more service fabric partitions then eventhub partitions, 
            // just assign them one by one and the remainder of the partitions will stay dormant

            if (serviceFabricPartitions.Length >= eventHubPartitions.Length)
            {
                var index = Array.IndexOf(serviceFabricPartitions, _partitionId.ToString());

                return new [] { eventHubPartitions[index] };
            }
            else
            {
                // otherwise distribute eventhub partitions evenly across service fabric partitions

                var remainder = eventHubPartitions.Length % serviceFabricPartitions.Length;
                var numberOfEventHubPartitionsPerServiceFabricPartition = eventHubPartitions.Length / serviceFabricPartitions.Length;
                if (remainder > 0)  numberOfEventHubPartitionsPerServiceFabricPartition++;
                var index = Array.IndexOf(serviceFabricPartitions, _partitionId.ToString());

                var allocated = new List<string>();
                for (var i = 0; i < numberOfEventHubPartitionsPerServiceFabricPartition; i++)
                {
                    var idx = (index*numberOfEventHubPartitionsPerServiceFabricPartition) + i;
                    if (eventHubPartitions.Length >= idx)
                    {
                        allocated.Add(eventHubPartitions[idx]);
                    }
                }

                return allocated.ToArray();
            }
        }
Ejemplo n.º 33
0
        public  Task OpenEventHubAsync(string eventHubConnectionString, string hubName)
        {
            
            return Task.Factory.StartNew(async () => {
                if (string.IsNullOrWhiteSpace(eventHubConnectionString))
                    throw new ArgumentException("invalid event hub connection string");

                if (string.IsNullOrWhiteSpace(hubName))
                    throw new ArgumentException("invalid hubname");
                this.IsOpen = true;

                var builder = new ServiceBusConnectionStringBuilder(eventHubConnectionString) { TransportType = TransportType.Amqp };
                var s = builder.ToString();
                _factory = MessagingFactory.CreateFromConnectionString(builder.ToString());
       
                _client = _factory.CreateEventHubClient(hubName);
                
                var runtimeInfo = await _client.GetRuntimeInformationAsync();

                _partitions.Clear();
                foreach (var p in runtimeInfo.PartitionIds)
                {
                
                    var partition = await _client.GetPartitionRuntimeInformationAsync(p);
                    var count = partition.LastEnqueuedSequenceNumber - partition.BeginSequenceNumber;
                    if (count != 0)
                        count++;
                    this.MessageCount = count;
                    _partitions.Add(new Partition(p, _messages));
                    _foundPartitions.OnNext(partition);
                }

                _foundPartitions.OnCompleted();
                
            });

        }