跳转到内容

操作工厂数据

适用版本EdgeCloudEnterprise

你可以通过 MQTT 和 API 操作 UNS 数据。

MQTT 客户端可以使用 Tier0 生成的凭据连接到 UNS broker。

在 Tier0 的 Edge 页签创建新的凭据,并记录下来。

  1. 安装并打开 MQTTX
  2. 使用 Tier0 UNS broker 信息和生成的凭据添加连接。
    Terminal window
    {
    "Host": "mqtt.tier0.dev",
    "Port": 8883,
    "Client ID": "<generated client ID>",
    "Username": "<generated username>",
    "Password": "<generated password>"
    }
  1. 确认 client 状态正常。
  2. 复制一个 UNS 模型 topic,并通过 client 向它发布一条 message。

使用 REST API 操作 UNS 数据。

API keys 位于 Settings > API Keys。不同 account type 允许创建的 key type 不同。

  • Service Key:只能由 AdminOwner 创建。
  • Personal Key:任何人都可以创建,permission scope 与该 account 相同。
  • /uns/create

    创建 UNS path 或 topic。使用 namespace 定义 node tree.

    Terminal window
    const BASE_URL = process.env.TIER0_BASE_URL;
    const API_KEY = process.env.TIER0_API_KEY;
    const response = await fetch(`${BASE_URL}/uns/create`, {
    method: 'POST',
    headers: {
    'content-type': 'application/json',
    'x-api-key': API_KEY,
    },
    body: JSON.stringify({
    namespace: [
    {
    name: 'DemoFactory',
    type: 'PATH',
    children: [
    {
    name: 'Site_01',
    type: 'PATH',
    children: [
    {
    name: 'Production',
    type: 'PATH',
    children: [
    {
    name: 'Produced_Qty',
    type: 'TOPIC',
    topicType: 'METRIC',
    displayName: 'Produced Quantity',
    description: 'Produced quantity from the production line.',
    enableHistory: true,
    fields: [
    { name: 'value', type: 'DOUBLE', unit: 'pcs' },
    ],
    },
    ],
    },
    ],
    },
    ],
    },
    ],
    }),
    });
    const result = await response.json();
    console.log(result.data.results);
  • /uns/read

    读取一个或多个 UNS topics 的最新值和元数据。

    Terminal window
    const BASE_URL = process.env.TIER0_BASE_URL;
    const API_KEY = process.env.TIER0_API_KEY;
    const response = await fetch(`${BASE_URL}/uns/read`, {
    method: 'POST',
    headers: {
    'content-type': 'application/json',
    'x-api-key': API_KEY,
    },
    body: JSON.stringify({
    topics: [
    'DemoFactory/Site_01/Production/Produced_Qty',
    ],
    include_leaf_value: true,
    include_metadata: true,
    }),
    });
    const result = await response.json();
    console.log(result.data.results);
  • /uns/update

    通过 path 更新 UNS 节点。使用 updateMask 指定需要变更的字段。

    Terminal window
    const BASE_URL = process.env.TIER0_BASE_URL;
    const API_KEY = process.env.TIER0_API_KEY;
    const response = await fetch(`${BASE_URL}/uns/update`, {
    method: 'POST',
    headers: {
    'content-type': 'application/json',
    'x-api-key': API_KEY,
    },
    body: JSON.stringify({
    path: 'DemoFactory/Site_01/Production/Produced_Qty',
    displayName: 'Produced Quantity',
    description: 'Updated production output quantity.',
    updateMask: ['displayName', 'description'],
    }),
    });
    const result = await response.json();
    console.log(result.data);
  • /uns/delete

    删除一个或多个 UNS 模型。使用 hard_delete: false 将模型移到回收站。

    Terminal window
    const BASE_URL = process.env.TIER0_BASE_URL;
    const API_KEY = process.env.TIER0_API_KEY;
    const response = await fetch(`${BASE_URL}/uns/delete`, {
    method: 'POST',
    headers: {
    'content-type': 'application/json',
    'x-api-key': API_KEY,
    },
    body: JSON.stringify({
    topics: [
    'DemoFactory/Site_01/Production/Produced_Qty',
    ],
    hard_delete: false,
    }),
    });
    const result = await response.json();
    console.log(result.data);