跳到主要内容

服务控制

在已配网的节点上更新服务参数,例如时区、定时和系统设置。

备注

userInstance —— 来自用户登录的 ESPRMUser 实例。

本模块的功能​

设备控制负责灯、开关等可控设备。服务控制通过对服务参数调用 setValue(),更新节点配置中的系统级服务参数(定时、时区、OTA 设置等)。

适用场景:在更新服务参数时使用。若只修改时区,使用 node.setTimeZone() 更简单。若要修改设备参数,请使用设备控制。

预期结果: 服务参数更新将应用到该节点。

常见工作流​

通过 Time 服务设置时区​

const { nodes } = await userInstance.getUserNodes();
const node = nodes[0];
const nodeConfig = node.nodeConfig ?? (await node.getNodeConfig());

const timeService = nodeConfig.services.find((s) => s.type === "esp.service.time");
const tzParam = timeService.params.find((p) => p.name === "TZ");

await tzParam.setValue("America/Los_Angeles");

导航至某个服务参数​

  1. 获取节点 — getUserNodes() 或 getNodeDetails(nodeId)
  2. 访问服务 — nodeConfig.services 或 getServices()
  3. 查找服务 — 按照 type 查找(例如,"esp.service.time")
  4. 查找参数 — 按照 name 或 type 查找
  5. 调用 setValue(newValue)

直接使用 getServices()​

const services = await node.getServices();
const timeService = services.find((s) => s.type === "esp.service.time");
const tzParam = timeService.params.find((p) => p.name === "TZ");
await tzParam.setValue("Asia/Kolkata");

按类型查找参数​

const tzParam = service.params.find((p) => p.type === "esp.param.tz");
if (tzParam) await tzParam.setValue("Europe/London");

错误处理​

try {
const nodeConfig = node.nodeConfig ?? (await node.getNodeConfig());
const service = nodeConfig.services.find((s) => s.type === "esp.service.time");
const param = service?.params.find((p) => p.name === "TZ");
if (!service || !param) return;
await param.setValue(timezone);
} catch (error) {
console.error("Failed to update service parameter:", error);
}

进阶概念​

服务的节点层级​

Node → Services → Service Parameters

设备与服务在 nodeConfig 下是并列关系;控制路径也是并行的——关于设备侧,请参见设备控制。

最佳实践​

  1. 在更新前,确认该服务存在于该节点
  2. 当固件中的名称不一致时,通过 type 匹配
  3. 仅更改时区时,使用 setTimeZone()
  4. 批量更新:当需要修改多个值时,使用 setMultipleParams()

相关资源​

On this page