예제 #1
0
        // Asynchronously create a PartitionReceiver for a partition and then start
        // reading any messages sent from the simulated client.
        private static async Task ReceiveMessagesFromDeviceAsync(string partition, CancellationToken ct)
        {
            ContinueLooping = true;
            // Create the receiver using the default consumer group.
            // For the purposes of this sample, read only messages sent since
            // the time the receiver is created. Typically, you don't want to skip any messages.
            var eventHubReceiver = s_eventHubClient.CreateReceiver("$Default", partition, EventPosition.FromEnqueuedTime(DateTime.Now));

            System.Diagnostics.Debug.WriteLine("Telemetry: Create receiver on partition: " + partition);
            OnSvcStatusUpdate?.Invoke("Telemetry: Create receiver on partition: " + partition);

            while (ContinueLooping)
            {
                if (ct.IsCancellationRequested)
                {
                    break;
                }
                System.Diagnostics.Debug.WriteLine("Telemetry: Listening for messages on: " + partition);
                OnSvcStatusUpdate?.Invoke("Telemetry: Listening for messages on: " + partition);
                // Check for EventData - this methods times out if there is nothing to retrieve.
                var events = await eventHubReceiver.ReceiveAsync(100);

                // If there is data in the batch, process it.
                if (events == null)
                {
                    continue;
                }

                foreach (EventData eventData in events)
                {
                    string data = Encoding.UTF8.GetString(eventData.Body.Array);
                    System.Diagnostics.Debug.WriteLine("Message received on partition {0}:", partition);
                    System.Diagnostics.Debug.WriteLine("  {0}:", data);
                    if (eventData.Properties != null)
                    {
                        if (eventData.Properties.Count != 0)
                        {
                            System.Diagnostics.Debug.WriteLine("Application properties (set by device):");
                            foreach (var prop in eventData.Properties)
                            {
                                System.Diagnostics.Debug.WriteLine("  {0}: {1}", prop.Key, prop.Value);
                            }
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("No Application Properties (set by device)");
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("No Application Properties (set by device");
                    }
                    if (eventData.SystemProperties != null)
                    {
                        if (eventData.SystemProperties.Count != 0)
                        {
                            System.Diagnostics.Debug.WriteLine("System properties (set by IoT Hub):");
                            foreach (var prop in eventData.SystemProperties)
                            {
                                System.Diagnostics.Debug.WriteLine("  {0}: {1}", prop.Key, prop.Value);
                            }
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("No System properties from IoT Hub");
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("No System properties from IoT Hub");
                    }

                    OnSvcRecvText?.Invoke(Azure_IoTHub_Telemetry.SyntheticIoTMessage.EventData_ToString(eventData));
                }
            }
            OnSvcStatusUpdate?.Invoke("Telemetry: Exiting - Wait for \"All Threads Done\"");
        }
예제 #2
0
 public static void Cancel()
 {
     OnSvcStatusUpdate?.Invoke("Telemetry - Cancelling");
     cts?.Cancel();
     ContinueLooping = false;
 }