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

tencent-iot-js-sdk

v0.2.11

Published

tencent cloud iot js sdk

Downloads

23

Readme

腾讯云 IoT JS SDK

这个 SDK 的目标是同时工作于以下环境:

  • 小程序。使用 dist/iot_sdk.miniprogram.js
  • Node.js。直接 require 此包
  • 浏览器 or React Native。使用 dist/iot_sdk.browser.js

安装

$ npm i tencent-iot-js-sdk

示例

const SDK = require('tencent-iot-js-sdk')

async function main() {
  const sdk = new SDK({
    // 从控制台申请一个应用,会得到 AppKey
    AppKey: 'xxxxxxx',
  });

  // 接收服务器的推送
  sdk.onPush(function (data) {
    console.log(`onPush %j`, data)
  })
  
  // 调用云api方法登录
  let response = await sdk.callYunApi({
    Action: 'AppGetToken',
    ActionParams: {
      UserName: 'iotappuser',
      Password: 'xxxx',
    }
  })
  // 获取登录令牌
  const AccessToken = response.AccessToken
  
  // 绑定登录令牌
  sdk.bindAccessToken(AccessToken);
  
  // 获取用户名下的设备列表
  response = await sdk.callYunApi({
    Action: 'AppGetDevices',
    ActionParams: {
    }
  })
  
  console.log(`AppGetDevices %j`, response)
    
}

main()

方法

constructor

AppKey:每个app都有对应的 AppKey,在控制台的应用管理中的 secretId 就是 AppKey。一个app只能操作它有权限的产品,不能跨产品操作。

AccessToken:登录之后服务器会返回 AccessToken,用于标记一个用户的会话。这个参数也可以后面再通过 bindAccessToken 传入。

new SDK({
  AppKey: 'xxxx',
  AccessToken: 'yyyyyy',
})

callYunApi(options)

  • options.Action string 云方法名
  • options.ActionParams object 方法对应的参数
  • options.WithAccessToken boolean 默认true。参考.bindAccessToken方法

Version 默认为 2018-01-23

云api的文档在:https://cloud.tencent.com/document/product/568/16436

bindAccessToken(AccessToken)

登录之后,调用这个接口绑定 AccessToken。这样在调用云api任何接口时,都会自动传 AccessToken 参数。如果需要暂时屏蔽这个特性,则在 callYunApi 时,传入 options.WithAccessToken=false

登录方式有多种,请参照 API 文档中的登录部分,并通过 callYunApi 进行登录。

activePush()

sdk 会在以下两种情况下自动绑定 websocket 与设备推送的映射关系:

  1. websocket 连接建立时
  2. bindAccessToken 时。

一般是够用的,但有些情况下,需要开发者手动调用 activePush() 来绑定:

  1. 用户添加了新设备时,新设备并不会与 websocket 自动绑定。

onPush(dataObj)

接收服务器推送的各种消息,通过 action 来标记不同消息类型。

目前已有的action:

  1. DeviceChange 设备上下线状态发生改变(type=='upAndDown')。或设备数据发生改变(type=='dataChange')。这时应用应主动刷新设备状态。

action: 'DeviceChange'

当设备数据变更或者设备上下线时,

dataObj.action === 'DeviceChange'

dataObj.params 的形式为

{
  type: changeType, // 'dataChange' or 'upAndDown'
  time: time, // 2018-05-15 22:09:07
  deviceId: deviceId, // iot-ohmfh2cs@G10A01_kit_1_222
}

license

MIT