function initiateDeviceSettings(node) {
        // Log the start
        node.log(node.deviceid + ' -> Initiate IoT Device settings.');
        // Set device protocol to selected (default to AMQP-WS)
        var deviceProtocol = (node.protocol == "amqp") ? Protocols.amqp : 
            (node.protocol == "amqpWs") ? Protocols.amqpWs : 
            (node.protocol == "mqtt") ? Protocols.mqtt :
            (node.protocol == "mqttWs") ? Protocols.mqttWs : 
            Protocols.amqpWs;
    
        // Set the security properties and finalize the connection string
        var options = {};
        if (node.authenticationmethod === "sas") {
            // Check if group or individual enrollment
            if (node.connectiontype !== "constr" && node.enrollmenttype === "group") {
                node.saskey = computeDerivedSymmetricKey(node.saskey, node.deviceid);
            }
        } else if (node.authenticationmethod === "x509") {
            options = {
                cert : node.cert,
                key : node.key
            };
        };

        // Create the client connection string based on the node settings.
        // Either use the provided conectiong string or use DPS to provision.
        node.log(node.deviceid + ' -> Initiate IoT Device connection string.');
        if (node.connectiontype === "constr") {
            initiateClient(node, options, deviceProtocol);
        } else if (node.connectiontype === "dps") {
            // Set provisioning protocol to selected (default to AMQP-WS)
            var provisioningProtocol = (node.protocol == "amqp") ? ProvisioningProtocols.amqp : 
                (node.protocol == "amqpWs") ? ProvisioningProtocols.amqpWs :
                (node.protocol == "mqtt") ? ProvisioningProtocols.mqtt :
                (node.protocol == "mqttWs") ? ProvisioningProtocols.mqttWs :
                ProvisioningProtocols.amqpWs;

            // Set security client based on SAS or X.509
            var provisioningSecurityClient = 
                (node.authenticationmethod == "sas") ? new SecurityClient.sas(node.deviceid, node.saskey) :
                    new SecurityClient.x509(node.deviceid, options);

            // Create provisioning client
            var provisioningClient = ProvisioningDeviceClient.create(GlobalProvisoningEndpoint, node.scopeid, new provisioningProtocol(), provisioningSecurityClient);

            // set the provisioning payload (for custom allocation)
            var payload = {};
            if (node.DPSpayload) {
                // Turn payload into JSON
                try {
                    payload = JSON.parse(node.DPSpayload);
                    node.log(node.deviceid + ' -> DPS Payload added.');
                } catch (err) {
                    // do nothing 
                }
            }
            provisioningClient.setProvisioningPayload(JSON.stringify(payload));

            // Register the device.
            node.log(node.deviceid + ' -> Provision IoT Device using DPS.');
            provisioningClient.register(function(err, result) {
                if (err) {
                    node.error(node.deviceid + ' -> Error registering device: ' + JSON.stringify(err));
                    setStatus(node, statusEnum.error);
                } else {
                    node.log(node.deviceid + ' -> Registration succeeded.');
                    node.log(node.deviceid + ' -> Assigned hub: ' + result.assignedHub);
                    var msg = {};
                    msg.topic = 'provisioning';
                    msg.deviceId = result.deviceId;
                    msg.payload = JSON.parse(JSON.stringify(result));
                    node.send(msg);
                    node.iothub = result.assignedHub;
                    node.deviceid = result.deviceId;
                    initiateClient(node, options, deviceProtocol);
                }
            });
        };
    }
// Create the device setting and registration
Create the device setting and registration
Create the device setting and registration
function initiateDeviceSettings(node) {
        node.log(node.deviceid + ' -> Initiate IoT Device settings.');
        var deviceProtocol = (node.protocol == "amqp") ? Protocols.amqp : 
            (node.protocol == "amqpWs") ? Protocols.amqpWs : 
            (node.protocol == "mqtt") ? Protocols.mqtt :
            (node.protocol == "mqttWs") ? Protocols.mqttWs : 
            Protocols.amqpWs;
        var options = {};
        if (node.authenticationmethod === "sas") {
            if (node.connectiontype !== "constr" && node.enrollmenttype === "group") {
                node.saskey = computeDerivedSymmetricKey(node.saskey, node.deviceid);
            }
        } else if (node.authenticationmethod === "x509") {
            options = {
                cert : node.cert,
                key : node.key
            };
        };
        node.log(node.deviceid + ' -> Initiate IoT Device connection string.');
        if (node.connectiontype === "constr") {
            initiateClient(node, options, deviceProtocol);
        } else if (node.connectiontype === "dps") {
            var provisioningProtocol = (node.protocol == "amqp") ? ProvisioningProtocols.amqp : 
                (node.protocol == "amqpWs") ? ProvisioningProtocols.amqpWs :
                (node.protocol == "mqtt") ? ProvisioningProtocols.mqtt :
                (node.protocol == "mqttWs") ? ProvisioningProtocols.mqttWs :
                ProvisioningProtocols.amqpWs;
            var provisioningSecurityClient = 
                (node.authenticationmethod == "sas") ? new SecurityClient.sas(node.deviceid, node.saskey) :
                    new SecurityClient.x509(node.deviceid, options);
            var provisioningClient = ProvisioningDeviceClient.create(GlobalProvisoningEndpoint, node.scopeid, new provisioningProtocol(), provisioningSecurityClient);
            var payload = {};
            if (node.DPSpayload) {
                try {
                    payload = JSON.parse(node.DPSpayload);
                    node.log(node.deviceid + ' -> DPS Payload added.');
                } catch (err) {
                }
            }
            provisioningClient.setProvisioningPayload(JSON.stringify(payload));
            node.log(node.deviceid + ' -> Provision IoT Device using DPS.');
            provisioningClient.register(function(err, result) {
                if (err) {
                    node.error(node.deviceid + ' -> Error registering device: ' + JSON.stringify(err));
                    setStatus(node, statusEnum.error);
                } else {
                    node.log(node.deviceid + ' -> Registration succeeded.');
                    node.log(node.deviceid + ' -> Assigned hub: ' + result.assignedHub);
                    var msg = {};
                    msg.topic = 'provisioning';
                    msg.deviceId = result.deviceId;
                    msg.payload = JSON.parse(JSON.stringify(result));
                    node.send(msg);
                    node.iothub = result.assignedHub;
                    node.deviceid = result.deviceId;
                    initiateClient(node, options, deviceProtocol);
                }
            });
        };
    }
function initiateDeviceSettings ( node ) { node . log ( node . deviceid + ' -> Initiate IoT Device settings.' ) ; var deviceProtocol = ( node . protocol == "amqp" ) ? Protocols . amqp : ( node . protocol == "amqpWs" ) ? Protocols . amqpWs : ( node . protocol == "mqtt" ) ? Protocols . mqtt : ( node . protocol == "mqttWs" ) ? Protocols . mqttWs : Protocols . amqpWs ; var options = { } ; if ( node . authenticationmethod === "sas" ) { if ( node . connectiontype !== "constr" && node . enrollmenttype === "group" ) { node . saskey = computeDerivedSymmetricKey ( node . saskey , node . deviceid ) ; } } else if ( node . authenticationmethod === "x509" ) { options = { cert : node . cert , key : node . key } ; } ; node . log ( node . deviceid + ' -> Initiate IoT Device connection string.' ) ; if ( node . connectiontype === "constr" ) { initiateClient ( node , options , deviceProtocol ) ; } else if ( node . connectiontype === "dps" ) { var provisioningProtocol = ( node . protocol == "amqp" ) ? ProvisioningProtocols . amqp : ( node . protocol == "amqpWs" ) ? ProvisioningProtocols . amqpWs : ( node . protocol == "mqtt" ) ? ProvisioningProtocols . mqtt : ( node . protocol == "mqttWs" ) ? ProvisioningProtocols . mqttWs : ProvisioningProtocols . amqpWs ; var provisioningSecurityClient = ( node . authenticationmethod == "sas" ) ? new SecurityClient . sas ( node . deviceid , node . saskey ) : new SecurityClient . x509 ( node . deviceid , options ) ; var provisioningClient = ProvisioningDeviceClient . create ( GlobalProvisoningEndpoint , node . scopeid , new provisioningProtocol ( ) , provisioningSecurityClient ) ; var payload = { } ; if ( node . DPSpayload ) { try { payload = JSON . parse ( node . DPSpayload ) ; node . log ( node . deviceid + ' -> DPS Payload added.' ) ; } catch ( err ) { } } provisioningClient . setProvisioningPayload ( JSON . stringify ( payload ) ) ; node . log ( node . deviceid + ' -> Provision IoT Device using DPS.' ) ; provisioningClient . register ( function ( err , result ) { if ( err ) { node . error ( node . deviceid + ' -> Error registering device: ' + JSON . stringify ( err ) ) ; setStatus ( node , statusEnum . error ) ; } else { node . log ( node . deviceid + ' -> Registration succeeded.' ) ; node . log ( node . deviceid + ' -> Assigned hub: ' + result . assignedHub ) ; var msg = { } ; msg . topic = 'provisioning' ; msg . deviceId = result . deviceId ; msg . payload = JSON . parse ( JSON . stringify ( result ) ) ; node . send ( msg ) ; node . iothub = result . assignedHub ; node . deviceid = result . deviceId ; initiateClient ( node , options , deviceProtocol ) ; } } ) ; } ; }
Create the device setting and registration
Create the device setting and registration
// Log the start // Set device protocol to selected (default to AMQP-WS) // Set the security properties and finalize the connection string // Check if group or individual enrollment // Create the client connection string based on the node settings. // Either use the provided conectiong string or use DPS to provision. // Set provisioning protocol to selected (default to AMQP-WS) // Set security client based on SAS or X.509 // Create provisioning client // set the provisioning payload (for custom allocation) // Turn payload into JSON // do nothing  // Register the device.