Ir al contenido

Trabajar con datos de fábrica

Disponible enEdgeCloudEnterprise

Puedes operar datos de UNS mediante MQTT y APIs.

Los MQTT clients pueden conectarse al broker UNS con credentials generadas en Tier0.

En la pestaña Edge de Tier0, crea una nueva credential y guárdala.

  1. Install and open MQTTX.
  2. Agrega una conexión con la información del broker UNS y las credentials generadas.
    Terminal window
    {
    "Host": "mqtt.tier0.dev",
    "Port": 8883,
    "Client ID": "<generated client ID>",
    "Username": "<generated username>",
    "Password": "<generated password>"
    }
  1. Asegúrate de que el client status sea normal.
  2. Copia un topic de modelo UNS y publica un message en él mediante el client.

Usa la REST API para operar datos de UNS.

Las API keys están en Settings > API Keys. Según el account type, los tipos de key permitidos son distintos.

  • Service Key: Solo la pueden crear Admin y Owner.
  • Personal Key: La puede crear cualquier persona, y su permission scope es el mismo que el de la cuenta.
  • /uns/create

    Crea un path o topic de UNS. Usa namespace para definir el 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

    Lee el valor más reciente y los metadatos de uno o varios 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

    Actualiza un UNS node por path. Usa updateMask para especificar qué campos deben cambiar.

    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

    Elimina uno o varios UNS models. Usa hard_delete: false para mover los models a la papelera.

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