简体   繁体   English

Azure IoT Edge 节点 SDK invokeDeviceMethod 无法异步工作

[英]Azure IoT Edge node SDK invokeDeviceMethod not working asynchronously

I am trying to return a result for a direct method call in an async fashion.我试图以异步方式返回直接方法调用的结果。

I tried:我试过:

var client = Client.fromConnectionString(process.env.AZ_IOT_CONNECTION_STRING);
var methodParams = {
  methodName: "method",
  payload: 10, // Number of seconds.
  responseTimeoutInSeconds: 60
};

// Call the direct method on your device using the defined parameters.
client.invokeDeviceMethod(
  req.params.deviceId,
  methodParams,
  (err, result) => {
    if (err) {
      console.error(err);
    } else {
      console.log("success");
    }
  }
);

On Device:在设备上:

const method = async (request, response) => {
  const longProcess = () => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve();
      }, 5000);
    });
  };

  try {
    await longProcess();
    response.send(200, `success`);
  } catch (error) {
    response.send(500, `Error: ${error}:`);
  }
};


client.onDeviceMethod("method", method);

Expected : Returns success after 5 seconds预期:5 秒后返回成功

Actual : returns "BadRequest" "errorCode :400027" but the method is executed correctly.实际:返回“BadRequest”“errorCode :400027”但该方法执行正确。

please work with the promise function instead of the callback like so:请使用 promise 函数而不是像这样的回调:

async deviceSyncInvoke(deviceID: string, methodName: string, data:any, responseTimeoutInSeconds?: number): Promise<[number, any]> {
        try{
            if (!this.client){
                await this.openClient();
            }

            const methodParam: DeviceMethodParams = {
                methodName: methodName,
                payload: JSON.stringify(data)
            }

            // The minimum and maximum values for responseTimeoutInSeconds are 5 and 300 seconds.
            //If timeout is not provided, it the default value of 30 seconds is used
            if (responseTimeoutInSeconds && responseTimeoutInSeconds !== undefined) {
                methodParam.responseTimeoutInSeconds = responseTimeoutInSeconds;
            }


            const res: ResultWithIncomingMessage<any> = await this.client.invokeDeviceMethod(deviceID, methodParam);
            if (res.message.statusCode == undefined || res.message.statusCode > 299) {
                throw new Error(`statusCode: ` + res.message.statusCode + `statusMessage: ` + res.message.statusMessage);
            }

            return [2001, "message invoking command with res: " + res];
        }catch(err) {
            return [5000, "error deviceSyncInvoke: " + err];
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 适用于Azure Node SDK的文档(非命令行)? - Docs for Azure Node SDK (not command line)? 在新 _blank 选项卡上异步设置 location.href 不适用于 MS Edge - Set location.href on new _blank tab asynchronously not working on MS Edge 语法错误:node_modules / aws-iot-device-sdk / thing / index.js中出现意外令牌 - SyntaxError: Unexpected token in node_modules/aws-iot-device-sdk/thing/index.js Azure IoT推送通知 - Azure IoT Push notifications 为Azure IoT中心运行节点server.js时,``向RegExp构造函数提供了无效标志&#39;&#39;错误 - 'Invalid flags supplied to RegExp constructor' error when running node server.js for Azure IoT Hub facebook javascript SDK-异步加载 - facebook javascript SDK - Loading asynchronously 异步加载Deezer Javascript SDK - asynchronously load Deezer Javascript SDK 节点js azure SDK getBlobToStream使用大量内存 - node js azure SDK getBlobToStream uses lots of memory 如何使用 MQTT-JS 或 JS AWS-SDK(节点 JS/JS)向 AWS IoT 的一个主题发送和接收数据 - How to use MQTT-JS or JS AWS-SDK (node JS/JS) to send and receive data to a topic of AWS IoT 更新面板无法异步工作 - Update Panel not working asynchronously
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM