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

offline-server-ble-comm-kit

v1.0.3

Published

offline server ble swap comm sdk

Readme

概述

​ 该SDK 是为 微信小程序 平台提供的 【应急蓝牙换电】功能

1. 连接目标柜子,获取蓝牙接口所提示的deviceId   (这一步为可选,可自己编写)
2. 开始应急蓝牙换电
  	1. 向已连接设备 下发**应急换电**请求指令
  	2. 监听换电流程中的状态变化以及通信错误

1. 连接目标柜子,获取蓝牙接口所提供的deviceid (可选)

注意: 已连接设备所获取的 deviceId 与 柜子编码 是两个概念

| 属性 | 类型 | 默认值 | 必填 | 说明 | | ----------------- | -------- | ------ | ---- | ------------------------------------------------------------ | | dev_sn | string | | 是 | 待连接的设备编号 | | open_comm_retry | boolean | false | 否 | 是否开启设备连接失败的重试功能(即:会提示失败信息以及重试按钮) | | discovery_timeout | number | 5000 | 否 | 搜索设备的超时时长(单位:ms),最短不低于5秒,最长不高于60秒 | | conn_timeout | number | 5000 | 否 | 连接设备的超时时长(单位:ms),最短不低于5秒,最长不高于60秒 | | success | function | | 否 | 接口调用成功的回调函数(函数参数: 已连接设备所获取的deviceid) | | fail | function | | 否 | 接口调用失败的回调函数 |

 const {connectTargetDevice} = require("offline-server-ble-comm-kit");
 connectTargetDevice({
 	"dev_sn": "123445",
 	"open_comm_retry": true,
 	"success": (deviceId) => {
        //连接成功
 	},
 	"fail": (err)=>{
        //连接失败
 	}
 })

2. 开始应急蓝牙换电

​ 蓝牙连接设备成功后,即可开始向设备发起 应急换电 请求

  1. SwapServerHandler

    有关于应急换电的处理构造函数

| 属性 | 类型 | 说明 | | -------- | ------ | ---------------------------------------------- | | userId | string | 用户编号(目的:用于赋值于换电占指令中userId) | | devSn | string | 待换电的设备编号 | | deviceId | string | 设备连接成功所获取的deviceId 字段 |

  1. SwapServerHandler.startBleOfflineSwap(Object object)

    下发“应急换电”的指令请求

    | 属性 | 类型 | 默认值 | 必填 | 说明 | | ---------------- | -------- | ------ | ---- | ---------------------------------------------------------- | | user_id | number | | 是 | 用户id | | user_token_ts | number | | 是 | 用户套餐过期时间戳(单位:秒) | | user_token | string | | 是 | 用户token | | requset_time_out | number | 15 | 否 | 下发请求等待超时时长(单位:秒),不低于5秒,不高于15秒 | | time_out | number | 170 | 否 | 整个换电流程的超时时长(单位:秒),不低于60秒,不高于180秒 | | success | function | | 否 | 发送请求成功的回调//{"isSuccess": boolean, "code": number} | | fail | function | | 否 | 发送请求失败的回调//{code: number, message: string} |

    参数: success

    解释有关成功回调函数的参数值

    | 属性 | 类型 | 说明 | | --------- | ------- | ------------------------------------------------------------ | | isSuccess | boolean | 是否可以继续换电(例如:有些情况换电柜不满足换电的条件) | | code | number | 换电请求所回复的结果(即:对应着mqtt消息(501)中的 result 字段) |

  2. SwapServerHandler.listenSwapProcessStatus(function callback)

    监听换电过程中换电的状态变化;

    | 属性 | 类型 | 说明 | | ------ | ------ | -------------------------------------------------- | | status | number | 换电的状态(换电中,换电异常,换电失败,换电成功) | | result | number | 换电结果 |

  3. SwapServerHandler.listenBleCommError(function callback)

    监听蓝牙通信中的通信异常;

    | 属性 | 类型 | 说明 | | ------- | ------ | -------- | | errCode | number | | | errMsg | string | 异常描述 |


								/******以下例子仅供参考******/
	const {
    	SwapServerHandler,
    	SwapProcessStatusMap,
    	setConsoleLogDisplay,
        showConsoleLog
	} = require("offline-server-ble-comm-kit");
	/**
     *  下发“应急蓝牙换电”请求
     * @param {string} devSn 待换电的设备编号  
     * @param {string} deviceId 已连接成功后所获取的 deviceId
     * @param {*} options 
     * //以下是对象格式(注:以下有两个user_id,请注意区分)
     * {
     *  "user_id": string,	//用于拼接mqtt消息的字段
        "token_info": {
            "user_id": number,	//用于生成token的数字类型userId
            "user_token_ts": number,
            "user_token": string,
        },
        "time_out": 170,
     * }
     */
	function  startSwapRequest(devSn, deviceId, options) {
        let swapServerHandler = new SwapServerHandler(options["user_id"], devSn, deviceId);
        let tokenInfo = options["token_info"] || {};
        wx.showLoading({
            title: "换电准备中...",
        });
        //开启“应急蓝牙换电”请求
        swapServerHandler.startBleOfflineSwap({
            "user_id": tokenInfo["user_id"],
            "user_token_ts": tokenInfo["user_token_ts"],
            "user_token": tokenInfo["user_token"],
            "time_out": options["time_out"],
            "success": (res) => { //{"isSuccess": boolean, "code": number}
                wx.hideLoading();
                Log.log("[connAndStartBleOfflineSwap] start success info:", res);
                //@todo 发送换电请求成功
                this.listenSwapProcessStatus(swapServerHandler);
            },
            "fail": (err) => { //{code: number, message: string}
                wx.hideLoading(); 
                Log.warn("[connAndStartBleOfflineSwap] start offline swap fail:", err);
                //@todo 发起换电请求失败
            }
        });
        //监听蓝牙通信中的通信异常
        swapServerHandler.listenBleCommError((err) => {  //{errCode: number, errMsg: string}
            Log.error("[listenBleCommError] comm err info:", err);
            //@todo 通信异常处理
        });
    }
	//监听换电流程的状态更新
    function listenSwapProcessStatus(swapServerHandler) {
        if (!(swapServerHandler instanceof SwapServerHandler)) {
            return;
        }
        wx.showLoading({
            title: '换电中...',
        });
        //监听当前换电流程的 状态更新
        swapServerHandler.listenSwapProcessStatus((info) => {
            //{status: number, result: number} 
            Log.log("[listenSwapProcessStatus] status change:", info);
            if (typeof info != "object") {
                return;
            }
            switch (info["status"]) {
                case SwapProcessStatusMap["SUCCESS"]: {
                    wx.hideLoading();
                    //@todo 换电成功
                    break;
                }
                case SwapProcessStatusMap["ABNORMAL"]:
                case SwapProcessStatusMap["FAIL"]: {
                    wx.hideLoading();
                    //@todo 换电失败
                    break;
                }
            }
        });
    }

3. 该工具内置对 蓝牙通信的日志本地存储;

​ 1. 提供设置 是否显示console 蓝牙通信的log;

const {
    	setConsoleLogDisplay,
    	showConsoleLog,
    	LocalFileHandler
} = require("offline-server-ble-comm-kit");

setConsoleLogDisplay(true);    //开启应急蓝牙业务逻辑日志显示
showConsoleLog(true); 	//开启蓝牙通信日志显示
const localFileHandler = new LocalFileHandler();	//声明定义
  1. 本地已存储应急换电时的 换电通信记录;

    说明: 文件名称以txnNo命名,每个文件存储着请求与响应的通信记录;

    1. LocalFileHandler.getLogFiles()

      获取当前目录下存储多少条通信记录;

      【返回值】 Array[number] 订单流水号(txnNo)集合

    2. LocalFileHandler.getTargetLogFile(Number txnNo)

      获取指定流水号(txnNo)中的请求与响应记录;

      【返回值】Array[string] 该流水号的i请求与响应通信记录

    3. LocalFileHandler.removeFile(Number txnNo)

      删除指定文件

​ ##### 5. 历史版本记录

| 版本号 | 版本描述 | | ------ | ------------------------------------------------------------ | | 1.0.3 | 1. 同步更新换电sdk 1.0.14 版本| | | |