How do I generate an Azure IoT Hub connection string from deviceInfo
, which is a JSON object of device information after I create a new device using the IoT Hub Service NodeJS API.
This is my code snippet below. Inside the callback, where the comment is, I'm trying to get a device connection string to resolve, rather than all the device information.
import iothub from 'azure-iothub';
const myIoTHub = iothub.Registry.fromConnectionString(...);
function createDevice(device) {
return new Promise((resolve, reject) => {
myIoTHub.create(device, function (err, deviceInfo, res) {
if (err) reject(err);
// deviceInfo ---> connectionString
resolve(connectionString);
});
});
}
I reviewed the documentation on Microsoft's website, but the only documentation specifically for connection strings is this. Here is the device information object definitions. I know I could parse it myself, but I also couldn't find a specific definition in the documentation on what a connection string consists of. From my experience, I know it's a host name, a device id, and a symmetric key - though I was hoping for an azure function to generate it to isolate myself from issues down the road if the connection string generation changes.
Any assistance would be appreciated.
This is the function that I came up with. However, I would like to use a function from the Azure IoT Hub package, if possible.
function generateConnectionString(deviceInfo, hub){
return `HostName=${hub}.azure-devices.net;DeviceId=${deviceInfo.deviceId};SharedAccessKey=${deviceInfo.authentication.symmetricKey.primaryKey}`;
}