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-smartiot

v0.2.17

Published

tencent cloud smartiot js sdk

Downloads

8

Readme

腾讯云 IoT JS SDK

环境

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

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

安装

$ npm i tencent-smartiot

示例

const SDK = require('tencent-smartiot')

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

  // 调用云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 传入。

wsOptions:针对sdk封装的ws客户端进行配置,请看源码中的 iot_web_socket.js 文件。

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

默认的超时时间为5s,超时会抛错。

bindAccessToken(AccessToken)

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

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

activePush()

bindAccessToken 之后,即可调用此方法来开启服务端推送。 每当用户名下的设备发生数据变动时,则会推送消息过来。

onPush 方法结合使用

onPush(fn)

当服务端推送消息时,会调用 fn(pushEvent),pushEvent格式如下

({ 
  action: 'DeviceChange',
  params: { 
    Type: 'control',
    Ts: 1541512121555,
    ProductId: 'iot-b87oxqbs',
    DeviceName: 'test-app-device',
    PIDN: 'iot-b87oxqbs/test-app-device',
    Data: { smart6: 1 }
  },
  push: true 
})

license

MIT