自动化
通过自动化为节点创建触发条件和操作,响应特定的条件或事件。
概览
在 RainMaker 中,自动化可以让你:
- 为节点创建自动化触发器。
- 获取并管理自动化任务。
- 基于设备状态设置条件操作。
添加自动化
使用 addAutomation 方法,为节点添加新的自动化触发器。
try {
const automationDetails = {
name: "Turn on lights when motion detected",
events: [
{
params: {
Motion: {
Motion: true,
},
},
check: "==",
},
],
eventOperator: "and",
actions: [
{
node_id: "target_node_id",
params: {
Light: {
Power: true,
Brightness: 80,
},
},
},
],
retrigger: false,
metadata: {
description: "Automation to turn on lights when motion is detected",
},
};
const automation = await node.addAutomation(automationDetails);
console.log("Automation created:", automation);
} catch (error) {
console.error("Error creating automation:", error);
}
ESPAutomationDetails 接口
addAutomation 方法需要 ESPAutomationDetails 对象,包含以下属性:
| 属性 | 类型 | 必需 | 说明 |
|---|---|---|---|
| name* | 字符串 | 是 | 自动化任务的名称。 |
| events* | ESPAutomationEvent[] | 是 | 触发自动化任务的事件数组。 |
| eventOperator* | ESPAutomationEventOperator | 是 | 组合多个事件的逻辑运算符(and 或 or)。 |
| actions* | ESPAutomationAction[] | 是 | 事件触发时要执行的操作数组。 |
| retrigger* | 布尔值 | 是 | 在自动化任务已激活时是否允许再次触发。 |
| metadata | 任意类型 | 否 | 与该自动化任务关联的可选元数据。 |
示例:使用 AND 运算符的多个事件
try {
const automationDetails = {
name: "Turn on lights when brightness is low AND motion detected",
events: [
{
params: {
Light: {
Brightness: 30,
},
},
check: "<=",
},
{
params: {
Motion: {
Motion: true,
},
},
check: "==",
},
],
eventOperator: "and",
actions: [
{
node_id: "light_node_id",
params: {
Light: {
Power: true,
Brightness: 100,
},
},
},
],
retrigger: false,
};
const automation = await node.addAutomation(automationDetails);
console.log("Automation created:", automation);
} catch (error) {
console.error("Error creating automation:", error);
}
示例:使用 OR 运算符的多个事件
try {
const automationDetails = {
name: "Turn on lights when motion detected OR time is after sunset",
events: [
{
params: {
Motion: {
Motion: true,
},
},
check: "==",
},
{
params: {
Time: {
Time: "18:00",
},
},
check: ">=",
},
],
eventOperator: "or",
actions: [
{
node_id: "light_node_id",
params: {
Light: {
Power: true,
},
},
},
],
retrigger: true,
};
const automation = await node.addAutomation(automationDetails);
console.log("Automation created:", automation);
} catch (error) {
console.error("Error creating automation:", error);
}
获取自动化任务
使用 getAutomations 方法获取某个节点的自动化任务列表,支持可选的分页。
try {
const automations = await node.getAutomations();
console.log("Node automations:", automations);
} catch (error) {
console.error("Error fetching automations:", error);
}
ESPPaginatedAutomationsResponse 接口
getAutomations 方法会返回 ESPPaginatedAutomationsResponse 对象,包含以下属性:
| 属性 | 类型 | 说明 |
|---|---|---|
| automations* | ESPAutomation[] | 当前页中的自动化任务列表。 |
| hasNext* | 布尔值 | 是否还有更多页可用。 |
| fetchNext | () => Promise<ESPPaginatedAutomationsResponse> | 获取下一页自动化任务的可选函数。 |
示例:处理分页
try {
let response = await node.getAutomations();
let allAutomations = [...response.automations];
// Fetch all pages if available
while (response.hasNext && response.fetchNext) {
response = await response.fetchNext();
allAutomations = [...allAutomations, ...response.automations];
}
console.log("All automations:", allAutomations);
console.log("Total count:", allAutomations.length);
} catch (error) {
console.error("Error fetching automations:", error);
}
示例:处理自动化任务
try {
const response = await node.getAutomations();
response.automations.forEach((automation) => {
console.log(`Automation: ${automation.name}`);
console.log(`Events: ${automation.events.length}`);
console.log(`Actions: ${automation.actions.length}`);
console.log(`Retrigger: ${automation.retrigger}`);
});
if (response.hasNext) {
console.log(
"More automations available. Use fetchNext() to retrieve them."
);
}
} catch (error) {
console.error("Error fetching automations:", error);
}
前提条件:设置地理坐标
在使用基于位置的自动化任务(如日照和天气自动化任务)之前,必须先设置用户的地理坐标,因为日照和天气自动化任务都依赖用户的位置来判定触发条件。