aliyun-iot-device-sdk
Advanced tools
Comparing version 0.0.3 to 0.2.3
{ | ||
"name": "aliyun-iot-device-sdk", | ||
"version": "0.0.3", | ||
"description": "阿里云IoT物联网套件客户端", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "nyc mocha", | ||
"coverage": "nyc report --reporter=html", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/xihu-fm/aliyun-iot-client-sdk.git" | ||
}, | ||
"version": "0.2.3", | ||
"description": "", | ||
"keywords": [ | ||
"aliyun", | ||
"IoT", | ||
"aliyun-iot-mqtt", | ||
"iot-hub" | ||
"aliyunIot", | ||
"iot", | ||
"iotkit", | ||
"linkdevelop", | ||
"bone" | ||
], | ||
"author": "wongxming", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/xihu-fm/aliyun-iot-client-sdk/issues" | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"lib": "babel src -d lib", | ||
"dev": "babel src -w -d lib", | ||
"test": "jest --coverage ./test/", | ||
"test:dev": "jest --coverage --watch ./test/", | ||
"rap": | ||
"rm -rf rap && mkdir rap && cp -R lib rap && cp package.json rap && cp README.md rap && cd rap && rap publish" | ||
}, | ||
"engines": { | ||
"node": ">= 7.6" | ||
"jest": { | ||
"verbose": true, | ||
"moduleDirectories": ["node_modules"] | ||
}, | ||
"readmeFilename": "README.md", | ||
"author": "xhowhy@qq.com", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"jest": "^22.0.4" | ||
}, | ||
"dependencies": { | ||
"mqtt": "^2.14.0" | ||
"mqtt": "^2.15.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^4.0.1", | ||
"nyc": "^11.3.0", | ||
"coveralls": "^3.0.0", | ||
"chai": "^4.1.2" | ||
"ruff": { | ||
"dependencies": { | ||
"mqtt": "^0.1.3" | ||
} | ||
} | ||
} |
331
README.md
@@ -1,73 +0,322 @@ | ||
## aliyun-iot-mqtt | ||
# Aliyun IoT Device SDK for Javascript(非官方) | ||
[![npm-version](https://img.shields.io/npm/v/aliyun-iot-mqtt.svg)](https://npmjs.org/package/aliyun-iot-mqtt) | ||
[![travis-ci](https://travis-ci.org/xihu-fm/aliyun-iot-mqtt.svg?branch=master)](https://travis-ci.org/xihu-fm/aliyun-iot-mqtt) | ||
[![coverage](https://coveralls.io/repos/github/xihu-fm/aliyun-iot-mqtt/badge.svg?branch=master)](https://coveralls.io/github/xihu-fm/aliyun-iot-mqtt?branch=master) | ||
[![npm-download](https://img.shields.io/npm/dm/aliyun-iot-mqtt.svg)](https://npmjs.org/package/aliyun-iot-mqtt) | ||
> 使用 Javascript 将设备接入到阿里云 IoT 套件和 LinkDevelop。 | ||
[Aliyun IoT Hub](https://www.aliyun.com/product/iot) MQTT client for Node.js | ||
## 安装 | ||
> 安装 Node.js 运行环境,版本 `>=4.0.0` 。 | ||
## Installation | ||
通过 npm 包管理工具安装: | ||
You can install it as dependency with npm. | ||
```bash | ||
npm install aliyun-iot-device-sdk --save | ||
``` | ||
```sh | ||
$ # save into package.json dependencies with -S | ||
$ npm install aliyun-iot-mqtt -S | ||
## 设备接入云端 | ||
```javascript | ||
var aliyunIot = require('aliyun-iot-device-sdk'); | ||
var device = aliyunIot.device({ | ||
productKey: '<productKey>', | ||
deviceName: '<deviceName>', | ||
deviceSecret: '<deviceSecret>' | ||
}); | ||
device.on('connect', () => { | ||
console.log('connect successfully!'); | ||
}); | ||
``` | ||
## Usage | ||
## IoT 套件基础版 API | ||
Aliyun IoT Hub mqtt client with authrozied by productKey & deviceName & deviceSecret. | ||
## 设备上报数据 | ||
```javascript | ||
device.publish('/a1E365xyP2X/testdevice/update', 'hello world!'); | ||
``` | ||
### GET Data | ||
## 云端下行消息监听 | ||
```js | ||
const Mqtt = require('aliyun-iot-mqtt'); | ||
```javascript | ||
device.subscribe('/a1E365xyP2X/testdevice/get'); | ||
const client = Mqtt.getAliyunIotMqttClient({ | ||
productKey: "", | ||
deviceName: "", | ||
deviceSecret: "", | ||
keepalive:120 // mqtt options | ||
device.on('message', (topic, payload) => { | ||
console.log(topic, payload.toString()); | ||
}); | ||
``` | ||
## Link Develop 和 IoT 套件高级版 API | ||
client.on('connect', function() { | ||
console.log("connect") | ||
}) | ||
IoT 套件高级版封装了物模型定义与 Alink 异步协议,SDK 封装使得设备与云端通信时不需要关心 MQTT topic,只需要调用属性上报(<a href="#postProps"><code>aliyunIot.device#<b>postProps()</b></code></a>)、服务监听(<a href="#serve"><code>aliyunIot.device#<b>serve()</b></code></a>)、事件上报(<a href="#postEvent"><code>aliyunIot.device#<b>postEvent()</b></code></a>)等相关 API。 | ||
client.end(function (){ | ||
console.log("end") | ||
}) | ||
### 高级版 API 调用示例:设备属性上报 | ||
```javascript | ||
device.postProps({ | ||
CurrentTemperature: 25 | ||
}); | ||
``` | ||
### Subscribe Topic | ||
调用 `device.postProps()` 等同于执行以下代码: | ||
```js | ||
client.subscribe(topic) | ||
```javascript | ||
// 消息 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); | ||
} | ||
}); | ||
``` | ||
### Publish Message | ||
```js | ||
client.publish(topic, 'Hello mqtt') | ||
client.publish(topic, 'Hello mqtt', { qos: 1 }) | ||
### 监听云端下发的服务消息 | ||
```javascript | ||
// 监听云端设置属性服务消息 | ||
device.serve('property/set', function(params) { | ||
// 处理服务参数 | ||
}); | ||
``` | ||
### Receive Message | ||
## API | ||
```js | ||
client.on('message', function(topic, message) { | ||
console.log(topic+"," + message.toString()) | ||
}) | ||
* <a href="#device"><code>aliyunIot.<b>device()</b></code></a> | ||
* <a href="#publish"><code>aliyunIot.device#<b>publish()</b></code></a> | ||
* <a href="#subscribe"><code>aliyunIot.device#<b>subscribe()</b></code></a> | ||
* <a href="#postProps"><code>aliyunIot.device#<b>postProps()</b></code></a> | ||
* <a href="#postEvent"><code>aliyunIot.device#<b>postEvent()</b></code></a> | ||
* <a href="#serve"><code>aliyunIot.device#<b>serve()</b></code></a> | ||
* <a href="#gateway"><code>aliyunIot.<b>gateway()</b></code></a> | ||
* <a href="#addTopo"><code>aliyunIot.gateway#<b>addTopo()</b></code></a> | ||
* <a href="#getTopo"><code>aliyunIot.gateway#<b>getTopo()</b></code></a> | ||
* <a href="#removeTopo"><code>aliyunIot.gateway#<b>removeTopo()</b></code></a> | ||
* <a href="#login"><code>aliyunIot.gateway#<b>login()</b></code></a> | ||
* <a href="#logout"><code>aliyunIot.gateway#<b>logout()</b></code></a> | ||
* <a href="#postSubDeviceProps"><code>aliyunIot.gateway#<b>postSubDeviceProps()</b></code></a> | ||
* <a href="#postSubDeviceEvent"><code>aliyunIot.gateway#<b>postSubDeviceEvent()</b></code></a> | ||
* <a href="#serveSubDeviceService"><code>aliyunIot.gateway#<b>serveSubDeviceService()</b></code></a> | ||
* <a href="#signUtil"><code>aliyunIot.<b>signUtil()</b></code></a> | ||
<a name="device"></a> | ||
### aliyunIot.device(options) | ||
和云端建立连接,返回一个 `Device` 连接实例,入参: | ||
* `options` 设备三元组 | ||
* `productKey` | ||
* `deviceName` | ||
* `deviceSecret` | ||
### Event `'connect'` | ||
`function(connack) {}` | ||
当连接到云端成功时触发。 | ||
### Event `'message'` | ||
`function(topic, message) {}` | ||
当接受到云端消息时触发,回调函数参数: | ||
* `topic` 消息主题 | ||
* `message` 消息 payload | ||
### Event `'error'` | ||
`function(error) {}` | ||
当设备不能连接到云端的时候触发。 | ||
<a name="publish"></a> | ||
### aliyunIot.device#publish(topic, message, [options], [callback]) | ||
等同于 [mqtt.Client#publish()](https://github.com/mqttjs/MQTT.js/blob/master/README.md#publish) 方法。 | ||
<a name="unsubscribe"></a> | ||
### aliyunIot.device#unsubscribe(topic, [callback]) | ||
等同于 [mqtt.Client#unsubscribe()](https://github.com/mqttjs/MQTT.js/blob/master/README.md#unsubscribe) 方法。 | ||
<a name="postProps"></a> | ||
### aliyunIot.device#postProps(params, [callback]) | ||
上报物模型属性: | ||
* `params` 属性参数,`Object` 类型 | ||
* `callback` | ||
* `err` 错误,比如超时或者 `res.code !== 200` | ||
* `res` 服务端 reply 消息内容 | ||
<a name="postEvent"></a> | ||
### aliyunIot.device#postEvent(eventIdentifier, params, [callback]) | ||
上报物模型事件: | ||
* `eventIdentifier` 事件 id `String` 类型 | ||
* `params` 事件参数,`Object` 类型 | ||
* `callback` | ||
* `err` 错误,比如超时 | ||
* `res` 服务端 reply 消息内容 | ||
<a name="serve"></a> | ||
### aliyunIot.device#serve(seviceIdentifier, [callback]) | ||
监听物模型服务: | ||
* `seviceIdentifier` 服务 id `String` 类型 | ||
* `callback` | ||
* `params` 服务参数 | ||
值得注意的是,`serve` 方法返回的是一个 `deServe` 函数,可以通过调用 `deServe` 函数取消服务监听: | ||
```javascript | ||
var deServe = device.serve('turnOn', function(params) { | ||
// 收到 turnOn 服务消息后取消对该服务的监听 | ||
deServe(); | ||
}); | ||
``` | ||
### Bugs | ||
<a name="gateway"></a> | ||
<img src='https://raw.githubusercontent.com/wongxming/dtalkNodejs/master/wongxming.jpg' width="240" height="240" /> | ||
### aliyunIot.gateway(options) | ||
和云端建立连接,返回一个网关 `Gateway` 类连接实例,继承自 `Device` 类。 | ||
* `options` 设备激活凭证 | ||
* `productKey` | ||
* `deviceName` | ||
* `deviceSecret` | ||
<a name="addTopo"></a> | ||
### aliyunIot.gateway#addTopo(deviceSign, [callback]) | ||
添加子设备到拓扑 | ||
* `deviceSign` 子设备身份加签信息,通过调用 <a href="#signUtil"><code>aliyunIot.<b>signUtil()</b></code></a> 方法进行加签 | ||
* `callback` | ||
* `err` 错误,比如超时或者 `res.code !== 200` | ||
* `res` 服务端 reply 消息内容 | ||
<a name="getTopo"></a> | ||
### aliyunIot.gateway#getTopo(thingId, [callback]) | ||
添加子设备到拓扑关系 | ||
* `thingId` 子设备身份 | ||
* `productKey` | ||
* `deviceName` | ||
* `callback` | ||
* `err` 错误,比如超时或者 `res.code !== 200` | ||
* `res` 服务端 reply 消息内容 | ||
<a name="removeTopo"></a> | ||
### aliyunIot.gateway#removeTopo(thingId, [callback]) | ||
从拓扑关系里移除子设备 | ||
* `thingId` 子设备身份 | ||
* `productKey` | ||
* `deviceName` | ||
* `callback` | ||
* `err` 错误,比如超时或者 `res.code !== 200` | ||
* `res` 服务端 reply 消息内容 | ||
<a name="login"></a> | ||
### aliyunIot.gateway#login(thingId, [callback]) | ||
子设备上线 | ||
* `deviceSign` 子设备身份加签信息,通过调用 <a href="#signUtil"><code>aliyunIot.<b>signUtil()</b></code></a> 方法进行加签 | ||
* `callback` | ||
* `err` 错误,比如超时或者 `res.code !== 200` | ||
* `res` 服务端 reply 消息内容 | ||
<a name="logout"></a> | ||
### aliyunIot.gateway#logout(thingId, [callback]) | ||
子设备下线 | ||
* `thingId` 子设备身份 | ||
* `productKey` | ||
* `deviceName` | ||
* `callback` | ||
* `err` 错误,比如超时或者 `res.code !== 200` | ||
* `res` 服务端 reply 消息内容 | ||
<a name="postSubDeviceProps"></a> | ||
### aliyunIot.gateway#postSubDeviceProps(thingId, params, [callback]) | ||
* `thingId` 子设备身份 | ||
* `productKey` | ||
* `deviceName` | ||
* `params` 属性参数,类型 `Object` | ||
* `callback` | ||
* `err` 错误,比如超时或者 `res.code !== 200` | ||
* `res` 服务端 reply 消息内容 | ||
<a name="postSubDeviceEvent"></a> | ||
### aliyunIot.gateway#postSubDeviceEvent(thingId, eventIdentifier, params, [callback]) | ||
* `thingId` 子设备身份 | ||
* `productKey` | ||
* `deviceName` | ||
* `eventIdentifier` 事件 id,`String` 类型 | ||
* `params` 事件参数,`Object`类型 | ||
* `callback` | ||
* `err` 错误,比如超时或者 `res.code !== 200` | ||
* `res` 服务端 reply 消息内容 | ||
<a name="serveSubDeviceService"></a> | ||
### aliyunIot.gateway#serveSubDeviceService(thingId, serviceIdentifier, [callback]) | ||
* `thingId` 子设备身份 | ||
* `productKey` | ||
* `deviceName` | ||
* `serviceIdentifier` 服务 id,`String` 类型 | ||
* `callback` | ||
* `params` 服务参数 | ||
<a name="signUtil"></a> | ||
### aliyunIot.signUtil() | ||
设备身份连接加签工具函数 | ||
* `deviceId` 设备激活凭证 | ||
* `productKey` | ||
* `deviceName` | ||
* `deviceSecret` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
49284
3
19
1023
322
2
2
2
Updatedmqtt@^2.15.0