跳到主要内容

场景自动化

更新时间:2025-10-17 16:42:25

App端可以通过调用场景相关接口配置场景自动化任务,详细描述请见控制台场景自动化文档

// 导入设备管理SDK头文件
#import <RTCXDeviceCenter/RTCXDeviceCenter.h>

场景自动化相关API接口如下:

@protocol RTCXSceneMgrProtocol <NSObject>
/**
* 创建场景
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)sceneCreatWithReq:(nonnull RTCXSceneCreatReq *)req onSuccess:(nullable RTCXSceneMgrOnSuccess)onSuccess onError:(nullable RTCXSceneMgrOnError)onError;
/**
* 删除场景
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)sceneDeleteWithReq:(nonnull RTCXSceneDeleteReq *)req onSuccess:(nullable RTCXSceneMgrOnSuccess)onSuccess onError:(nullable RTCXSceneMgrOnError)onError;
/**
* 重命名场景
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)sceneRenameWithReq:(nonnull RTCXSceneRenameReq *)req onSuccess:(nullable RTCXSceneMgrOnSuccess)onSuccess onError:(nullable RTCXSceneMgrOnError)onError;
/**
* 更新场景
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)sceneUpdateWithReq:(nonnull RTCXSceneUpdateReq *)req onSuccess:(nullable RTCXSceneMgrOnSuccess)onSuccess onError:(nullable RTCXSceneMgrOnError)onError;
/**
* 执行场景
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)sceneExecuteWithReq:(nonnull RTCXSceneExecuteReq *)req onSuccess:(nullable RTCXSceneMgrOnSuccess)onSuccess onError:(nullable RTCXSceneMgrOnError)onError;
/**
* 获取场景列表
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)sceneListWithReq:(nonnull RTCXSceneListReq *)req onSuccess:(nullable RTCXSceneMgrOnSuccess)onSuccess onError:(nullable RTCXSceneMgrOnError)onError;
/**
* 获取场景详情
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)sceneDetailWithReq:(nonnull RTCXSceneDetailReq *)req onSuccess:(nullable RTCXSceneMgrOnSuccess)onSuccess onError:(nullable RTCXSceneMgrOnError)onError;
@end

使用 RTCXDeviceSDK 单例宏对象调用示例如下:

创建场景

RTCXSceneConditionReq *condition = [[RTCXSceneConditionReq alloc] init];
condition.uri = @"condition/device/property";
condition.productKey = @"xLeWcoelZbyOePbJludl";
condition.deviceName = @"20240824001";
condition.identifier = @"alarmCode";
condition.propertyName = @"";
condition.compareType = @"==";
condition.compareValue = @"xxx";
RTCXSceneActionReq *action = [[RTCXSceneActionReq alloc] init];
action.uri = @"action/device/invokeService";
action.productKey = @"xLeWcoelZbyOePbJludl";
action.deviceName = @"demoWang";
action.identifier = @"QueryRecordTimeList";
action.args = @"{\"value\":\"1\"}";
RTCXSceneCreatReq *sampleReq = [[RTCXSceneCreatReq alloc] init];
sampleReq.enable = YES;
sampleReq.mode = @"any";
sampleReq.name = @"sample scene";
sampleReq.conditions = @[condition];
sampleReq.actions = @[action];
[RTCXDeviceService(RTCXSceneMgrProtocol) sceneCreatWithReq:sampleReq onSuccess:^(id _Nullable data, id _Nullable rawData) {
[weakSelf showToast:NSLocalizedString(@"创建场景成功", nil) superView:[UIApplication sharedApplication].keyWindow];
[self.navigationController popViewControllerAnimated:YES];
} onError:^(NSError * _Nullable error) {
[weakSelf showToast:[NSString stringWithFormat:@"%ld : %@",error.code,error.userInfo[NSLocalizedFailureReasonErrorKey]]];
}];

删除场景

RTCXSceneDeleteReq *req = [[RTCXSceneDeleteReq alloc] init];
req.sceneId = model.sceneId;
[RTCXDeviceService(RTCXSceneMgrProtocol) sceneDeleteWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
[weakSelf showToast:NSLocalizedString(@"删除成功", nil)];
} onError:^(NSError * _Nullable error) {
[weakSelf showToast:[NSString stringWithFormat:@"%ld : %@",error.code,error.userInfo[NSLocalizedFailureReasonErrorKey]]];
}];

重命名场景

RTCXSceneRenameReq *req = [[RTCXSceneRenameReq alloc] init];
req.sceneId = model.sceneId;
req.enable = model.enable;
req.name = name;
[RTCXDeviceService(RTCXSceneMgrProtocol) sceneRenameWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
[weakSelf showToast:NSLocalizedString(@"重命名成功", nil)];
} onError:^(NSError * _Nullable error) {
[weakSelf showToast:[NSString stringWithFormat:@"%ld : %@",error.code,error.userInfo[NSLocalizedFailureReasonErrorKey]]];
}];

更新场景

RTCXSceneConditionReq *condition = [[RTCXSceneConditionReq alloc] init];
condition.uri = @"condition/device/property";
condition.productKey = @"xLeWcoelZbyOePbJludl";
condition.deviceName = @"20240824001";
condition.identifier = @"alarmCode";
condition.propertyName = @"";
condition.compareType = @"==";
condition.compareValue = @"xxx";
RTCXSceneActionReq *action = [[RTCXSceneActionReq alloc] init];
action.uri = @"action/device/invokeService";
action.productKey = @"xLeWcoelZbyOePbJludl";
action.deviceName = @"demoWang";
action.identifier = @"QueryRecordTimeList";
action.args = @"{\"value\":\"1\"}";
RTCXSceneUpdateReq *updateReq = [[RTCXSceneUpdateReq alloc] init];
updateReq.sceneId = model.sceneId;
updateReq.enable = YES;
updateReq.mode = @"any";
updateReq.name = @"sample scene";
updateReq.conditions = @[condition];
updateReq.actions = @[action];
[RTCXDeviceService(RTCXSceneMgrProtocol) sceneUpdateWithReq:updateReq onSuccess:^(id _Nullable data, id _Nullable rawData) {
[weakSelf showToast:NSLocalizedString(@"更新场景成功", nil) superView:[UIApplication sharedApplication].keyWindow];
[self.navigationController popViewControllerAnimated:YES];
} onError:^(NSError * _Nullable error) {

}];

执行场景

RTCXSceneExecuteReq *req = [[RTCXSceneExecuteReq alloc] init];
req.sceneId = model.sceneId;
[RTCXDeviceService(RTCXSceneMgrProtocol) sceneExecuteWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
[weakSelf showToast:NSLocalizedString(@"执行场景成功", nil)];
} onError:^(NSError * _Nullable error) {
[weakSelf showToast:[NSString stringWithFormat:@"%ld : %@",error.code,error.userInfo[NSLocalizedFailureReasonErrorKey]]];
}];

获取场景列表

RTCXSceneListReq *req = [[RTCXSceneListReq alloc] init];
req.pageNo = @1;
req.pageSize = @50;
[RTCXDeviceService(RTCXSceneMgrProtocol) sceneListWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
RTCXSceneListRsp *rsp = data;
} onError:^(NSError * _Nullable error) {
[weakSelf showToast:[NSString stringWithFormat:@"%ld : %@",error.code,error.userInfo[NSLocalizedFailureReasonErrorKey]]];
}];

获取场景详情

RTCXSceneDetailReq *req = [[RTCXSceneDetailReq alloc] init];
req.sceneId = Model.sceneId;
[RTCXDeviceService(RTCXSceneMgrProtocol) sceneDetailWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
RTCXSceneDetailRsp *rsp = data;
} onError:^(NSError * _Nullable error) {
[weakSelf showToast:[NSString stringWithFormat:@"%ld : %@",error.code,error.userInfo[NSLocalizedFailureReasonErrorKey]]];
}];