自动化
通过自动化为节点创建触发条件和操作,响应特定的条件或事件。
概览
在 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);
}
前提条件:设置地理坐标
在使用基于位置的自动化任务(如日照和天气自动化任务)之前,必须先设置用户的地理坐标,因为日照和天气自动化任务都依赖用户的位置来判定触发条件。
设置地理坐标
使用 setGeoCoordinates 方法保存用户的纬度和经度。
try {
const geoCoordinates = {
latitude: "37.7749", // Example: San Francisco latitude
longitude: "-122.4194", // Example: San Francisco longitude
};
const response = await user.setGeoCoordinates(geoCoordinates);
console.log("Geo coordinates set successfully:", response);
} catch (error) {
console.error("Error setting geo coordinates:", error);
}
获取地理坐标
使用 getGeoCoordinates 方法获取已保存的地理坐标。
try {
const coordinates = await user.getGeoCoordinates();
console.log("Latitude:", coordinates.latitude);
console.log("Longitude:", coordinates.longitude);
} catch (error) {
console.error("Error retrieving geo coordinates:", error);
}
注意: 如果在未先设置地理坐标的情况下尝试创建日照或天气自动化任务,SDK 会自动获取已保存的坐标。若未找到坐标,则会报错。
添加基于日照的自动化任务
使用 addDaylightBasedAutomation 方法创建基于日照事件(如日出与日落)的自动化任务。
try {
const daylightAutomationDetails = {
name: "Turn on lights at sunset",
events: ["sunset"], // Can be "sunrise" or "sunset"
eventOperator: "or",
actions: [
{
nodeId: "light_node_id",
deviceName: "Light",
param: "Power",
value: true,
},
],
retrigger: true,
metadata: {
description: "Automatically turn on lights when the sun sets",
},
};
const automation = await user.addDaylightBasedAutomation(
daylightAutomationDetails
);
console.log("Daylight automation created:", automation);
} catch (error) {
console.error("Error creating daylight automation:", error);
}
ESPDaylightAutomationDetails 接口
addDaylightBasedAutomation 方法需要 ESPDaylightAutomationDetails 对象,包含以下属性:
| 属性 | 类型 | 必需 | 说明 |
|---|---|---|---|
| name* | 字符串 | 是 | 自动化任务的名称。 |
| events* | ESPDaylightEvent[] | 是 | 日照事件数组(sunrise 或 sunset)。 |
| eventOperator* | ESPAutomationEventOperator | 是 | 组合多个事件的逻辑运算符(and 或 or)。 |
| actions* | ESPAutomationAction[] | 是 | 事件触发时要执行的操作数组。 |
| retrigger* | 布尔值 | 是 | 在自动化任务已激活时是否允许再次触发。 |
| location | ESPGeoCoordinates | 否 | 可选的位置覆写配置。若未提供,则使用已保存的地理坐标。 |
| metadata | 任意类型 | 否 | 与该自动化任务关联的可选元数据。 |
示例:在日出时开灯
try {
const daylightAutomationDetails = {
name: "Morning lights",
events: ["sunrise"],
eventOperator: "or",
actions: [
{
nodeId: "bedroom_light_node_id",
deviceName: "Light",
param: "Power",
value: true,
},
{
nodeId: "bedroom_light_node_id",
deviceName: "Light",
param: "Brightness",
value: 50,
},
],
retrigger: true,
metadata: {
description: "Gradually turn on bedroom lights at sunrise",
},
};
const automation = await user.addDaylightBasedAutomation(
daylightAutomationDetails
);
console.log("Sunrise automation created:", automation.automationId);
} catch (error) {
console.error("Error creating sunrise automation:", error);
}
示例:在日落时执行多个操作
try {
const daylightAutomationDetails = {
name: "Evening routine",
events: ["sunset"],
eventOperator: "or",
actions: [
{
nodeId: "living_room_light_node_id",
deviceName: "Light",
param: "Power",
value: true,
},
{
nodeId: "outdoor_light_node_id",
deviceName: "Light",
param: "Power",
value: true,
},
{
nodeId: "thermostat_node_id",
deviceName: "Thermostat",
param: "Temperature",
value: 22,
},
],
retrigger: true,
};
const automation = await user.addDaylightBasedAutomation(
daylightAutomationDetails
);
console.log("Evening automation created successfully");
} catch (error) {
console.error("Error creating evening automation:", error);
}
示例:使用自定义位置
try {
// 使用自定义的位置而不是已保存的地理坐标
const customLocation = {
latitude: "40.7128", // 纽约
longitude: "-74.0060",
};
const daylightAutomationDetails = {
name: "NYC sunset lights",
events: ["sunset"],
eventOperator: "or",
actions: [
{
nodeId: "light_node_id",
deviceName: "Light",
param: "Power",
value: true,
},
],
retrigger: true,
location: customLocation,
};
const automation = await user.addDaylightBasedAutomation(
daylightAutomationDetails
);
console.log("Custom location automation created:", automation);
} catch (error) {
console.error("Error creating automation:", error);
}