Azure IoT Central - 显示命令执行结果

Azure IoT Central - Display Command execution results

如何在 IoT Central - 命令页面中显示命令结果?

流量:

{
  "status": 200,
  "payload": {
    "command": "ping",
    "result_code": "ok",
    "result_data": "ping 5 ms"
  }
}

如何在 IoT Central 中显示此数据?

现在 IoT Central 仅显示默认 "Sent at ..." 消息。

如果在设备上处理命令时设置与命令同名的报告 属性,则可以设置在 IoT Central UI 中显示的值。例如,使用 Node.js 您可以为 countdown 方法创建处理程序,如下所示:

function onCountdown(request, response) {
  console.log('received a request for Countdown');
  console.log(JSON.stringify(request.payload, null, 2));
  var fakeResponsePayload = {
    key: 'value'
  };

  response.send(200, fakeResponsePayload, function (err) {
    if (err) {
      console.error('Unable to send method response: ' + err.toString());
    } else {
      console.log('response to Countdown sent.');
      client.getTwin(function(err, twin) {
        if (err) {
          console.error('could not get twin');
        } else {
          console.log('twin created');
          var patch = {
            countdown:{
              value: 18
            }
          };
          twin.properties.reported.update(patch, function(err) {
            if (err) {
              console.error('unable to update twin: ' + err.toString());
            } else {
              console.log('twin state reported');
            }
          });
        }
      });      
    }
  });
}

client.onDeviceMethod('countdown', onCountdown);

注意命令的字段名的名称是countdown.

这里还有一个C++例子:https://github.com/Azure/iot-central-firmware/blob/master/MXCHIP/mxchip_advanced/src/registeredMethodHandlers.cpp