SmartDevicesManager.batchAdd(configs: AddConfig[], options?: BatchAddOptions)

版本要求:
@ray-js/panel-sdk≥1.16.0

描述

批量向多设备管理器中添加设备实例。内部会先批量拉取设备信息(单次网络请求),再按 concurrency 并发初始化,支持部分失败并在返回结果中区分成功与失败列表。 每台设备都可独立配置 interceptors 与 logConfig。

参数

参数类型必填说明
是设备配置列表
否配置选项

返回值

类型:Promise<BatchAddResult>
包含 success 与 failed 的结果对象;其中 success 为成功初始化的设备实例映射,failed 为初始化失败的设备列表

引用对象

typeBatchAddResult
属性类型描述
successRecord<string, SmartDeviceModel>成功初始化的设备实例映射
failedFailedDeviceInfo[]初始化失败的设备列表
typeInitDpStateInterceptor
export type InitDpStateInterceptor<
  S extends ReadonlyDpSchemaList = any,
  P = InitDpStateInterceptorParams
> = Interceptor<S, P, InitDpStateInterceptorParams>;
typeInitDevInfoInterceptor
export type InitDevInfoInterceptor<
  S extends ReadonlyDpSchemaList = any,
  P = {
    devInfo: GetSmartDeviceModelDevInfo<any>;
    action: InitDevInfoInterceptorAction;
  }
> = Interceptor<
  S,
  P,
  {
    devInfo: GetSmartDeviceModelDevInfo<any>;
    action: InitDevInfoInterceptorAction;
  }
>;
typeOnDpDataChangeInterceptor
export type OnDpDataChangeInterceptor<
  S extends ReadonlyDpSchemaList = any,
  P = DpDataChangeParams
> = Interceptor<S, P, DpDataChangeParams>;
typeOnDeviceOnlineStatusUpdateInterceptor
export type OnDeviceOnlineStatusUpdateInterceptor<
  S extends ReadonlyDpSchemaList = any,
  P = DeviceOnlineStatusUpdateParams
> = Interceptor<S, P, DeviceOnlineStatusUpdateParams>;
typeOnDeviceInfoUpdatedInterceptor
export type OnDeviceInfoUpdatedInterceptor<
  S extends ReadonlyDpSchemaList = any,
  P = DeviceInfoUpdatedParams
> = Interceptor<S, P, DeviceInfoUpdatedParams>;
typeOnNetworkStatusChangeInterceptor
export type OnNetworkStatusChangeInterceptor<
  S extends ReadonlyDpSchemaList = any,
  P = NetworkStatusChangeParams
> = Interceptor<S, P, NetworkStatusChangeParams>;
typeOnBluetoothAdapterStateChangeInterceptor
export type OnBluetoothAdapterStateChangeInterceptor<
  S extends ReadonlyDpSchemaList = any,
  P = BluetoothAdapterStatusChangeParams
> = Interceptor<S, P, BluetoothAdapterStatusChangeParams>;
typeInitDpStateInterceptorParams
属性类型必填描述
是当前设备功能点状态
是新的设备功能点状态
action"init" | "onDpDataChange" | "onDeviceInfoUpdated" | "dp-kit"是拦截器动作
typeGetSmartDeviceModelDevInfo
属性类型必填描述
dpCodesobject是
dpsobject是
idCodesobject是
codeIdsobject是
schemaS是
typeSmartDeviceModelAbilitySmartDeviceModel 的 abilities 容器类型,仅用于泛型约束与内部组合。
export type SmartDeviceModelAbility<A extends SmartDeviceAbility = SmartDeviceAbility> = {
  [abilityName: string]: A;
};
typePublishDpsOptionspublishDps 的底层选项类型,供 SDK 内部请求链复用。
属性类型必填描述
否
typeDpValue功能点值类型,可能为 boolean、number、string
export type DpValue = boolean | number | string;
typeSendDpOptionDP 下发选项,可作为 createDpKit 的全局默认配置,也可在单次 publishDps / action 调用时覆盖。 全局 `sendDpOption` 适合声明产品级默认行为; 单次下发传入的 options 适合临时覆盖节流、防抖、协议解析或乐观更新策略。 注:依赖 dp-kit 拦截器才可使用。
属性类型必填描述最低版本
immediateboolean否是否立即触发 state 更新,必须依赖 dp-kit 拦截器才可使用-
否是否忽略 DP 功能点上报,默认 falsev1.11.0
否是否将 dpData 和云端设备属性同步,默认 falsev1.11.0
orderedboolean否多个 DP 是否按对象里的顺序下发,必须依赖 dp-kit 拦截器才可使用,默认 false-
checkRepeatboolean否是否进行重复值判断不下发,必须依赖 dp-kit 拦截器才可使用,默认 false
与当前 `dpState` 进行比较,重复值检出不下发
-
delaynumber否延迟下发,必须依赖 dp-kit 拦截器才可使用,默认 0,单位 ms-
throttlenumber否下发节流 (与防抖冲突),必须依赖 dp-kit 拦截器才可使用,默认 0,单位 ms-
debouncenumber否下发防抖 (与节流冲突),必须依赖 dp-kit 拦截器才可使用,默认 0,单位 ms-
否单次下发时临时指定的 DP 协议转换器,必须依赖 dp-kit 拦截器才可使用。
key 为 dpCode,value 为 `{ parser, formatter }` 对象:
- parser(dpValue: string) => any — 将原始 DP 字符串解析为结构化对象
- formatter(parsedValue: any) => string — 将结构化对象序列化为 DP 字符串
仅影响本次调用,不覆盖 createDpKit 中的全局 protocols 配置
-
typeCapability
export type Capability = number;
typeDevAttribute
export type DevAttribute = number;

合法值

DevInfo.schema.mode
值说明最低版本
rw--
ro--
wr--
DevInfo.schema.property.type
值说明最低版本
string--
bool--
value--
enum--
bitmap--
raw--
DevInfo.schema.type
值说明最低版本
raw--
obj--
FunConfig.tyabirysr4_app
值说明最低版本
follow--
--app-B1--
FunConfig.tyabis5d9w_app
值说明最低版本
follow--
--app-M1--
LogConfig.level
值说明最低版本
VERBOSE--
SUCCESS--
INFO--
WARN--
FATAL--

示例代码

批量添加设备

import { SmartDevicesManager, createDpKit } from '@ray-js/panel-sdk';

const deviceManager = new SmartDevicesManager();
const dpKit = createDpKit({ protocols });

const result = await deviceManager.batchAdd([
  {
    key: 'lamp1',
    deviceId: 'xxx_device_id_1',
    interceptors: dpKit.interceptors, // 搭配 dp-kit 使用结构化 DP
  },
  {
    key: 'lamp2',
    deviceId: 'xxx_device_id_2',
    interceptors: dpKit.interceptors,
    logConfig: { level: 'VERBOSE' }, // 单独开启此设备的详细日志
  },
  {
    key: 'failedDevice',
    deviceId: 'invalid_device_id',
  },
], { concurrency: 5 });

if (result.failed.length > 0) {
  console.warn('部分设备初始化失败:', result.failed);
}
// batchAdd 返回示例:
{
  "success": {
    "lamp1": "<SmartDeviceModel>",
    "lamp2": "<SmartDeviceModel>"
  },
  "failed": [
    {
      "key": "failedDevice",
      "deviceId": "invalid_device_id",
      "error": "Device info not found for invalid_device_id"
    }
  ]
}