Read Events from EventHub in Unity3D and HoloLens

Viewed 267

We are trying to show events from an Azure EventHub in our HoloLens application. When we run our project in Unity 3D, it works and paint the data from the events.

But when we deploy the same solution on the HoloLens, we don't receive events and also we don't receive any error.

Here is the code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Microsoft.MixedReality.Toolkit.UI;
    using TMPro;
    using System;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading;
    using Azure.Messaging.EventHubs;
    using Azure.Messaging.EventHubs.Processor;
    using Azure.Storage.Blobs;
    using System.Text.Json;
    using System.Text.Json.Serialization;
    
    namespace Microsoft.MixedReality.Toolkit.Examples.Demos
    {
        [AddComponentMenu("Scripts/MRTK/Examples/SliderLunarLander")]
        public class Manager : MonoBehaviour
        {
            [SerializeField]
            
            public Animator helice2;
            public TextMeshPro textovelocidad;
            public EventProcessorClient processor;
            public BlobContainerClient storageClient;
    
            private const string storageConnectionString = "XXXX";
            private const string blobContainerName = "XXXX";
            private const string eventHubsConnectionString = "XXXX";
            private const string consumerGroup = "hololens";
            private const string EventHubName = "XXXX";
    
            private float? speedValue;
            private string errorText = string.Empty;
            private CancellationToken cancellationToken;
    
            async void Start()
        {
            errorText = "Init EventReader...";

            storageClient = new BlobContainerClient(storageConnectionString, blobContainerName);

            var clientOptions = new EventProcessorClientOptions()
            {
                ConnectionOptions = new EventHubConnectionOptions ()
                { 
                    TransportType = EventHubsTransportType.AmqpWebSockets
                }
            };

            processor = new EventProcessorClient
            (
                storageClient,
                consumerGroup,
                eventHubsConnectionString,
                clientOptions
            );            
            
            processor.ProcessEventAsync += processEventHandler;
            processor.ProcessErrorAsync += processErrorHandler;

            errorText = "Start EventReader...";

            await processor.StartProcessingAsync();

            try
            {
                //The processor performs its work in the background; block until cancellation
                //to allow processing to take place.
                errorText = "Infinite Loop...";
                await Task.Delay(Timeout.Infinite);
            }
            catch (TaskCanceledException tcex)
            {
                //This is expected when the delay is canceled.
                textoError.text = tcex.Message;
            }

            //Debug.Log("Device client initialited...");            
        }

        public void OnSliderUpdated(SliderEventData eventData)
        {           
                helice2.speed = eventData.NewValue;            
        }

        async void OnDestroy()
        {
            try
            {
                await processor.StopProcessingAsync();
            }
            finally
            {
                //To prevent leaks, the handlers should be removed when processing is complete.
                processor.ProcessEventAsync -= processEventHandler;
                processor.ProcessErrorAsync -= processErrorHandler;
            }
        }

        private void Update()
        {
            if (speedValue != null)
            {
                helice2.speed = float.Parse(speedValue.ToString());
            }
            
            textovelocidad.text = (helice2.speed).ToString("F2") + " r.p.m.";
            textovelocidad.text += " | " + infoText; 
            textoError.text = errorText;
        }

        async Task processEventHandler(ProcessEventArgs eventArgs)
        {
            try
            {
                // Write the body of the event to the console window
                //Console.WriteLine("\tRecevied event: {0}", Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray()));
                infoText = " Event Received ";

                string json = Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray());

                var deviceTurbine = JsonSerializer.Deserialize<DeviceTurbine>(json);
                
                if (deviceTurbine != null)
                {
                    speedValue = deviceTurbine.speed;
                }

                // Update checkpoint in the blob storage so that the app receives only new events the next time it's run
                await eventArgs.UpdateCheckpointAsync(eventArgs.CancellationToken);

                
            }
            catch (Exception ex)
            {
                // Handle the exception from handler code
                // Debug.LogError(ex.Message);
                errorText = ex.Message;
            }
        }

        Task processErrorHandler(ProcessErrorEventArgs eventArgs)
        {
            try
            {
                // Write details about the error to the console window
                //Console.WriteLine($"\tPartition '{ eventArgs.PartitionId}': an unhandled exception was encountered. This was not expected to happen.");
                //Console.WriteLine(eventArgs.Exception.Message);
                errorText = eventArgs.Exception.Message;
            }
            catch
            {
                // Handle the exception from handler code
                errorText = eventArgs.Exception.Message;
            }

            return Task.CompletedTask;
        }
        }
    
        class DeviceTurbine
        {
            private float _speed;
            private DateTime _timestamp;
            private string _deviceId;
            public float speed
            {
                get; set;
            }
    
            public DateTime timestamp
            {
                get;set;
            }
    
            public string deviceId
            {
                get; set;
            }
    
    
        }
    }
0 Answers
Related