小程序基础定时开发,详细流程步骤(一)

1. 前置知识

智能小程序提供了基本的定时能力,支持设备定时和群组定时,设备可以是 Wi-Fi 设备、蓝牙 Mesh 子设备、Zigbee 子设备的。并封装了针对设备功能(DP)的定时器信息的增删改查接口。App 通过定时接口设置好定时器信息后,云模组会自动根据定时要求进行预订的操作。

体验流程

  1. 使用智能生活 App 扫一扫功能,扫描下方二维码添加测试专用虚拟设备。

  1. 点击 "打开 vConsole" 按钮,面板将会重启。重启后右下角将显示 vConsole 按钮。

  1. 点击需要测试的功能 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

定时分类,字符串类型,由业务自定义。用于对设备下的定时任务进行分组。无分组可以传空字符串。

点击查看接下来的步骤。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IoT砖家涂拉拉

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

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

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

打赏作者

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

抵扣说明:

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

余额充值