Get list of direct methods registered with IotHub device

736 Views Asked by At

I am working on one sample IoT project. Where 1 IoT device is registered on IotHub. It is exposing 1 direct method to control device temperature. On, device startup it is registering callback on IoTHub to listen for method invocation requests.

As per my understanding and knowledge, there is no way on cloud side that we can know that particular device is exposing this many direct methods and what is name of that method. (Because of MQTT/AMQP use internally).

Still, to be sure of if there is any work around to get direct methods registered by end device. Is there any SDK function or REST API to get list of direct methods registered by end-device.

2

There are 2 best solutions below

2
Matthijs van der Veer On BEST ANSWER

You're correct in assuming there is no built-in support that lists the direct methods of your device. A device doesn't publish what methods it has implemented by default.

Options:

IoT PnP

Microsoft created IoT Plug and play, which focuses on "device models". When a Plug and Play device connects to the IoT Hub, it can make its device model known. Part of this model is the concept of "commands", which translates to a direct method for IoT Hub. Your device probably doesn't have this device model yet, as PnP is pretty new. A device manufacturer/developer can integrate this model into the device.

Create your own index command

If you are the one writing code for this device, and you don't want to do PnP, you could create a direct method that lists all the methods supported by your device. Of course, you would need to know the name of that direct method to call it.

0
Roman Kiss On

Recently, the Azure IoT Hub (version 2020-09-30) has been publicly enabled for IoT Plug and Play, where the device model is the "glue" between the device and service facing sides. See more details about this concept here. The device twin has been extended for new property such as modelId, which it represents an identifier of the pnp model in the repository, see more details here.

Once, the modelId has been populated in the device twin, the device knows all expected direct methods included their request/response schemas, c2d messaging, reported and desired properties and telemetry data. On the other side such as a service side, the invoker knows how to invoke a direct method on the device, etc.

The following is an example of the short pnp model with one telemetry data (Temperature) and one command for invoking a direct method SetTemp on the device in the synchronous manner (no c2d message). It has been created in the IoT Central App:

pnp model (modelId = "dtmi:rk2021iotcfree:Test6vj;1"):

{
    "@id": "dtmi:rk2021iotcfree:Test6vj;1",
    "@type": "Interface",
    "contents": [
      {
        "@id": "dtmi:rk2021iotcfree:Test6vj:Temperature;1",
        "@type": [
          "Telemetry",
          "Temperature"
        ],
        "displayName": {
          "en": "Temperature"
        },
        "name": "Temperature",
        "schema": "double",
        "unit": "degreeCelsius"
      },
      {
        "@id": "dtmi:rk2021iotcfree:Test6vj:SetTemp;1",
        "@type": "Command",
        "commandType": "synchronous",
        "displayName": {
          "en": "SetTemp"
        },
        "name": "SetTemp",
        "request": {
          "@id": "dtmi:rk2021iotcfree:Test6vj:SetTemp:__request:temp;1",
          "@type": "CommandPayload",
          "displayName": {
            "en": "temp"
          },
          "name": "temp",
          "schema": "double"
        }
      }
    ],
    "displayName": {
      "en": "Test"
    },
    "@context": [
      "dtmi:iotcentral:context;2",
      "dtmi:dtdl:context;2"
    ]
}

Based on the modelId, the simulated device10 has been connected as a pnp device to the Azure IoT Hub and the screen snippet shows a received message on the direct method SetTemp invoked from the Azure IoT Explorer tool:

enter image description here

enter image description here

the following screen snippet shows a device twin of the device10, as you can see there is a modelId property: enter image description here

I do recommend to use the pnp model for your solution. if you are interesting only for commands, you can create a small subset of the model just only for that, see the following example:

{
    "@id": "dtmi:rk2021iotcfree:Test6vj;1",
    "@type": "Interface",
    "contents": [    
      {   
        "@type": "Command",
        "commandType": "synchronous",       
        "name": "SetTemp"     
      }
    ],
    "@context": [
      "dtmi:dtdl:context;2"
    ]
}

where:

"@id": "dtmi:rk2021iotcfree:Test6vj;1"

represents the modelId

"commandType": "synchronous" 

represents the direct method call

"name": "SetTemp" 

represents the method name