0
votes

I'm using custom allocation policy to register my device through DPS. Reference code for C# can be found here.

I have ported most of the code for Azure function from C# to NodeJS as below:-

module.exports = async function (context, req) {
    const regId = req.body.deviceRuntimeContext.registrationId;
    const response = {
        status: 200,
        message: 'Device registered successfully'
    };
    if (!regId)
    {
        response.status = 500
    }
    const requestCustomPayload = req.body.deviceRuntimeContext.payload;
    context.res = {
        iotHubHostName: req.body.deviceRuntimeContext.payload.hubName
    };
}

Now, the issue I'm facing is updating the initial twin for the device in above code. If you check the above link for c# code it has an class called TwinState and TwinCollection which are used to update the intial twin of the device, but same classes or similar api's I was not able to find in NodeJS.

Does the nodejs Azure IoT sdk provide a way to update the initial twin?

2
the TwinState is just a wrapper for tags and desired properties, see details in the docs.microsoft.com/en-us/dotnet/api/…Roman Kiss
@RomanKiss you are right. Following that link i was able to figure out the data to be sent. But the catch is we need to send the data in body field. Check the answer below.iAviator

2 Answers

0
votes

I was able to achieve the custom allocation in node.js Azure function. Below is the code:-

module.exports = async function (context, req) {
    const regId = req.body.deviceRuntimeContext.registrationId;
    if(req && req.body && req.body.deviceRuntimeContext && req.body.deviceRuntimeContext.payload && req.body.deviceRuntimeContext.registrationId) {
        const requestCustomPayload = req.body.deviceRuntimeContext.payload;
        context.res = {
            body: {
                iotHubHostName: req.body.deviceRuntimeContext.payload.hubName,
                initialTwin: {
                    tags: {
                            deviceName: "test"
                          }
                    },
                    properties: {
                        Desired: {}
                    }
                }
            }
        };
    } else {
        context.res = {
            status: 500,
            message: `Somethig went wrong. Req object is ${JSON.stringify(req)}`
        }
    }
}

Some observations in above code

  1. The returned object from the function has a body field in which we set the hubName and the Initial twin properties.
  2. D is caps in the Desired field under properties field of initial twin
  3. The returned object from function is assigned to context.res

Here is the official video from Azure guys.

0
votes

A very simple way to initialize IoT Hub device twins when a device is first provisioned by DPS is to use the "initial Device Twin" state feature of the DPS enrollment group.

You define the initial twin state and when a device is provisioned by the enrollment group, the device twin is automatically populated for the new device.

{
  "tags": {
    "AnyKey": "AnyValue"
  },
  "properties": {
    "desired": {
        "newKey": 200,
        "newKey2": "this is a test",
    }
  }
}