【问题标题】:Retreiving device twin information in an Azure function?在 Azure 函数中检索设备孪生信息?
【发布时间】:2024-05-19 15:10:02
【问题描述】:

所以我有一个从 csv 文件中检索数据的天蓝色函数。我想要做的是在 azure 函数中检索该信息时首先检查我是否可以获得设备孪生的详细信息。这一切都是在javascript中完成的 所以首先我实例化细节:

const connectionstring="<connection string>"
const devicename= "<getdevicename>".

然后我在其他一些随机代码中间调用该函数:

myfunction();

myfunction 的函数是一个异步函数:

async function myfucntion(){
var devicetwin= await registryManager.GetTwinAsync(devicename);
context.log("device is "+ devicetwin.deviceId+ "status is " + deviceTwin.status);
}

但是没有显示任何详细信息。我检查了设备名称,它也与双胞胎匹配。

【问题讨论】:

  • 数据请求是否成功?
  • 是的,数据请求成功
  • registryManager.GetTwinAsync(devicename).then(data => {console.log(data}).catch(err=>console.log(err))。试试看会发生什么
  • 仍然没有得到正确的信息
  • 我猜“registryManager.GetTwinAsync(devicename)”有什么问题

标签: javascript azure


【解决方案1】:

你应该像下面这样修改你的代码,它对我有用。

var Registry = require('azure-iothub').Registry;

var devicename="MyASAIoTDevice"

var registryManager = Registry.fromConnectionString("HostName=yyio*********lIlBmTU=");

async function test (){

    try {
        var devicetwin= await registryManager.getTwin(devicename);
        console.log("device is "+ devicetwin.responseBody.deviceId+ "status is " + devicetwin.responseBody.status);
    } catch (error) {
        console.log(error);
   }
}
test();

【讨论】: