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?
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
D
is caps in the Desired
field under properties field of initial twinHere is the official video from Azure guys.