Device Schedule

Last Updated on : 2024-05-12 05:18:50download

Overview

The device schedule service enables you to:

  • Get the list of device or group schedules
  • Add a device or group schedule
  • Update a device or group schedule
  • Delete a device or group schedule
  • Update the status of a device or group schedule

Implementation

Prerequisites

The class IThingTimerManager is responsible for scheduling tasks. This section uses the device schedule to describe the implementation. Group schedules work the same way, with the only difference being the request parameter groupId.

The schedule service only works if a device or group exists in a home.

Get the schedule management class.

  // Get the schedule management class.
  IThingTimerManager timerManager = ThingDeviceDetailKit.getInstance().getDeviceTimerManager();

Step 1: Create schedule

To schedule a device to execute a specific command at a set time, use addTimer() to create a schedule.

Parameters:

Parameter Required
deviceId Yes
category Yes
time Yes
dps Yes
loops Yes
aliasName Yes
isOpen Yes
isAppPush Yes
callback Yes
    TimerManagerBuilder builder = new TimerManagerBuilder.Builder()
            .setDeviceId(deviceId)
            .setCategory("schedule")
            .setTime("12:12")
            .setDps({"1":false})
            .setLoops("0000000")
            .setAliasName("aliasName")
            .isOpen(true)
            .isAppPush(true)
            .setCallback(new IThingTimerCallBack() {
                @Override
                public void successful(@Nullable List<? extends AlarmTimerBean> list) {
                  // After a schedule is created, a schedule list that includes the newly added one will be returned.
                  if (list!=null&&list.size()>0){
                    AlarmTimerBean alarmTimerBean = list.get(0);
                    // groupId is the schedule ID.
                    String groupId = alarmTimerBean.getGroupId();
                  }

                }

                @Override
                public void fail(int i, @NonNull String s) {

                }
            })
            .build();
    timerManager.addTimer(builder);

Step 2: Edit schedule

Call editTimer() to edit a schedule.

Parameters:

Parameter Required
deviceid Yes
timerId Yes
time Yes
loops Yes
aliasName Yes
isAppPush Yes
callback Yes
    TimerManagerBuilder builder = new TimerManagerBuilder.Builder()
            .setDeviceId(deviceId)
            .setTime("12:32")
            .setTimerId("xxxx")
            .setDps({"1":false})
            .setLoops("1000000")
            .setAliasName("aliasName")
            .isOpen(true)
            .isAppPush(false)
            .setCallback(new IThingTimerCallBack() {
                @Override
                public void successful(@Nullable List<? extends AlarmTimerBean> list) {
                  // After a schedule is edited, a schedule list that includes the edited one will be returned.
                  if (list!=null&&list.size()>0){
                    AlarmTimerBean alarmTimerBean = list.get(0);
                    // groupId is the schedule ID.
                    String groupId = alarmTimerBean.getGroupId();
                  }

                }

                @Override
                public void fail(int i, @NonNull String s) {

                }
            })
            .build();
    timerManager.editTimer(builder);

Step 3: Update schedule

Call updateTimerStatus() to update the schedule status.

Parameters:

Parameter Required
deviceid Yes
timerId Yes
isOpen Yes
callback Yes
    TimerManagerBuilder builder = new TimerManagerBuilder.Builder()
            .setDeviceId(deviceId)
            .setTimerId("xxxx")
            .setCallback(new IThingTimerCallBack() {
                @Override
                public void successful(@Nullable List<? extends AlarmTimerBean> list) {
                  // Updating the schedule status will trigger the success callback, returning a null list of data.
                }

                @Override
                public void fail(int i, @NonNull String s) {

                }
            })
            .build();
    timerManager.updateTimerStatus(builder);

Step 4: Delete schedule

Call deleteTimer() to delete a schedule.

Parameters:

Parameter Required
deviceid Yes
timerId Yes
callback Yes
    TimerManagerBuilder builder = new TimerManagerBuilder.Builder()
            .setDeviceId(deviceId)
            .setTimerId("xxxx")
            .setCallback(new IThingTimerCallBack() {
                @Override
                public void successful(@Nullable List<? extends AlarmTimerBean> list) {
                  // Deleting the schedule status will trigger the success callback, returning a null list of data.
                }

                @Override
                public void fail(int i, @NonNull String s) {

                }
            })
            .build();
    timerManager.deleteTimer(builder);

Step 5: Get schedule list

Call getTimerList() to get the list of schedules for a device.

Parameters:

Parameter Required
deviceid Yes
callback Yes
    TimerManagerBuilder builder = new TimerManagerBuilder.Builder()
            .setDeviceId(deviceId)
            .setCallback(new IThingTimerCallBack() {
                @Override
                public void successful(@Nullable List<? extends AlarmTimerBean> list) {
                  // A successful request returns the list of schedules.
                }

                @Override
                public void fail(int i, @NonNull String s) {

                }
            })
            .build();
    timerManager.getTimerList(builder);

Demo

This topic describes the service implementation in the sequence of read, create, update, and delete operations. In real-world projects, you can implement them as needed. For more information, see the Device Schedule Module in the BizBundle SDK Demo.