init
描述
根据当前小程序环境启动参数自动获取 deviceId 异步生成一个智能设备模型实例对象。其中第一个类型入参为当前设备的 dp schema 描述,第二个类型入参为自定义的设备 abilities 能力
类型
declare type SmartDeviceModelOptions = {
deviceId?: string;
abilities?: SmartDeviceModelAbility;
mixin?: SmartDeviceModel<ReadonlyDpSchemaList, SmartDeviceModelAbility>[];
logConfig?: LogConfig;
/**
* 智能设备模型拦截器,非稳定 API
*
* @unstable
*/
interceptors?: {
request?: {
/**
* @default [logger]
*/
publishDps?: PublishDpsInterceptor<any>[];
};
response?: {
/**
* @default [logger]
*/
onDpDataChange?: OnDpDataChangeInterceptor<any>[];
/**
* @default [logger]
*/
onDeviceOnlineStatusUpdate?: OnDeviceOnlineStatusUpdateInterceptor<any>[];
/**
* @default [logger]
*/
onDeviceInfoUpdated?: OnDeviceInfoUpdatedInterceptor<any>[];
/**
* @default [logger]
*/
onNetworkStatusChange?: OnNetworkStatusChangeInterceptor<any>[];
/**
* @default [logger]
*/
onBluetoothAdapterStateChange?: OnBluetoothAdapterStateChangeInterceptor<any>[];
};
};
};
export default class SmartDeviceModel<
S extends ReadonlyDpSchemaList,
A extends SmartDeviceModelAbility = SmartDeviceModelAbility,
> {
static init: <
S1 extends ReadonlyDpSchemaList,
A1 extends SmartDeviceModelAbility = SmartDeviceModelAbility,
>(
options?: SmartDeviceModelOptions,
) => Promise<SmartDeviceModel<S1, A1>>;
}
👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。
请求参数
参数 | 数据类型 | 说明 | 是否必填 |
---|---|---|---|
options | SmartDeviceModelOptions | 初始化智能设备模型配置项 | 否 |
返回参数
参数 | 数据类型 | 说明 |
---|---|---|
SmartDeviceInstance | Promise<SmartDeviceModel<S1, A1>> | 智能设备模型实例 |
请求示例
import { SmartDeviceModel } from '@ray-js/panel-sdk';
const schema = [
{
attr: 0,
canTrigger: true,
code: 'power',
defaultRecommend: false,
editPermission: false,
executable: true,
extContent: '',
iconname: 'icon-dp_power2',
id: 1,
mode: 'rw',
name: '开关',
property: {
type: 'bool',
},
type: 'obj',
},
] as const; // 注意此处的 as const 非常重要,必须强制断言成常量
type Schema = typeof schema;
type LampAbilities = {
toggleMusic: (a: string) => void;
};
const device = await SmartDeviceModel.init<Schema, LampAbilities>();
返回示例
无
onInitialized
描述
智能设备模型初始化完毕事件
类型
export default class SmartDeviceModel<
S extends ReadonlyDpSchemaList,
A extends SmartDeviceModelAbility = SmartDeviceModelAbility,
> {
static onInitialized: (listener: () => void) => number;
}
请求参数
参数 | 数据类型 | 说明 | 是否必填 |
---|---|---|---|
param | (listener: () => void) => number | 初始化完毕回调事件 | 是 |
返回参数
无
请求示例
import { SmartDeviceModel } from '@ray-js/panel-sdk';
SmartDeviceModel.onInitialized(() => {
console.log('智能设备初始化完毕!');
});
返回示例
1
👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。