![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
aliyun-iot-device-sdk
Advanced tools
使用 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!');
});
device.publish('/<productKey>/<deviceName>/update', 'hello world!');
device.subscribe('/<productKey>/<deviceName>/get');
device.on('message', (topic, payload) => {
console.log(topic, payload.toString());
});
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) {
// 处理服务参数
});
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()
和云端建立连接,返回一个 Device
连接实例,入参:
options
productKey
(String
)deviceName
(String
)deviceSecret
(String
)regionId
(String
) 阿里云 regionIdtls
(Bool
) 是否开启 TLS 加密,Node.js 中如果开启将使用 TLS 协议进行连接,浏览器如果开启将上使用 WSS 协议'connect'
function(connack) {}
当连接到云端成功时触发。
'message'
function(topic, message) {}
当接受到云端消息时触发,回调函数参数:
topic
消息主题message
消息 payload'error'
function(error) {}
当设备不能连接到云端的时候触发。
等同于 mqtt.Client#publish() 方法。
等同于 mqtt.Client#unsubscribe() 方法。
上报物模型属性:
params
属性参数,Object
类型callback
err
错误,比如超时或者 res.code !== 200
res
服务端 reply 消息内容上报物模型事件:
eventIdentifier
事件 id String
类型params
事件参数,Object
类型callback
err
错误,比如超时res
服务端 reply 消息内容监听物模型服务:
seviceIdentifier
服务 id String
类型callback
params
服务参数值得注意的是,serve
方法返回的是一个 deServe
函数,可以通过调用 deServe
函数取消服务监听:
var deServe = device.serve('turnOn', function(params) {
// 收到 turnOn 服务消息后取消对该服务的监听
deServe();
});
和云端建立连接,返回一个网关 Gateway
类连接实例,继承自 Device
类。
options
设备激活凭证
productKey
deviceName
deviceSecret
添加子设备到拓扑
deviceSign
子设备身份加签信息,通过调用 aliyunIot.signUtil()
方法进行加签callback
err
错误,比如超时或者 res.code !== 200
res
服务端 reply 消息内容添加子设备到拓扑关系
thingId
子设备身份
productKey
deviceName
callback
err
错误,比如超时或者 res.code !== 200
res
服务端 reply 消息内容从拓扑关系里移除子设备
thingId
子设备身份
productKey
deviceName
callback
err
错误,比如超时或者 res.code !== 200
res
服务端 reply 消息内容子设备上线
deviceSign
子设备身份加签信息,通过调用 aliyunIot.signUtil()
方法进行加签callback
err
错误,比如超时或者 res.code !== 200
res
服务端 reply 消息内容子设备下线
thingId
子设备身份
productKey
deviceName
callback
err
错误,比如超时或者 res.code !== 200
res
服务端 reply 消息内容thingId
子设备身份
productKey
deviceName
params
属性参数,类型 Object
callback
err
错误,比如超时或者 res.code !== 200
res
服务端 reply 消息内容thingId
子设备身份
productKey
deviceName
eventIdentifier
事件 id,String
类型params
事件参数,Object
类型callback
err
错误,比如超时或者 res.code !== 200
res
服务端 reply 消息内容thingId
子设备身份
productKey
deviceName
serviceIdentifier
服务 id,String
类型callback
params
服务参数设备身份连接加签工具函数
deviceId
设备激活凭证
productKey
deviceName
deviceSecret
FAQs
aliyun iot device sdk
We found that aliyun-iot-device-sdk demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.