场景自动化
更新时间:2025-10-17 13:13:47
信息
场景自动化SDK提供设备联动能力。
可选能力,如果不需要该功能可删除libscene-Release.aar
package com.polaris.iot.appsdk.libscene
object DeviceSceneMgr {
/**
* 创建场景
*
* @param sceneCreateReq 创建参数[SceneCreateReq]
* @param callback 回调[IoTCallback]
*/
fun sceneCreate(sceneCreateReq: SceneCreateReq, callback: IoTCallback)
/**
* 场景删除
*
* @param sceneId 场景ID
* @param callback 回调[IoTCallback]
*/
fun sceneDelete(sceneId: Long, callback: IoTCallback)
/**
* 更新场景
*
* @param sceneUpdateReq 创建参数[SceneUpdateReq]
* @param callback 回调[IoTCallback]
*/
fun sceneUpdate(sceneUpdateReq: SceneUpdateReq, callback: IoTCallback?)
/**
* 更新场景名称和是否启用
*
* @param sceneUpdateBasicReq 创建参数[SceneUpdateBasicReq]
* @param callback 回调[IoTCallback]
*/
fun sceneUpdate(sceneUpdateBasicReq: SceneUpdateBasicReq, callback: IoTCallback?)
/**
* 场景执行
*
* @param sceneId 场景ID
* @param callback 回调[IoTCallback]
*/
fun sceneExecute(sceneId: Long, callback: IoTCallback)
/**
* 场景列表
*
* @param pageNo 页码,从1开始
* @param pageSize 分页大小,最大50
* @param callback 回调[IoTCallback]
*/
fun sceneList(pageNo: Int, pageSize: Int, callback: IoTCallback)
/**
* 场景详情
*
* @param sceneId 场景ID
* @param callback 回调[IoTCallback]
*/
fun sceneDetail(sceneId: Long, callback: IoTCallback)
}
/**
* 场景条件模式 暂时只支持any
*/
enum class SceneModeEnum(val mode: String) {
/**
* 表示任何一个条件满足就执行动作。
*/
Any("any"),
/**
* 表示所有条件满足时才执行动作。
*/
All("all"),
}
创建场景
创建场景
/**
* 创建场景参数
*
* @property name 场景名称
* @property enable 是否启用
* @property mode 场景条件类型 any:表示任何一个条件满足就执行动作。 all:表示所有条件满足时才执行动作。 暂时只支持any
* @property conditions 场景条件
* @property actions 场景动作
* @constructor Create empty Scene create req
*/
data class SceneCreateReq(
val name: String,
val enable: Boolean,
val mode: String,
val conditions: List<SceneCondition>,
val actions: List<SceneAction>
)
/**
* 场景条件
*
* @property uri condition/device/property:属性条件,condition/device/event:事件条件
* @property params 场景条件参数
* @constructor Create empty Scene condition
*/
data class SceneCondition(
val uri: String,
val params: SceneConditionParams
) : Serializable
/**
* 场景条件参数
*
* @property productKey 设备型号
* @property deviceName 设备激活码
* @property identifier 物模型属性标识
* @property propertyName 物模型属性的名称
* @property compareType 比较类型,如>、<、>=、==、<=、!=、in
* @property compareValue 用于比较的期望的值类型 当是平台返回时compareValue仅是字符串根据实际业务处理字符串
* @constructor Create empty Scene condition params
*/
data class SceneConditionParams(
val productKey: String,
val deviceName: String,
val identifier: String?,
val propertyName: String,
val compareType: String,
val compareValue: Any,
) : Serializable
/**
* 场景执行动作
*
* @property uri action/device/setProperty: 属性设置,action/device/invokeService:服务调用
* @property params 执行动作参数
*/
data class SceneAction(
val uri: String,
val params: SceneActionParams
) : Serializable
/**
* 场景执行动作参数
*
* @property productKey 设备型号
* @property deviceName 设备激活码
* @property identifier 物模型属性或事件标识符
* @property args 属性设置的内容或者服务入参,无入参填 {}
*/
data class SceneActionParams(
val productKey:String,
val deviceName:String,
val identifier:String?,
val args:String?,
): Serializable
示例
private void createScene() {
showLoading();
String sceneName = binding.etSceneName.getText().toString();
boolean enable = binding.switchStatus.isChecked();
SceneModeEnum mode = binding.rgConditionMode.getCheckedRadioButtonId() == R.id.rbAll ? SceneModeEnum.All : SceneModeEnum.Any;
List<SceneCondition> conditions = mSceneConditionAdapter.getDataList();
List<SceneAction> actions = mSceneActionAdapter.getDataList();
SceneCreateReq sceneCreateReq = new SceneCreateReq(sceneName, enable, mode, conditions, actions);
DeviceSceneMgr.sceneCreate(sceneCreateReq, new IoTCallback() {
@Override
public void onFailure(@Nullable IoTRequest request, @NonNull IoTSDKError exception) {
hideLoading();
IoTLogger.e(TAG, "sceneCreate onFailure", exception);
showToast(R.string.error_api_failed);
}
@Override
public void onResponse(@NonNull IoTRequest request, @NonNull IoTResponse response) {
hideLoading();
finish();
}
});
}
删除场景
示例
Long mSceneId = 1
showLoading();
DeviceSceneMgr.sceneDelete(mSceneId, new IoTCallback() {
@Override
public void onFailure(@Nullable IoTRequest request, @NonNull IoTSDKError exception) {
hideLoading();
showToast(R.string.error_api_failed);
IoTLogger.e(TAG, "sceneDelete onFailure" + mSceneId, exception);
}
@Override
public void onResponse(@NonNull IoTRequest request, @NonNull IoTResponse response) {
hideLoading();
finish();
}
});
更新场景
/**
* 更新场景参数
*
* @property sceneId 场景ID
* @property name 场景名称
* @property enable 是否启用
* @property mode 场景条件类型 any:表示任何一个条件满足就执行动作。 all:表示所有条件满足时才执行动作。 暂时只支持any
* @property conditions 场景条件,不能为空
* @property actions 场景动作,不能为空
* @constructor Create empty Scene create req
*/
data class SceneUpdateReq(
val sceneId: Long,
val name: String,
val enable: Boolean,
val mode: String,
val conditions: List<SceneCondition>,
val actions: List<SceneAction>
)
/**
* 更新场景基本信息
*
* @property sceneId 场景ID
* @property name 场景名称
* @property enable 是否启用
*/
data class SceneUpdateBasicReq(
val sceneId: Long,
val name: String,
val enable: Boolean,
)
示例
更新全部信息
long sceneId = 1;
String sceneName = "场景名称";
boolean enable = false;
//场景条件,不能为空
List<SceneCondition> conditions = new ArrayList<>();
//场景动作,不能为空
List<SceneAction> actions = new ArrayList<>();
SceneUpdateReq sceneUpdateReq = new SceneUpdateReq(sceneId, sceneName, enable, mode, conditions, actions);
DeviceSceneMgr.sceneUpdate(sceneUpdateReq, new IoTCallback() {
@Override
public void onFailure(@Nullable IoTRequest request, @NonNull IoTSDKError exception) {
IoTLogger.e(TAG, "updateScene onFailure", exception);
showToast(R.string.error_api_failed);
}
@Override
public void onResponse(@NonNull IoTRequest request, @NonNull IoTResponse response) {
}
});
更新基础信息
long sceneId = 1;
String sceneName = "场景名称";
boolean enable = false;
DeviceSceneMgr.sceneUpdate(new SceneUpdateBasicReq(sceneId, sceneName, enable), new IoTCallback() {
@Override
public void onFailure(@Nullable IoTRequest request, @NonNull IoTSDKError exception) {
IoTLogger.e(TAG, "updateScene onFailure", exception);
showToast(R.string.error_api_failed);
}
@Override
public void onResponse(@NonNull IoTRequest request, @NonNull IoTResponse response) {
}
})
场景执行
示例
long sceneId = 1;
DeviceSceneMgr.sceneExecute(sceneId, new IoTCallback() {
@Override
public void onFailure(@Nullable IoTRequest request, @NonNull IoTSDKError exception) {
showToast(R.string.error_api_failed);
IoTLogger.e(TAG, "sceneExecute onFailure" + mSceneId, exception);
}
@Override
public void onResponse(@NonNull IoTRequest request, @NonNull IoTResponse response) {
hideLoading();
}
})
场景列表
/**
* 场景列表接口返回
*
* @property total 总数
* @property pageNo 页码
* @property pageSize 分页条数
* @property sceneList 场景列表
* @constructor Create empty Scene create req
*/
data class SceneListRsp(
val total: Int,
val pageNo: Int,
val pageSize: Int,
val sceneList: List<SceneListItem>?,
)
/**
* 场景列表项
*
* @property sceneId 场景ID
* @property name 场景名称
* @property enable 是否启用
* @property mode 场景条件类型 any:表示任何一个条件满足就执行动作。 all:表示所有条件满足时才执行动作。 列表返回时没有值,详情接口返回
* @property conditions 场景条件 列表返回时没有值,详情接口返回
* @property actions 场景动作 列表返回时没有值,详情接口返回
* @constructor Create empty Scene create req
*/
data class SceneListItem(
val sceneId: Long,
var name: String,
var enable: Boolean,
var mode: String?,
var conditions: List<SceneCondition>?,
var actions: List<SceneAction>?
) : Serializable
示例
private static final int PAGE_SIZE = 10;
private void getSceneList(final int pageNo) {
DeviceSceneMgr.sceneList(pageNo, PAGE_SIZE, new IoTCallback() {
@Override
public void onFailure(@Nullable IoTRequest request, @NonNull IoTSDKError exception) {
IoTLogger.e(TAG, "sceneList failed", exception);
showToast(R.string.error_api_failed);
}
@Override
public void onResponse(@NonNull IoTRequest request, @NonNull IoTResponse response) {
SceneListRsp rsp = (SceneListRsp) response.getData();
if (rsp == null || rsp.getPageNo() != pageNo) return;
List<SceneListItem> rspList = rsp.getSceneList();
if (rspList == null) return;
}
});
}
场景详细
示例
private void getSceneDetail() {
DeviceSceneMgr.sceneDetail(mSceneId, new IoTCallback() {
@Override
public void onFailure(@Nullable IoTRequest request, @NonNull IoTSDKError exception) {
IoTLogger.e(TAG, "sceneDetail failed", exception);
showToast(R.string.error_api_failed);
}
@Override
public void onResponse(@NonNull IoTRequest request, @NonNull IoTResponse response) {
SceneListItem rsp = (SceneListItem) response.getData();
if (rsp == null) return;
mSceneConditionAdapter.setDataList(rsp.getConditions());
mSceneActionAdapter.setDataList(rsp.getActions());
binding.etSceneName.setText(rsp.getName());
binding.switchStatus.setChecked(rsp.getEnable());
int checkId = SceneModeEnum.Any.getMode().equals(rsp.getMode()) ? R.id.rbAny : R.id.rbAll;
binding.rgConditionMode.check(checkId);
}
});
}