跳到主要内容

Automation Store

AutomationStore 负责管理应用程序中的自动化触发器和自动化规则。通过自动化功能可以创建基于上下文的规则,根据节点参数变化、天气状况或日出日落等条件自动触发设备操作。

概述

AutomationStore 支持以下功能:

  • 管理和同步自动化列表
  • 按事件类型(节点、天气、日照)分类管理自动化
  • 管理地理坐标信息
  • 启用或禁用自动化
  • 与后端服务自动同步
  • 响应式状态管理

访问 AutomationStore

可通过 CDF 实例访问 AutomationStore:

const automationStore = espCDF.automationStore;

构造函数

constructor

创建一个新的 AutomationStore 实例。

new AutomationStore(rootStore?: CDF)

参数

  • rootStore(可选):CDF 根 Store 实例。

属性

属性类型描述
automationListESPAutomation[] (computed)包含 AutomationStore 中所有自动化的计算属性数组(整合了节点、天气和日照自动化)。
nodeAutomationListESPAutomation[] (computed)节点自动化的计算属性数组(由节点参数变化触发)。
weatherAutomationListESPAutomation[] (computed)天气自动化的计算属性数组(由天气条件触发)。
daylightAutomationListESPAutomation[] (computed)日照自动化的计算属性数组(由日出/日落触发)。
nodeAutomations{ [key: string]: ESPAutomation }以自动化 ID 为键的节点自动化字典。
weatherAutomations{ [key: string]: ESPAutomation }以自动化 ID 为键的天气自动化字典。
daylightAutomations{ [key: string]: ESPAutomation }以自动化 ID 为键的日照自动化字典。
geoCoordinatesESPGeoCoordinates | null天气和日照自动化所需的地理坐标。
hasNextboolean标识是否还有更多自动化可供获取(用于分页)。
enabledAutomationsESPAutomation[] (computed)所有已启用的自动化列表。
disabledAutomationsESPAutomation[] (computed)所有已禁用的自动化列表。
enabledNodeAutomationsESPAutomation[] (computed)已启用的节点自动化列表。
disabledNodeAutomationsESPAutomation[] (computed)已禁用的节点自动化列表。
enabledWeatherAutomationsESPAutomation[] (computed)已启用的天气自动化列表。
disabledWeatherAutomationsESPAutomation[] (computed)已禁用的天气自动化列表。
enabledDaylightAutomationsESPAutomation[] (computed)已启用的日照自动化列表。
disabledDaylightAutomationsESPAutomation[] (computed)已禁用的日照自动化列表。
automationsByNodeId{ [key: string]: ESPAutomation[] }按节点 ID 分组的自动化字典。
afterSetAutomationListHook(automations: ESPAutomation[]) => void自动化列表设置完成后调用的钩子函数。
beforeSetAutomationListHook(automations: ESPAutomation[]) => void自动化列表设置之前调用的钩子函数。

使用示例

// 获取所有自动化
const allAutomations = automationStore.automationList;
console.log("Total automations:", allAutomations.length);

// 获取节点自动化
const nodeAutomations = automationStore.nodeAutomationList;
console.log("Node automations:", nodeAutomations);

// 获取天气自动化
const weatherAutomations = automationStore.weatherAutomationList;
console.log("Weather automations:", weatherAutomations);

// 获取日照自动化
const daylightAutomations = automationStore.daylightAutomationList;
console.log("Daylight automations:", daylightAutomations);

// 通过 ID 获取自动化
const automation = automationStore.nodeAutomations["automation123"];

// 获取地理坐标
const coordinates = automationStore.geoCoordinates;
if (coordinates) {
console.log("Latitude:", coordinates.latitude);
console.log("Longitude:", coordinates.longitude);
}

// 检查分页
if (automationStore.hasNext) {
await automationStore.fetchNext();
}

// 获取已启用的自动化
const enabled = automationStore.enabledAutomations;

// 获取特定节点的自动化
const nodeAutomations = automationStore.automationsByNodeId["node123"];

AutomationStore 方法

同步自动化列表

使用 syncAutomationList() 方法可从云端获取自动化列表并更新 Store,获取到的自动化会按事件类型自动分类整理。

try {
await automationStore.syncAutomationList();
console.log("Automations synced:", automationStore.automationList);
} catch (error) {
console.error("Error syncing automations:", error);
}

设置自动化列表

使用 setAutomationList() 方法可在 Store 中设置自动化列表,列表会按事件类型自动分类。

/*
- automations: 要设置的自动化数组
*/

automationStore.setAutomationList(automations);

使用钩子设置自动化列表

使用 setAutomationListWithHooks() 方法可在设置自动化列表时触发预处理和后处理钩子,开发者可以利用此方法在设置自动化列表前后插入自定义逻辑。

/*
- automationList: 要设置的自动化数组
*/

automationStore.setAutomationListWithHooks(automationList);

添加自动化

使用 addAutomation() 方法可向 Store 添加新的自动化,添加后的自动化将自动变为可观察对象。

/*
- automation: 要添加的自动化
*/

try {
const automation = await userStore.user.addWeatherBasedAutomation({...});
const observableAutomation = automationStore.addAutomation(automation);
console.log("Automation added:", observableAutomation);
} catch (error) {
console.error("Error adding automation:", error);
}

通过 ID 获取自动化

使用 getAutomationById() 方法可根据 ID 从所有自动化列表中查找并获取指定的自动化。

/*
- automationId: 自动化 ID
*/

const automation = automationStore.getAutomationById("automation123");
if (automation) {
console.log("Automation found:", automation);
} else {
console.log("Automation not found");
}

通过节点 ID 获取自动化

使用 getAutomationsByNodeId() 方法可获取指定节点关联的所有自动化(仅适用于节点自动化)。

/*
- nodeId: 节点 ID
*/

const nodeAutomations = automationStore.getAutomationsByNodeId("node123");
console.log("Node automations:", nodeAutomations);

按位置获取天气自动化

使用 getWeatherAutomationsByLocation() 方法可获取指定地理位置的所有天气自动化。

/*
- location: 位置坐标
*/

const locationAutomations = automationStore.getWeatherAutomationsByLocation({
latitude: 37.7749,
longitude: -122.4194
});
console.log("Location automations:", locationAutomations);

按位置获取日照自动化

使用 getDaylightAutomationsByLocation() 方法可获取指定地理位置的所有日照自动化。

/*
- location: 位置坐标
*/

const locationAutomations = automationStore.getDaylightAutomationsByLocation({
latitude: 37.7749,
longitude: -122.4194
});
console.log("Location automations:", locationAutomations);

更新自动化

使用 updateAutomation() 方法可更新 Store 中的指定自动化。

/*
- automationId: 要更新的自动化 ID
- update: 更新数据
*/

automationStore.updateAutomation("automation123", {
automationName: "Updated Name"
});

批量更新自动化

使用 updateMultipleAutomations() 方法可一次性更新多个自动化。

/*
- updates: 自动化更新数组
*/

automationStore.updateMultipleAutomations([
{ automationId: "automation1", automationName: "Updated Name 1" },
{ automationId: "automation2", enabled: false }
]);

删除自动化

使用 deleteAutomations() 方法可从 Store 中删除指定的自动化。

/*
- ids: 要删除的自动化 ID 数组
*/

try {
await automationStore.deleteAutomations(["automation1", "automation2"]);
console.log("Automations deleted successfully");
} catch (error) {
console.error("Error deleting automations:", error);
}

获取下一页

使用 fetchNext() 方法可在分页模式下获取下一页自动化数据。

if (automationStore.hasNext) {
try {
const response = await automationStore.fetchNext();
console.log("Next automations:", response.automations);
} catch (error) {
console.error("Error fetching next page:", error);
}
}

处理自动化响应

使用 processAutomationsRes() 方法可处理自动化 API 的响应数据并更新 Store。

/*
- response: 来自自动化 API 的响应
*/

const processedResponse = automationStore.processAutomationsRes(response);

设置地理坐标

使用 setGeoCoordinates() 方法可在 Store 中设置地理坐标信息。

/*
- coordinates: 要设置的地理坐标
*/

automationStore.setGeoCoordinates({
latitude: 37.7749,
longitude: -122.4194
});

清除 Store

使用 clear() 方法可清除 Store 中的所有自动化数据。

automationStore.clear();

添加自定义属性

使用 addProperty() 方法可动态向 Store 添加可观察属性,同时自动生成对应的 gettersetter 方法。

/*
- propertyName: 要添加的属性名称
- initialValue: 属性的初始值
*/

automationStore.addProperty("customData", {});
// 创建:customData、getCustomData()、setCustomData(value)

通过 User 实例调用的 SDK 方法

自动化相关操作可通过 ESPRMUser 实例执行,详细文档请参阅 ESPRMUser API 参考

自动化方法

  • getAutomations(params) - 获取自动化列表,支持分页。详细文档请参阅 自动化
  • getAutomationDetail(automationId) - 获取自动化详情,详细文档请参阅 自动化
  • addWeatherBasedAutomation(params) - 添加天气自动化,详细文档请参阅 自动化
  • addDaylightBasedAutomation(params) - 添加日照自动化,详细文档请参阅 自动化
  • setGeoCoordinates(coordinates) - 设置地理坐标,详细文档请参阅 自动化
  • getGeoCoordinates() - 获取地理坐标,详细文档请参阅 自动化

通过 Automation 实例调用的 SDK 方法

AutomationStore 中的每个自动化都是 ESPAutomation 实例,可直接在自动化对象上调用以下方法。

自动化管理方法

  • updateName(name) - 修改自动化名称
  • updateLocation(location) - 修改自动化位置
  • enable(enabled) - 启用或禁用自动化
  • updateEvents(events) - 修改自动化触发事件
  • updateActions(actions) - 修改自动化执行动作
  • setRetrigger(retrigger) - 设置重复触发行为
  • update(automationDetails) - 批量修改自动化属性
  • delete() - 删除自动化

访问自动化属性

从 Store 获取自动化对象后,可以访问以下属性:

const automation = automationStore.getAutomationById("automation123");

// 自动化属性
console.log("Automation ID:", automation.automationId);
console.log("Automation Name:", automation.automationName);
console.log("Event Type:", automation.eventType);
console.log("Enabled:", automation.enabled);
console.log("Location:", automation.location);
console.log("Events:", automation.events);
console.log("Actions:", automation.actions);
console.log("Node ID:", automation.nodeId); // 用于节点自动化
console.log("Retrigger:", automation.retrigger);

自动化类型

AutomationStore 按事件类型对自动化进行分类管理:

节点自动化

节点自动化由节点参数变化触发,与特定节点相关联。

const nodeAutomations = automationStore.nodeAutomationList;
const nodeAutomationsForNode = automationStore.getAutomationsByNodeId("node123");

天气自动化

天气自动化由天气条件变化触发,需要配置地理坐标才能使用。

const weatherAutomations = automationStore.weatherAutomationList;
const locationAutomations = automationStore.getWeatherAutomationsByLocation({
latitude: 37.7749,
longitude: -122.4194
});

日照自动化

日照自动化由日出或日落触发,需要配置地理坐标才能使用。

const daylightAutomations = automationStore.daylightAutomationList;
const locationAutomations = automationStore.getDaylightAutomationsByLocation({
latitude: 37.7749,
longitude: -122.4194
});

响应式更新

AutomationStore 支持响应式更新,可以监听数据变化:

import { useEffect } from 'react';

useEffect(() => {
console.log("Automation list updated:", automationStore.automationList);
}, [automationStore.automationList]);

useEffect(() => {
console.log("Node automations:", automationStore.nodeAutomationList);
}, [automationStore.nodeAutomationList]);

useEffect(() => {
console.log("Weather automations:", automationStore.weatherAutomationList);
}, [automationStore.weatherAutomationList]);

自动同步机制

通过 SDK 创建、更新或删除自动化时,AutomationStore 会自动同步数据。自动化操作会通过拦截器自动更新 Store 状态。

相关资源