智能小程序 Ray 开发面板 SDK —— 智能设备模型 API 接口汇总(三)

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;

请求参数

参数数据类型说明
DpCodestringDP Code 功能点标识,如 power

返回参数

参数数据类型说明
resultDpValue根据当前 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;
  };
};

请求参数

参数数据类型说明
DpCodestringDP 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 小程序开发。  

请求参数

参数数据类型说明
DpCodestringDP Code 功能点标识,如 power

返回参数

参数数据类型说明
resultDpAction根据 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 小程序开发。  

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IoT砖家涂拉拉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值