/// <summary>
 /// The class constructor
 /// </summary>
 /// <param name="deviceClient">Instance of class for Device Steaming from the SDK</param>
 /// <param name="_OnRecvText">Callback to handle received message, post stripping of flags</param>
 /// <param name="deviceCurrentSettings">Optional custom class to handle processing of flags</param>
 public DeviceStream_Device(DeviceClient deviceClient, ActionReceivedTextIO onRecvdText, ActionCommandD actionCommand = null, ActionReceivedText onDeviceStatusUpdateD = null, bool keepDeviceListening = false, DeviceAndSvcCurrentSettings deviceCurrentSettings = null)
 {
     this.deviceClient     = deviceClient;
     OnRecvdTextIO         = onRecvdText;
     OnDeviceStatusUpdateD = onDeviceStatusUpdateD;
     KeepDeviceListening   = keepDeviceListening;
     ActionCmdD            = actionCommand;
     if (deviceCurrentSettings != null)
     {
         DeviceCurrentSettings = deviceCurrentSettings;
     }
     else
     {
         DeviceCurrentSettings = new DeviceAndSvcCurrentSettings();
     }
     DeviceCurrentSettings.KeepDeviceListening = KeepDeviceListening;
 }
        /// <summary>
        /// Method called from app to instantiate this class and start Device Streaming on Device.
        /// </summary>
        /// <param name="device_cs">Device Id eg "MyDevice"</param>
        /// <param name="onRecvdTextD">Callback to handle received message, post stripping of flags</param>
        /// <param name="deviceCurrentSettings">Optional custom class to handle processing of flags</param>
        /// <returns>The running task</returns>
        public static async Task RunDevice(string device_cs, ActionReceivedTextIO onRecvdTextD, ActionReceivedText onDeviceStatusUpdateD = null, ActionCommandD actionCommandD = null, bool keepDeviceListening = false, DeviceAndSvcCurrentSettings deviceCurrentSettings = null)
        {
            bool __keepDeviceListening = false;

            do
            {
                TransportType device_hubTransportTryp = DeviceStreamingCommon.device_transportType;
                try
                {
                    using (DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(device_cs, device_hubTransportTryp))
                    {
                        if (deviceClient == null)
                        {
                            System.Diagnostics.Debug.WriteLine("Failed to create DeviceClient!");
                            //return null;
                        }

                        deviceStream_Device = new DeviceStream_Device(deviceClient, onRecvdTextD, actionCommandD, onDeviceStatusUpdateD, keepDeviceListening, deviceCurrentSettings);

                        if (deviceStream_Device == null)
                        {
                            System.Diagnostics.Debug.WriteLine("Failed to create DeviceStreamClient!");
                            //return null;
                        }

                        try
                        {
                            await deviceStream_Device.RunDeviceAsync();//.GetAwaiter().GetResult();
                        }
                        catch (Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException)
                        {
                            System.Diagnostics.Debug.WriteLine("3 Error RunDevice(): Hub connection failure");
                        }
                        catch (Microsoft.Azure.Devices.Common.Exceptions.DeviceNotFoundException)
                        {
                            System.Diagnostics.Debug.WriteLine("3 Error RunDevice(): Device not found");
                        }
                        catch (TaskCanceledException)
                        {
                            System.Diagnostics.Debug.WriteLine("3 Error RunDevice(): Task canceled");
                        }
                        catch (OperationCanceledException)
                        {
                            System.Diagnostics.Debug.WriteLine("3 Error RunDevice(): Operation canceled");
                        }
                        catch (Exception ex)
                        {
                            if (!ex.Message.Contains("Timed out"))
                            {
                                System.Diagnostics.Debug.WriteLine("3 Error RunDevice(): " + ex.Message);
                            }
                            else
                            {
                                System.Diagnostics.Debug.WriteLine("3 Error RunDevice(): Timeout");
                            }
                        }
                    }
                    //return null;
                }
                catch (Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException)
                {
                    System.Diagnostics.Debug.WriteLine("4 Error RunDevice(): Hub connection failure");
                }
                catch (Microsoft.Azure.Devices.Common.Exceptions.DeviceNotFoundException)
                {
                    System.Diagnostics.Debug.WriteLine("4 Error RunDevice(): Device not found");
                }
                catch (TaskCanceledException)
                {
                    System.Diagnostics.Debug.WriteLine("4 Error RunDevice(): Task canceled");
                }
                catch (OperationCanceledException eex)
                {
                    System.Diagnostics.Debug.WriteLine("4 Error RunDevice(): Operation canceled \r\n" + eex.Message);
                }
                catch (Exception ex)
                {
                    if (!ex.Message.Contains("Timeout"))
                    {
                        System.Diagnostics.Debug.WriteLine("4 Error RunDevice(): " + ex.Message);
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("4 Error RunDevice(): Timeout");
                    }
                }
                __keepDeviceListening = deviceStream_Device.DeviceCurrentSettings.KeepDeviceListening;
                if (__keepDeviceListening)
                {
                    deviceStream_Device.UpdateStatus("Continuing to listen");
                }
                else
                {
                    deviceStream_Device.UpdateStatus("Not listening");
                }
                //Nb: deviceStream_Device is disposed here.
            } while (__keepDeviceListening);
        }