定时任务

更新时间:2023-03-07 08:09:19下载pdf

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

功能说明

  • 封装类:

    类名 说明
    TuyaSmartTimer 定时任务接口封装
  • 以下多个接口用到了 taskName 参数,具体可理解为一个分组,一个分组可以有多个定时器。每个定时属于或不属于一个分组,分组仅用于展示。例如:

    • 一个开关可能有多个功能,可以根据每个设备功能设置一个定时分组。

    • 每个分组可以添加多个定时器,用于控制这个设备功能各个时段的开启或关闭。

      每个定时任务下可以包含多个定时器。如下图所示:

      定时任务

增加定时任务

接口说明

为设备或群组新增一个定时器,到指定的任务(task)下。每个设备或群组定时的上限为 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;

参数说明

参数 说明
task 定时任务名称
loops 循环次数,格式为 0000000,每一位数字的取值可以是
  • 0:关闭
  • 1:开启
从左至右依次表示周日、周一、周二、周三、周四、周五、周六,如每个周一循环该任务,则取值为 0100000
bizId 设备或群组 ID
bizType
  • 0:设备
  • 1:设备群组
time 定时的时间,如 18:00
dps 设备功能命令
status 是否开启定时
isAppPush 是否需要推送
aliasName 备注信息
success 成功回调
failure 失败回调

示例代码

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)")
        }
    }
}

批量修改普通定时状态或删除定时器

接口说明

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

参数说明

参数 说明
timerIds 批量修改的定时 ID
bizId 设备或群组 ID
bizType
  • 0:设备
  • 1:设备群组
updateType 更新类型
  • 0:关闭定时器
  • 1:开启定时器
  • 2:删除定时器
success 成功回调
failure 失败回调

示例代码

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)")
        }
    }

修改任务的定时信息

接口说明

更新设备或群组下某个任务的定时器信息。

- (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;

参数说明

参数 说明
timerId 更新的定时器 ID
loops 循环次数,格式为 0000000,每一位数字的取值可以是
  • 0:关闭
  • 1:开启
从左至右依次表示周日、周一、周二、周三、周四、周五、周六,如每个周一循环该任务,则取值为 0100000
bizId 设备或群组 ID
bizType
  • 0:设备
  • 1:设备群组
time 定时的时间,如 18:00
dps 设备功能命令
status 是否开启定时
isAppPush 是否需要推送
aliasName 备注信息
success 成功回调
failure 失败回调

示例代码

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)")
        }
    }
}

查询定时任务下所有定时

接口说明

查询设备或群组下某个任务的定时器。

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

参数说明

参数 说明
task 定时任务名称
bizId 设备或群组 ID
bizType
  • 0:设备
  • 1:设备群组
success 成功回调,返回定时器的数组
failure 失败回调

示例代码

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)")
        }
    })
}

查询设备或群组下所有的定时任务

接口说明

查询设备或群组下所有的定时器,根据任务分类。

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

参数说明

参数 说明
bizId 设备或群组 ID
bizType
  • 0:设备
  • 1:设备群组
success 成功回调,返回定时器的数组
failure 失败回调

示例代码

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)")
        }
    })
}

修改分类下所有定时任务状态或删除定时器

接口说明

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

参数说明

参数 说明
task 定时任务名称
bizId 设备或群组 ID
bizType
  • 0:设备
  • 1:设备群组
updateType 更新类型
  • 0:关闭定时器
  • 1:开启定时器
  • 2:删除定时器
success 成功回调,返回定时器的数组
failure 失败回调

示例代码

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)")
        }
    }
}