protected async Task GetOrCreateDeviceIdentity() { IotHubConnectionStringBuilder builder = IotHubConnectionStringBuilder.Create(this.iothubConnectionString); RegistryManager rm = RegistryManager.CreateFromConnectionString(builder.ToString()); Device device = await rm.GetDeviceAsync(this.deviceId); if (device != null) { Console.WriteLine($"Device '{device.Id}' already registered on IoT hub '{builder.HostName}'"); this.context = new DeviceContext { Device = device, IotHubConnectionString = this.iothubConnectionString, RegistryManager = rm, RemoveDevice = false, MessageGuid = Guid.NewGuid().ToString() }; } else { await this.CreateDeviceIdentity(rm); } }
async Task CreateDeviceIdentity(RegistryManager rm) { var device = new Device(this.deviceId) { Authentication = new AuthenticationMechanism() { Type = AuthenticationType.Sas }, Capabilities = new DeviceCapabilities() { IotEdge = false } }; IotHubConnectionStringBuilder builder = IotHubConnectionStringBuilder.Create(this.iothubConnectionString); Console.WriteLine($"Registering device '{device.Id}' on IoT hub '{builder.HostName}'"); device = await rm.AddDeviceAsync(device); this.context = new DeviceContext { Device = device, DeviceClientInstance = Option.None <DeviceClient>(), IotHubConnectionString = this.iothubConnectionString, RegistryManager = rm, RemoveDevice = true, MessageGuid = Guid.NewGuid().ToString() }; }
// Set IoTHub connection strings, using either the user provided value or the configuration public void SetCurrentIotHub() { try { // Retrieve connection string from file/storage this.connString = this.connectionStringManager.GetIotHubConnectionString(); // Parse connection string, this triggers an exception if the string is invalid IotHubConnectionStringBuilder connStringBuilder = IotHubConnectionStringBuilder.Create(this.connString); // Prepare registry class used to create/retrieve devices this.registry = this.registry.CreateFromConnectionString(this.connString); this.log.Debug("Device registry object ready", () => new { this.ioTHubHostName }); // Prepare hostname used to build device connection strings this.ioTHubHostName = connStringBuilder.HostName; this.log.Info("Selected active IoT Hub for devices", () => new { this.ioTHubHostName }); // Prepare the auth key used for all the devices this.fixedDeviceKey = connStringBuilder.SharedAccessKey; this.log.Debug("Device authentication key defined", () => new { this.ioTHubHostName }); this.setupDone = true; } catch (Exception e) { this.log.Error("IoT Hub connection setup failed", e); throw; } }
// Set IoTHub connection strings, using either the user provided value or the configuration, // initialize the IoT Hub registry, and perform other initializations. // TODO: use the simulation object to decide which conn string to use public async Task InitAsync() { this.instance.InitOnce(); try { // Retrieve connection string from file/storage this.connString = await this.connectionStringManager.GetConnectionStringAsync(); // Parse connection string, this triggers an exception if the string is invalid IotHubConnectionStringBuilder connStringBuilder = IotHubConnectionStringBuilder.Create(this.connString); // Prepare registry class used to create/retrieve devices this.registry.Init(this.connString); this.log.Debug("Device registry object ready", () => new { this.ioTHubHostName }); // Prepare hostname used to build device connection strings this.ioTHubHostName = connStringBuilder.HostName; this.log.Info("Selected active IoT Hub for devices", () => new { this.ioTHubHostName }); // Prepare the auth key used for all the devices this.fixedDeviceKey = connStringBuilder.SharedAccessKey; this.log.Debug("Device authentication key defined", () => new { this.ioTHubHostName }); this.instance.InitComplete(); } catch (Exception e) { var msg = "IoT Hub connection setup failed"; this.log.Error(msg, e); this.diagnosticsLogger.LogServiceError(msg, e.Message); throw; } }
protected async Task ConnectToEdgeAndSendDataAsync() { var builder = IotHubConnectionStringBuilder.Create(this.iothubConnectionString); DeviceClient deviceClient; if (this.authType == AuthenticationType.Sas) { string leafDeviceConnectionString = $"HostName={builder.HostName};DeviceId={this.deviceId};SharedAccessKey={this.context.Device.Authentication.SymmetricKey.PrimaryKey};GatewayHostName={this.edgeHostName}"; deviceClient = DeviceClient.CreateFromConnectionString(leafDeviceConnectionString, this.deviceTransportSettings); } else { var auth = new DeviceAuthenticationWithX509Certificate(this.deviceId, this.clientCertificate.Expect(() => new InvalidOperationException("Missing client certificate"))); deviceClient = DeviceClient.Create(builder.HostName, this.edgeHostName, auth, this.deviceTransportSettings); } this.context.DeviceClientInstance = Option.Some(deviceClient); Console.WriteLine("Leaf Device client created."); var message = new Message(Encoding.ASCII.GetBytes($"Message from Leaf Device. Msg GUID: {this.context.MessageGuid}")); Console.WriteLine($"Trying to send the message to '{this.edgeHostName}'"); await deviceClient.SendEventAsync(message); Console.WriteLine("Message Sent."); await deviceClient.SetMethodHandlerAsync("DirectMethod", DirectMethod, null); Console.WriteLine("Direct method callback is set."); }
private ValueTask <DeviceClient> BuildDeviceClientAsync(DeviceServiceSettings deviceServiceSettings) { if (cachedDeviceClient != null) { return(new ValueTask <DeviceClient>(cachedDeviceClient)); } return(new ValueTask <DeviceClient>(CreateAndCacheDeviceClient())); async Task <DeviceClient> CreateAndCacheDeviceClient() { var registryManager = RegistryManager.CreateFromConnectionString(deviceServiceSettings.IoTHubConnectionString); var device = await registryManager.GetDeviceAsync(deviceServiceSettings.DeviceName); if (device == null) { var message = $"Device {deviceServiceSettings.DeviceName} is not registered!"; DeviceSimulatorActorEventSource.Current.ExceptionOccured(message); throw new InvalidOperationException(message); } var deviceKeyInfo = new DeviceAuthenticationWithRegistrySymmetricKey(deviceServiceSettings.DeviceName, device.Authentication.SymmetricKey.PrimaryKey); var iotHubConnectionStringBuilder = IotHubConnectionStringBuilder.Create(deviceServiceSettings.IoTHubConnectionString); cachedDeviceClient = DeviceClient.Create( iotHubConnectionStringBuilder.HostName, deviceKeyInfo, DeviceClientTransportSettings); return(cachedDeviceClient); } }
/// <summary> /// Get IoTHub connection string from either the user provided value or the configuration /// </summary> public void SetCurrentIotHub() { string connString = this.connectionStringManager.GetIotHubConnectionString(); this.registry = this.registry.CreateFromConnectionString(connString); this.ioTHubHostName = IotHubConnectionStringBuilder.Create(connString).HostName; this.log.Info("Selected active IoT Hub for devices", () => new { this.ioTHubHostName }); }
private static async Task <string> BuildConnectionStringAsync(RegistryManager registryManager, DeviceItem deviceItem, TraceWriter log) { var iotHubConnectionString = IotHubConnectionStringBuilder.Create(Environment.GetEnvironmentVariable("IoTHubConnectionString")); var device = await registryManager.GetDeviceAsync(deviceItem.Name); var deviceKeyInfo = new DeviceAuthenticationWithRegistrySymmetricKey(deviceItem.Name, device.Authentication.SymmetricKey.PrimaryKey); var deviceConnectionString = ClientIotHubConnectionStringBuilder.Create(iotHubConnectionString.HostName, deviceKeyInfo); return(deviceConnectionString.ToString()); }
protected async Task ConnectToEdgeAndSendDataAsync() { using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(600))) // Long timeout is needed because registry manager takes a while for the device identity to be usable { Exception savedException = null; try { var builder = IotHubConnectionStringBuilder.Create(this.iothubConnectionString); DeviceClient deviceClient; if (this.authType == AuthenticationType.Sas) { string leafDeviceConnectionString = $"HostName={builder.HostName};DeviceId={this.deviceId};SharedAccessKey={this.context.Device.Authentication.SymmetricKey.PrimaryKey};GatewayHostName={this.edgeHostName}"; deviceClient = DeviceClient.CreateFromConnectionString(leafDeviceConnectionString, this.deviceTransportSettings); } else { var auth = new DeviceAuthenticationWithX509Certificate(this.deviceId, this.clientCertificate.Expect(() => new InvalidOperationException("Missing client certificate"))); deviceClient = DeviceClient.Create(builder.HostName, this.edgeHostName, auth, this.deviceTransportSettings); } this.context.DeviceClientInstance = Option.Some(deviceClient); Console.WriteLine("Leaf Device client created."); var message = new Message(Encoding.ASCII.GetBytes($"Message from Leaf Device. Msg GUID: {this.context.MessageGuid}")); Console.WriteLine($"Trying to send the message to '{this.edgeHostName}'"); while (!cts.IsCancellationRequested) // Retries are needed as the DeviceClient timeouts are not long enough { try { await deviceClient.SendEventAsync(message); Console.WriteLine("Message Sent."); await deviceClient.SetMethodHandlerAsync("DirectMethod", DirectMethod, null); Console.WriteLine("Direct method callback is set."); break; } catch (Exception e) { savedException = e; } } } catch (OperationCanceledException e) { throw new InvalidOperationException("Failed to connect to edge and send data", savedException ?? e); } catch (Exception e) { throw new InvalidOperationException("Failed to connect to edge and send data", e); } } }
private void BuildHub(string deviceSasKey) { var currentLocation = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location); if (_inContainer) { currentLocation = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); } Environment.SetEnvironmentVariable(HubService.Constants.ConfigKey.EdgeHubDevServerCertificateFile, Path.Combine(currentLocation, @"Certificates/edge-hub-dev/edge-hub-server.ca.pem")); Environment.SetEnvironmentVariable(HubService.Constants.ConfigKey.EdgeHubDevServerPrivateKeyFile, Path.Combine(currentLocation, @"Certificates/edge-hub-dev/edge-hub-server.pem")); Environment.SetEnvironmentVariable(HubService.Constants.ConfigKey.EdgeHubDevTrustBundleFile, Path.Combine(currentLocation, @"Certificates/edge-hub-dev/edge-hub-server.ca.pem")); Environment.SetEnvironmentVariable("AuthenticationMode", "Cloud"); var storageFolder = Path.Combine(currentLocation, @"Storage"); var hubStorageFolder = Path.Combine(storageFolder, HubService.Constants.EdgeHubStorageFolder); if (!Directory.Exists(hubStorageFolder)) { Directory.CreateDirectory(hubStorageFolder); } Environment.SetEnvironmentVariable("storageFolder", storageFolder); var csBuilder = IotHubConnectionStringBuilder.Create(_iotHubConnectionString); var edgeConnectionString = new ModuleConnectionStringBuilder(csBuilder.HostName, _deviceId) .Create(Devices.Edge.Agent.Core.Constants.EdgeHubModuleIdentityName) .WithSharedAccessKey(deviceSasKey) .Build(); Environment.SetEnvironmentVariable(Devices.Edge.Agent.Core.Constants.EdgeHubConnectionStringKey, edgeConnectionString); Environment.SetEnvironmentVariable(Devices.Edge.Agent.Core.Constants.IotHubConnectionStringKey, edgeConnectionString); var edgeHubConfiguration = new ConfigurationBuilder() .AddJsonFile("appSettings.json", true) .AddEnvironmentVariables() .Build(); _hub._Init(edgeHubConfiguration, _container); }
// Set IoTHub connection strings, using either the user provided value or the configuration, // initialize the IoT Hub registry, and perform other initializations. // TODO: use the simulation object to decide which conn string to use public async Task InitAsync() { // Until SimulationContext is merged, this class is a singleton and we want // to avoid multiple initializations to run. // Later, with SimulationContext, the instance will be cached and this code won't be needed. // InitOnce() is temporarily disabled until the SimulationContext is merged in. // Note: when removing this workaround, Autofac config should be changed to not // be a singleton (see DependencyResolution.cs). bool TODO_FIX_ME = true; if (TODO_FIX_ME) { if (this.instance.IsInitialized) { return; } } else { this.instance.InitOnce(); } try { // Retrieve connection string from file/storage this.connString = await this.connectionStringManager.GetConnectionStringAsync(); // Parse connection string, this triggers an exception if the string is invalid IotHubConnectionStringBuilder connStringBuilder = IotHubConnectionStringBuilder.Create(this.connString); // Prepare registry class used to create/retrieve devices this.registry.Init(this.connString); this.log.Debug("Device registry object ready", () => new { this.ioTHubHostName }); // Prepare hostname used to build device connection strings this.ioTHubHostName = connStringBuilder.HostName; this.log.Info("Selected active IoT Hub for devices", () => new { this.ioTHubHostName }); // Prepare the auth key used for all the devices this.fixedDeviceKey = connStringBuilder.SharedAccessKey; this.log.Debug("Device authentication key defined", () => new { this.ioTHubHostName }); this.instance.InitComplete(); } catch (Exception e) { var msg = "IoT Hub connection setup failed"; this.log.Error(msg, e); this.diagnosticsLogger.LogServiceError(msg, e.Message); throw; } }
private async Task <string> ProvisionDeviceAsync(string manifest) { IotHubConnectionStringBuilder.Create(_iotHubConnectionString); var registryManager = RegistryManager.CreateFromConnectionString(_iotHubConnectionString); var device = await registryManager.GetDeviceAsync(_deviceId) ?? await registryManager.AddDeviceAsync( new Device(_deviceId) { Capabilities = new DeviceCapabilities() { IotEdge = true } }); var sasKey = device.Authentication.SymmetricKey.PrimaryKey; try { var oldModules = await registryManager.GetModulesOnDeviceAsync(_deviceId); foreach (var oldModule in oldModules) { if (!oldModule.Id.StartsWith("$")) { await registryManager.RemoveModuleAsync(oldModule); } } } catch { // ignored } foreach (var module in _modules) { try { await registryManager.AddModuleAsync(new Module(_deviceId, module.Name)); } catch (ModuleAlreadyExistsException) { } } await registryManager.ApplyConfigurationContentOnDeviceAsync(_deviceId, manifest.FromJson <ConfigurationContent>()); return(sasKey); }
private async Task <string> GetModuleConnectionStringAsync(string iotHubConnectionString, string deviceId, string moduleName) { var csBuilder = IotHubConnectionStringBuilder.Create(iotHubConnectionString); var registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString); var module = await registryManager.GetModuleAsync(deviceId, moduleName); var sasKey = module.Authentication.SymmetricKey.PrimaryKey; return(new ModuleConnectionStringBuilder(csBuilder.HostName, deviceId) .Create(moduleName) .WithGatewayHostName(Environment.MachineName) .WithSharedAccessKey(sasKey) .Build()); }
protected async Task GetOrCreateDeviceIdentityAsync() { var settings = new HttpTransportSettings(); this.proxy.ForEach(p => settings.Proxy = p); IotHubConnectionStringBuilder builder = IotHubConnectionStringBuilder.Create(this.iothubConnectionString); RegistryManager rm = RegistryManager.CreateFromConnectionString(builder.ToString(), settings); Option <string> edgeScope = await this.edgeDeviceId .Map(id => GetScopeIfExitsAsync(rm, id)) .GetOrElse(() => Task.FromResult <Option <string> >(Option.None <string>())); Device device = await rm.GetDeviceAsync(this.deviceId); if (device != null) { Console.WriteLine($"Device '{device.Id}' already registered on IoT hub '{builder.HostName}'"); if (this.authType == AuthenticationType.SelfSigned) { var thumbprints = this.thumbprints.Expect(() => new InvalidOperationException("Missing thumbprints list")); if (!thumbprints.Contains(device.Authentication.X509Thumbprint.PrimaryThumbprint) || !thumbprints.Contains(device.Authentication.X509Thumbprint.SecondaryThumbprint)) { // update the thumbprints before attempting to run any tests to ensure consistency device.Authentication.X509Thumbprint = new X509Thumbprint { PrimaryThumbprint = thumbprints[0], SecondaryThumbprint = thumbprints[1] }; } } edgeScope.ForEach(s => device.Scope = s); await rm.UpdateDeviceAsync(device); this.context = new DeviceContext { Device = device, IotHubConnectionString = this.iothubConnectionString, RegistryManager = rm, RemoveDevice = false, MessageGuid = Guid.NewGuid().ToString() }; } else { await this.CreateDeviceIdentityAsync(rm, edgeScope); } }
protected async Task ConnectToEdgeAndSendData() { IotHubConnectionStringBuilder builder = IotHubConnectionStringBuilder.Create(this.iothubConnectionString); string leafDeviceConnectionString = $"HostName={builder.HostName};DeviceId={this.deviceId};SharedAccessKey={this.context.Device.Authentication.SymmetricKey.PrimaryKey};GatewayHostName={this.edgeHostName}"; DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(leafDeviceConnectionString, this.deviceTransportSettings); this.context.DeviceClientInstance = Option.Some(deviceClient); Console.WriteLine("Leaf Device client created."); var message = new Message(Encoding.ASCII.GetBytes($"Message from Leaf Device. MsgGUID: {this.context.MessageGuid}")); Console.WriteLine($"Trying to send the message to '{this.edgeHostName}'"); await deviceClient.SendEventAsync(message); Console.WriteLine("Message Sent."); await deviceClient.SetMethodHandlerAsync("DirectMethod", DirectMethod, null).ConfigureAwait(false); Console.WriteLine("Direct method callback is set."); }
protected async Task GetOrCreateDeviceIdentityAsync() { IotHubConnectionStringBuilder builder = IotHubConnectionStringBuilder.Create(this.iothubConnectionString); RegistryManager rm = RegistryManager.CreateFromConnectionString(builder.ToString()); Device device = await rm.GetDeviceAsync(this.deviceId); if (device != null) { Console.WriteLine($"Device '{device.Id}' already registered on IoT hub '{builder.HostName}'"); if (this.authType == AuthenticationType.SelfSigned) { var thumbprints = this.thumbprints.Expect(() => new InvalidOperationException("Missing thumprints list")); if (!thumbprints.Contains(device.Authentication.X509Thumbprint.PrimaryThumbprint) || !thumbprints.Contains(device.Authentication.X509Thumbprint.SecondaryThumbprint)) { // update the thumbprints before attempting to run any tests to ensure consistency device.Authentication.X509Thumbprint = new X509Thumbprint { PrimaryThumbprint = thumbprints[0], SecondaryThumbprint = thumbprints[1] }; await rm.UpdateDeviceAsync(device); } } this.context = new DeviceContext { Device = device, IotHubConnectionString = this.iothubConnectionString, RegistryManager = rm, RemoveDevice = false, MessageGuid = Guid.NewGuid().ToString() }; } else { await this.CreateDeviceIdentityAsync(rm); } }
private async Task <DeviceClient> GetDeviceClient(string deviceId, CancellationToken cancellationToken) { if (!DeviceClients.ContainsKey(deviceId)) { // Check if device exists _logger.LogInformation($"Initializing device {deviceId}"); var device = await IoTHubManager.GetDeviceAsync(deviceId, cancellationToken); if (device == null) { await IoTHubManager.AddDeviceAsync(new Device(deviceId), cancellationToken); device = await IoTHubManager.GetDeviceAsync(deviceId, cancellationToken); _logger.LogInformation("Device " + deviceId + " created"); } var deviceConnectionString = $"HostName={IotHubConnectionStringBuilder.Create(_iotHubSettings.IoTHubOwnerConnectionString).HostName};DeviceId={deviceId};SharedAccessKey={device.Authentication.SymmetricKey.PrimaryKey}"; DeviceClients.Add(deviceId, DeviceClient.CreateFromConnectionString(deviceConnectionString)); } return(DeviceClients[deviceId]); }
public static IotHubConnectionString Parse(string connectionString) { var builder = IotHubConnectionStringBuilder.Create(connectionString); return(builder.ToIotHubConnectionString()); }
public static async Task RunAsync( [IoTHubTrigger("messages/events", Connection = "IoTHubEventHubConnectionString", ConsumerGroup = "eventtotwinfunction")] EventData message, Microsoft.Azure.WebJobs.ExecutionContext context, CancellationToken token, ILogger log) { var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); if (!message.Properties.ContainsKey("UpdateTwin")) { return; } var connectionString = config["IoTHubConnectionString"]; var deviceId = (string)message.SystemProperties["iothub-connection-device-id"]; // standard system property if (_manager == null) { _manager = RegistryManager.CreateFromConnectionString(connectionString); } var twin = await _manager.GetTwinAsync(deviceId, token); // merge properties twin.Properties.Reported.ClearMetadata(); var reported = JObject.Parse(twin.Properties.Reported.ToJson()); var eventData = JObject.Parse(Encoding.UTF8.GetString(message.Body.ToArray())); eventData.Remove("timeStamp"); // this one always changes and we do not need it in the properties // remove arrays, since they are not supported in reported properties var arrays = eventData.Values().OfType <JArray>().Select(item => item.Path).ToList(); foreach (var arrayPath in arrays) { eventData.Remove(arrayPath); } JObject updatedProps = (JObject)reported.DeepClone(); updatedProps.Merge(eventData); if (!JToken.DeepEquals(updatedProps, reported)) { // need to update try { var device = await _manager.GetDeviceAsync(deviceId, token); var builder = IotHubConnectionStringBuilder.Create(connectionString); var deviceClient = DeviceClient.Create(builder.HostName, new DeviceAuthenticationWithRegistrySymmetricKey(deviceId, device.Authentication.SymmetricKey.PrimaryKey)); await deviceClient.UpdateReportedPropertiesAsync(new TwinCollection(updatedProps.ToString()), token); // call below can only be used for desired properties! So we cannot use it even though it is tempting to do so! //await _manager.UpdateTwinAsync(deviceId, twin, twin.ETag, token); log.LogInformation($"Digital twin for {deviceId} updated from {reported} to {updatedProps}"); } catch (Exception e) { log.LogError($"Error updating reported properties of digital twin of {deviceId} to {updatedProps}: {e}"); throw; } } }
static async Task CallDirectMethodFromCloud( string serviceClientConnectionString, string deviceId, string moduleId, TransportType transportType, TimeSpan delay, CancellationTokenSource cts) { Logger.LogInformation("CallDirectMethodFromCloud started."); ModuleClient moduleClient = null; ServiceClient serviceClient = null; try { Guid batchId = Guid.NewGuid(); int count = 1; IotHubConnectionStringBuilder iotHubConnectionStringBuilder = IotHubConnectionStringBuilder.Create(serviceClientConnectionString); Logger.LogInformation($"Prepare to call Direct Method from cloud ({iotHubConnectionStringBuilder.IotHubName}) on device [{deviceId}] module [{moduleId}]"); serviceClient = ServiceClient.CreateFromConnectionString(serviceClientConnectionString, Microsoft.Azure.Devices.TransportType.Amqp); var cloudToDeviceMethod = new CloudToDeviceMethod("HelloWorldMethod").SetPayloadJson("{ \"Message\": \"Hello\" }"); moduleClient = await ModuleUtil.CreateModuleClientAsync( transportType, ModuleUtil.DefaultTimeoutErrorDetectionStrategy, ModuleUtil.DefaultTransientRetryStrategy, Logger); while (!cts.Token.IsCancellationRequested) { Logger.LogInformation($"Calling Direct Method from cloud ({iotHubConnectionStringBuilder.IotHubName}) on device [{deviceId}] module [{moduleId}] of count {count}."); try { CloudToDeviceMethodResult result = await serviceClient.InvokeDeviceMethodAsync(deviceId, moduleId, cloudToDeviceMethod, CancellationToken.None); if (result.Status == (int)HttpStatusCode.OK) { var eventMessage = new Message(Encoding.UTF8.GetBytes($"Direct Method [{transportType}] Call succeeded.")); eventMessage.Properties.Add("sequenceNumber", count.ToString()); eventMessage.Properties.Add("batchId", batchId.ToString()); Logger.LogInformation($"Calling Direct Method from cloud with count {count} succeeded."); await moduleClient.SendEventAsync(RouteOutputName, eventMessage); } else { Logger.LogError($"Calling Direct Method from cloud with count {count} failed with status code {result.Status}."); } count++; } catch (Exception e) { Logger.LogError($"Exception caught with count {count}: {e}"); } await Task.Delay(delay, cts.Token); } } catch (Exception e) { Logger.LogError($"Exception caught: {e}"); throw; } finally { Logger.LogInformation("Close connection for service client and module client"); if (serviceClient != null) { await serviceClient.CloseAsync(); } if (moduleClient != null) { await moduleClient.CloseAsync(); } } Logger.LogInformation("CallDirectMethodFromCloud finished."); }
static async Task CallDirectMethodFromCloud( string serviceClientConnectionString, string deviceId, string moduleId, TimeSpan delay, AnalyzerClient analyzerClient, CancellationTokenSource cts) { Logger.LogInformation("CallDirectMethodFromCloud started."); ServiceClient serviceClient = null; try { int count = 1; IotHubConnectionStringBuilder iotHubConnectionStringBuilder = IotHubConnectionStringBuilder.Create(serviceClientConnectionString); Logger.LogInformation($"Prepare to call Direct Method from cloud ({iotHubConnectionStringBuilder.IotHubName}) on device [{deviceId}] module [{moduleId}]"); serviceClient = ServiceClient.CreateFromConnectionString(serviceClientConnectionString, Microsoft.Azure.Devices.TransportType.Amqp); var cloudToDeviceMethod = new CloudToDeviceMethod("HelloWorldMethod").SetPayloadJson("{ \"Message\": \"Hello\" }"); while (!cts.Token.IsCancellationRequested) { Logger.LogInformation($"Calling Direct Method from cloud ({iotHubConnectionStringBuilder.IotHubName}) on device [{deviceId}] module [{moduleId}] of count {count}."); try { CloudToDeviceMethodResult result = await serviceClient.InvokeDeviceMethodAsync(deviceId, moduleId, cloudToDeviceMethod, CancellationToken.None); if (result.Status == (int)HttpStatusCode.OK) { Logger.LogDebug($"Calling Direct Method from cloud with count {count} returned with status code {result.Status}."); } else { Logger.LogError($"Calling Direct Method from cloud with count {count} failed with status code {result.Status}."); } await CallAnalyzerToReportStatusAsync(moduleId, result, analyzerClient); count++; } catch (Exception e) { Logger.LogError($"Exception caught with count {count}: {e}"); } await Task.Delay(delay, cts.Token); } } catch (Exception e) { Logger.LogError($"Exception caught: {e}"); throw; } finally { Logger.LogInformation("Close connection for service client and module client"); if (serviceClient != null) { await serviceClient.CloseAsync(); } } Logger.LogInformation("CallDirectMethodFromCloud finished."); }
static async Task Main(string[] args) { var interval = 1; var connectionString = ""; var registryManager = RegistryManager.CreateFromConnectionString(connectionString); var numberOfDevices = Enumerable.Range(1, 50); var devices = new ConcurrentBag <DeviceItem>(); await ForEachAsync(numberOfDevices, 10, async (deviceNumber) => { var deviceName = $"SimulatedFridge-{deviceNumber:0000}"; Console.WriteLine($"Registering device ({deviceName})"); var device = await registryManager.GetDeviceAsync(deviceName); if (device == null) { device = await registryManager.AddDeviceAsync(new Device(deviceName)); } devices.Add(new DeviceItem() { Id = deviceName, }); var twin = new Twin() { Tags = { ["IsSimulated"] = "Y" } }; await registryManager.UpdateTwinAsync(device.Id, twin, "*"); }); await ForEachAsync(numberOfDevices, 10, async (deviceNumber) => { var deviceName = $"SimulatedLightBulbs-{deviceNumber:0000}"; Console.WriteLine($"Registering device ({deviceName})"); var device = await registryManager.GetDeviceAsync(deviceName); if (device == null) { device = await registryManager.AddDeviceAsync(new Device(deviceName)); } devices.Add(new DeviceItem() { Id = deviceName, }); var twin = new Twin() { Tags = { ["IsSimulated"] = "Y" } }; await registryManager.UpdateTwinAsync(device.Id, twin, "*"); }); await ForEachAsync(devices, 100, async (deviceItem) => { var device = await registryManager.GetDeviceAsync(deviceItem.Id); var iotHubConnectionString = IotHubConnectionStringBuilder.Create(connectionString); var deviceKeyInfo = new DeviceAuthenticationWithRegistrySymmetricKey(deviceItem.Id, device.Authentication.SymmetricKey.PrimaryKey); var deviceConnectionStringBuilder = ClientIotHubConnectionStringBuilder.Create(iotHubConnectionString.HostName, deviceKeyInfo); var deviceConnectionString = deviceConnectionStringBuilder.ToString(); var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString); await deviceClient.OpenAsync(); var message = FetchClientMessage(deviceItem); while (true) { await deviceClient.SendEventAsync(message); Console.WriteLine($"Sent data for ({deviceItem.Id})"); await Task.Delay(interval * 1000); message = FetchClientMessage(deviceItem); } }); await Task.Delay(1000); Console.ReadKey(); }