0

how to display Commands results in IoT Central - Commands page?

enter image description here

Flow:

  • Command sent to Device
  • Device received the command and returns Payload:
{
  "status": 200,
  "payload": {
    "command": "ping",
    "result_code": "ok",
    "result_data": "ping 5 ms"
  }
}

How to display this data in IoT Central?

Now IoT Central shows only default "Sent at ..." message.

Patrick
  • 2,044
  • 1
  • 25
  • 44
el-niko
  • 113
  • 1
  • 10
  • 1
    It looks like the current version of the Azure IoT Central doesn't allow either to show this payload or mapping to the app properties. Please, make the feedback to the team: https://feedback.azure.com/forums/911455-azure-iot-central – Roman Kiss Mar 21 '19 at 16:58
  • Done! Please, vote: https://feedback.azure.com/forums/911455-azure-iot-central/suggestions/37173469-show-commands-execution-result-on-command-tile – el-niko Mar 22 '19 at 12:04

1 Answers1

1

If you set a reported property with the same name as the command when you handle the command on the device, then you can set a value that displays in the IoT Central UI. For example, using Node.js you can create a handler for a countdown method like this:

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);

Note that the name of the field name of the command is countdown.

enter image description here

There is also a C++ example here: https://github.com/Azure/iot-central-firmware/blob/master/MXCHIP/mxchip_advanced/src/registeredMethodHandlers.cpp

Dominic Betts
  • 2,306
  • 1
  • 18
  • 10
  • it works, thanks! Please add this example to the documentation if you are related to MS – el-niko Mar 25 '19 at 13:33
  • if I got it right, the Azure IoT Central App doesn't handle a real response and status from the device direct method, but it shows a value of the reported property with the same name as a command (device direct method name)? So, in other words, if the device updated this reported property in any time, the value is show-up in this command dashboard without its invoking, isn't? That's happen in my test. Beside that, the Azure IoT Central App didn't handle an error (status) from the device direct method. It always accepted like OK (200). – Roman Kiss Mar 25 '19 at 13:43
  • @el-niko how you show your response payload object? – Roman Kiss Mar 25 '19 at 13:47
  • @roman-kiss like in example, there is a need to set a reported property with the same name as the command and pass the command result or code there. IoT central does not handle the **response** object. – el-niko Mar 25 '19 at 13:59
  • 1
    @el-niko That's my point. The device code must handle using an additional 2 calls for twin properties (such as the GET and PATCH) and then send a fake response. In the case of something happen, the IoT Central doesn't show this error, always handle like OK (200). Beside that, if your backend using multiple invokers (for instance HTTPs clients and IoTC), this "not handle response and status" and PATCHing reported property can create some design issues, in other words, it is making a device direct method "a dirty" instead of clear generic request/response message exchange pattern. – Roman Kiss Mar 25 '19 at 14:24
  • 1
    @el-niko In addition, there is a "harcoded name" between the reported property and direct name (command) and limitation of the value such as only the number, text, date and toggle, in the case of complex or simple object, it will show up a text [object Object]. I have expected in this feature mapping a response to the IoT Central app properties. Definitely, migrating devices from the Azure IoT Hub to the Azure IoT Central App will be in this case necessary to modified a device code (such as adding the GET and PATCH reported property) – Roman Kiss Mar 25 '19 at 14:38