/// <summary> /// write the keys from the deployed template into some files. /// function.json : useful for indicating the triggering of the function app. Writes the key to the event hub linked to the iot hub /// config.yaml : write the connection string of the iot hub, for connecting freshly created devices /// </summary> /// <param name="outputs">the outputs from the deployment template</param> private static void WriteKeysIntoFiles(object outputs) { dynamic jsonOutputs = JObject.Parse(outputs.ToString()); // Write the Event Hub key into the function.json of the azure function app dynamic jsonFunction = JObject.Parse(File.ReadAllText("..\\..\\FunctionAppCore\\IoTHubTrigger\\function.json")); dynamic bindings = jsonFunction["bindings"]; //bindings[0]["connection"] = outputs[""]; //bindings[0]["connection"] = jsonOutputs["eventHubEndPoint"]["value"] + ";EntityPath=dedieumaiothubcreation"; jsonFunction["bindings"] = bindings; string jsonUpdated = JsonConvert.SerializeObject(jsonFunction, Formatting.Indented); File.WriteAllText(@"..\..\FunctionAppCore\IoTHubTrigger\function.json", jsonUpdated); // Write the iot hub connection string into the config.yaml, used for device creation const string configFilePath = @"../../config.yaml"; iotHubConnectionString = jsonOutputs["hubKeys"]["value"]; IoTHubExamples.Core.Configuration config = configFilePath.GetIoTConfiguration(); config.AzureIoTHubConfig.ConnectionString = iotHubConnectionString; string[] connectionStrSplitted = iotHubConnectionString.Split(new char[] { ';', '=' }); config.AzureIoTHubConfig.IoTHubUri = connectionStrSplitted[1]; if (configFilePath.UpdateIoTConfiguration(config).Item1) { Console.WriteLine($"Saved iot hub connection string {jsonOutputs["hubKeys"]["value"]}"); } }
/// <summary> /// Create numberOfDevices devices linked to the iot hub /// </summary> /// <param name="numberOfDevices">the number of devices to create</param> public static void CreateDevice(int numberOfDevices) { const string configFilePath = @"../../config.yaml"; IoTHubExamples.Core.Configuration config = configFilePath.GetIoTConfiguration(); deviceConfigs = config.DeviceConfigs; AzureIoTHubConfig azureConfig = config.AzureIoTHubConfig; _registryManager = RegistryManager.CreateFromConnectionString(azureConfig.ConnectionString); deviceConfigs = config.DeviceConfigs = new System.Collections.Generic.List <DeviceConfig>(); for (int deviceNumber = 0; deviceNumber < numberOfDevices; deviceNumber++) { var testDevice = new DeviceConfig() { DeviceId = $"{"test"}{deviceNumber:0000}", Nickname = $"{"test"}{deviceNumber:0000}", Status = "Enabled" }; deviceConfigs.Add(testDevice); Task <string> task = AddDeviceAsync(testDevice); task.Wait(); testDevice.Key = task.Result; } if (configFilePath.UpdateIoTConfiguration(config).Item1) { foreach (var testDevice in deviceConfigs) { Console.WriteLine( $"DeviceId: {testDevice.DeviceId} has DeviceKey: {testDevice.Key} \r\nConfig file: {configFilePath} has been updated accordingly."); } } }
/// <summary> /// Send a Message to each of the devices created into config.yaml /// </summary> public static void SendDataToDevices() { const string configFilePath = @"../../config.yaml"; IoTHubExamples.Core.Configuration config = configFilePath.GetIoTConfiguration(); List <DeviceConfig> testDevices = config.DeviceConfigs; foreach (DeviceConfig device in testDevices) { string connectionDevice = "HostName=" + config.AzureIoTHubConfig.IoTHubUri + ";DeviceId=" + device.DeviceId + ";SharedAccessKey=" + device.Key; Random rand = new Random(); double temperature = 20 + rand.NextDouble() * 15; double humidity = 60 + rand.NextDouble() * 20; var telemetryDataPoint = new { temperature, humidity, date = DateTime.Now }; string messageString = JsonConvert.SerializeObject(telemetryDataPoint); Microsoft.Azure.Devices.Client.Message message = new Microsoft.Azure.Devices.Client.Message(Encoding.ASCII.GetBytes(messageString)); message.Properties.Add("temperatureAlert", (temperature) > 30 ? "true" : "false"); DeviceClient.CreateFromConnectionString(connectionDevice, Microsoft.Azure.Devices.Client.TransportType.Mqtt).SendEventAsync(message); Console.WriteLine("Sending Message : {0}", messageString); } }