model
描述
智能设备模型
类型
export default class SmartDeviceModel<
S extends ReadonlyDpSchemaList,
A extends SmartDeviceModelAbility = SmartDeviceModelAbility,
> {
model: {
props: GetSmartDeviceModelProps<S>;
watch: GetSmartDeviceModelWatch<S>;
actions: GetSmartDeviceModelActions<S>;
abilities: A;
};
}
model.props
描述
智能设备 DP 功能点状态,基于智能设备模型自动代理生成
类型
export declare type GetSmartDeviceModelProps<S extends ReadonlyDpSchemaList> = {
readonly [Dp in S[number]['code']]: GetDpValueBySchema<
Extract<
S[number],
{
code: Dp;
}
>
>;
};
export declare type DpValue = boolean | number | string;
请求参数
参数 | 数据类型 | 说明 |
---|---|---|
DpCode | string | DP Code 功能点标识,如 power |
返回参数
参数 | 数据类型 | 说明 |
---|---|---|
result | DpValue | 根据当前 DP 功能点类型返回对应值,如布尔型返回 boolean,数值型返回 number 等 |
请求示例
const device = await SmartDeviceModel.init<Schema, LampAbilities>();
console.log('=== 当前智能设备的开关状态 ', device.model.props.power);
返回示例
true
👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。
model.watch
描述
智能设备 DP 功能点监听器,基于智能设备模型自动代理生成
类型
export declare type GetSmartDeviceModelWatch<S extends ReadonlyDpSchemaList> = {
readonly [Dp in S[number]['code']]: (
listener: SmartDeviceModelDpListener,
) => {
unwatch: () => void;
};
};
请求参数
参数 | 数据类型 | 说明 |
---|---|---|
DpCode | string | DP Code 功能点标识,如 power |
返回参数
参数 | 数据类型 | 说明 |
---|---|---|
result | { unwatch: () => void; } | 当前监听器的 unwatch 移除监听方法 |
请求示例
const device = await SmartDeviceModel.init<Schema, LampAbilities>();
device.model.watch.power((value, data) => {
console.log('=== 监听当前智能设备的开关状态', value, data);
});
返回示例
{ unwatch: function() {} }
model.actions
描述
智能设备 DP 功能点行为,基于智能设备模型自动代理生成,根据不同的功能点类型会生成不同的行为,详见下方类型定义
类型
export declare type GetSmartDeviceModelActions<S extends ReadonlyDpSchemaList> =
{
readonly [Dp in S[number]['code']]: GetDpActionBySchema<
Extract<
S[number],
{
code: Dp;
}
>
>;
};
👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。
请求参数
参数 | 数据类型 | 说明 |
---|---|---|
DpCode | string | DP Code 功能点标识,如 power |
返回参数
参数 | 数据类型 | 说明 |
---|---|---|
result | DpAction | 根据 DP 功能点类型返回不同的 action 行为,详见下方类型 |
// 所有 DP 功能点均存在该类型
export declare type DpCommonAction<param extends any = string> = {
/**
* 下发 DP 点
* @param value - DP 对应的值
*/
set: (value: param) => Promise<boolean>;
};
export declare type DpBooleanAction = DpCommonAction<boolean> & {
/**
* 打开当前布尔类型功能点
*/
on: () => Promise<boolean>;
/**
* 关闭当前布尔类型功能点
*/
off: () => Promise<boolean>;
/**
* 切换当前布尔类型功能点
*/
toggle: () => Promise<boolean>;
};
export declare type DpNumberAction = DpCommonAction<number> & {
/**
* 根据当前功能点步长 step 进行递增
*/
inc: () => Promise<boolean>;
/**
* 根据当前功能点步长 step 进行递减
*/
dec: () => Promise<boolean>;
};
export declare type DpBitmapAction = DpCommonAction<number> & {
/**
* 开启指定 bit 位
* @param idx - 指定 bit 位
*/
on: (idx: number) => Promise<boolean>;
/**
* 关闭指定 bit 位
* @param idx - 指定 bit 位
*/
off: (idx: number) => Promise<boolean>;
/**
* 切换指定 bit 位
* @param idx - 指定 bit 位
*/
toggle: (idx: number) => Promise<boolean>;
};
export declare type DpEnumAction = DpCommonAction & {
/**
* 切换到前一个枚举值
*/
prev: () => Promise<boolean>;
/**
* 切换到下一个枚举值
*/
next: () => Promise<boolean>;
/**
* 随机切换到一个枚举值
*/
random: () => Promise<boolean>;
};
请求示例
const device = await SmartDeviceModel.init<Schema, LampAbilities>();
// 切换当前智能设备的开关状态
device.model.actions.power.toggle();
返回示例
Promise<pending>
model.abilities
描述
智能设备高级能力,支持自定义组合
类型
export default class SmartDeviceModel<
S extends ReadonlyDpSchemaList,
A extends SmartDeviceModelAbility = SmartDeviceModelAbility,
> {
model: {
abilities: A;
};
}
请求参数
自定义实现
返回参数
自定义实现
请求示例
const device = await SmartDeviceModel.init<Schema, LampAbilities>();
// 切换当前智能设备的音乐功能
device.model.abilities.toggleMusic();
返回示例
无
👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。