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

点此查看1~4步流程。

5. 添加定时任务

文档及定义:addTimer

import { addTimer, getLaunchOptionsSync } from '@ray-js/ray'

          // 通过启动参数获取设备 id 或群组 id
          const { query: { deviceId, groupId } } = getLaunchOptionsSync();
          const category = ''

          addTimer({
          deviceId,
          groupId, // groupId 和 deviceId 只能二选一。
          category: 'schedule', // 可为空字符串
          timer: {
          time: '10:03',
          loops: '1111111',
          dps: {
          20: false,
          },
          aliasName: '',
          isAppPush: false,
          },
          success: (result: { timerId: number }) => {
          console.log('add success:', timerId)
          },
          fail: (error) => {
          console.log('error:', error)
          },
          })
        

字段对照

  • time: 定时时间
  • loops: 重复规则
  • aliasName: 备注
  • isAppPush: 执行通知
  • dps: 定时需要下发的状态值

6. 更新定时任务

文档及定义:updateTimer

import { updateTimer, getLaunchOptionsSync } from
          '@ray-js/ray'

          // 通过启动参数获取设备 id 或群组 id
          const { query: { deviceId, groupId } } = getLaunchOptionsSync();
          const category = ''
          const timerId = xxx // 列表中获取的任务id

          updateTimer({
          deviceId,
          groupId, // groupId 和 deviceId 只能二选一。
          timer: {
          timerId,
          time: '20:03',
          loops: '0110101',
          dps: {
          20: true,
          },
          aliasName: '',
          isAppPush: false,
          },
          success: () => {
          console.log('update success:')
          },
          fail: (error) => {
          console.log('error:', error)
          },
          })
        

7. 删除定时任务

文档及定义:removeTimer

import { removeTimer, getLaunchOptionsSync } from
          '@ray-js/ray'

          // 通过启动参数获取设备 id 或群组 id
          const { query: { deviceId, groupId } } = getLaunchOptionsSync();
          const category = ''
          const timerId = xxx // 列表中获取的任务id

          removeTimer({
          deviceId,
          groupId, // groupId 和 deviceId 只能二选一。
          timerId,
          success: () => {
          console.log('delete success:')
          },
          fail: (error) => {
          console.log('error:', error)
          },
          })
        

8. 更新定时任务启用状态

文档及定义:updateTimerStatus

import { updateTimerStatus, getLaunchOptionsSync } from
          '@ray-js/ray'

          // 通过启动参数获取设备 id 或群组 id
          const { query: { deviceId, groupId } } = getLaunchOptionsSync();
          const category = ''
          const timerId = xxx // 列表中获取的任务id

          updateTimerStatus({
          deviceId,
          groupId, // groupId 和 deviceId 只能二选一。
          timerId,
          status: true, // true or false 表示启用或关闭定时任务
          success: () => {
          console.log('delete success:')
          },
          fail: (error) => {
          console.log('error:', error)
          },
          })
        

9. 注册与取消定时变化事件监听

文档及定义:

import { onTimerUpdate, offTimerUpdate, getLaunchOptionsSync
          } from '@ray-js/ray'

          // 通过启动参数获取设备 id 或群组 id
          const { query: { deviceId, groupId } } = getLaunchOptionsSync();
          const category = ''
          const timerId = xxx // 列表中获取的任务id

          function Index(){
          const _onTimerUpdate = useCallback(() =>{
          // TODO: 处理更新事件,刷新界面等
          }, [])

          useEffect(() =>{
          onTimerUpdate(_onTimerUpdate)
          return() => {
          offTimerUpdate()
          }
          }, [deviceId, groupId])

          ...
          return <View>...</View>
          }
        

10. 打开 App 定时管理界面

文档及定义:openTimerPage

import { openTimerPage, getLaunchOptionsSync } from
          '@ray-js/ray'

          // 通过启动参数获取设备 id 或群组 id
          const { query: { deviceId, groupId } } = getLaunchOptionsSync();
          const category = ''
          const timerId = xxx // 列表中获取的任务id

          openTimerPage({
          deviceId, // 设备 id ,deviceId 和 groupId 至少传一个
          category: '',
          repeat: 1,
          data: [
          {
          dpName: I18n.t(`dp_switch_led`),
          dpId: 20,
          selected: 1,
          rangeKeys: [true, false],
          rangeValues: ['开启', '关闭'],
          },
          ],
          success: () => {
          console.log('delete success:')
          },
          fail: (error) => {
          console.log('error:', error)
          },
          })
        

定时界面

11. 定时日志查询

可通过设备日志查询定时信息。

可通过类型筛选来过滤定时信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IoT砖家涂拉拉

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

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

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

打赏作者

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

抵扣说明:

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

余额充值