Azure / azure-iot-sdk-node

A Node.js SDK for connecting devices to Microsoft Azure IoT services
https://docs.microsoft.com/en-us/azure/iot-hub/
Other
261 stars 227 forks source link

[Technical Question] Device provision using custom allocation policy #888

Closed amit12cool closed 3 years ago

amit12cool commented 4 years ago

I need to allocate my device using custom allocation policy for DPS. I have set up the infrastructure that is the enrollment group, Azure function and my device. I'm abe to send the custom payload to Azure function. But below are some issues I need help into:-

1) Is there any reference for NodeJS Azure function which handles the DPS custom allocation policy logic? I found for C# only in Azure docs.

2) Is it possible I parse the relevant payload in my Azure function and reassign the policy to say Evenly distributed thus IoT Hub for device registration will be chosen by DPS.

3) Do you have any example to update the initial twin of the DPS in Azure function in Nodejs?

4) Also could you guide me to link in service sdk to create the parent chile relationship between edge device and leaf device.

Note:- Using custom allocation policy I just need to fetch the payload and update the initial twin of device only.

AB#8208968

amit12cool commented 4 years ago

@anthonyvercolano Did you get a chance to look at this? I did an initial set up of Azure function in Nodejs as below:-

module.exports = async function (context, req) {
    const regId = req.body.deviceRuntimeContext.registrationId;
    console.log('regId is', regId);
    const response = {
        status: 200,
        message: 'Device registered successfully'
    };
    if (!regId)
    {
        response.status = 500
    }
    console.log('req is', JSON.stringify(req.body));
    const requestCustomPayload = req.body.deviceRuntimeContext.payload;
    console.log('edgeDeviceId is', req.body.deviceRuntimeContext.payload.edgeDeviceId);
    console.log('hubName is', req.body.deviceRuntimeContext.payload.hubName);
    return {
        iotHubHostName: req.body.deviceRuntimeContext.payload.hubName,
        initialTwin: {
            tags: {
                iotScope: {
                    deviceScope: req.body.deviceRuntimeContext.payload.edgeDeviceId
                }
            },
            properties: {
                desired: {}
            }
        }
    };
}

In the above code I'm finding it difficult to update the twin state. Does the device provisioning service sdk for node js provides an api to update the initial twin like c# does here. Unfortunately, Im getting the error on device side as error registering device: ProvisioningRegistrationFailedError: registration failed. Im using the azure-sdk-node register_symkeygroup.js class to register my device.

YoDaMa commented 3 years ago

Hi @amit12cool, this question seems to be related to your other issue, #893. Thanks for your patience with both. To respond to your questions:

Is there any reference for NodeJS Azure function which handles the DPS custom allocation policy logic? I found for C# only in Azure docs.

No there is currently not. This is a helpful suggestion though and we will look into writing a Node.js specific guide for DPS custom allocation policy, in concert with the DPS team.

Is it possible I parse the relevant payload in my Azure function and reassign the policy to say Evenly distributed thus IoT Hub for device registration will be chosen by DPS.

If you are creating a custom allocation policy you are overriding the default allocation policy (evely weighted distribution). Is this what you are referring to?

Do you have any example to update the initial twin of the DPS in Azure function in Nodejs?

No. We will consider writing one.

Also could you guide me to link in service sdk to create the parent chile relationship between edge device and leaf device.

Here is a guide, "Connect a downstream device to an Azure IoT Edge gateway", linked here.

amit12cool commented 3 years ago

@elhorton @YoDaMa Apologies for delay in response. Most of you answers are clear except one to which I would like to discuss further.

If you are creating a custom allocation policy you are overriding the default allocation policy (evenly weighted distribution). Is this what you are referring to? ---->I need to fallback to the evenly distributed policy of DPS after parsing my payload in Azure function. Is this feasible through Azure function code?

YoDaMa commented 3 years ago

---->I need to fallback to the evenly distributed policy of DPS after parsing my payload in Azure function. Is this feasible through Azure function code?

This to me is confusing. You'll have to clarify your use case. Are you saying you want to set up a custom allocation policy, which uses the built in evenly weighted distribution? That sounds like you just want to use the default allocation policy, so I'm not sure why you would need a custom allocation policy set up via an Azure Function.

amit12cool commented 3 years ago

In my Azure function I just want to use the custom payload being sent by device and update the initial twin of the device only rest the iot hub returned from the Azure function should be based on evenly distributed algorithm.


From: Yoseph Maguire notifications@github.com Sent: Wednesday, February 17, 2021 9:13:11 PM To: Azure/azure-iot-sdk-node azure-iot-sdk-node@noreply.github.com Cc: Amit Dhawan amit.dhawan@outlook.com; Mention mention@noreply.github.com Subject: Re: [Azure/azure-iot-sdk-node] [Technical Question] Device provision using custom allocation policy (#888)

---->I need to fallback to the evenly distributed policy of DPS after parsing my payload in Azure function. Is this feasible through Azure function code?

This to me is confusing. You'll have to clarify your use case. Are you saying you want to set up a custom allocation policy, which uses the built in evenly weighted distribution? That sounds like you just want to use the default allocation policy, so I'm not sure why you would need a custom allocation policy set up via an Azure Function.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/Azure/azure-iot-sdk-node/issues/888#issuecomment-780646960, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABTB2JHAHMHV2JPZBJRLNHLS7PPY7ANCNFSM4Q6ATBWA.

rajeevmv commented 3 years ago

If the allocation policy in DPS is set to custom allocation, it cannot fallback to the evenly weighted distribution policy based on the response from the custom function. When using custom allocation, you take on the functionality of selecting the Hub where the device should be placed.

amit12cool commented 3 years ago

Ok..Thanks that answers my question. One more clarification required:- Is it possible we can call the evenly distributed or lowest latency algo may be provided by Azure mode sdk from with in Azure function?

nberdy commented 3 years ago

Hi Amit. You can use whatever algo you like in your code. I recommend you hash by registration ID, but if that doesn't work for you then you can use one that gives you the distribution characteristics you are looking for.

YoDaMa commented 3 years ago

ok with that I will be closing this thread, since it has contained alot of different questions. If you would like to ask another question feel free to open another issue. For questions aligned toward the device provisioning service your best bet is to reach out to them directly, since the expertise of the maintainers of this repository is focused on the code in this repository.