Scheduled Tasks

Last Updated on : 2023-03-07 08:09:24download

Smart Life App SDK provides basic capabilities to manage scheduled tasks. For example, set scheduled tasks for devices and groups. Supported types of devices include Wi-Fi devices, Bluetooth mesh sub-devices, and Zigbee sub-devices. This SDK encapsulates the API methods to query, add, modify, or delete timers for data points (DPs). The app can be used to set timers based on specific scheduled task API calls. Then, network modules will automatically run predefined tasks.

Functional description

  • Encapsulation class

    Class name Description
    TuyaSmartTimer Encapsulates API methods for scheduled tasks.
  • Multiple API methods require the taskName parameter. It can be regarded as a group of timers. Each timer belongs to at most one group. A group is only used for display. Example:

    • A switch might support multiple DPs. A group of timers can be created for each DP.

    • Multiple timers can be added to each group to control the switch status of the DP in different periods.

      A scheduled task can include multiple timers. The following figure shows the architecture of the device, scheduled tasks, and timers.

      Scheduled Tasks

Add a scheduled task

API description

Adds a timer to a specified scheduled task specified by task for a device or group. The maximum number of timers allowed for each device or group is 30.

- (void)addTimerWithTask:(NSString *)task
                   loops:(NSString *)loops
                   bizId:(NSString *)bizId
                 bizType:(NSUInteger)bizType
                    time:(NSString *)time
                     dps:(NSDictionary *)dps
                  status:(BOOL)status
               isAppPush:(BOOL)isAppPush
               aliasName:(NSString *)aliasName
                 success:(TYSuccessHandler)success
                 failure:(TYFailureError)failure;

Parameters

Parameter Description
task The name of the scheduled task.
loops The way the scheduled task is repeated. Format: 0000000. Valid values of each digit:
  • 0: disabled
  • 1: enabled
The digits represent Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday sequentially from left to right. For example, for a scheduled task that repeats each Monday, set the value to 0100000.
bizId The ID of the device or group.
bizType
  • 0: device
  • 1: group of devices
time The scheduled time. Example: 18:00.
dps The list of DPs to be sent.
status Specifies whether to enable the timer.
isAppPush Specifies whether to enable push notifications.
aliasName The remarks.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)addTimer {
	// self.timer = [[TuyaSmartTimer alloc] init];
	  NSDictionary *dps = @{@"1": @(YES)};
    [self.timer addTimerWithTask:@"timer_task_name" loops:@"1000000" bizId:@"device_id" bizType:0 time:@"18:00"  dps:dps status:YES isAppPush:YES aliasName:@"timer for device xxx" success:^{
        NSLog(@"addTimerWithTask success");
    } failure:^(NSError *error) {
        NSLog(@"addTimerWithTask failure: %@", error);
    }];
}

Swift:

func addTimer() {
    let dps = ["1" : true]
    self.timer.add(withTask: "timer_task_name", loops: "1000000", bizId: "device_id", bizType: 0, time: "18:00", dps: dps, status: true, isAppPush: true, aliasName: "timer for device xxx") {
        print("addTimerWithTask success")
    } failure: { (error) in
        if let e = error {
            print("addTimerWithTask failure: \(e)")
        }
    }
}

Modify common timer status or delete timers in bulk

API description

- (void)updateTimerStatusWithTimerIds:(NSArray<NSString *> *)timerIds
                                bizId:(NSString *)bizId
                              bizType:(NSUInteger)bizType
                           updateType:(int)updateType
                              success:(TYSuccessHandler)success
                              failure:(TYFailureError)failure;

Parameters

Parameter Description
timerIds The list of target timer IDs.
bizId The ID of the device or group.
bizType
  • 0: device
  • 1: group of devices
updateType The type of update.
  • 0: disable timers
  • 1: enable timers
  • 2: delete timers
success The success callback.
failure The failure callback.

Example

ObjC:

    [self.timer updateTimerStatusWithTimerIds:@[@"2222", @"timer_id2"] bizId:@"device_id" bizType:0 updateType:1 success:^{
        NSLog(@"updateTimer success");
    } failure:^(NSError *error) {
        NSLog(@"updateTimer failure: %@", error);
    }];

Swift:

    self.timer.updateTimerStatus(withTimerIds: ["232323", "233"], bizId: "device_id", bizType: 0, updateType: 1) {
        print("updateTimer success")
    } failure: { (error) in
        if let e = error {
            print("updateTimer failure: \(e)")
        }
    }

Modify timer information of a scheduled task

API description

Updates the timer information of a scheduled task for a device or group.

- (void)updateTimerWithTimerId:(NSString *)timerId
                         loops:(NSString *)loops
                         bizId:(NSString *)bizId
                       bizType:(NSUInteger)bizType
                          time:(NSString *)time
                           dps:(NSDictionary *)dps
                        status:(BOOL)status
                     isAppPush:(BOOL)isAppPush
                     aliasName:(NSString *)aliasName
                       success:(TYSuccessHandler)success
                       failure:(TYFailureError)failure;

Parameters

Parameter Description
timerId The target timer ID.
loops The way the scheduled task is repeated. Format: 0000000. Valid values of each digit:
  • 0: disabled
  • 1: enabled
The digits represent Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday sequentially from left to right. For example, for a scheduled task that repeats each Monday, set the value to 0100000.
bizId The ID of the device or group.
bizType
  • 0: device
  • 1: group of devices
time The scheduled time. Example: 18:00.
dps The list of DPs to be sent.
status Specifies whether to enable the timer.
isAppPush Specifies whether to enable push notifications.
aliasName The remarks.
success The success callback.
failure The failure callback.

Example

ObjC:

- (void)updateTimer {
	  // self.timer = [[TuyaSmartTimer alloc] init];
	  NSDictionary *dps = @{@"1": @(YES)};

    [self.timer updateTimerWithTimerId:@"timer_id" loops:@"1000000" bizId:@"device_id" bizType:0 time:@"18:00" dps:dps status:YES isAppPush:YES aliasName:@"timer for device xxx" success:^{
        NSLog(@"updateTimer success");
    } failure:^(NSError *error) {
        NSLog(@"updateTimer failure: %@", error);
    }];
}

Swift:

func updateTimer() {
    let dps = ["1" : true]
    self.timer.updateTimer(withTimerId: "timer_id", loops: "1000000", bizId: "device_id", bizType: 0, time: "18:00", dps: dps, status: true, isAppPush: true, aliasName: "timer for device xxx") {
        print("updateTimer success")
    } failure: { (error) in
        if let e = error {
            print("updateTimer failure: \(e)")
        }
    }
}

Query all timers of a scheduled task

API description

Returns all timers of a scheduled task for a device or group.

- (void)getTimerListWithTask:(NSString *)task
                       bizId:(NSString *)bizId
                     bizType:(NSUInteger)bizType
                     success:(void(^)(NSArray<TYTimerModel *> *list))success
                     failure:(TYFailureError)failure;

Parameters

Parameter Description
task The name of the scheduled task.
bizId The ID of the device or group.
bizType
  • 0: device
  • 1: group of devices
success The success callback. An array of timers is returned.
failure The failure callback.

Example

ObjC:

- (void)getTimer {
	  // self.timer = [[TuyaSmartTimer alloc] init];

    [self.timer getTimerListWithTask:@"timer_task_name" bizId:@"device_id" bizType:0 success:^(NSArray<TYTimerModel *> *list) {
        NSLog(@"getTimer success %@:", list);
    } failure:^(NSError *error) {
        NSLog(@"getTimer failure: %@", error);
    }];
}

Swift:

func getTimer() {

    self.timer.getListWithTask("timer_task_name", bizId: "device_id", bizType: 0, success: { (list) in
        print("getTimer success: \(list)")
    }, failure: { (error) in
        if let e = error {
            print("getTimer failure: \(e)")
        }
    })
}

Query all scheduled tasks for a device or group

API description

Returns all timers for a device or group. Each timer is assigned to a scheduled task.

- (void)getTimerTaskListWithbizId:(NSString *)bizId
                     bizType:(NSUInteger)bizType
                     success:(void(^)(NSArray<TYCategoryTimersModel *> *list))success
                     failure:(TYFailureError)failure

Parameters

Parameter Description
bizId The ID of the device or group.
bizType
  • 0: device
  • 1: group of devices
success The success callback. An array of timers is returned.
failure The failure callback.

Example

ObjC:

- (void)getTimer {
	  // self.timer = [[TuyaSmartTimer alloc] init];

    [self.timer getTimerTaskListWithBizId:@"device_id" bizType:0 success:^(NSArray<TYCategoryTimersModel *> *list) {
        NSLog(@"getTimer success %@:", list);
    } failure:^(NSError *error) {
        NSLog(@"getTimer failure: %@", error);
    }];
}

Swift:

func getTimer() {

    self.timer.getTimerTaskListWithBizId("device_id",  bizType: 0, success: { (list) in
        print("getTimer success: \(list)")
    }, failure: { (error) in
        if let e = error {
            print("getTimer failure: \(e)")
        }
    })
}

Modify scheduled task status or delete timers by category

API description

- (void)updateTimerTaskStatusWithTask:(NSString *)task
                                bizId:(NSString *)bizId
                              bizType:(NSUInteger)bizType
                           updateType:(NSUInteger)updateType
                              success:(TYSuccessHandler)success
                              failure:(TYFailureError)failure;

Parameters

Parameter Description
task The name of the scheduled task.
bizId The ID of the device or group.
bizType
  • 0: device
  • 1: group of devices
updateType The type of update.
  • 0: disable timers
  • 1: enable timers
  • 2: delete timers
success The success callback. An array of timers is returned.
failure The failure callback.

Example

ObjC:

- (void)updateTimerTask {
	  // self.timer = [[TuyaSmartTimer alloc] init];

    [self.timer updateTimerTaskStatusWithTask:@"timer_task_name" bizId:@"device_id" bizType:0 updateType:1 success:^{
        NSLog(@"updateTimer success");
    } failure:^(NSError *error) {
        NSLog(@"updateTimer failure: %@", error);
    }];
}

Swift:

func updateTimerTask() {
    self.timer.updateTaskStatus(withTask: "timer_task_name", bizId: "device_id", bizType: 0, updateType: 1) {
        print("updateTimer success: \(list)")
    } failure: { (error) in
         if let e = error {
            print("updateTimer failure: \(e)")
        }
    }
}