콘텐츠로 이동

공장 데이터 작업

지원 에디션EdgeCloudEnterprise

MQTT와 API를 통해 UNS 데이터를 조작할 수 있습니다.

MQTT client는 Tier0에서 생성한 credential로 UNS broker에 연결할 수 있습니다.

Tier0의 Edge 탭에서 새 credential을 만들고 기록해 둡니다.

  1. MQTTX를 설치하고 엽니다.
  2. Tier0 UNS broker 정보와 생성된 credential로 connection을 추가합니다.
    Terminal window
    {
    "Host": "mqtt.tier0.dev",
    "Port": 1883,
    "Client ID": "<generated client ID>",
    "Username": "<generated username>",
    "Password": "<generated password>"
    }
  1. client status가 정상인지 확인합니다.
  2. UNS model topic을 복사하고 client를 통해 message를 publish합니다.

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 node를 업데이트합니다. 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 models를 삭제합니다. hard_delete: false를 사용하면 models를 휴지통으로 이동합니다.

    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);