private bool ConnectRegistryManager(out RegistryManager registryManager, IotHubType type) { string connectionString; if (type == IotHubType.Master) { connectionString = _connectionStringMaster; } else { connectionString = _connectionStringSlave; } try { registryManager = RegistryManager.CreateFromConnectionString(connectionString); _logger.LogDebug($"Connected to {type} IoT Hub."); } catch (Exception ex) { _logger.LogError(ex, $"Can not connect to {type} IoT Hub."); registryManager = null; return(false); } return(true); }
private bool ValidateConnectionString(string connectionString, IotHubType type) { // Validate Master connection string if (string.IsNullOrEmpty(connectionString)) { _logger.LogError($"{type} IoT Hub connection string not found in configuration."); return(false); } else { // Just show Master IoT Hub Hostname if (TryGetHostFromConnectionString(connectionString, out string hostName)) { _logger.LogDebug($"Using {type} IoT Hub: {hostName}"); } else { _logger.LogError($"Invalid {type} IoT Hub connection string in configuration. Can not find \"HostName=\"."); return(false); } } return(true); }
private async Task <bool> AddDeviceToIotHub(bool isSuccess, RegistryManager registryManager, DeviceInfo deviceInfo, IotHubType type) { var deviceInfoSlave = new DeviceInfo(); // Prepare Scope if (!string.IsNullOrEmpty(deviceInfo.Device.Scope)) { string edgeDeviceIdSlave = GetDeviceIdFromScope(deviceInfo.Device.Scope); if (string.IsNullOrEmpty(edgeDeviceIdSlave)) { _logger.LogError($"{deviceInfo.Device.Id} has invalid Edge device scope: {deviceInfo.Device.Scope}."); } else if (deviceInfo.Device.Id != edgeDeviceIdSlave) { Device edgeDeviceInfoSlave = null; try { edgeDeviceInfoSlave = await registryManager.GetDeviceAsync(edgeDeviceIdSlave); await PreventIotHubThrottling(); } catch (Exception ex) { _logger.LogError(ex, $"{edgeDeviceIdSlave}: Failed loading device from {type} IoT Hub."); } if (edgeDeviceInfoSlave != null) { deviceInfo.Device.Scope = edgeDeviceInfoSlave.Scope; } else { _logger.LogWarning($"{deviceInfo.Device.Id}: Device has Edge device parent of {edgeDeviceIdSlave} but that device has not been created in Slave IoT Hub yet. It will be added at next run."); } } } // Add device to IoT Hub try { deviceInfoSlave.Device = await registryManager.AddDeviceAsync(deviceInfo.Device); await PreventIotHubThrottling(); } catch (Exception ex) { _logger.LogError(ex, $"{deviceInfo.Device.Id}: Can not add device to {type} IoT hub."); isSuccess = false; } // Get Device Twin from IoT Hub try { deviceInfoSlave.Twin = await registryManager.GetTwinAsync(deviceInfo.Device.Id); await PreventIotHubThrottling(); } catch (Exception ex) { _logger.LogError(ex, $"{deviceInfo.Device.Id}: Can not read device Twin from {type} IoT Hub."); isSuccess = false; } // Update Device Twin in IoT Hub try { deviceInfoSlave.Twin = await registryManager.UpdateTwinAsync(deviceInfo.Device.Id, deviceInfo.Twin, deviceInfoSlave.Twin.ETag); await PreventIotHubThrottling(); } catch (Exception ex) { _logger.LogError(ex, $"{deviceInfo.Device.Id}: Can not update device Twin in {type} IoT Hub."); isSuccess = false; } return(isSuccess); }
private async Task <List <DeviceInfo> > GetDeviceListFromIotHub(RegistryManager registryManager, string deviceId, IotHubType type) { var deviceInfoList = new List <DeviceInfo>(); var queryString = "SELECT * FROM devices"; if (!string.IsNullOrEmpty(deviceId)) { queryString += $" WHERE deviceId = '{deviceId}'"; } try { var query = registryManager.CreateQuery(queryString); while (query.HasMoreResults) { var twinList = await query.GetNextAsTwinAsync(); foreach (var twin in twinList) { var device = await registryManager.GetDeviceAsync(twin.DeviceId); device.ETag = null; deviceInfoList.Add(new DeviceInfo(device, twin, false)); await PreventIotHubThrottling(); } } } catch (Exception ex) { _logger.LogError(ex, $"Failed loading devices from {type} IoT Hub."); deviceInfoList = new List <DeviceInfo>(); if (ExceptionHandler.IsFatal(ex)) { throw; } } return(deviceInfoList); }