开发者社区> 问答> 正文

Nodejs语言调用IoT套件POP API



1.添加阿里云POP API的SDK


@alicloud/pop-core https://www.npmjs.com/package/@alicloud/pop-core
{
  "name": "aliyun-iot-pop-api",
  "version": "0.0.1",
  "description": "aliyun-iot-pop-api",
  "private": true,
  "dependencies": {
    "@alicloud/pop-core": "1.5.2",
    "co": "4.6.0"
  },
  "repository": {
    "url": "https://yuque.com/wongxming/iot-tech"
  },
  "author": "wongxming",
  "license": "MIT"
}






2.IoT套件POP API调用示例



2.1 以QueryProductList 为例


https://help.aliyun.com/document_detail/69271.html



2.2 调用代码


/**
* package.json 添加依赖:"@alicloud/pop-core": "1.5.2"
*/
const co = require('co');
const RPCClient = require('@alicloud/pop-core').RPCClient;

const options = {
    accessKey: "自己的accessKey",
    accessKeySecret: "自己的accessKeySecret",
};
//1.创建client
const client = new RPCClient({
    accessKeyId: options.accessKey,
    secretAccessKey: options.accessKeySecret,
    endpoint: options.endpoint || 'https://iot.cn-shanghai.aliyuncs.com',
    apiVersion: options.apiVersion || '2018-01-20'
});


co(function*() {
    // 2.构造iot API
    // 这里是POP API的Action
    const action ='QueryProductList';
    // 这里是POP API的入参params
    const params = {
        CurrentPage: 1,
        PageSize: 10
    };
    //2.发送请求
    const response = yield client.request(action, params);

    console.log(JSON.stringify(response));
});


2.3 返回JSON结构


{
    "Data":{
        "PageCount":1,
        "PageSize":10,
        "CurrentPage":1,
        "List":{
            "ProductInfo":[
                
                {
                    "Description":"二进制",
                    "DataFormat":0,
                    "ProductKey":"a1pWti1bx1v",
                    "NodeType":0,
                    "ProductName":"二进制解析",
                    "DeviceCount":1,
                    "GmtCreate":1526629467000
                },
                {
                    "DataFormat":1,
                    "ProductKey":"a1uqtK9VHcO",
                    "NodeType":0,
                    "ProductName":"安卓温湿度计",
                    "DeviceCount":2,
                    "GmtCreate":1523935911000
                }
            ]
        },
        "Total":2
    },
    "RequestId":"EAB7B35E-A9BE-4988-AE2A-1DE1318DBCAF",
    "Success":true
}



3.IoT套件服务端POP API列表


https://help.aliyun.com/document_detail/69893.html








展开
收起
游客886 2018-06-28 11:42:15 3210 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
探究 Node.js 的服务端之路 立即下载
个推微服务实践 基于OpenResty 和Node.js 立即下载
沪江基于Node.js大规模应用实践 立即下载