1. 前置知识
智能小程序提供了基本的定时能力,支持设备定时和群组定时,设备可以是 Wi-Fi 设备、蓝牙 Mesh 子设备、Zigbee 子设备的。并封装了针对设备功能(DP)的定时器信息的增删改查接口。App 通过定时接口设置好定时器信息后,云模组会自动根据定时要求进行预订的操作。
体验流程
- 使用智能生活 App 扫一扫功能,扫描下方二维码添加测试专用虚拟设备。
- 点击
"打开 vConsole"
按钮,面板将会重启。重启后右下角将显示vConsole
按钮。
- 点击需要测试的功能 API,然后打开 vConsole 进行结果查看
👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。
2. 常用 API
syncTimerTask
获取设备或群组下某个分类的定时列表信息。
addTimer
为设备或群组新增一个定时器,到指定的任务(task)下。每个设备或群组定时的上限为 30 个。
updateTimer
更新设备或群组下某个任务的定时器信息。
removeTimer
删除设备或群组下某个任务的定时器信息。
updateTimerStatus
更新设备或群组下某个任务的定时器状态。
onTimerUpdate
监听定时器任务状态切换以及任务被删除通知。
offTimerUpdate
取消监听定时器任务状态切换以及任务被删除通知。
openTimerPage
跳转至 App 内置的默认定时页面,可对定时任务进行调整,适用于没有自定义定时 UI 界面的场景。
3. 类型定义
export type TimerModel = {
/**
* timerId
* 定时器 id
*/
timerId: string
/**
* date
* 日期
*/
date: string
/**
* time
* 定时器运行的时间
*/
time: string
/**
* status
* 状态
*/
status: boolean
/**
* loops
* 七位数字字符串,"1000000" 代表周日,"0100000" 代表周一
*/
loops: string
/**
* dps
* dp 点数据,示例:
* {
* "1": true,
* "2": false
* }
*/
dps: Record<string, {}>
/**
* timezoneId
* 时区
*/
timezoneId: string
/**
* aliasName
* 别名
*/
aliasName: string
/**
* isAppPush
* 是否发送执行通知
*/
isAppPush: boolean
/**
* id
* 任务 id
*/
id: string
}
export type AddTimerModel = {
/**
* time
* 定时器运行的时间
*/
time: string
/**
* loops
* 七位数字字符串,"1000000" 代表周日,"0100000" 代表周一
*/
loops: string
/**
* dps
* dp 点数据,示例:
* {
* "1": true,
* "2": false
* }
*/
dps: Record<string, {}>
/**
* aliasName
* 别名
*/
aliasName: string
/**
* isAppPush
* 是否发送执行通知
*/
isAppPush: boolean
}
export type UpdateTimerModel = {
/**
* timerId
* 定时器 id
*/
timerId: string
/**
* time
* 定时器运行的时间
*/
time: string
/**
* loops
* 七位数字字符串,"1000000" 代表周日,"0100000" 代表周一
*/
loops: string
/**
* dps
* dp 点数据,示例:
* {
* "1": true,
* "2": false
* }
*/
dps: Record<string, {}>
/**
* aliasName
* 别名
*/
aliasName: string
/**
* isAppPush
* 是否发送执行通知
*/
isAppPush: boolean
}
import {
addTimer,
getLaunchOptionsSync,
openTimerPage,
removeTimer,
syncTimerTask,
updateTimer,
updateTimerStatus,
onTimerUpdate,
offTimerUpdate,
} from '@ray-js/ray'
👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。
4. 获取定时列表
文档及定义: syncTimerTask
import { syncTimerTask, getLaunchOptionsSync } from
'@ray-js/ray'
// 通过启动参数获取设备 id 或群组 id
const { query: { deviceId, groupId } } = getLaunchOptionsSync();
const category = ''
syncTimerTask({
groupId,
deviceId, // groupId 和 deviceId 只能二选一。
category,
success: (result: { timers: TimerModel[] }) => {
const { timers } = result
console.log('timers: ', timers)
},
fail: (error) => {
console.log('error:', error)
},
})
category
定时分类,字符串类型,由业务自定义。用于对设备下的定时任务进行分组。无分组可以传空字符串。