npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

qcloud-virtual-device

v0.3.0

Published

qcloud-virtual-device 用于模拟一个建立在腾讯云物联网开发平台上的虚拟设备,提供上报属性,事件,响应action等一系列物模型相关的操作。去了解[物模型](https://cloud.tencent.com/document/product/1081/34916)

Downloads

5

Readme

qcloud-virtual-device

qcloud-virtual-device 用于模拟一个建立在腾讯云物联网开发平台上的虚拟设备,提供上报属性,事件,响应action等一系列物模型相关的操作。去了解物模型

安装

npm i qcloud-virtual-device # yarn add qcloud-virtual-device

GET STARTED

创建一个普通设备

const { VirtualDevice } = require('qcloud-virtual-device');

// 传入设备三元组信息,实例化一个device
const device = new VirtualDevice({
  productId: 'your_productId',
  deviceName: 'your_device_name',
  deviceSecret: 'your_device_secret',
});

device.on('connect', () => {
  // 获取云端最新的物模型数据
  device.getStatus({
    type: 'report'
  }).then((v) => {
    deviceData = v.data.reported;
    console.log('deviceData', deviceData);
  });
});

// 连接到云端
device.connect();

完整例子可以参看demo;

创建一个网关设备

const { GatewayDevice } = require('qcloud-virtual-device');

const device = new GatewayDevice({
  productId: 'your_productId',
  deviceName: 'your_device_name',
  deviceSecret: 'your_device_secret',
});

device.on('connect', async () => {
  // 获取网关设备下的子设备
  const subDevices = await device.getSubDevices();
  console.log(subDevices);
  // 绑定一个子设备
  device.bindSubDevice({
    deviceName: 'dev_1',
    productId: '1JTGFHHTR9',
    deviceSecret: 'deviceSecret'
  });
});

// 连接到云端
device.connect();

API

device.clientToken()

生成一个clientToken, 用于消息上报和消息响应的配对

clientToken(): string;

device.connect(url?: string, options)

将虚拟设备连接到云端, 默认使用mqtt://${productId}.iotcloud.tencentdevices.com作为mqtt URL,

connect(url?: string, options?: Omit<mqtt.IClientOptions, 'username' | 'password'>): mqtt.MqttClient;

在 options中可以传入 keepalive, reconnectPeriod等参数,详细介绍可参考: https://github.com/mqttjs/MQTT.js#client

device.reportProperty(payload)

设备往云端上报属性,返回一个Promise,可以拿到report_reply中的消息来判断是否上报成功

reportProperty(payload: Record<string, any>): Promise;

device.postEvent(payload)

发送一个物模型事件到云端,可以选info, warn, fault三种事件类型

  device.postEvent({
    eventId: 'open_door',
    type: 'info',
    params: {result: 1}
  });

device.replyAction(payload)

对小程序的action指令进行回复, 通常和 onAction 一起使用

device.onAction('add_user', ({ clientToken, params }, reply) => {
  // ...一系列添加用户的操作
  device.replyAction({
    actionId: 'add_user',
    clientToken,
    response: { result: 1 }
  });
  // 或者直接使用包装好的 reply 参数,并传入 response
  reply({ result: 1 });
});

device.on('connect', () => {})

设备连接到云端触发connect事件

device.on('error', (error) => {})

设备出现错误触发error事件

device.on('action', ({ actionId, clientToken, timestamp, params}) => {})

设备收到来自控制端的 action 时触发

device.onAction(actionId, ({params}, reply) => {})

监听一个特定的action, 在回调函数中,可以通过params获取 action 传入的参数,可以通过 reply 对 action 进行回复。

device.onControl({ clientToken: string; params: any }) => {})

当客户端下发控制指令时触发,回调函数中可以通过 params 获取发生变化的物模型参数

device.client: mqtt.MqttClient | null

设备连接到云端后,可以通过device.client获得 mqttClient