工場データを扱う
対応エディションEdgeCloudEnterprise
MQTT と API を通じて UNS データを操作できます。
MQTT clients は、Tier0 で生成された credentials を使って UNS broker に接続できます。
MQTT Credentials を生成する
Section titled “MQTT Credentials を生成する”Tier0 の Edge タブで新しい credential を作成し、記録します。
MQTT Clients を接続する
Section titled “MQTT Clients を接続する”- MQTTX をインストールして開きます。
- UNS broker の情報と生成された credentials を使って connection を追加します。
Terminal window {"Host": "mqtt.tier0.dev","Port": 8883,"Client ID": "<generated client ID>","Username": "<generated username>","Password": "<generated password>"}
- MQTT Explorer をインストールして開きます。
- UNS broker の情報と生成された credentials を使って新しい connection を追加します。
Terminal window {"Protocol": "mqtt://","Host": "mqtt.tier0.dev","Port": 8883,"Username": "<generated username>","Password": "<generated password>"} - Change the client ID to the credential ID in ADVANCED before connecting.
- Event Flow を使用 - Tier0 内の Node-RED ベースのデータ処理モジュールです。
- Flows で Event Flow を作成し、
mqtt innode から開始します。 - node 内で UNS broker を Server(flow と同じ名前)として選択し、Topic を UNS model に設定します。
- Flows で Event Flow を作成し、
- 独立した Node-RED Instance を使用
- Node-RED instance をインストールして開きます。
mqtt innode に、次の情報で server を追加します。
Terminal window {{/* under Connection */}"Server": "mqtt://mqtt.tier0.dev","Port": 8883,"Client ID": "<generated clientId>",{/* under Security */}"Username": "<generated username>","Password": "<generated password>"}
接続をテストする
Section titled “接続をテストする”- client status が正常であることを確認します。
- UNS model topic をコピーし、client から message を publish します。
REST API を使用して UNS データを操作します。
API Key を取得する
Section titled “API Key を取得する”API keys は Settings > API Keys にあります。account type によって作成できる key type が異なります。
- Service Key: Admin と Owner のみ作成できます。
- Personal Key: 誰でも作成でき、permission scope は account と同じです。
-
/uns/createUNS 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);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/read1 つ以上の 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/updatepathで 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);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/delete1 つ以上の 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);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 リファレンス
Section titled “API リファレンス”- UNS 上にアプリを構築 — UNS データで産業アプリケーションを構築します。
- UNS データを分析 — Marimo Notebook と Python で UNS データを分析します。