Call function

How to define a Remote Function callback

A remote function is defined in your C++/Python application’s code as such:

// Define the remote function callback
void helloCallback(FunctionEvent &event)
{
    // Get the parameters of the remote function call
    sl_iot::json params = event.getEventParameters();
    // Check if parameters are present and valid
    if (params.contains("message") && params["message"].is_string())
    {
        std::cout << "Hello, thanks for the message!" << std::endl;

        // Update the result and status of the event
        event.status = 0;
        event.result = result;
    }
    else
    {
        HubClient::sendLog("Error in remote function parameters", LOG_LEVEL::ERROR);
        event.status = 1;
        event.result = "Addition remote function was used with wrong arguments.";
    }
}

// Set your callback parameters
CallbackParameters callback_params;
callback_params.setRemoteCallback("helloMessage", CALLBACK_TYPE::ON_REMOTE_CALL);

// Register your callback function
HubClient::registerFunction(helloCallback, callback_params);

How to trigger a Remote Function

A remote function can be triggered from the REST API:

  • REST API: provides the /workspaces/:workspaceId/devices/:deviceId/functions/:functionName endpoint.

Using curl, you can trigger the callback defined above using:

curl -s -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${access_token}"\
    -X POST ${region_url}/workspaces/${workspace_id}/devices/${device_id}/functions/tuto05_add \
    -d "{ \
    \"parameters\": { \
      \"num1\": ${num1}, \
\"num2\": ${num2} \
      }\
    }"

If the status value is different from 0, this means that the function encountered an error.