/// <summary> /// Factory /// </summary> /// <param name="product"></param> /// <param name="cs"></param> /// <param name="deviceId"></param> /// <param name="moduleId"></param> /// <param name="transportSetting"></param> /// <param name="timeout"></param> /// <param name="retry"></param> /// <param name="onConnectionLost"></param> /// <param name="logger"></param> /// <returns></returns> public static async Task <IClient> CreateAsync(string product, IotHubConnectionStringBuilder cs, string deviceId, string moduleId, ITransportSettings transportSetting, TimeSpan timeout, IRetryPolicy retry, Action onConnectionLost, ILogger logger) { if (cs == null) { logger.Information("Running in iotedge context."); } else { logger.Information("Running outside iotedge context."); } var client = await CreateAsync(cs, transportSetting); var adapter = new ModuleClientAdapter(client); // Configure client.OperationTimeoutInMilliseconds = (uint)timeout.TotalMilliseconds; client.SetConnectionStatusChangesHandler((s, r) => adapter.OnConnectionStatusChange(deviceId, moduleId, onConnectionLost, logger, s, r)); if (retry != null) { client.SetRetryPolicy(retry); } client.ProductInfo = product; await client.OpenAsync(); return(adapter); }
/// <summary> /// Factory /// </summary> /// <param name="product"></param> /// <param name="cs"></param> /// <param name="transportSetting"></param> /// <param name="timeout"></param> /// <param name="retry"></param> /// <param name="onConnectionLost"></param> /// <param name="logger"></param> /// <returns></returns> public static async Task <IClient> CreateAsync(string product, IotHubConnectionStringBuilder cs, ITransportSettings transportSetting, TimeSpan timeout, IRetryPolicy retry, Action onConnectionLost, ILogger logger) { var client = await CreateAsync(cs, transportSetting); var adapter = new ModuleClientAdapter(client); // Configure client.OperationTimeoutInMilliseconds = (uint)timeout.TotalMilliseconds; client.SetConnectionStatusChangesHandler((s, r) => { logger.Info($"Module {cs.DeviceId}_{cs.ModuleId} connection status " + $"changed to {s} due to {r}."); if (r == ConnectionStatusChangeReason.Client_Close && !adapter.IsClosed) { adapter.IsClosed = true; onConnectionLost?.Invoke(); } }); if (retry != null) { client.SetRetryPolicy(retry); } client.DiagnosticSamplingPercentage = 5; client.ProductInfo = product; await client.OpenAsync(); return(adapter); }
/// <summary> /// Create client adapter /// </summary> /// <param name="product"></param> /// <param name="onError"></param> /// <param name="transportSetting"></param> /// <returns></returns> private Task <IClient> CreateAdapterAsync(string product, Action onError, ITransportSettings transportSetting = null) { if (_cs != null && string.IsNullOrEmpty(_cs.ModuleId)) { return(DeviceClientAdapter.CreateAsync(product, _cs, transportSetting, _timeout, RetryPolicy, onError, _logger)); } return(ModuleClientAdapter.CreateAsync(product, _cs, transportSetting, _timeout, RetryPolicy, onError, _logger)); }
/// <summary> /// Create client adapter /// </summary> /// <param name="product"></param> /// <param name="onError"></param> /// <param name="transportSetting"></param> /// <returns></returns> private Task<IClient> CreateAdapterAsync(string product, Action onError, ITransportSettings transportSetting = null) { if (string.IsNullOrEmpty(ModuleId)) { if (_cs == null) { throw new InvalidConfigurationException( "No connection string for device client specified."); } return DeviceClientAdapter.CreateAsync(product, _cs, DeviceId, transportSetting, _timeout, RetryPolicy, onError, _logger); } return ModuleClientAdapter.CreateAsync(product, _cs, DeviceId, ModuleId, transportSetting, _timeout, RetryPolicy, onError, _logger); }