有没有办法从azure功能应用程序更新设备初始双胞胎? [英] Is there a way to update device initial twin from azure function app?
问题描述
我正在使用自定义分配策略通过DPS注册设备.可以在此处.
I'm using custom allocation policy to register my device through DPS. Reference code for C# can be found here.
我已将Azure函数的大部分代码从C#移植到NodeJS,如下所示:-
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
};
}
现在,我面临的问题是在上述代码中更新设备的初始孪生兄弟.如果您在上面的链接中查找c#代码,则它具有一个名为TwinState和TwinCollection的类,它们用于更新设备的初始孪生子,但是在NodeJS中找不到相同的类或类似的api.
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.
nodejs Azure IoT SDK是否提供一种更新初始双胞胎的方法?
Does the nodejs Azure IoT sdk provide a way to update the initial twin?
推荐答案
我能够在node.js Azure函数中实现自定义分配.下面是代码:-
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
- 该函数返回的对象有一个body字段,在其中设置了hubName和Initial twin属性.
-
D
是初始双胞胎的属性"字段下的"Desired
"字段中的大写字母 - 从函数返回的对象已分配给context.res
- The returned object from the function has a body field in which we set the hubName and the Initial twin properties.
D
is caps in theDesired
field under properties field of initial twin- The returned object from function is assigned to context.res
此处是来自Azure家伙的官方视频.
Here is the official video from Azure guys.
这篇关于有没有办法从azure功能应用程序更新设备初始双胞胎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!