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 🙏

© 2026 – Pkg Stats / Ryan Hefner

kaihong-hyper-device

v0.0.13

Published

kaihong-hyper-device

Readme

超级设备

kaihong-hyper-device

  • 支持两种协议:http、websocket
  • 通过 http 获取当前用户下产品、设备信息
  • 通过 websocket 协议获取用户订阅的设备实时上报信息
  • 后续考虑支持用户写设备数据

SDK 接入

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>xxx 应⽤系统</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
    <script type="text/javascript">
      window.onload = function () {
        // set localStorage.setItem('X-Access-Token', token);
        // 初始化开鸿HyperDevice对象,实例化时主要去做鉴权操作
        const hyperDevice = new HyperDevice({
          // secure: false,
          // host: location.host,
          // basename: "",
        });
      };
    </script>
  </head>
  <body>
    <div>获取设备消息</div>
  </body>
</html>

参数说明

| 参数名 | 是否必填 | 参数说明 | | ---------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | secure | 否 | 开鸿服务器网站是否启用 SSL 证书,http(s)或 ws(s)访问对应的接口;默认随当前前端部署环境,读取 location.protocol,如果配置了 true 或 false 则强制配置 https 或 http 前缀 | | host | 否 | 接口域名,默认使用 location.host,私有化部署时需要 | | basename | 否 | 一般接口都是根域名部署的,比如 http://www.baidu.com/api, 如果是根域名加路径访问,需要配置此参数(比如配置/children),则访问 http://www.baidu.com/children/api | | targetHost | 否 | 用于配置 https 环境能正常访问 http 接口的入口:比如 targetHost: http://dev.kaihong.com |

API - 实例方法

HTTP 相关方法

getDeviceList(params, callback)

获取设备列表信息;

| 参数名 | 说明 | 类型 | 是否必填 | 版本 | | -------- | ---------------------------------------------------------------------------------------------------- | -------- | -------- | ----- | | params | params 中传入 productId,可查询该产品下的设备列表 { productId: xxx }, 传入 null 查询普通设备 | object | 是 | 1.0.0 | | callback | ({ code, message, data }) => void; code 为 200 时,处理正常业务逻辑其他情况提示错误信息 message | function | 是 | 1.0.0 |

getDeviceDetail(deviceId, callback)

获取设备详情信息;

| 参数名 | 说明 | 类型 | 是否必填 | 版本 | | -------- | ---------------------------------------------------------------------------------------------------- | -------- | -------- | ----- | | deviceId | 设备 ID | string | 是 | 1.0.0 | | callback | ({ code, message, data }) => void; code 为 200 时,处理正常业务逻辑其他情况提示错误信息 message | function | 是 | 1.0.0 |

getProductList(params, callback)

获取设备列表信息;

| 参数名 | 说明 | 类型 | 是否必填 | 版本 | | -------- | ---------------------------------------------------------------------------------------------------- | -------- | -------- | ----- | | params | 预设的后续支持传入分页或查询参数,前期可传入 null | object | 是 | 1.0.0 | | callback | ({ code, message, data }) => void; code 为 200 时,处理正常业务逻辑其他情况提示错误信息 message | function | 是 | 1.0.0 |

getProductDetail(productId, callback)

获取设备详情信息;

| 参数名 | 说明 | 类型 | 是否必填 | 版本 | | --------- | ---------------------------------------------------------------------------------------------------- | -------- | -------- | ----- | | productId | 产品 ID | string | 是 | 1.0.0 | | callback | ({ code, message, data }) => void; code 为 200 时,处理正常业务逻辑其他情况提示错误信息 message | function | 是 | 1.0.0 |

执行设备功能

invokedFunction({ deviceId, functionId, data }, callback)

// 调用接口
// http://dev.iot.kaihong.com/api/device/invoked/${deviceId}/function/${functions.id} 传参: { functions[x].inputs.id: 65 }
// 比如风机设备设置转速

// method: POST
// url: http://dev.iot.kaihong.com/api/device/invoked/3d-preview-child/function/setSpeed
// data: { rotationRate: 65 }

// 实例调用
invokedFunction(
  {
    deviceId: "3d-preview-child",
    functionId: "setSpeed",
    data: { rotationRate: 65 },
  },
  ({ code = 200, message = "", data = null }) => {
    console.log("code: ", code, ", message: ", message, ", data: ", data);
  }
);

修改设备属性

modifyDeviceProperty({ deviceId, data }, callback)

// 调用接口
// http://dev.iot.kaihong.com/api/device-instance/${deviceId}/property 传参: { [property]: [propertyValue], ... }
// 比如风机设备设置转速

// method: PUT
// url: http://dev.iot.kaihong.com/api/device-instance/3d-preview-child/property
// data: { rotationRate: 65 }

// 实例调用
modifyDeviceProperty(
  {
    deviceId: "3d-preview-child",
    data: { rotationRate: 65 },
  },
  ({ code = 200, message = "", data = null }) => {
    console.log("code: ", code, ", message: ", message, ", data: ", data);
  }
);

设备事件监听(待开发)

  1. 突发事件
  2. 上报自定义事件

对设备物模型中的自定义事件进行监听动作;

WebSocket 相关方法

connect(callback)

建立 websocket 连接,在 callback 回调中进行设备上下线监听,属性变化监听等动作;

subscribeTopic(params, callback)

自定义订阅 topic,Topic 列表,返回一个包含 cancel 的对象,用于取消 callback 函数的监听

const { cancel } = subscribeTopic(params, callback);

// cancel();

| 参数名 | 说明 | 类型 | 是否必填 | 版本 | | -------- | ----------------------------------------------------------------------------------------------------------------- | ------------- | -------- | ----- | | params | { id, topic, parameter }, id 确保唯一,用于注册回调函数;topic 参考 Topic 列表链接;parameter 为 topic 相关的传参 | string/object | 是 | 1.0.0 | | callback | (payload) => void; // 响应主体数据- payload | function | 是 | 1.0.0 |

onDeviceOnline(params, callback)

设备上线监听,返回一个包含 cancel 的对象,用于取消 callback 函数的监听

const { cancel } = onDeviceOnline(params, callback);

// cancel();

| 参数名 | 说明 | 类型 | 是否必填 | 版本 | | -------- | ------------------------------------------------------------------------------------------------------------------------------------------ | ------------- | -------- | ----- | | params | 单独传 string 表示设备 id,或者传对象 { deviceId, debounce, debounceTime } , debounce 传 true 表示启用防抖,debounceTime 默认 500,单位 ms | string/object | 是 | 1.0.0 | | callback | () => void; | function | 是 | 1.0.0 |

onDeviceOffline(params, callback)

设备下线监听,返回一个包含 cancel 的对象,用于取消 callback 函数的监听

const { cancel } = onDeviceOffline(params, callback);

// cancel();

| 参数名 | 说明 | 类型 | 是否必填 | 版本 | | -------- | ------------------------------------------------------------------------------------------------------------------------------------------ | ------------- | -------- | ----- | | params | 单独传 string 表示设备 id,或者传对象 { deviceId, debounce, debounceTime } , debounce 传 true 表示启用防抖,debounceTime 默认 500,单位 ms | string/object | 是 | 1.0.0 | | callback | () => void; | function | 是 | 1.0.0 |

onDevicePropertyChange({ deviceId, properties }, callback)

监听设备属性变化,返回一个包含 cancel 的对象,用于取消 callback 函数的监听

const { cancel } = onDevicePropertyChange({ deviceId, properties }, callback);

// cancel();

| 参数名 | 说明 | 类型 | 是否必填 | 版本 | | ---------- | ---------------------------------------------- | -------- | -------- | ----- | | deviceId | 设备 id | string | 是 | 1.0.0 | | properties | 不传监听所有属性,传了只监听具体某个或几个属性 | string[] | 否 | 1.0.0 | | callback | (payload) => void; // 响应主体数据- payload | function | 是 | 1.0.0 |

disconnect()

主动断开 websocket 连接

onError(callback)

监听 websocket 相关的报错信息

| 参数名 | 说明 | 类型 | 是否必填 | 版本 | | -------- | ---------------------------------------------------------- | -------- | -------- | ----- | | callback | callback: (errorEvent: WebSocketEventMap["error"]) => void | function | 是 | 1.0.0 |

onClose(callback)

监听 websocket 连接关闭之后触发的回调

| 参数名 | 说明 | 类型 | 是否必填 | 版本 | | -------- | ---------------------------------------------------------- | -------- | -------- | ----- | | callback | callback: (errorEvent: WebSocketEventMap["close"]) => void | function | 是 | 1.0.0 |

快速上⼿

安装

CDN

<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

NPM / YARN

## npm
npm install kaihong-hyper-device
## 或yarn
yarn add kaihong-hyper-device

使⽤

非 webpack 构建的项⽬
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>xxx 应⽤系统</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
    <script type="text/javascript">
      // 初始化开鸿HyperDevice对象,实例化时主要去做鉴权操作
      const hyperDevice = new HyperDevice({
        // secure: false,
        // host: location.host,
        // basename: "",
      });
    </script>
  </head>
  <body>
    <div>--设备接入--</div>
  </body>
</html>

React 项⽬

// src/pages/device/index.tsx
import { Button } from "antd";
import { HyperDevice } from "kaihong-hyper-device";
function DeviceList() {
  // set localStorage.setItem('X-Access-Token', token);
  // 初始化开鸿HyperDevice对象,实例化时主要去做鉴权操作
  const hyperDevice = new HyperDevice({
    // secure: false,
    // host: location.host,
    // basename: "",
  });

  // http
  hyperDevice.getDeviceList(
    null,
    ({ code = 200, message = "", data = null }) => {
      console.log("code: ", code, ", data:", data);
    }
  );

  hyperDevice.getDeviceDetail(
    "3d-preview-child",
    ({ code = 200, message = "", data = null }) => {
      console.log("code: ", code, ", data:", data);
    }
  );

  hyperDevice.getProductList(
    null,
    ({ code = 200, message = "", data = null }) => {
      console.log("code: ", code, ", data:", data);
    }
  );

  hyperDevice.getProductDetail(
    "wind_mill",
    ({ code = 200, message = "", data = null }) => {
      console.log("code: ", code, ", data:", data);
    }
  );

  // ws
  hyperDevice.connect(() => {
    // 监听设备上线
    hyperDevice.onDeviceOnline("3d-preview-child", () => {
      console.log("设备3d-preview-child: 上线了");
    });
    // 监听设备下线
    hyperDevice.onDeviceOffline("3d-preview-child", () => {
      console.log("设备3d-preview-child: 下线了");
    });
    // 监听属性变化
    const devicePropertyChange1 = hyperDevice.onDevicePropertyChange(
      {
        deviceId: "3d-preview-child",
        productId: "wind_mill",
        properties: ["rotationRate", "fanRunStatus"],
      },
      (value) => {
        console.log("onDevicePropertyChange value: ", value);
      }
    );
    // devicePropertyChange1.cancel();
  });
  // hyperDevice.disconnect(); - 断开连接

  // 监听error事件
  hyperDevice.onError((errorEvent) => {
    console.log("onError: ", errorEvent);
  });
  // 监听close事件
  hyperDevice.onClose((closeEvent) => {
    console.log("closeEvent: ", closeEvent);
  });
  return <Button onClick={handleClick}>获取设备列表</Button>;
}

export default DeviceList;