1.0.2 • Published 5 years ago
jd-iot-device-sdk v1.0.2
Aliyun IoT Device SDK for Javascript
使用 Javascript 将设备接入到阿里云 IoT 套件和 LinkDevelop。
安装
安装 Node.js 运行环境,版本
>=4.0.0
。
通过 npm 包管理工具安装:
npm install aliyun-iot-device-sdk --save
设备接入云端
var aliyunIot = require('aliyun-iot-device-sdk');
var device = aliyunIot.device({
productKey: '<productKey>',
deviceName: '<deviceName>',
deviceSecret: '<deviceSecret>'
});
device.on('connect', () => {
console.log('connect successfully!');
});
IoT 套件基础版 API
设备上报数据
device.publish('/<productKey>/<deviceName>/update', 'hello world!');
云端下行消息监听
device.subscribe('/<productKey>/<deviceName>/get');
device.on('message', (topic, payload) => {
console.log(topic, payload.toString());
});
Link Develop 和 IoT 套件高级版 API
IoT 套件高级版封装了物模型定义与 Alink 异步协议,SDK 封装使得设备与云端通信时不需要关心 MQTT topic,只需要调用属性上报(aliyunIot.device#postProps())、服务监听(aliyunIot.device#serve())、事件上报(aliyunIot.device#postEvent())等相关 API。
设备属性上报
device.postProps({
CurrentTemperature: 25
});
调用 device.postProps()
等同于执行以下代码:
// 消息 id 用来确保服务端异步响应的时序
var msgId = "123";
// 发布属性上报 topic
device.publish('/sys/<productKey>/<deviceName>/thing/event/property/post', JSON.stringify({
{
id: msgId,
version: '1.0',
params: {
CurrentTemperature: 25,
},
method: 'thing.event.property.post'
}
}));
// 监听属性上报响应 topic
device.subscribe('/sys/<productKey>/<deviceName>/thing/event/property/post_reply');
device.on('message', function(topic, payload){
var res = payload.toString();
if (res.id === msgId) {
// 在这里处理服务端响应
console.log(res.data);
}
});
监听云端下发的服务调用消息
// 监听云端设置属性服务消息
device.serve('property/set', function(params) {
// 处理服务参数
});
API
- aliyunIot.device()
- aliyunIot.device#publish()
- aliyunIot.device#subscribe()
- aliyunIot.device#postProps()
- aliyunIot.device#postEvent()
- aliyunIot.device#serve()
- aliyunIot.gateway()
- aliyunIot.gateway#addTopo()
- aliyunIot.gateway#getTopo()
- aliyunIot.gateway#removeTopo()
- aliyunIot.gateway#login()
- aliyunIot.gateway#logout()
- aliyunIot.gateway#postSubDeviceProps()
- aliyunIot.gateway#postSubDeviceEvent()
- aliyunIot.gateway#serveSubDeviceService()
- aliyunIot.signUtil()
aliyunIot.device(options)
和云端建立连接,返回一个 Device
连接实例,入参:
options
productKey
(String
)deviceName
(String
)deviceSecret
(String
)regionId
(String
) 阿里云 regionIdtls
(Bool
) 是否开启 TLS 加密,Node.js 中如果开启将使用 TLS 协议进行连接,浏览器如果开启将上使用 WSS 协议
Event 'connect'
function(connack) {}
当连接到云端成功时触发。
Event 'message'
function(topic, message) {}
当接受到云端消息时触发,回调函数参数:
topic
消息主题message
消息 payload
Event 'error'
function(error) {}
当设备不能连接到云端的时候触发。
aliyunIot.device#publish(topic, message, options, callback)
等同于 mqtt.Client#publish() 方法。
aliyunIot.device#unsubscribe(topic, callback)
等同于 mqtt.Client#unsubscribe() 方法。
aliyunIot.device#postProps(params, callback)
上报物模型属性:
params
属性参数,Object
类型callback
err
错误,比如超时或者res.code !== 200
res
服务端 reply 消息内容
aliyunIot.device#postEvent(eventIdentifier, params, callback)
上报物模型事件:
eventIdentifier
事件 idString
类型params
事件参数,Object
类型callback
err
错误,比如超时res
服务端 reply 消息内容
aliyunIot.device#serve(seviceIdentifier, callback)
监听物模型服务:
seviceIdentifier
服务 idString
类型callback
params
服务参数
值得注意的是,serve
方法返回的是一个 deServe
函数,可以通过调用 deServe
函数取消服务监听:
var deServe = device.serve('turnOn', function(params) {
// 收到 turnOn 服务消息后取消对该服务的监听
deServe();
});
aliyunIot.gateway(options)
和云端建立连接,返回一个网关 Gateway
类连接实例,继承自 Device
类。
options
设备激活凭证productKey
deviceName
deviceSecret
aliyunIot.gateway#addTopo(deviceSign, callback)
添加子设备到拓扑
deviceSign
子设备身份加签信息,通过调用 aliyunIot.signUtil() 方法进行加签callback
err
错误,比如超时或者res.code !== 200
res
服务端 reply 消息内容
aliyunIot.gateway#getTopo()
添加子设备到拓扑关系
callback
err
错误,比如超时或者res.code !== 200
res
服务端 reply 消息内容
aliyunIot.gateway#removeTopo(thingId, callback)
从拓扑关系里移除子设备
thingId
子设备身份productKey
deviceName
callback
err
错误,比如超时或者res.code !== 200
res
服务端 reply 消息内容
aliyunIot.gateway#login(thingId, callback)
子设备上线
deviceSign
子设备身份加签信息,通过调用 aliyunIot.signUtil() 方法进行加签callback
err
错误,比如超时或者res.code !== 200
res
服务端 reply 消息内容
aliyunIot.gateway#logout(thingId, callback)
子设备下线
thingId
子设备身份productKey
deviceName
callback
err
错误,比如超时或者res.code !== 200
res
服务端 reply 消息内容
aliyunIot.gateway#postSubDeviceProps(thingId, params, callback)
thingId
子设备身份productKey
deviceName
params
属性参数,类型Object
callback
err
错误,比如超时或者res.code !== 200
res
服务端 reply 消息内容
aliyunIot.gateway#postSubDeviceEvent(thingId, eventIdentifier, params, callback)
thingId
子设备身份productKey
deviceName
eventIdentifier
事件 id,String
类型params
事件参数,Object
类型callback
err
错误,比如超时或者res.code !== 200
res
服务端 reply 消息内容
aliyunIot.gateway#serveSubDeviceService(thingId, serviceIdentifier, callback)
thingId
子设备身份productKey
deviceName
serviceIdentifier
服务 id,String
类型callback
params
服务参数
aliyunIot.signUtil(deviceId)
设备身份连接加签工具函数
deviceId
设备激活凭证productKey
deviceName
deviceSecret