Calls a service of a specified device.

callThingService(params, callback)

Parameter Type Description
params object The parameter object. For more information, see the table Parameter descriptions of params.
callback(err, data) function The callback function. The callback function must follow standard JavaScript practices. For more information, see the table Parameter descriptions of callback.
Table 1. Parameter descriptions of params
Parameter Type Description
productKey String The key of the product to which the device belongs. It is a unique identifier issued by IoT Platform for the product.
deviceName String The name of the device. The device name is generated when the device is created.
service String

The identifier of the service that is called.

Log on to the IoT Platform console. On the Define Feature tab of the product details page, click TSL Model to view the identifier of each service.

payload String|Buffer

The identifiers and values of the input parameters for the specified service.

You can click TSL Model to view the values of the identifier parameter and other parameters.

Table 2. Parameter descriptions of callback
Parameter Type Description
err Error
  • If the call is successful, err is null.
  • If the call fails, err contains the error information.
data object The return value of the called service.

Sample requests

The following example calls the turn service for the LightDev2 device.

'use strict';

const leSdk = require('linkedge-core-sdk');
const iotData = new leSdk.IoTData();

const callServiceParams = {
  productKey: 'a1ZJTVsqj2y', // Please replace it with your Product Key.
  deviceName: 'LightDev2',   // Please replace it with your Device Name.
  service: 'turn',           // The service defined in the Product TSL.
  payload: {'LightSwitch': 0},
};
/* Promise wrapper for callThingService. */
function callThingService(params) {
  return new Promise((resolve, reject) => {
    iotData.callThingService(params, (err, data) => {
      err ? reject(err) : resolve(data);
    });
  });
}

exports.handler = function (event, context, callback) {
  callThingService(callServiceParams).then((data) => {
    console.log("-- Call service Success. Return " + JSON.stringify(data));
    callback(null);
  }).catch((err) => {
    console.log(err);
    callback(err);
  });
};