public DeviceStream_Svc(ServiceClient deviceClient, String deviceId, string msgOut, ActionReceivedText onRecvdTextD, int devKeepListening = 2, int devAutoStart = 2,ActionReceivedText onStatusUpdateD = null, bool keepAlive = false, bool responseExpected = true, DeviceAndSvcCurrentSettings svcCurrentSettings = null) { _serviceClient = deviceClient; _deviceId = deviceId; OnRecvdTextD = onRecvdTextD; OnStatusUpdateD = onStatusUpdateD; if (svcCurrentSettings != null) SvcCurrentSettings = svcCurrentSettings; else SvcCurrentSettings = new DeviceAndSvcCurrentSettings(); SvcCurrentSettings.KeepAlive = keepAlive; SvcCurrentSettings.ResponseExpected = responseExpected; DevKeepListening = devKeepListening; DevAutoStart = devAutoStart; MsgOut = msgOut; }
/// <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; }
public static async Task RunSvc(string s_connectionString, String deviceId, string msgOut, ActionReceivedText onRecvdTextD, int devKeepListening=2, int devAutoStart=2, ActionReceivedText oOnStatusUpdate=null, bool keepAlive = false, bool responseExpected = true, DeviceAndSvcCurrentSettings deviceAndSvcCurrentSettings = null ) { if (deviceStream_Svc != null) { System.Diagnostics.Debug.WriteLine("Svc Socket is already open!"); return; } else { try { using (ServiceClient serviceClient = ServiceClient.CreateFromConnectionString(s_connectionString, DeviceStreamingCommon.s_transportType)) { if (serviceClient == null) { System.Diagnostics.Debug.WriteLine("Failed to create SericeClient!"); //return null; } //Attach keepalive and respond info if set deviceStream_Svc = new DeviceStream_Svc(serviceClient, deviceId, msgOut, onRecvdTextD, devKeepListening , devAutoStart , oOnStatusUpdate, keepAlive,responseExpected, deviceAndSvcCurrentSettings); if (deviceStream_Svc == null) { System.Diagnostics.Debug.WriteLine("Failed to create DeviceStreamSvc!"); } else { try { await deviceStream_Svc.RunSvcAsync();//.GetAwaiter().GetResult(); } catch (Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException) { System.Diagnostics.Debug.WriteLine("3 Error RunSvc(): Hub connection failure"); } catch (Microsoft.Azure.Devices.Common.Exceptions.DeviceNotFoundException) { System.Diagnostics.Debug.WriteLine("3 Error RunSvc(): Device not found"); } catch (TaskCanceledException) { System.Diagnostics.Debug.WriteLine("3 Error RunSvc(): Task canceled"); } catch (OperationCanceledException) { System.Diagnostics.Debug.WriteLine("3 Error RunSvc(): Operation canceled"); } catch (Exception ex) { if (!ex.Message.Contains("Timed out")) System.Diagnostics.Debug.WriteLine("3 Error RunSvc(): " + ex.Message); else { System.Diagnostics.Debug.WriteLine("3 Error RunSvc(): Timeout"); } } } } } catch (Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException) { System.Diagnostics.Debug.WriteLine("4 Error RunSvc(): Hub connection failure"); } catch (Microsoft.Azure.Devices.Common.Exceptions.DeviceNotFoundException) { System.Diagnostics.Debug.WriteLine("4 Error RunSvc(): Device not found"); } catch (TaskCanceledException) { System.Diagnostics.Debug.WriteLine("4 Error RunSvc(): Task canceled"); } catch (OperationCanceledException) { System.Diagnostics.Debug.WriteLine("4 Error RunSvc(): Operation canceled"); } catch (Exception ex) { if (!ex.Message.Contains("Timeout")) System.Diagnostics.Debug.WriteLine("4 Error RunSvc(): " + ex.Message); else { System.Diagnostics.Debug.WriteLine("4 Error RunSvc(): Timeout"); } } } }
/// <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); }