All Products
Search
Document Center

Link IoT Edge:getThingProperties

Last Updated:Aug 01, 2023

Obtains the value of a property for a specified device.

getThingProperties(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 devices belong. 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.

payload

Array

The array of identifiers for the device properties that you want to obtain. If the call is successful, the current values of the properties are returned.

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

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 value of the specified device properties.

Sample requests

The following example obtains the LightSwitch property value of the LightDev2 device. The LightSwitch property indicates whether the device is switched on.

'use strict';

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

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

exports.handler = function (event, context, callback) {
  getThingProperties(getPropertiesParams).then((data) => {
    console.log(data); // Return value example: { LightSwitch: 0 } 
    if (null == data.LightSwitch) {
      console.log("-- [Warn] No property LightSwitch return.");
    } else {
      console.log("-- [Success] LightSwitch = " + data.LightSwitch);
      if (data.LightSwitch == '0') {
        console.log("-- Light is off.");
      } else {
        console.log("-- Light is on.");
      }
    }
    callback(null);
  }).catch((err) => {
    console.log(err);
    callback(err);
  });
};