/// <inheritdoc/> public Task <IClient> CreateAsync(string product, IProcessControl ctrl) { var client = new IoTHubClient(ctrl); var connection = _hub.Connect(DeviceId, ModuleId, client); client.Connection = connection ?? throw new CommunicationException("Failed to connect to fake hub"); return(Task.FromResult <IClient>(client)); }
static async Task Main() { Console.WriteLine("Hello World!"); IoTHubClient iotHubClient = new IoTHubClient(); IoTHubClientAuto iotHubClientAuto = new IoTHubClientAuto(); await iotHubClientAuto.InitIoTHubClientAsync(); await iotHubClient.InitIoTHubClientAsync(); await iotHubClient.RunSampleAsync(TimeSpan.FromMinutes(5)); }
public static async Task Run([MqttTrigger("minicursoiot/+/#", ConnectionString = "MqttConnectionString")] IMqttMessage data, ILogger log, ExecutionContext context) { log.LogInformation($"Payload {Convert.ToBase64String(data.GetMessage())}"); var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); if (IoTHub == null) { IoTHub = new IoTHubClient(config["IoTHubConnectionString"]); } await IoTHub.SendRaw(data.Topic, data.GetMessage()); }
private static async Task SendDeviceToCloudMessagesAsync(string deviceId, string targetSite, string messageContent = null) { List <object> events = new List <object>(); if (messageContent == null || messageContent.Length == 0) { for (int i = 0; i < 10; ++i) { var body = new { Timestamp = DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(i)) }; events.Add(body); } } NameValueCollection keyFields = new NameValueCollection(); keyFields.Add(Names.EventKeyFieldDeviceId, deviceId); keyFields.Add(Names.EventKeyFieldTargetSite, targetSite); await IoTHubClient.SendMessageToIoTHubAsync(connectionString, devices, keyFields, events, messageContent); }
private static async Task SendEventsFromCSVFile(string deviceId, string targetSite, string fileDataPath, string fieldDefinitionsPath) { try { EventsContainer events = null; if (fieldDefinitionsPath.Length > 0) { events = new EventsContainer(fieldDefinitionsPath); } else { events = new EventsContainer(fileDataPath); } if (events.EventsFlag) { NameValueCollection keyFields = new NameValueCollection(); if (deviceId.Length != 0) { keyFields.Add(Names.EventKeyFieldDeviceId, deviceId); keyFields.Add(Names.EventKeyFieldTargetSite, targetSite); } bool continueProcess = events.ReplayFlag; if (continueProcess) { Console.WriteLine("Press Any Key to Halt Replay Process"); } int iterationsCount = 0; do { List <Task> tasks = new List <Task>(devices.Count()); foreach (string[] messageValues in events.GetValuesList()) { List <object> dataEvents = new List <object>(); dataEvents.Add(events.GetEventMessageForValues(messageValues)); if (deviceId.Length == 0) { keyFields = events.GetKeyFields(messageValues); } int waitPeriod = 0; if (int.TryParse(messageValues[0], out waitPeriod)) { Thread.Sleep(waitPeriod); } tasks.Add(IoTHubClient.SendMessageToIoTHubAsync(connectionString, devices, keyFields, dataEvents)); } await Task.WhenAll(tasks); await Task.Delay(TimeSpan.FromSeconds(1)); if (events.ReplayFlag) { Console.WriteLine(" Finished Iteration {0}", ++iterationsCount); } if (Console.KeyAvailable) { continueProcess = false; } } while (continueProcess); } } catch (Exception ex) { Console.WriteLine("Send failed. {0}", ex.Message); } }