Receiving a Command

API Function

This API is used to register the broadcast for receiving commands from the IoT platform.

API Description

1
IOTA_TOPIC_SERVICE_COMMAND_RECEIVE/{deviceId};

Parameter Description

Enumerated Item

Value

Type

Description

EN_IOTA_DATATRANS_IE_RESULT

0

unsigned int

Command execution result.

  • 0: The topic subscription is successful.
  • 1: The topic subscription fails.

EN_IOTA_DATATRANS_IE_DEVICEID

1

String

Identifier of a device.

EN_IOTA_DATATRANS_IE_REQUESTID

2

String

Identifier of a request.

EN_IOTA_DATATRANS_IE_SERVICEID

3

String

Identifier of a service.

EN_IOTA_DATATRANS_IE_METHOD

4

String

Service method.

EN_IOTA_DATATRANS_IE_CMDCONTENT

5

String

Command content. You need to parse service command parameters that are assembled in JSON format according to the command definition to obtain parameter values.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
//Call this API to register the function for subsequent processing after device commands are received.
HW_iNT Switch_CommandRecvtHandler(HW_UiNT uiCookie, HW_MSG pstMsg) 
{
    HW_cHAR *pcMethod, *pcServiceId, *pcCmdContent, *pcDeviceId;
    pcDeviceId = HW_MsgGetStr(pstMsg, EN_IOTA_DATATRANS_IE_DEVICEID);
    pcServiceId = HW_MsgGetStr(pstMsg, EN_IOTA_DATATRANS_IE_SERVICEID);
    pcMethod = HW_MsgGetStr(pstMsg, EN_IOTA_DATATRANS_IE_METHOD);
    pcCmdContent = HW_MsgGetStr(pstMsg, EN_IOTA_DATATRANS_IE_CMDCONTENT);
if (strcmp(pcServiceId, "switch"))
    {
//Call this API to use the JSON components to parse pcCmdContent based on command parameters in the profile.
        //Send command to Switch
    }
    return 0;
}
//Call this API to register the broadcast for receiving device commands immediately after the device is added.
HW_BroadCastReg("IOTA_TOPIC_SERVICE_CMD_RECEIVE/XXXX_XXXX_XXXX_XXXX",
Device_AddResultHandler);

After a device is added, register the broadcast for receiving commands for the device. The broadcast is in format of IOTA_TOPIC_SERVICE_CMD_RECEIVE/Device ID. When the AgentLite receives commands delivered by the IoT platform to the device, it broadcasts these commands to the broadcast processing function registered by the device. If the commands do not need to be dispatched by device ID, the broadcast can be in format of IOTA_TOPIC_SERVICE_CMD_RECEIVE.