// Async method to send simulated telemetry internal async Task SendDeviceToCloudMessagesAsync(ModuleClient moduleClient, string deviceId, string moduleId, ILogger logger) { // Initial telemetry values int counter = 1; string logPrefix = "data".BuildLogPrefix(); string messageString = string.Empty; Message message = null; using (logger.BeginScope($"{logPrefix}::{ModuleSettings.ArtifactId}::MEASURED DATA")) { var defaultModel = ModuleSettings.SupportedModels.SingleOrDefault(i => i.ModelId == ModuleSettings.DefaultModelId); if (defaultModel == null) { throw new Exception("No supported model corresponds to the default model Id."); } while (true) { //Randomize data messageString = await _dtdlMessagingService.GetRandomizedMessageAsync(deviceId, moduleId, ModuleSettings.DefaultModelId, defaultModel.ModelPath); message = new Message(Encoding.UTF8.GetBytes(messageString)); message.Properties.Add("messageType", "dtdlMessage"); // Add a custom application property to the message. // An IoT hub can filter on these properties without access to the message body. //message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false"); message.ContentType = "application/json"; message.ContentEncoding = "utf-8"; // Send the tlemetry message await moduleClient.SendEventAsync(message); counter++; logger.LogDebug($"{logPrefix}::{ModuleSettings.ArtifactId}::Sent message: {messageString}."); logger.LogDebug($"{logPrefix}::{ModuleSettings.ArtifactId}::COUNTER: {counter}."); if (_stopProcessing) { logger.LogDebug($"{logPrefix}::{ModuleSettings.ArtifactId}::STOP PROCESSING."); break; } await Task.Delay(_telemetryInterval * 1000); } } }
// Async method to send simulated telemetry internal async Task SendDeviceToCloudMessagesAsync(string deviceId) { int counter = 0; string logPrefix = "data".BuildLogPrefix(); string messageString = string.Empty; Message message = null; using (_logger.BeginScope($"{logPrefix}::{DateTime.Now}::{_deviceSettings.ArtifactId}::MEASURED DATA")) { while (true) { //Randomize data messageString = await _dtdlMessageService.GetRandomizedMessageAsync(deviceId, string.Empty, _deviceSettings.DefaultModelId, _defaultModel.ModelPath); message = new Message(Encoding.UTF8.GetBytes(messageString)); message.Properties.Add("messageType", "dtdlMessage"); // Add a custom application property to the message. // An IoT hub can filter on these properties without access to the message body. //message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false"); message.ContentType = "application/json"; message.ContentEncoding = "utf-8"; // Send the tlemetry message await _deviceClient.SendEventAsync(message); counter++; _logger.LogDebug($"{logPrefix}::{_deviceSettings.ArtifactId}::Sent message: {messageString}."); _logger.LogDebug($"{logPrefix}::{_deviceSettings.ArtifactId}::COUNTER: {counter}."); if (_stopProcessing) { _logger.LogDebug($"{logPrefix}::STOP PROCESSING."); break; } await Task.Delay(_telemetryInterval * 1000); } } }