Queries devices by tags.
Description
If you specify multiple tags, IoT Platform returns a list of devices that match all the specified tags.
getThingsWithTags(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. |
Parameter | Type | Description |
---|---|---|
payload | Array |
The tag information. It is an array that consists of the {<tag name>:<tag value>} key-value pairs. |
Parameter | Type | Description |
---|---|---|
err | Error |
|
data | Array | This function returns an array of devices that meet the tag conditions. |
Sample requests
The following example queries devices that have the tag location: master bedroom
.
'use strict';
const leSdk = require('linkedge-core-sdk');
const iotData = new leSdk.IoTData();
const deviceTags = {
payload: [{'location': 'master bedroom'}],
};
/* Promise wrapper for getThingsWithTags. */
function getThingsWithTags(params) {
return new Promise((resolve, reject) => {
iotData.getThingsWithTags(params, (err, things) => {
err ? reject(err) : resolve(things);
});
});
}
exports.handler = function (event, context, callback) {
getThingsWithTags(deviceTags).then((things) => {
console.log(JSON.stringify(things));
for(var i=0; i<things.length; i++) {
console.log('-- productKey='+things[i]["productKey"] + ', deviceName='+things[i]["deviceName"]);
}
callback(null);
}).catch((err) => {
console.log(err);
callback(err);
});
};