Working with Factory Data
Data in Tier0 unified namespace can be altered and managed through MQTT and APIs with authorization.
MQTT clients can connect to the UNS broker with credentials generated on Tier0.
Generating MQTT Credentials
Section titled “Generating MQTT Credentials”On the Edge tab in Tier0, create a new credential and make a note of it.
Connecting MQTT Clients
Section titled “Connecting MQTT Clients”- Install and open MQTTX.
- Add a connection with the information of the UNS broker and the generated credentials.
Terminal window {"Host": "mqtt.tier0.dev","Port": 1883,"Client ID": "<generated client ID>","Username": "<generated username>","Password": "<generated password>"}
- Install and open MQTT Explorer.
- Add a new connection with the UNS broker information and the generated credentials.
Terminal window {"Protocol": "mqtt://","Host": "mqtt.tier0.dev","Port": 1883,"Username": "<generated username>","Password": "<generated password>"} - Change the client ID to the credential ID in ADVANCED before connecting.
-
Use Event Flow - Data processing module in Tier0 based on Node-RED.
- Create an Event Flow in Flows, and start it with an
mqtt innode. - Select the UNS broker as Server (same name as the flow) and set Topic to a UNS model in the node.
- Create an Event Flow in Flows, and start it with an
-
Use Independent Node-RED Instance
- Install and open the Node-RED instance.
- Add a server in the
mqtt innode with the following information.
Terminal window {{/* under Connection */}"Server": "mqtt://mqtt.tier0.dev","Port": 1883,"Client ID": "<generated clientId>",{/* under Security */}"Username": "<generated username>","Password": "<generated password>"}
Testing Connections
Section titled “Testing Connections”- Make sure the client status is normal.
- Copy a UNS model topic and publish a message to it through the client.
Use the REST API to operate on UNS data.
Getting API Key
Section titled “Getting API Key”API keys are from Settings > API Keys. Depend on different account type, allowed key types are different.
- Service Key: Can only be created by Admin and Owner.
- Personal Key: Can be created by anyone, and the permission scope is the same as that of the account.
Examples
Section titled “Examples”-
/uns/createCreate UNS path or topic. Use
namespaceto define the 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);Terminal window import osimport requestsbase_url = os.environ["TIER0_BASE_URL"]api_key = os.environ["TIER0_API_KEY"]response = requests.post(f"{base_url}/uns/create",headers={"x-api-key": api_key},json={"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"},],},],},],},],},],},timeout=30,)response.raise_for_status()result = response.json()print(result["data"]["results"])Terminal window curl -X POST "$TIER0_BASE_URL/uns/create" \-H "content-type: application/json" \-H "x-api-key: $TIER0_API_KEY" \-d '{"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" }]}]}]}]}]}' -
/uns/readRead the latest value and metadata for one or more 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);Terminal window import osimport requestsbase_url = os.environ["TIER0_BASE_URL"]api_key = os.environ["TIER0_API_KEY"]response = requests.post(f"{base_url}/uns/read",headers={"x-api-key": api_key},json={"topics": ["DemoFactory/Site_01/Production/Produced_Qty",],"include_leaf_value": True,"include_metadata": True,},timeout=30,)response.raise_for_status()result = response.json()print(result["data"]["results"])Terminal window curl -X POST "$TIER0_BASE_URL/uns/read" \-H "content-type: application/json" \-H "x-api-key: $TIER0_API_KEY" \-d '{"topics": ["DemoFactory/Site_01/Production/Produced_Qty"],"include_leaf_value": true,"include_metadata": true}' -
/uns/updateUpdate a UNS node by
path. UseupdateMaskto specify which fields should change.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);Terminal window import osimport requestsbase_url = os.environ["TIER0_BASE_URL"]api_key = os.environ["TIER0_API_KEY"]response = requests.post(f"{base_url}/uns/update",headers={"x-api-key": api_key},json={"path": "DemoFactory/Site_01/Production/Produced_Qty","displayName": "Produced Quantity","description": "Updated production output quantity.","updateMask": ["displayName", "description"],},timeout=30,)response.raise_for_status()result = response.json()print(result["data"])Terminal window curl -X POST "$TIER0_BASE_URL/uns/update" \-H "content-type: application/json" \-H "x-api-key: $TIER0_API_KEY" \-d '{"path": "DemoFactory/Site_01/Production/Produced_Qty","displayName": "Produced Quantity","description": "Updated production output quantity.","updateMask": ["displayName", "description"]}' -
/uns/deleteDelete one or more UNS models. Use
hard_delete: falseto move models to the recycle bin.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);Terminal window import osimport requestsbase_url = os.environ["TIER0_BASE_URL"]api_key = os.environ["TIER0_API_KEY"]response = requests.post(f"{base_url}/uns/delete",headers={"x-api-key": api_key},json={"topics": ["DemoFactory/Site_01/Production/Produced_Qty",],"hard_delete": False,},timeout=30,)response.raise_for_status()result = response.json()print(result["data"])Terminal window curl -X POST "$TIER0_BASE_URL/uns/delete" \-H "content-type: application/json" \-H "x-api-key: $TIER0_API_KEY" \-d '{"topics": ["DemoFactory/Site_01/Production/Produced_Qty"],"hard_delete": false}'
API reference
Section titled “API reference”- Build Apps on UNS — Build industrial applications with UNS data.
- Analyze UNS Data — Analyze UNS data with Marimo Notebook and Python.