async function connect() {
    try {
        // calc device symmetric key from group symmetric key
        const deviceSymmetricKey = deviceKey || computeDerivedSymmetricKey(groupSymmetricKey, deviceId);

        // DPS provision with device symmetric key
        const provisioningSecurityClient = new SymmetricKeySecurityClient(deviceId, deviceSymmetricKey);
        const provisioningClient = ProvisioningDeviceClient.create(provisioningHost, scopeId, new ProvisioningTransport(), provisioningSecurityClient);

        // set the model to register against
        provisioningClient.setProvisioningPayload({
            iotcModelId: modelId
        });

        // register the device and get the hub host name
        const connectionString = await new Promise((resolve, reject) => {
            provisioningClient.register((dpsError, dpsResult) => {
                if (dpsError) {
                    console.log(`DPS register failed: ${JSON.stringify(dpsError, null, 4)}`);

                    return reject(dpsError);
                }

                console.log('registration succeeded');
                console.log(`assigned hub: ${dpsResult.assignedHub}`);
                console.log(`deviceId: ${dpsResult.deviceId}`);

                return resolve(`HostName=${dpsResult.assignedHub};DeviceId=${dpsResult.deviceId};SharedAccessKey=${deviceSymmetricKey}`);
            });
        });

        // create client from connection string
        client = Client.fromConnectionString(connectionString, Protocol);

        // cannot use the default retry logic built into the SDK as it will not fallback to DPS
        client.setRetryPolicy(new MultiHubRetryPolicy());

        // monitor for connects, disconnects, errors, and c2d messages
        client.on('connect', connectHandler);
        client.on('disconnect', disconnectHandler);
        client.on('error', errorHandler);

        if (c2dCommandReceiveOn) {
            client.on('message', messageHandler);
        }

        // connect to IoT Hub
        await client.open();

        // obtain twin object
        deviceTwin = await client.getTwin();

        if (desiredPropertyReceiveOn) {
            deviceTwin.on('properties.desired', desiredPropertyHandler);
        }

        // handlers for the direct method
        if (directMethodReceiveOn) {
            client.onDeviceMethod('echo', echoCommandDirectMethodHandler);
        }
    }
    catch (err) {
        console.error(`Could not connect: ${err.message}`);
        throw new Error(`Hub connect error! Error: ${err.message}`);
    }
}
// connect to IoT Central/Hub via Device Provisioning Servicee (DPS)
connect to IoT Central/Hub via Device Provisioning Servicee (DPS)
connect to IoT Central / Hub via Device Provisioning Servicee ( DPS )
async function connect() {
    try {
        const deviceSymmetricKey = deviceKey || computeDerivedSymmetricKey(groupSymmetricKey, deviceId);
        const provisioningSecurityClient = new SymmetricKeySecurityClient(deviceId, deviceSymmetricKey);
        const provisioningClient = ProvisioningDeviceClient.create(provisioningHost, scopeId, new ProvisioningTransport(), provisioningSecurityClient);
        provisioningClient.setProvisioningPayload({
            iotcModelId: modelId
        });
        const connectionString = await new Promise((resolve, reject) => {
            provisioningClient.register((dpsError, dpsResult) => {
                if (dpsError) {
                    console.log(`DPS register failed: ${JSON.stringify(dpsError, null, 4)}`);
                    return reject(dpsError);
                }
                console.log('registration succeeded');
                console.log(`assigned hub: ${dpsResult.assignedHub}`);
                console.log(`deviceId: ${dpsResult.deviceId}`);
                return resolve(`HostName=${dpsResult.assignedHub};DeviceId=${dpsResult.deviceId};SharedAccessKey=${deviceSymmetricKey}`);
            });
        });
        client = Client.fromConnectionString(connectionString, Protocol);
        client.setRetryPolicy(new MultiHubRetryPolicy());
        client.on('connect', connectHandler);
        client.on('disconnect', disconnectHandler);
        client.on('error', errorHandler);
        if (c2dCommandReceiveOn) {
            client.on('message', messageHandler);
        }
        await client.open();
        deviceTwin = await client.getTwin();
        if (desiredPropertyReceiveOn) {
            deviceTwin.on('properties.desired', desiredPropertyHandler);
        }
        if (directMethodReceiveOn) {
            client.onDeviceMethod('echo', echoCommandDirectMethodHandler);
        }
    }
    catch (err) {
        console.error(`Could not connect: ${err.message}`);
        throw new Error(`Hub connect error! Error: ${err.message}`);
    }
}
async function connect ( ) { try { const deviceSymmetricKey = deviceKey || computeDerivedSymmetricKey ( groupSymmetricKey , deviceId ) ; const provisioningSecurityClient = new SymmetricKeySecurityClient ( deviceId , deviceSymmetricKey ) ; const provisioningClient = ProvisioningDeviceClient . create ( provisioningHost , scopeId , new ProvisioningTransport ( ) , provisioningSecurityClient ) ; provisioningClient . setProvisioningPayload ( { iotcModelId : modelId } ) ; const connectionString = await new Promise ( ( resolve , reject ) => { provisioningClient . register ( ( dpsError , dpsResult ) => { if ( dpsError ) { console . log ( ` ${ JSON . stringify ( dpsError , null , 4 ) } ` ) ; return reject ( dpsError ) ; } console . log ( 'registration succeeded' ) ; console . log ( ` ${ dpsResult . assignedHub } ` ) ; console . log ( ` ${ dpsResult . deviceId } ` ) ; return resolve ( ` ${ dpsResult . assignedHub } ${ dpsResult . deviceId } ${ deviceSymmetricKey } ` ) ; } ) ; } ) ; client = Client . fromConnectionString ( connectionString , Protocol ) ; client . setRetryPolicy ( new MultiHubRetryPolicy ( ) ) ; client . on ( 'connect' , connectHandler ) ; client . on ( 'disconnect' , disconnectHandler ) ; client . on ( 'error' , errorHandler ) ; if ( c2dCommandReceiveOn ) { client . on ( 'message' , messageHandler ) ; } await client . open ( ) ; deviceTwin = await client . getTwin ( ) ; if ( desiredPropertyReceiveOn ) { deviceTwin . on ( 'properties.desired' , desiredPropertyHandler ) ; } if ( directMethodReceiveOn ) { client . onDeviceMethod ( 'echo' , echoCommandDirectMethodHandler ) ; } } catch ( err ) { console . error ( ` ${ err . message } ` ) ; throw new Error ( ` ${ err . message } ` ) ; } }
connect to IoT Central/Hub via Device Provisioning Servicee (DPS)
connect to IoT Central / Hub via Device Provisioning Servicee ( DPS )
// calc device symmetric key from group symmetric key // DPS provision with device symmetric key // set the model to register against // register the device and get the hub host name // create client from connection string // cannot use the default retry logic built into the SDK as it will not fallback to DPS // monitor for connects, disconnects, errors, and c2d messages // connect to IoT Hub // obtain twin object // handlers for the direct method